Repository navigation
fix(audit-engine): keep the fetch deadline armed through the body read - #191
Conversation
|
Acceptance findings addressed in 37ac3b3. Two of the four needed a correction to the finding itself; details below. HIGH —
|
Five fetchers cleared their AbortController timer where the fetch resolved, i.e. when the response HEADERS landed, leaving the body read with no time bound. readBodyCapped and response.text() cap BYTES, not seconds, so an origin that answers 200 and then trickles or stalls its body parked the caller forever. Same class as #187 fixed in the crawler; these are the stragglers. clearTimeout moves into a finally that wraps the body read. The script fetchers split their retry loop out of the attempt so each retry arms a fresh deadline instead of inheriting a spent one. The CLI forks are the live path for the `squirrel` binary (apps/cli imports its own crawler/, not the engine package), so fixing only the package would have left users hanging. Sites fixed: - packages/audit-engine/src/script-fetcher.ts (body read) - packages/audit-engine/src/external-checker.ts (WAF 403 body read) - apps/cli/src/crawler/script-fetcher.ts (fork) - apps/cli/src/crawler/external-checker.ts (fork) - apps/cli/src/crawl/sitemaps.ts (text read ran after the timer cleared) Scripts and external links are fetched during rules enrichment, after the crawl, so the failure mode was a post-crawl hang: an audit with all its pages that never finishes. The sitemap read hangs the crawl preamble. Regression tests serve headers then hold the body stream open forever; all five hang without the fix and time out at the deadline with it. The engine tests are added to test:golden so CI actually runs them. Refs squirrelscan/repo#1729 Signed-off-by: Nik Cubrilovic <[email protected]>
A bare `void body.cancel()` leaves a rejected cancel unhandled — an already-errored stream rejects, and an unhandled rejection is a worse outcome than an undrained socket. Signed-off-by: Nik Cubrilovic <[email protected]>
…y read Acceptance-review follow-ups on the #1729 deadline sweep. packages/crawler/src/fetcher.ts had the same clear-at-headers bug wearing a try/finally that looks correct: requestWithTiming cleared its timer in a finally that closes at the fetch, then returned the Response for fetchPageStandard to read much later. The same finally also removed the fiber-interrupt bridge, so the watchdog's abort never reached the reader -- readBodyCapped stayed parked on reader.read() for the life of the process, holding the socket and its buffered chunks. Not an unbounded hang: crawler.ts's per-URL watchdog (>=120s) is the backstop. But a stalled page burned the whole watchdog instead of timeoutMs. requestWithTiming now returns a TimedResponse whose deadline stays armed; the caller owns it across the redirect loop and the body read, drops superseded hops (which the loop never did, leaking their sockets), and Effect.ensuring releases whatever is still held on every exit -- a try/finally inside Effect.gen would not run on failure. The body read's interrupt bridge now aborts the request rather than only losing a race, and a deadline that fires mid-body reports a timeout, not a parse error. fetchPageStandard is split behind Effect.suspend so the held-response state is per RUN: callers retry this same Effect value. External checker (both copies): a 403 whose body could not be read no longer abandons WAF detection. Detection runs on the response headers, which are evidence in their own right -- most WAFs are identifiable from cf-ray / x-sucuri-id / a provider server value alone. Giving up there reported live, bot-guarded links as broken, and the deadline now makes that read fail more often. Sitemaps: the terminal catchAll carries the failure reason instead of collapsing every non-HTTP failure to "Unexpected error", so a body-read timeout, a connect failure and an HTTP error are distinguishable. Its error-channel value no longer mimics the success shape. Tests: each new case was verified against origin/main -- 4 of 5 engine and 6 of 8 CLI cases fail there, and both crawler stall cases fail, one by hanging and one by observing that the server never saw the client abort. The retry tests now stall attempt 2's body, so they pin that the retry's deadline is fresh AND covers its body; the earlier form passed unfixed. Refs squirrelscan/repo#1729 Signed-off-by: Nik Cubrilovic <[email protected]>
Round-2 review findings, both reproduced with an instrumented probe before being fixed and re-probed after. release() only disarmed the timer. Every path that releases WITHOUT a body read therefore left the stalled body and its socket parked for the life of the process -- the exact leak class this change exists to kill. Two such paths: a terminal 403/429/5xx that fails applyStatusGuards before any read, and a redirect hop still held when the NEXT request fails (the loop drops a superseded hop only once a newer response arrives). Probe on both: `server saw cancel: false`. release() now aborts as well. That is safe unconditionally because release runs only as the effect leaves: on the success path the stream is already drained and the abort is a no-op. The 503 challenge read swallowed every failure via orElseSucceed, including the timeout this PR newly maps, so a stalled 503 body came back as the generic `network: Server error: 503` -- blaming the origin for our own deadline. It now re-fails on a timeout and still degrades every other read failure to "no body", so the guard can classify the 503 from headers alone. Three tests added, each verified to fail against the previous commit: two use a server-side `req.signal` observer to prove the connection is actually cancelled (terminal status, failed hop), and one asserts the stalled-503 classification is `timeout`. Rebased onto main after #193 merged. Refs squirrelscan/repo#1729 Signed-off-by: Nik Cubrilovic <[email protected]>
37ac3b3 to
cea5229
Compare
|
Both round-2 findings confirmed and fixed in cea5229. Branch rebased onto main after #193 merged. I reproduced each with an instrumented probe before touching the code, so these are measured rather than reasoned: and after: HIGH —
|
Five fetchers cleared their
AbortControllertimer where the fetch resolved, i.e. when the response headers landed, leaving the body read with no time bound at all.readBodyCappedandresponse.text()cap bytes, not seconds, so an origin that answers 200 and then trickles or stalls its body parked the caller forever.Same bug class as #187, which fixed it in the crawler. These are the stragglers.
Sites fixed
packages/audit-engine/src/script-fetcher.tsreadBodyCappedafterclearTimeoutpackages/audit-engine/src/external-checker.tsgetResponse.text()on a 403, for WAF detectionapps/cli/src/crawler/script-fetcher.tsresponse.text()(fork of the engine fetcher)apps/cli/src/crawler/external-checker.tsapps/cli/src/crawl/sitemaps.tsresponse.text()in a later Effect stage, outside the timer's scopeThe two CLI files are forks, not re-exports:
apps/cli/src/audit/adapter.tsimports@/crawler/*, so they are what thesquirrelbinary actually runs. Fixing only the package would have left every CLI user hanging.Fix
clearTimeoutmoves into afinallythat wraps the body read, matchingpackages/crawler/src/deadline.tsandpackages/fetchers/src/index.ts. The script fetchers additionally split their retry loop out of the attempt, so each retry arms a fresh deadline rather than inheriting a spent one or keeping it alive across the backoff.Impact
Scripts and external links are fetched during rules enrichment, after pages are collected, so the failure mode was the post-crawl hang class rather than a crawl-phase wedge: the audit has all its pages and never finishes. The sitemap read hangs the crawl preamble instead.
Behaviour changes only on the previously-hanging path. A stalled script body now returns
error: "timeout"; a stalled WAF 403 body returns the plain 403 it already knew from the headers (the WAF verdict degrades to unknown, matching the existing "ignore body read errors" branch); a stalled sitemap returns a failure result.Tests
apps/cli/tests/crawler/fetch-deadline.test.tsandpackages/audit-engine/tests/fetch-deadline.test.tsserve headers immediately, then hold the body stream open forever. Verified by reverting each source file tomainand re-running: all five cases hang and fail on Bun's test timeout without the fix, and return within the deadline with it. Each fetcher also has a fast-path test proving normal responses are still read in full.packages/audit-engine/package.json'stest:goldenglob gainstests/*deadline*.test.ts— the CI engine job runs only that script, so a new test file would otherwise never execute in CI.bun testinapps/cli: 1903 pass, 0 failbun run test:engine: 66 pass, 0 fail, golden baseline unchanged (pages=518 findings=98221 healthScore=48)bun run typecheck(all workspaces),lint,format:check: cleanNo HTML output bytes change, so
REPORT_HTML_VERSIONis untouched.Follow-up, not in this PR
apps/cli/src/tools/request.ts(request/requestOnceWithTiming) has the same shape: it clears the deadline and hands theResponseto callers that read the body later (robots.ts, reachability, and others). Fixing it properly means moving those call sites to awithRequestDeadline(timeoutMs, perform, use)contract, which is a wider refactor than this PR should carry. Filed separately.Refs squirrelscan/repo#1729