Skip to content

fix(autoindex): cap CSV record size so one quoted field cannot allocate the whole file - #3202

Merged
xerj-team merged 1 commit into
xerj-org:mainfrom
Varshith-Kali:fix-csv-record-size-cap
Nov 25, 2026
Merged

xerj-team merged 1 commit into
xerj-org:mainfrom
Varshith-Kali:fix-csv-record-size-cap

Conversation

@Varshith-Kali

Copy link
Copy Markdown
Contributor

Written by an AI coding agent (Muse), run by @Varshith-Kali, who has reviewed this change.

Fixes #3201.

Motivation: the CSV extractor reads records through csv::ReaderBuilder with no per-record size cap, while a quoted field may span newlines. One hostile file with a single giant quoted field makes the parser buffer the whole input before returning the first record, then each field is copied into a String and a JSON map, so the bytes are held several times over. The module header at extract/mod.rs claims every extractor is bounded-memory; CSV was the exception.

Root cause: the Rust csv crate (v1) offers no record or field size option on ReaderBuilder, and open_reader only caps total bytes when a sampling limit is passed, while the index path calls extract with None, meaning the full stream. Gzip input is decoded with no cap on decompressed bytes.

Fix: a 16 MiB per-record cap (MAX_CSV_RECORD_BYTES), the same per-unit convention as MAX_LINE for JSONL and the YAML whole-file cap. A RecordByteCap shared between the reader and the extractor counts decoded bytes consumed since the last completed record and errors past the cap, before the parser can grow further. An oversize data record is counted as junk and extraction stops there, because the parser is mid-record inside the oversize field and cannot resume; the records before it still extract. An oversize header row junks the file instead of failing it, because an extract error aborts the whole autoindex run unless the file changed mid-run. A record 1 MiB under the cap still extracts unchanged. CHANGELOG entry added under [Unreleased].

Reference: the Python csv module has enforced a per-field limit since 2003 (Modules/_csv.c, parse_add_char: field larger than field limit), because an unmatched quote could otherwise read the entire file into the field buffer. The Rust crate has no equivalent, so the bound lives in the reader instead. Approach only, nothing copied.

Verified, commands run in this environment, output observed:

  • cargo test -p xerj-autoindex --lib csv_x: 13 passed, 0 failed (includes the 3 new tests)
  • The 2 oversize tests were watched fail on unmodified main (3 records extracted instead of 1; header row extracted instead of junk) and pass with the fix
  • cargo test -p xerj-autoindex --lib: 2048 passed, 1 failed (walk_only_summarises_without_opening_file_contents, verified failing on clean main too, a pre-existing sandbox failure; walk.rs does not use open_reader)
  • cargo fmt --all --check: clean
  • cargo clippy -p xerj-autoindex --lib --tests: no warnings in the touched files
  • cargo build --release -j 32 -p xerj-autoindex: green in 2m16s

Assumed, NOT tested:

  • The enforced threshold is approximate by up to one buffered fill (256 KiB BufReader plus the csv crate read chunk): read-ahead may be attributed to the record in flight, so a record just under the cap can be refused early, and the parser may hold up to one fill past the cap before the error lands. Either way the allocation stays bounded at roughly the cap plus one fill.
  • No end-to-end autoindex run was done over a real corpus with this build; the extractor is covered by unit tests only.

Not run: the ES-YAML conformance gate. It needs a full-server release binary plus a live node running 1454 cases, and this change touches only the CSV extractor read path (no query, mapping, index, or wire code). CI covers it.

CLA: the CLA signature is for Varshith Puli personally and is not signed here; the verification/cla-signed check will need his signature.

@cla-bot

cla-bot Bot commented Nov 15, 2026

Copy link
Copy Markdown

Thanks for this contribution — we'd like to merge it.

Before we can, we need a signed Contributor License Agreement from: @Varshith-Kali

It is a one-time thing per contributor, not per pull request. Read CLA.md, then open a small pull request adding your GitHub username to .contributors — that pull request is your signature, since it is made from your own account.

Once it is merged, comment @cla-bot check here and this check will go green. If you believe you are already covered, comment @cla-bot check and it will re-verify.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

ES-YAML conformance gate, run locally after the PR opened: 1448 passed, 0 failed, 10 skipped, 1458 total, exit 0. Command: started the release binary from this head with a fresh data dir, then cargo run --release -p es-yaml-runner -- --dir tests/es-compat-yaml/yaml. The PR body said the gate was not run locally; that is now out of date, the gate is green on this head.

@xerj-team

Copy link
Copy Markdown
Collaborator

