Repository navigation
Conversation
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Parser context cleanup src/request_body_processor/xml.h, src/request_body_processor/xml.cc |
The XML class declares and uses freeArgsParserCtx(). The helper frees m_data.parsing_ctx_arg with xmlFreeParserCtxt() and resets the pointer to null during destruction and malformed-document failure handling. |
Malformed XML regression coverage test/test-cases/regression/request-body-parser-xml-into-args.json, test/test-suite.in |
Two malformed XML cases verify the failure log, HTTP 200 response, and empty body. The test suite manifest registers the new test file. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Possibly related issues
- owasp-modsecurity/ModSecurity#3563: This issue concerns the malformed-document failure path, but this change does not alter the error decision or address the reported false positive.
Possibly related PRs
- owasp-modsecurity/ModSecurity#3647: Both changes modify ARGS libxml2 parser-context handling in
xml.cc; they address complementary parts of the context lifecycle.
Suggested reviewers: airween
Merge Risk: 
Minimal · up to 11db8
-
The parser cleanup change is ready to merge with no established current-head risk.
Pre-merge checks: 
5
-
Full details: Title check
-
Explanation
The title "Fix leak of the ARGS XML parser context on malformed documents" matches the main change. The raw summary shows XML now frees the ARGS parser context in the destructor and before complete() returns failure for a malformed document. The objectives describe the same fix, and the regression test covers malformed XML inputs. The title is specific, short, and free of file lists or vague terms.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
370210c to
030d382
Compare
|
fzipi
left a comment
There was a problem hiding this comment.
Thanks @tomsommer, good find, and a clean fix.
What I checked:
- Ran macOS
leaks --atExitagainstregression_testswith your newrequest-body-parser-xml-into-args.json. On master it reports 24 leaks / 34,176 bytes, rooted in twoxmlNewParserCtxtallocations (one per case). With this PR it reports 0 leaks, andrequest-body-parser-xml.jsonstill passes. - No double free:
freeArgsParserCtx()nulls the pointer, and the remaining frees incomplete()null it too. The copy inxml_parser_stateis only used from SAX callbacks while parsing, andxmlFreeParserCtxt()doesn't call those. In~XML()the free happens before the members holding the SAX handler and user data are destroyed. - The new test file is registered in
test-suite.inand runs undermake check. The full suite with this PR merged together with #3632, #3635 and #3636 has no failures.
Comments:
- (non-blocking) The new JSON cases also pass on master, since they only check the "Failed to parse document" log line. They're useful coverage of the path, but a regression of the leak itself would only show up under valgrind or leaks, and CI has no such job. No change needed; noting it for the record.
- (non-blocking) The only red jobs are the two macOS LMDB ones (
action-expirevar.json,issue-1831.json). Nothing here touches collection/LMDB code, the same job is green on current v3/master, and these runs predate some LMDB fixes there, so a rebase or re-run should clear them.
LGTM.
With SecParseXmlIntoArgs set to On, XML::processChunk() creates a second libxml2 push parser context, xml_data::parsing_ctx_arg, in addition to the regular parsing_ctx. That context is only released in XML::complete(), in the block that terminates the ARGS parsing. XML::complete() finishes the main context first and returns early with "XML: Failed to parse document." when the document is not well formed, before reaching the block that frees parsing_ctx_arg. ~XML() released parsing_ctx and doc but not parsing_ctx_arg, so nothing ever freed it. A remote client could therefore leak the ARGS parser context, including its body-sized input buffer, on every request carrying a malformed XML body. parsing_ctx_arg is now freed in ~XML() and, through the same small helper (XML::freeArgsParserCtx()), also freed and cleared before the early return in XML::complete() so that the memory is released as soon as the parsing is known to have failed rather than at the end of the transaction. The copy of the pointer kept in xml_data::xml_parser_state is only used from the SAX callbacks while parsing is in progress, and both changes stay inside the existing WITH_LIBXML2 guard. A regression test covering a mismatched end tag and a truncated document is added; under valgrind the unfixed code reports the context allocated in xmlCreatePushParserCtxt() as definitely lost for each request.
030d382 to
11db826
Compare
|



what
xml_data::parsing_ctx_argin~XML(), following the existing pattern forparsing_ctx.parsing_ctx_argbefore the earlyreturn falseinXML::complete()when the document is not well formed.test/test-cases/regression/request-body-parser-xml-into-args.json(mismatched end tag, truncated document) withSecParseXmlIntoArgs On.why
SecParseXmlIntoArgs On,XML::processChunk()creates a second libxml2 push parser context (parsing_ctx_arg) for the ARGS extraction. It was only released in the second block ofXML::complete().complete()finishes the main context first and returns early with "XML: Failed to parse document." when the document is not well formed, before reaching that block.~XML()releasedparsing_ctxanddocbut notparsing_ctx_arg, so nothing ever freed it.Transaction::processRequestBody()).xml_data::xml_parser_stateis a non-owning alias only dereferenced from SAX callbacks while parsing is in progress, so freeing in~XML()cannot double free. Both edits are inside the existingWITH_LIBXML2guard.Evidence, unfixed tree,
libtool --mode=execute valgrind --leak-check=full ./regression_tests test-cases/regression/request-body-parser-xml-into-args.json:With the fix:
All heap blocks were freed -- no leaks are possible.make check: TOTAL 5045, PASS 5029, SKIP 16, FAIL 0.references
Summary by CodeRabbit
Bug Fixes
Tests