feat(sdk): a typed decision, and characteristics for who the caller is - #28
Merged
Conversation
protect() returned { allowed, detection } and the adapters typed the value
they handed to onBlocked as `any`, so integrating meant destructuring a
blob and string-matching to find out which rule fired. Three things the
SDK already knew had nowhere to go:
- Which rules ran. The engine collapsed to one deciding rule plus a
violations array, so a filter rule that never ran for want of enrichment
was indistinguishable from one that ran and passed. Both reported ALLOW.
Every rule now appears in results[] with RUN / DRY_RUN / NOT_RUN /
CACHED, and a dry-run rule that matched reports DENY -- what it WOULD
have done is the reason you turned it on.
- "Challenge this one." We ship a proof-of-work captcha and the decision
type could not express it, so it was reachable only by hand.
- A stable id. 'rule_' + Date.now() is not unique under concurrency and
correlates with nothing.
characteristics answers a separate question the SDK had also hard-coded:
rate limits keyed on IP, which is the wrong subject for an authenticated
API and the wrong subject for us specifically, since the actor model
exists because IP is not identity. An absent characteristic falls back to
the IP rather than keying under empty -- otherwise one tenant's limit
takes out all anonymous traffic, invisibly.
Decision caching is deliberately narrow: server-derived DENY and CHALLENGE
only. Caching ALLOW is how a client that has since started misbehaving
keeps sailing through, and rule outcomes must not be cached because a rate
limiter has to see every request.
allowed is unchanged, including failing open on error. 327 tests pass, and
the any budgets drop (fastify to zero).
Closes WebDecoy/app#730
Closes WebDecoy/app#731
This was referenced Aug 22, 2026
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 #730 and #731. First of the milestone-26 batch, and the foundational one — #734 (testing utilities) and the
anyburndown both build on it.#730 — the decision
protect()returned{ allowed, detection }, and all three adapters typed the value they hand toonBlockedasany. Integrating meant destructuring a blob and string-matching to find out which rule fired. Three things the SDK already knew had nowhere to go:Which rules ran. The engine collapsed to one deciding rule plus a violations array. A
filter()rule that never ran for want of IP enrichment and one that ran and passed both reported ALLOW — indistinguishable, and the first reads as "checked, and this IP is fine". Every rule now appears inresults[]:stateRUNDRY_RUNNOT_RUNCACHEDA dry-run rule that matched reports
conclusion: 'DENY'withstate: 'DRY_RUN'. Reading itsactionverbatim would show it as ALLOW, which is the opposite of what the operator turned it on to see."Challenge this one." We ship a proof-of-work captcha in
@webdecoy/clientand the decision type could not express a challenge, so it was reachable only by wiring it up by hand.CHALLENGEis now a conclusion, produced when the server says challenge and the score cleared the threshold.A stable id.
'rule_' + Date.now()is not unique under concurrency and correlates with nothing. Now a randomdec_…, also stamped ondetection.detection_idso the two join. Random rather than sequential because ids leave the process, and a counter leaks request volume to anyone who sees two.Plus
deniedBy('tripwire'), andERRORas a distinct conclusion fromDENY— both serve the request, but only one of them means "we never decided".Compatibility.
allowedis unchanged, including failing open on ERROR.onBlockedgets the decision as a trailing argument, so existing handlers are untouched;detectionis now typedSDKDetectionResponseinstead ofany.One trap worth noting:
protect()used to do{ ...result, edge }, which would silently strip the methods off a class. It now callsdecision.withEdge(edge), and there's a test that would have caught the spread.#731 — characteristics
rateLimit({ keyBy })was the only way to change what a rule keyed on, and it was per-rule. Everything else was IP-only — the wrong subject for an authenticated API, and the wrong subject for us specifically, since the actor model exists precisely because IP is not identity.A rule's own
keyBystill wins. An absent characteristic falls back to the IP, not to an empty component — otherwise every unauthenticated request shares one bucket, so a limit meant for one tenant takes out anonymous traffic site-wide, and it's invisible until it happens. A characteristic that throws is treated the same way.Decision caching
Deliberately narrow — server-derived
DENYandCHALLENGEonly:ALLOWis never cached. That's how a client that has since started misbehaving keeps sailing through, and it saves the cheap request — a low-risk one already returns without calling out.Bounded at 10k entries, oldest-first eviction, refreshed on overwrite so a repeatedly-denied key isn't evicted ahead of one nothing has touched.
Verification
21 new tests, 327 total, all passing.
no-explicit-anybudgets drop: express 14→12, nextjs 5→3, fastify 2→0. Build, lint andcheck:edgegreen.