Skip to content

Do not keep the @validateSchema parser errors in the operator - #3636

Open
tomsommer wants to merge 2 commits into
owasp-modsecurity:v3/masterfrom
tomsommer:fix/validateschema-error-string-growth
Open

tomsommer wants to merge 2 commits into
owasp-modsecurity:v3/masterfrom
tomsommer:fix/validateschema-error-string-growth

Conversation

@tomsommer

@tomsommer tomsommer commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

what

  • ValidateSchema::evaluate() passes a local std::string as the libxml2 schema-parser error/warning context instead of the operator member m_err; the member is removed.
  • Regression case added to request-body-parser-xml.json: the existing bad-schema case with multiMatch, so the same operator instance is evaluated twice in one transaction; it asserts the error text is not repeated. It fails before the change and passes after it.

why

  • error_load() / warn_load() append into their context and nothing ever cleared m_err. An operator instance lives as long as the rule set, i.e. the process lifetime in nginx, and the schema is re-parsed on every evaluation. So every evaluation whose schema parse emits a warning or error grew the string without bound, the logged message for one transaction contained the messages of all previous ones, and concurrent transactions appended to the same string, which is a data race.
  • The accumulated text is per-evaluation state, so it belongs in a local. The check of it in the branch where the parser context cannot be created is dropped because no callback has run at that point.
  • The member sat inside the WITH_LIBXML2 guard and had no other reader; --without-libxml compilation of the translation unit was verified.

Evidence (unfixed tree, new test): the second evaluation logs

XML: Failed to load Schema: test-cases/data/SoapEnvelope-bad.xsd. XML Error: Failed to parse the XML resource '...'.
XML Error: Failed to parse the XML resource '...'.

with the message repeated once per previous evaluation. After the fix each evaluation logs it once. This is growth of a live string, so valgrind reports no lost blocks; the test is the evidence. make check on this branch: TOTAL 5044, PASS 5028, SKIP 16, FAIL 0.

references

Summary by CodeRabbit

  • Bug Fixes
    • Improved XML schema validation error reporting in debug logs.
    • Prevented repeated schema evaluations from producing duplicate XML error messages or carrying over stale diagnostics.
    • Requests with an invalid SOAP schema continue to be rejected as expected.
  • Tests
    • Added regression coverage for repeated XML schema validation, including checks for the response status and diagnostic output.

@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e0f62dfe-184a-48d5-a0c3-d1be20d8082a
Commits

Reviewing files that changed from the base of the PR and between a7f1d9d and c85e26e.

Files selected for processing: 1
  • src/operators/validate_schema.cc

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

Walkthrough

The schema validation operator now stores parser diagnostics in a local string instead of a persistent member. Failure logs use the local diagnostics. A regression test checks repeated evaluations with a bad schema.

Changes: Schema Error Isolation

Layer / File(s) Summary
Local schema error handling
src/operators/validate_schema.cc, src/operators/validate_schema.h
ValidateSchema::evaluate collects parser errors in a local string and uses it in failure logs. The persistent m_err member was removed.
Repeated evaluation regression coverage
test/test-cases/regression/request-body-parser-xml.json
The XML request-body test checks error output across repeated bad-schema evaluations and uses the updated validation rule options.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • owasp-modsecurity/ModSecurity#3678: This PR changes parser-error handling in the @validateSchema path; the issue’s proposed REQBODY_ERROR_MSG behavior remains unimplemented.

Suggested labels: 3.x

Suggested reviewers: zimmerle

Merge Risk: Minimal · up to c85e2

No actionable merge-blocking risk was identified in the schema error isolation change.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within survi… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Do not keep the @validateSchema parser errors in the operator" matches the main change. The PR stops ValidateSchema::evaluate() from keeping parser error text in the m_err member. It passes…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Do not keep the @validateSchema parser errors in the operator" matches the main change. The PR stops ValidateSchema::evaluate() from keeping parser error text in the m_err member. It passes a local string as the libxml2 error context and removes m_err. The title is specific and short, and it names the operator and the behavior that changed.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

