fix: load rules by Include, report the blocking rule, and run the CRS tests - #8
Merged
Conversation
… tests
Three problems, the third of which is why the first two went unnoticed.
**Rule files were concatenated, which broke CRS data paths.** `load_rules`
read every `--rules` glob match and spliced the text together, losing the one
thing the engine needs to resolve `@pmFromFile`/`@ipMatchFromFile` paths and
nested `Include`s: which file a directive came from. Those paths are relative
to their own rule file, so a concatenated string resolved them against the
process's working directory instead. Stock CRS references
`scanners-user-agents.data` among others, so the command in the README:
zentinel-zentinelsec-agent --rules /etc/modsecurity/crs/crs-setup.conf \
--rules "/etc/modsecurity/crs/rules/*.conf"
failed at startup unless it happened to be run from /etc/modsecurity/crs/rules.
The patterns are still resolved here, so a bad path is still reported against
the pattern that produced it, but the engine is now handed quoted absolute
`Include` directives and does the reading itself. Same rules, 703 of them,
independent of the working directory.
**A block reported the wrong rule.** `X-WAF-Rule` is built from the first
reported id, and the ids came from `matched_rules()` -- every rule that
matched, including CRS setup actions and chain starters whose chain never
completed. Under stock CRS its first entry is 900990, a setup rule, not the
rule that blocked. They now come from `Intervention::rule_ids`, which is the
rules that caused the intervention, and `X-WAF-Message` carries the rule's own
`msg` instead of a constant string.
**The CRS integration tests never ran, and could not have failed if they had.**
They returned early when `test-rules/crs` was absent and CI never fetched it,
so they reported green as skips. The assertion was also vacuous:
`assert!(blocked || !rule_ids.is_empty())` where `rule_ids` came from
`matched_rules()`, which under stock CRS always contains the 901xxx setup
rules -- so it held whatever the engine did. And the fixture loaded five
hand-picked rule files, omitting REQUEST-920-PROTOCOL-ENFORCEMENT.conf, which
is where the rule that denies every request lives.
They now load the full rule set through `ZentinelSecEngine` itself rather than
a separately built engine, fail loudly with fetch instructions when the
checkout is missing, and assert on which rules fire. CI fetches CRS.
Two of the new tests -- that ordinary traffic is not blocked -- are `#[ignore]`d
because they fail against zentinel-modsec 0.3.0, where CRS 920100 denies every
request. They are the acceptance criteria for that dependency bump; remove the
attribute once it lands.
Adds `ZentinelSecEngine::rule_count` and `::modsec` so tests can exercise the
rules the agent actually loaded.
Closes #7
Refs zentinelproxy/zentinel-modsec#29
Refs zentinelproxy/zentinel-modsec#30
Claude-Session: https://claude.ai/code/session_01VkHAQ33U78fmpk6r161DKc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #7.
Three problems, the third of which is why the first two went unnoticed.
Rule files were concatenated, which broke CRS data paths
load_rulesread every--rulesglob match and spliced the text together. That loses the one thing the engine needs to resolve@pmFromFile/@ipMatchFromFilepaths and nestedIncludes: which file a directive came from. Those paths are relative to their own rule file, so a concatenated string resolved them against the process's working directory.Same rule set, same engine version, two ways of handing it over:
Stock CRS references
scanners-user-agents.dataamong others, so the command in the README failed at startup unless it happened to be run from/etc/modsecurity/crs/rules.The patterns are still resolved here — a bad path is still reported against the pattern that produced it, and the file count is still available for logging — but the engine is now handed quoted absolute
Includedirectives and does the reading itself. Quoted, so a path containing spaces stays one argument.A block reported the wrong rule
X-WAF-Ruleis built from the first reported id, and the ids came frommatched_rules()— every rule that matched, including CRS setup actions and chain starters whose chain never completed. Under stock CRS:So whoever was debugging a block got a CRS setup rule. Ids now come from
Intervention::rule_ids, andX-WAF-Messagecarries the rule's ownmsgrather than the constant"Blocked by ZentinelSec".The CRS tests never ran, and could not have failed if they had
Three separate reasons, each sufficient on its own:
test-rules/crswas absent, and CI never fetched it — so they reported green as skips.assert!(blocked || !rule_ids.is_empty()), whererule_idscame frommatched_rules(), which under stock CRS always contains the 901xxx setup rules. It held no matter what the engine did.REQUEST-920-PROTOCOL-ENFORCEMENT.conf— the file containing the rule that denies every request.They now load the full rule set through
ZentinelSecEngineitself, rather than a separately built engine that could load it differently; fail loudly with fetch instructions when the checkout is missing (ZENTINELSEC_SKIP_CRS_TESTSopts out locally, CI must not); and assert on which rules fire. CI fetches CRS.Two tests are ignored on purpose
crs_does_not_block_ordinary_trafficandcrs_does_not_block_an_ordinary_xml_postfail againstzentinel-modsec0.3.0, where CRS 920100 denies every request:That is the defect fixed by zentinelproxy/zentinel-modsec#30, so these are
#[ignore]d with that reason recorded in the attribute. They are the acceptance criteria for the dependency bump — remove the attribute when it lands and they should pass. (The duplicated id in that output is also fixed there.)I would rather leave them present-and-ignored than delete them: they are the tests whose absence let a 100%-block-rate defect ship.
Verification
cargo clippy --workspace --all-targets -- -D warningsclean.cargo fmt --all --checkclean.ZentinelSecEngine::rule_countand::modsecso tests can exercise the rules the agent actually loaded.