ModSecurity v2 reference adapter
Runs the conformance data in ../../tests against
ModSecurity v2.9.15 as unittest,
inside the container image Dockerfile builds: Debian bookworm, Apache httpd 2.4,
mod_security2.so and v2’s own unit tester msc_test, both built from the v2.9.15 tag.
docker build -t seclang-modsecurity2 -f adapters/modsecurity2/Dockerfile .
docker run --rm -v "$PWD":/repo seclang-modsecurity2 \
uv run python -m unittest discover -s adapters/modsecurity2 # everything
docker run --rm -v "$PWD":/repo seclang-modsecurity2 \
uv run python -m unittest discover -s adapters/modsecurity2 -p test_adapter.py # adapter self-testsThe image sets MODSECURITY2_HTTPD, MODSECURITY2_MODULE and MODSECURITY2_MSC_TEST;
without them (on the host) the conformance tests and the engine smoke tests are skipped.
The image is built from the repository root so it can install the locked Python
dependencies (pyproject.toml, uv.lock) into /opt/venv; Dockerfile.dockerignore
narrows the build context to those two files. A run therefore needs no network, leaves the
host’s .venv alone and writes no bytecode into the checkout. The Debian base is pinned by
digest; apt resolves bookworm’s current packages when the image is built (Apache 2.4.68 on
2026-11-09), and CI keeps the built image cached until the Dockerfile or the lock changes.
v2 has no library API to drive in-process: standalone/ does not link on v2/master
(the fix is unmerged on v2/issue-3451-standalone-link), so the adapter runs the module
the way it is deployed, under Apache. data.py, gaps.py, engine_tier.check_stage and
unit_tier.unit_rules are imported from ../libmodsecurity; they do not load the v3
library.
How results are gated
compat/known-gaps.md is the expected-failure list; rows whose Engine cell names
ModSecurity v2 apply. For each test file:
| Row for this engine | Result | Outcome |
|---|---|---|
| none | all pass | pass |
| none | any failure | fail (a real defect in the engine or in the specification) |
| present | any failure | pass, failures printed as expected (known gap: …) |
| present | all pass | fail with known-gaps row is obsolete (remove the row) |
Extended features v2 does not implement are skipped, not failed: profiles through
requires: (IMPLEMENTED in test_conformance.py), unit files through the
UNSUPPORTED_* sets (empty for v2.9.15).
How the tiers are driven
- Engine tier (
v2_engine.py,httpd.py): per profile a temporaryServerRootholds the profile’sfiles:, its rules and anhttpd.confthat loadsmpm_event,authz_core,unique_id(v2 refuses every request without it),remoteip,proxy,proxy_httpandsecurity2, proxies every request to a stub backend and appendsSecDebugLog/SecDebugLogLevel 4after the rules.apache2 -tdecidesexpect_error; thenapache2 -X(one process, so the debug log is in request order) serves the profile’s stages. Each stage is sent raw over a socket; the backend answers with the stage’sresponse(or200, empty), so phases 3–5 run.- Triggered rules: v2.9.15 writes one debug-log line per matched rule,
nologincluded, and for a chain only when every member matched (with the head’s id). The adapter reads the[id "N"]of every line startingWarning.orAccess. - Interruption:
Access denied with code N(deny),with connection close(drop),with redirection to URL using status N(redirect),using proxy to(proxy). The status is v2’s own from that line: Apache answers 500 for a code it has no reason phrase for (418). An error status with no such line is v2’s (rule_id: 0) only when v2 wrote a non-ruleModSecurity:message to the error log (a body limit, a body processor error); any other status the backend was not asked for is reported asApache answered N without ModSecurity, not as an engine result. Rule messages alone do not count: Apache rejects..in a URI after phase 1 has run. - Client address: a
remote_addrother than127.0.0.1is sent inX-Seclang-Remote-Addr, whichmod_remoteipturns intoREMOTE_ADDR(IPv4 and IPv6) and removes before rules seeREQUEST_HEADERS. - Logs: phase 5 runs after the response is sent, so each stage waits (up to 2 s) for
the logging phase’s
Audit log: …line.log_containssearches the error log, where v2 writes itsModSecurity:messages.
- Triggered rules: v2.9.15 writes one debug-log line per matched rule,
- Unit tier (
v2_unit.py):msc_test -t tfn|op -n NAME -p PARAM [-r RET]with the input on stdin as raw bytes;-pis escaped as\xNNfor every byte outside 0x21–0x7e and every\(tests/msc_test.cdecodes it). Exit 0 is a pass.msc_testreports only the operator’s return code, so there_groupscases run through httpd with the v3 adapter’s capture rules plusctl:forceRequestBodyVariable=On(v2 leavesREQUEST_BODYabsent without a body processor, ADR-0022); those whose parameter holds a control character cannot be written as a directive and fall back tomsc_test(match result only).
Known limits
- The debug-log formats are those of v2.9.15;
test_adapter.ParseDebugLogTestspins them. - Apache sits in front of the engine: a request it rejects (malformed request line,
%not followed by two hex digits,..above the root) never completes in v2. Such a failure says so and is never a known-gaps row; it waits the full 2 s for a logging phase that does not come. - Each profile starts its own httpd (about 0.1 s); the whole run takes about 25 s.