@fzipi fzipi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @tomsommer, this is the right fix: the schema-parse error text is per-evaluation state, so it belongs in a local and not in the shared operator.

What I checked:

  • Built master and this branch (libxml2 2.9.13). On master your new request-body-parser-xml.json case fails: the second evaluation's message carries the first one's XML Error: ... line. With the change, the file is 6/6.
  • The libxml2 callbacks run synchronously inside xmlSchemaParse(), and the parser context is freed on every return path while schemaErr is still in scope, so there is no dangling ctx. evaluate() no longer writes any member, so the cross-transaction race on m_err is gone too.
  • Ran the full make check with this PR merged together with #3632, #3633 and #3635: no failures.

Comments:

  1. (non-blocking, pre-existing) Since you are touching that block: std::stringstream err("XML: Failed to create validation context."); followed by err << " " << schemaErr; overwrites the start of the message, because the stream does not start in append mode. Building a std::string, or opening the stream with std::ios_base::out | std::ios_base::ate, would fix it. Fine to leave for a follow-up.
  2. (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.

ValidateSchema::evaluate() installs ValidateSchema::error_load() and
ValidateSchema::warn_load() as the libxml2 schema parser callbacks and
passes the address of the ValidateSchema member m_err as their user
context. Those callbacks append to that string and nothing ever clears
it.

An operator instance is created while the rules are parsed and lives for
as long as the rule set does, which for a web server means the lifetime
of the process. Therefore:

 * every evaluation whose schema parse emits a warning or an error makes
   m_err grow by the size of the new message, without any bound, and
 * the message logged for one transaction contains the messages of all
   the previous transactions that used the same rule, and
 * concurrent transactions sharing the rule set append to the same
   std::string at the same time, which is a data race.

The accumulated text is per evaluation state, so it is moved to a local
std::string in evaluate() and the member is removed from the header. The
member was declared inside the WITH_LIBXML2 guard and is only used by
the guarded code, so builds configured with --without-libxml are
unaffected.

The check of the accumulated text in the branch where the parser
context itself cannot be created is dropped: at that point no callback
has run yet, so the local is always empty.

The unbounded part of the growth is cross request and cannot be observed
by the single transaction regression harness, but the accumulation is
visible within one transaction as soon as the same operator instance is
evaluated twice. The regression test added to
request-body-parser-xml.json therefore reuses the existing
SoapEnvelope-bad.xsd case, which fails to parse, and adds multiMatch so
that the operator runs once per transformation. Before this change the
debug log of the second evaluation reads

  XML: Failed to load Schema: test-cases/data/SoapEnvelope-bad.xsd. XML Error: Failed to parse the XML resource '...'.
  XML Error: Failed to parse the XML resource '...'.

with the message repeated once per previous evaluation; after it every
evaluation logs the message exactly once. The expected debug_log of the
new test asserts that no log line starts with a repeated "XML Error:",
so it fails before the change and passes after it.
@tomsommer
tomsommer force-pushed the fix/validateschema-error-string-growth branch from 9e48194 to a7f1d9d Compare October 27, 2026 05:43
std::stringstream err("...") does not open the stream in append mode,
so the following err << " " << schemaErr wrote over the beginning of
"XML: Failed to create validation context." instead of after it. Build
the message with operator<< like the other two error paths do.

xmlSchemaNewValidCtxt() only fails on allocation failure, so there is
no regression test for this path.
@tomsommer

Copy link
Copy Markdown
Contributor Author

Thanks @fzipi. Rebased on current v3/master, and the stringstream is fixed in c85e26e: the message is now built with operator<< like the other two error paths. xmlSchemaNewValidCtxt() only fails on allocation failure, so there's no regression test for that branch.

@sonarqubecloud

Copy link
Copy Markdown

@tomsommer

Copy link
Copy Markdown
Contributor Author

@fzipi your approval was dismissed by the c85e26e push (rebase plus the operator<< change you suggested), so the PR is blocked on a fresh review again. Nothing else has changed since. Could you re-approve when you have a moment?

This branch has not been deployed

No deployments
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.

2 participants