http rules: make the LogQL prefilter a superset of classify()

`http-sqli` was unreachable end to end: the prefilter admitted only
`union select`, so boolean (`' OR '1'='1`, `%27%20OR%20…`) and time-based
(`sleep(`, `waitfor delay`) payloads were dropped in Loki before Python saw
them, and the canary passed because it used the one admitted shape.

The prefilter now carries the missing shapes as plain literals (a nested
`(?:...)` group made Loki's filter 10x slower, and re-ordering the alternation
measured 20-60 s, so the old block stays first and the additions append).
Over-match measured on 2026-11-27 06:00-07:00Z over 29 426 lines: 1 202 -> 1 548
lines/h (+29 %), 10-minute query 3.9 s. The extra lines are Forgejo browse URLs
that classify() drops; a calm 10-minute window of 394 admitted lines yields 0
findings.

Durability: tests/fixtures/attack-shapes.json is one table of 105 payload
shapes iterated by both tests - classify() must flag each one and
config.HTTP_LINE_FILTER must match its serialized access line. It fails with 44
dropped shapes on the old filter and passes now, so this class of drift (any
rule kind, not just sqli) cannot come back silently.

Docs also record the data-source limit the bug surfaced: Cloudflare collapses
dot-segments in the path, so `/../../etc/passwd` arrives as `/etc/passwd` and
path traversal is only detectable in a query string (or off-tunnel).
This commit is contained in:
Sirius DevOps 2026-11-27 03:13:16 -05:00
commit ef7e584d27
5 changed files with 867 additions and 18 deletions

View file

@ -8,7 +8,10 @@ ending in `.gitignore`, one ending in `.env.example` behind an RSS route, and a
from __future__ import annotations
import json
import re
import unittest
from pathlib import Path
from netsec_scan import config, http_rules
@ -86,6 +89,66 @@ class TestClassification(unittest.TestCase):
self.assertIsNone(http_rules.classify(rec(path, code=200, ua=ua)), path)
class TestPrefilterSuperset(unittest.TestCase):
"""The LogQL prefilter must admit every shape `classify()` can flag.
A too-narrow prefilter is invisible from Python: Loki never returns the
line, so no amount of testing `classify()` can catch it. That is how
`http-sqli` shipped unreachable for weeks — only `union select` reached the
classifier, and the canary happened to use exactly that shape.
One table, iterated by both tests: `tests/fixtures/attack-shapes.json` is
the contract. Adding a rule or a payload shape to `http_rules` without
adding it here (and to `config.HTTP_LINE_FILTER`) is the drift this catches.
"""
@staticmethod
def table():
path = Path(__file__).with_name("fixtures") / "attack-shapes.json"
return json.loads(path.read_text(encoding="utf-8"))["shapes"]
@staticmethod
def record(shape):
return {
"path": shape["path"],
"method": shape.get("method", "GET"),
"response_code": shape.get("response_code", 404),
"user_agent": shape.get("user_agent", "curl/8.23.0"),
"authority": shape.get("authority", "siriusdevops.com"),
"client_ip": shape.get("client_ip", "203.0.113.7"),
"peer": "10.10.0.1",
}
def test_table_matches_classify(self):
for shape in self.table():
with self.subTest(path=shape["path"]):
hit = http_rules.classify(self.record(shape))
self.assertIsNotNone(hit, shape["path"])
self.assertEqual(
hit, (shape["category"], shape["severity"], shape["rule"]))
def test_logql_prefilter_admits_every_classified_shape(self):
prefilter = re.compile(config.HTTP_LINE_FILTER)
missed = [
shape["path"]
for shape in self.table()
if not prefilter.search(json.dumps(self.record(shape), separators=(",", ":")))
]
self.assertEqual(
missed, [],
"classify() flags these, but the Loki prefilter drops them before "
"Python ever sees the line: %r" % missed[:8])
def test_the_four_reported_sqli_payloads_are_in_the_table(self):
"""Regression pin for t_a3604aab: these four were dropped, verbatim."""
shapes = {s["path"] for s in self.table()}
for path in ("/?id=1%27%20OR%20%271%27=%271",
"/?id=1' OR '1'='1",
"/?id=1%27%3BWAITFOR%20DELAY%20%270:0:5%27",
"/?q=1%27%20AND%20sleep(5)--"):
self.assertIn(path, shapes)
class TestForgejoFalsePositives(unittest.TestCase):
"""The shapes that made a naive rule unusable on this stream."""