dify icon indicating copy to clipboard operation
dify copied to clipboard

[Bug]: .xls extractor emits unescaped double quotes in column headers

Open indexGin opened this issue 2 weeks ago • 4 comments

Phenomenon

Uploading an .xls workbook whose column headers contain double quotes produces corrupted extraction output. ExcelExtractor renders every .xls row as "key":"value";... and escapes quotes inside cell values, but it interpolates the column header verbatim. A column named Size "L" therefore yields "Size "L"":"large" -- the row layout is broken for downstream consumers and the header text itself is mangled.

The .xlsx branch already escapes header names in _find_header_and_columns; only the .xls branch is affected.

Minimal reproduction

  1. Create an .xls file with a column header containing a double quote, e.g. Size "L", and one row whose value is large.
  2. Extract it with ExcelExtractor("/tmp/sample.xls").extract().

Observed document text: "Size "L"":"large". Expected: "Size \"L\"":"large".

A unit-level reproduction stubs pandas.ExcelFile to return a dataframe with a {'Size "L"': "large"} row; the current code returns the unescaped header.

Expected behavior

Header names should be escaped exactly like cell values, so quoted headers produce well-formed "key":"value" pairs, matching the behavior of the .xlsx branch.

Impact

Spreadsheet headers containing quotes are common (size labels, quoted terms, survey questions). Any .xls file with such a header silently feeds malformed text into the knowledge base. This is user-visible data corruption on an ordinary upload path, and it is hard to trace back to the extractor.


Additional context (added 2026-11-20, per the maintainer's request)

Dify version / source commit: Not a released build. Verified against the main source tree at commit bc7e82dc72c6e547f51897781468f39772c5751a — the merge base of PR #46543 when it was opened on 2026-11-17.

Cloud or Self Hosted: Self Hosted (Source), i.e. a local source checkout using the repository's own pytest suite. This is a source-only reproduction: it was not observed on a deployed Cloud or Docker instance. The affected code is a pure/shared parsing or serialization helper used by every deployment mode, so the behaviour does not depend on the deployment type. In a running deployment the same helper is reached through normal product paths, but no claim of an observed production incident is made here.

Steps to reproduce:

  1. Check out main at commit bc7e82dc72c6 and enter api/.
  2. Run pytest tests/unit_tests/core/rag/extractor/test_excel_xls_header_quote.py -q (regression test added by PR #46543). The test creates a tiny .xls workbook (or stubs pandas.ExcelFile) with a column header Size "L" and one row whose value is large.
  3. Call ExcelExtractor(path).extract() and inspect the emitted document text.

✔️ Expected Behavior: The header is escaped like the .xlsx branch does, so the row reads "Size \"L\"":"large".

❌ Actual Behavior: The .xls branch interpolates the header verbatim and emits "Size "L"":"large", breaking the row layout and mangling the header text.

Complete reproduction: PR #46543 adds api/tests/unit_tests/core/rag/extractor/test_excel_xls_header_quote.py plus the fix; running that test file against bc7e82dc72c6e547f51897781468f39772c5751a fails before the fix and passes after it. The exact minimal snippet is also in the issue/PR description.

indexGin avatar Nov 17 '26 04:11 indexGin

This is distinct from #42043, which reported unescaped cell values in .xls files. The current .xls branch escapes v but still interpolates k unchanged, so that value-side fix does not cover quoted headers. [1] [2]

There is also a regression-test gap: test_extract_xls_escapes_double_quotes_like_xlsx uses only the plain headers note and plain. Adding your quoted-header case there, ideally with quotes in both the header and value, would cover the missing behavior. [3]

For affected uploads, saving a copy as .xlsx should bypass this specific escaping problem, since that branch escapes column names in _find_header_and_columns. [4]

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about dify

dosubot[bot] avatar Nov 17 '26 04:11 dosubot[bot]

Confirmed, nice catch. The .xls path reads via pandas and dumps column names directly into the string template without escaping quotes like _find_header_and_columns() does for .xlsx.

The cleanest fix without touching the downstream string builder is sanitizing the columns right after loading the dataframe in ExcelExtractor._extract_xls:

df.columns = [str(col).replace('"', '\\"') for col in df.columns]

Or if you'd rather sanitize during row construction to avoid altering the raw dataframe structure:

for col, val in row.items():
    escaped_col = str(col).replace('"', '\\"')
    escaped_val = str(val).replace('"', '\\"') if pd.notna(val) else ""
    row_strings.append(f'"{escaped_col}":"{escaped_val}"')

Unit test to add to test_excel_extractor.py:

def test_xls_extractor_escapes_double_quotes_in_headers():
    mock_df = pd.DataFrame([{"Size \"L\"": "large"}])
    with patch("pandas.ExcelFile") as mock_excel:
        instance = mock_excel.return_value
        instance.sheet_names = ["Sheet1"]
        instance.parse.return_value = mock_df

        docs = ExcelExtractor("dummy.xls").extract()
        assert len(docs) > 0
        assert '"Size \\"L\\"":"large"' in docs[0].page_content

Are you putting together a PR for this, or would you like someone else to submit it?

grinem avatar Nov 19 '26 20:11 grinem

Hi @indexGin, thanks for opening this issue.

Why this is being closed

The .xls example and expected output are useful, but the report does not say which Dify version or source commit was tested, how Dify was deployed, or whether the behavior was observed through an actual upload or only with a mocked extractor. The bug report template requests version, deployment and reproduction context so we can verify where the problem occurs. This closure does not imply the observation is incorrect.

Next steps

Please add the exact Dify version or commit and deployment type, say whether the reproduction is source-only, and provide the steps and observed output for the tested environment. A small sanitized sample workbook or a complete script to create one would help. Once the missing context is available, please request reopening so the issue and linked fix can be reviewed.

crazywoola avatar Nov 20 '26 03:11 crazywoola

The missing bug-report context is now in the issue body: the exact source commit (bc7e82dc72c6e547f51897781468f39772c5751a), the deployment type, an explicit source-only declaration, step-by-step reproduction commands, and observed versus expected results. I kept the original report intact and only added the details requested in the closing comment.

Requesting that this report be reopened, as suggested. Once it is ready for review, PR #46543 (the fix plus its regression test) can be reassessed. Thank you for the clear checklist — happy to supply anything else that would help.

indexGin avatar Nov 20 '26 07:11 indexGin