Skip to content

feat: filter expression rules + IP enrichment (#8 #9) - #24

Merged
cport1 merged 1 commit into
mainfrom
feat/filter-rules-enrichment
Jul 19, 2026
Merged

feat: filter expression rules + IP enrichment (#8 #9)#24
cport1 merged 1 commit into
mainfrom
feat/filter-rules-enrichment

Conversation

@cport1

@cport1 cport1 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

The flagship P1 parity feature: expression-based rules with an admin builder, plus the IP enrichment that powers their ip.* fields.

What's here

Filter expression language (#9) — a faithful PHP port of @webdecoy/node's filter/ engine (tokenizer → recursive-descent parser → evaluator):

  • Fields: ip.vpn/proxy/tor/relay/hosting, ip.country/country_name/city/timezone, ip.asn/asn_org, ip.abuse_score/total_reports/is_high_risk, req.path/method/ip/user_agent, req.header("name").
  • Operators: and/or/not, ==/!=/>/>=/</<=, in/not in, matches (regex ≤500 chars).
  • Parsed at construction — a syntax error fails fast and is surfaced in the admin, never at request time.

IP enrichment (#8)WebDecoy_IP_Enrichment: GET /api/v1/sdk/ip/{ip}/enrichment, Bearer key, 1h transient cache (+ short negative cache so a bad IP doesn't refetch), fail-open. Fetched lazily — only when a configured filter rule actually references ip.*, and only with an API key (premium). Without a key, ip.* resolves to undefined and its comparisons are false; req.* rules still work.

Admin Rules tab — add/remove rule rows (name, expression, action block/throttle, dry-run), a field/operator reference, and inline syntax errors. @webdecoy/node has no such builder; here non-developers can author rules.

Parity is the whole point

Docs teach one syntax, so an expression must mean the same thing in both SDKs. The evaluator reproduces JavaScript semantics that PHP would otherwise get wrong:

  • numeric ===ip.asn == 4808 matches an integer enrichment value against a float literal (JS treats 1 === 1.0 as equal; naive PHP === would not);
  • number vs string are never equal (ip.asn == "4808" is false);
  • JS truthiness ("0" and [] are truthy);
  • fail-open — any comparison touching an undefined operand is false, and and/or short-circuit exactly as node does.

Tests

tests/FilterTest.php — 12 parity tests covering operators, precedence, arrays/in, matches, req.header(), the int/float === trap, number-vs-string, fail-open with no enrichment, dry-run, needsEnrichment, and syntax-error fail-fast. Full suite 35 assertions, green (php tests/run.php). The enrichment client's cache/fail-open behavior was verified separately with a stubbed-WP harness.

Closes #8, closes #9. Part of #16.

Ports @webdecoy/node's filter DSL to PHP and adds an admin rule builder.

- sdk/src/Rules/Filter/{Tokenizer,Parser,Evaluator} + FilterRule: faithful
  port of node's tokenizer → recursive-descent parser → evaluator. Fields:
  ip.vpn/proxy/tor/relay/hosting, ip.country/city/timezone/asn/asn_org,
  ip.abuse_score/total_reports/is_high_risk, req.path/method/ip/user_agent,
  req.header("name"). Operators: and/or/not, ==/!=/>/>=/</<=, in/not in,
  matches. Parsed at construction (fail fast). JS semantics reproduced
  exactly — numeric === (1 === 1.0), number!=string, JS truthiness, and
  fail-open on undefined operands — so an expression means the same thing in
  both SDKs.
- WebDecoy_IP_Enrichment: GET /api/v1/sdk/ip/{ip}/enrichment, Bearer key,
  1h transient cache (+ short negative cache), fail-open. Fetched only when a
  configured filter rule references ip.* (premium only).
- Wiring: filter_rules option (repeater), sanitize with parse-on-save
  validation (invalid rules flagged + disabled, never fatal), FilterRules fed
  into the engine, req.header lookups get the full request header map.
- New Settings → Rules tab: add/remove rule rows, expression + action +
  dry-run, inline syntax errors, field/operator reference.
- tests/FilterTest.php: 12 parity tests incl. the int/float === trap,
  precedence, fail-open, and syntax-error fail-fast (35 assertions total).

Closes #8, closes #9. Part of #16.
Co-authored-by: Claude <noreply@anthropic.com>
@cport1
cport1 merged commit 491fa45 into main Jul 19, 2026
3 checks passed
@cport1
cport1 deleted the feat/filter-rules-enrichment branch July 19, 2026 20:03
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.

Filter rules — expression DSL + admin rule builder IP enrichment client — VPN/proxy/Tor, geo, ASN, abuse score

1 participant