feat: filter expression rules + IP enrichment (#8 #9) - #24
Merged
Conversation
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>
15 tasks
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.
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'sfilter/engine (tokenizer → recursive-descent parser → evaluator):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").and/or/not,==/!=/>/>=/</<=,in/not in,matches(regex ≤500 chars).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 referencesip.*, 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/nodehas 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:
===—ip.asn == 4808matches an integer enrichment value against a float literal (JS treats1 === 1.0as equal; naive PHP===would not);ip.asn == "4808"is false);"0"and[]are truthy);and/orshort-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.