protect: make fail-open observable; stop startup hanging on the rule API - #118
Merged
Conversation
…ule API
Two enforcement-visibility gaps from an external review.
1. FAIL-OPEN WAS SILENT. The guard deliberately passes traffic it can't inspect —
a request body over the cap, a response over the screening cap, a live stream, a
binary body, a read/decode failure, a DNS resolver failure (or no resolver on this
runtime). Each is a real hole in enforcement, and nothing recorded it, so
"always-on" read as "always inspected". Every such bypass is now counted and
reported: `onSkip({ phase, reason, detail, count })` plus
`protection.coverage() -> { skipped: { 'response:body-cap': n, … } }`.
readTextResponse now returns { text } | { skip: reason } so the reason is precise
(body-cap / live-stream / binary-body / read-failed / decode-failed), and the node
response + request paths and the egress resolver report through the same channel.
A throwing onSkip can never affect request handling.
2. STARTUP COULD HANG FOR 30s. createProtection awaited the rule fetch with the
client's full 30s timeout, so a slow API delayed app boot — and hosted platforms
fail a deploy whose health check is slow. The INITIAL load now gets a short budget
(bootTimeoutMs, default 5s) and falls back to last-known-good / bundled rules,
which was already the fallback chain; refreshes keep the full budget
(refreshTimeoutMs). Verified: a hanging fetch boots in ~300ms, still protected by
the fallback ruleset.
Also documents the mode-default discrepancy honestly: this API defaults to dry-run
while the scaffolded guard passes mode: 'block' — both intentional, previously
confusing.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Adds observable fail-open coverage tracking and startup timeout for rule API fetches. 🎯 Quality: 100% Elite · 📦 Size: Medium 📈 This month: Your 52nd PR — above team average · Averaging Excellent |
Contributor
Author
|
/review |
devlob
approved these changes
Aug 13, 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.
Two enforcement-visibility gaps raised by an external review.
1. Fail-open was silent
The guard deliberately passes traffic it can't inspect — a request body over the cap, a response over the screening cap, a live stream, a binary body, a read/decode failure, a DNS resolver failure (or no resolver on this runtime). Each of those is a real hole in enforcement, and nothing recorded it — so "always-on" read as "always inspected", and a secret-redaction rule could silently never apply.
Now every such bypass is counted and reported:
readTextResponsereturns{ text } | { skip: reason }, so the reason is precise (body-cap,live-stream,non-text-content-type,binary-body,read-failed,decode-failed,clone-failed).resolver-failed/resolver-unavailable) all report through the same channel.onSkipcan never affect request handling.2. Startup could hang for 30 s
createProtectionawaited the rule fetch with the client's full 30 s timeout, so a slow/hanging API delayed app boot — and hosted platforms fail a deploy whose health check is slow (Replit publishes fail past ~5 s). The initial load now gets a short budget —bootTimeoutMs, default 5 s — and falls back to last-known-good / bundled rules (already the existing fallback chain, so protection is retained). Refreshes keep the full budget (refreshTimeoutMs). Both rule clients accepttimeoutMs.Verified by test: a fetch that never resolves boots in ~300 ms and the guard is still enforcing the fallback ruleset.
Also
Documents the mode-default discrepancy honestly: this API defaults to
dry-run, while the scaffolded guard passesmode: 'block'(dropping to dry-run only onPATCHSTACK_MODE=dry-run). Both intentional; previously the comment and the generated guard appeared to contradict each other.Tests
tests/protect/coverage-skips.test.ts— cap/stream/binary skips are recorded and reported, repeats count, coverage stays empty when everything was inspected, a throwingonSkipis harmless, and the non-blocking-boot case. Full suite green (715), typecheck + build clean.