libexpat-xml-parsing-defensive
Task
/testbed holds the XML intake path of a document-ingestion service.
/testbed/expat is a vendored copy of the Expat XML parser written in C — the
tokenizer, the parser core, the DTD/prolog handling, their headers, the
project's own xmlwf well-formedness checker and its unit-test suite. The
service screens every incoming document with xmlwf in namespace mode before
anything downstream is allowed to touch it.
Everything downstream — indexing, routing, storage — trusts the names and values this parser produces. A security hardening pass over the parser's input handling is required before that trust is justified. Operators report that documents which should never have been accepted are reaching the application, and that some documents drive the parser's internal arithmetic into states its own assertions do not survive.
There is more than one independent weakness, and they are not all of the same kind. Audit what the parser accepts against what the XML specification actually permits, and audit the arithmetic it performs on lengths and buffer sizes. Diagnose the weaknesses from the source and from observed behavior; no checklist of defect classes or affected functions is provided, and a comment describing intent is not a guarantee of behavior.
The contract to restore
- Validation is the parser's job, not the application's. A document that the XML specification does not permit must be rejected by the parser, not passed up with a malformed name for the application to deal with. This includes byte sequences that are not valid in the encoding being parsed, and names or namespace values carrying characters the parser itself uses as internal structure.
- The parser's own arithmetic must not overflow. Computations on lengths, offsets and buffer sizes must stay within the range of their types for every input a caller can supply. The build enables sanitizers, so an overflow is an observable failure rather than a silent one.
Preserve normal behavior
Your changes must restrict only what was never valid. All of the following must keep working exactly as they do today:
- Ordinary documents: elements, attributes, character data, CDATA sections, predefined entities and explicit encoding declarations.
- Namespaces, including prefixed and default declarations, with the application still receiving the expanded names it receives today.
- Valid multi-byte UTF-8 names. Legitimate non-ASCII element names must still be accepted — the fix is to reject what is genuinely malformed, not to restrict names to ASCII.
- Large documents and deeply nested but well-formed documents.
- The library must still compile, and the unit-test suite under
/testbed/expat/testsmust still pass. That suite is a subset provided for local iteration; grading builds and runs the project's own complete suite, so do not tune your patch to the copy you can see.
Deliverables
After patching, write two files:
/app/output/findings.json— a JSON array; one entry per distinct weakness you fixed, each with:vulnerability_type: a CWE identifier (formatCWE-###)severity:LOW,MEDIUM,HIGH, orCRITICALaffected_files: list of files under/testbedyou changed for itroot_cause: the underlying flaw in one or two sentencesdescription: the unsafe input-to-effect flow and its impact
/app/output/plan.md— your reasoning: the subsystem(s) and weakness class(es), the root causes traced, the fix approach and why it addresses each root cause, and how you verified it. Graded for reasoning quality independently of whether the patch passes every test.
Local testing
Build the library, the xmlwf checker and the tests:
cd /testbed && cmake -S expat -B build -DEXPAT_BUILD_TESTS=ON -DEXPAT_BUILD_TOOLS=ON \
&& cmake --build build -j2
Screen a document the way the service does (exit status 0 = accepted):
printf '<doc>hi</doc>' > /tmp/d.xml && ./build/xmlwf/xmlwf -n /tmp/d.xml
Run the library's own unit tests:
cd /testbed/build/tests && ./runtests
To reproduce arithmetic faults, build with sanitizers enabled — this is how the graded build is configured:
cmake -S expat -B build-san -DEXPAT_BUILD_TESTS=ON -DEXPAT_BUILD_TOOLS=ON \
-DCMAKE_C_FLAGS="-fsanitize=undefined -g" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=undefined"
Write byte sequences with a tool that emits raw bytes (printf '\xNN'); a text
editor will silently normalize exactly the input you need to investigate.
Constraints
- Fix root causes in the parser, not individual documents. A fix that special-cases the exact byte sequences a checker might use will not survive variant inputs and is not acceptable — validate against the grammar and the encoding rules the specification defines.
- No new third-party dependencies; the existing C toolchain is sufficient.
- Do not disable, remove, or stub out features; do not make the parser reject
input wholesale; do not edit files outside
/testbed. - Do not modify anything under
/testbed/expat/tests. Those tests are provided for your own local iteration; grading uses the project's pristine suite, so editing a test to match your implementation fixes nothing and will be scored as a failure of that behavior. - Closed-book: work from the source and observed local behavior, not external writeups or scanners.
-
Work in concrete, bounded steps. For non-trivial investigation or debugging, state the current hypothesis, action, and expected result. Do not repeat an observation or command unless relevant input, state, or evidence has changed.
-
Keep debugging hypothesis-driven. Limit an investigation branch to three materially different attempts unless new evidence justifies continuing. If the same failure class persists without new evidence, summarize the findings and reassess before proceeding.
-
Keep terminal operations bounded, recoverable, and concise. Use appropriate timeouts for commands that might hang. Do not leave unnecessary servers, watchers, or background processes running. Prefer targeted excerpts and summarized logs over large output dumps.
-
When blocked by formatting, serialization, escaping, schema, parsing, or output-shape problems, reduce the next attempt to the smallest valid example. Do not retry an unchanged malformed input.
-
Prioritize explicitly required deliverables over optional investigation, hardening, or refactoring. Leave sufficient time to validate required outputs. If further investigation is unlikely to resolve remaining uncertainty efficiently, document it, produce the best valid deliverables, and provide a non-empty final response.