Skip to content

Fix DSV export for NUL bytes - #7782

Closed
DhruvShah-Dev wants to merge 2 commits into
getredash:masterfrom
DhruvShah-Dev:fix/csv-export-nul-bytes
Closed

Fix DSV export for NUL bytes#7782
DhruvShah-Dev wants to merge 2 commits into
getredash:masterfrom
DhruvShah-Dev:fix/csv-export-nul-bytes

Conversation

@DhruvShah-Dev

@DhruvShah-Dev DhruvShah-Dev commented Aug 9, 2026

Copy link
Copy Markdown

What type of PR is this?

Bug fix

Description

CSV and TSV export can fail when query results contain embedded NUL bytes because Python's CSV writer raises while trying to escape those values. This change removes NUL bytes from string fields before DSV serialization and avoids mutating the stored query result row during export.

Fixes #7758

Validation

  • git diff --check
  • python -m pytest tests\serializers\test_query_results.py (blocked locally: missing Redash runtime dependency redis)

Review in cubic

@greptile-apps

greptile-apps Bot commented Aug 9, 2026

Copy link
Copy Markdown

Greptile Summary

The PR prevents embedded NUL bytes from breaking CSV and TSV exports while preserving stored query-result rows.

  • Adds DSV-specific string sanitization that removes NUL bytes.
  • Copies each row before applying type conversions, avoiding mutation during export.
  • Adds regression coverage for exporting a string containing an embedded NUL byte.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
redash/serializers/query_result.py Sanitizes string values immediately before DSV writing and performs conversions on a shallow row copy.
tests/serializers/test_query_results.py Adds focused regression coverage confirming embedded NUL bytes are removed from exported string fields.

Reviews (2): Last reviewed commit: "Apply backend formatter" | Re-trigger Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@DhruvShah-Dev

Copy link
Copy Markdown
Author

Updated this PR with a formatting-only commit to satisfy backend-lint.

Validated locally:

  • python -m black --check redash\serializers\query_result.py tests\serializers\test_query_results.py
  • python -m ruff check redash\serializers\query_result.py tests\serializers\test_query_results.py

The targeted pytest command still cannot collect locally because this checkout is missing the Redash runtime dependency redis.

@yoshiokatsuneo

yoshiokatsuneo commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

The python CSV exportor issue looks already being solved on the Python3.13.
I can suggest to test the issue on the latest Redash and the latest usable Python(3.13).

python/cpython#97503

@DhruvShah-Dev

Copy link
Copy Markdown
Author

Closing this after testing the same csv.DictWriter path on Python 3.13.3, where a value containing \x00 writes successfully without raising.

Given that the original failure is no longer reproducible on the current supported Python version, withdrawing this sanitization change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSV download fails with "_csv.Error: need to escape, but no escapechar set" when query results contain NUL bytes (\x00)

2 participants