Thanks @Varshith-Kali, I reviewed this and it looks correct: the shared RecordByteCap limits one record without false trips on normal files, an oversize header junk-files the file instead of failing the run, and the at-cap and over-cap tests prove both sides. Two things are still needed before merge: your CLA line (steps in the comment on #3159), and a green CI run. CI is approved, but the runners are not picking up jobs right now, so it has not run yet. Once both are done, this merges as is.

@Varshith-Kali
Varshith-Kali force-pushed the fix-csv-record-size-cap branch from 05ca0ad to aac39bb Compare November 17, 2026 23:34
@cla-bot

cla-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

Thanks for this contribution — we'd like to merge it.

Before we can, we need a signed Contributor License Agreement from: @Varshith-Kali

It is a one-time thing per contributor, not per pull request. Read CLA.md, then open a small pull request adding your GitHub username to .contributors — that pull request is your signature, since it is made from your own account.

Once it is merged, comment @cla-bot check here and this check will go green. If you believe you are already covered, comment @cla-bot check and it will re-verify.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks, glad the shared RecordByteCap and the at-cap and over-cap behaviour read right. The CLA line and a green CI run are still the open items, with CI approved but still queued. Also rebased onto current main, no code changes in the rebase.

@Varshith-Kali
Varshith-Kali force-pushed the fix-csv-record-size-cap branch from aac39bb to 5b9a9d6 Compare November 18, 2026 07:09
@cla-bot

cla-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for this contribution — we'd like to merge it.

Before we can, we need a signed Contributor License Agreement from: @Varshith-Kali

It is a one-time thing per contributor, not per pull request. Read CLA.md, then open a small pull request adding your GitHub username to .contributors — that pull request is your signature, since it is made from your own account.

Once it is merged, comment @cla-bot check here and this check will go green. If you believe you are already covered, comment @cla-bot check and it will re-verify.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Rebased onto main dc1a462. The only conflict was CHANGELOG.md, both entries kept. No code change from the tested head.

@drk-ram drk-ram 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.

Reviewed engine/crates/xerj-autoindex/src/extract/csv_x.rs and mod.rs (5b9a9d63):

  • Streaming per-record byte cap (RecordByteCap / MAX_CSV_RECORD_BYTES = 16 MiB): Enforcing the 16 MiB cap inside the underlying Read wrapper (open_reader_with_cap) stops the csv parser before an unclosed quote can buffer a multi-gigabyte file into a single field, and resetting the counter after each valid header/record while treating a tripped header/record as stats.junk += 1 ensures one hostile CSV file cannot abort an entire autoindex run (#3201).

@Varshith-Kali
Varshith-Kali force-pushed the fix-csv-record-size-cap branch from 5b9a9d6 to 43d39d4 Compare November 21, 2026 01:07
@cla-bot

cla-bot Bot commented Nov 21, 2026

Copy link
Copy Markdown

Thanks for this contribution — we'd like to merge it.

Before we can, we need a signed Contributor License Agreement from: @Varshith-Kali

It is a one-time thing per contributor, not per pull request. Read CLA.md, then open a small pull request adding your GitHub username to .contributors — that pull request is your signature, since it is made from your own account.

Once it is merged, comment @cla-bot check here and this check will go green. If you believe you are already covered, comment @cla-bot check and it will re-verify.

…te the whole file

Motivation: the CSV extractor reads records through csv::ReaderBuilder with no per-record size cap, while a quoted field may span newlines. One hostile file with a single giant quoted field makes the parser buffer the whole input before returning the first record, then each field is copied into a String and a JSON map, so the bytes are held several times over. The module header at extract/mod.rs claims every extractor is bounded-memory; CSV was the exception (issue xerj-org#3201).

Root cause: the Rust csv crate (v1) offers no record or field size option on ReaderBuilder, and open_reader only caps total bytes when a sampling limit is passed, while the index path calls extract with None, meaning the full stream. Gzip input is decoded with no cap on decompressed bytes. So nothing between the file and the record allocation bounded one record.

Fix: a 16 MiB per-record cap (MAX_CSV_RECORD_BYTES), the same per-unit convention as MAX_LINE for JSONL and the YAML whole-file cap. A RecordByteCap shared between the reader and the extractor counts decoded bytes consumed since the last completed record and errors past the cap, before the parser can grow further. An oversize data record is counted as junk and extraction stops there, because the parser is mid-record inside the oversize field and cannot resume; the records before it still extract. An oversize header row junks the file instead of failing it, because an extract error aborts the whole autoindex run unless the file changed mid-run. A record 1 MiB under the cap still extracts unchanged. The reference is Python's csv module, which has enforced a per-field limit since 2003 (Modules/_csv.c, parse_add_char: field larger than field limit), because an unmatched quote could otherwise read the entire file into the field buffer; the Rust crate has no equivalent, so the bound lives in the reader instead. CHANGELOG entry added under [Unreleased].

Evidence: three new regression tests in csv_x.rs. The oversize-record and oversize-header tests were watched fail on unmodified main (3 records extracted instead of 1; header row extracted instead of junk) and pass with the fix. Full crate lib suite: 2048 passed, 1 failed (walk_only_summarises_without_opening_file_contents, verified failing on clean main too, a pre-existing sandbox failure; walk.rs does not use open_reader). csv_x tests: 13 passed. cargo fmt --all clean. cargo clippy -p xerj-autoindex: no warnings in the touched files; pre-existing warnings elsewhere (unused rustls workspace dep, chunks_exact in xerj-common, fetch_update deprecation in lib.rs) left untouched. Scoped release build cargo build --release -j 32 -p xerj-autoindex: green in 2m16s. ES-YAML gate: not run locally yet, see the PR body.

Written by an AI coding agent (Muse), run by Varshith-Kali, who has reviewed this change.
@Varshith-Kali
Varshith-Kali force-pushed the fix-csv-record-size-cap branch from 43d39d4 to 063296e Compare November 21, 2026 10:10
@cla-bot

cla-bot Bot commented Nov 21, 2026

Copy link
Copy Markdown

Thanks for this contribution — we'd like to merge it.

Before we can, we need a signed Contributor License Agreement from: @Varshith-Kali

It is a one-time thing per contributor, not per pull request. Read CLA.md, then open a small pull request adding your GitHub username to .contributors — that pull request is your signature, since it is made from your own account.

Once it is merged, comment @cla-bot check here and this check will go green. If you believe you are already covered, comment @cla-bot check and it will re-verify.

@xerj-team
xerj-team merged commit 35ccd19 into xerj-org:main Nov 25, 2026
0 of 2 checks passed
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 extractor has no per-record size cap: one quoted field can allocate the whole file

3 participants