Skip to content

fix(rules): GraphQL probes fetch undecoded and inflate no further than their cap #722 - #765

Merged
nc9 merged 2 commits into
mainfrom
fix/graphql-probe-undecoded-722
Oct 14, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/graphql-probe-undecoded-722

Conversation

@nc9

@nc9 nc9 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #722.

What changed

sendGraphqlProbe (packages/rules/src/security/graphql-probe.ts), which sends every request for security/graphql-introspection and security/graphql-get-mutations, called globalThis.fetch and then read the body under its byte cap. Bun inflates a Content-Encoding body in full as it arrives, before any reader runs, so a site with active probing on could answer a probe with a gzip or brotli bomb and cost the audit gigabytes of memory. Since #699 (squirrelscan/repo#2717), every other request the crawl and the audit send to a site avoids this.

  • Request: the probe now goes out through fetchUndecoded from @squirrelscan/utils/response-body, which sets decompress: false and the shared Accept-Encoding: gzip, deflate, br, zstd, so the request still looks the same to the site. The URL, method, body, cookies (credentials: "omit"), redirect: "manual", the budget and the timeout are unchanged.
  • Read: the probe keeps its own capped reader, because it needs to know whether the body was truncated (the truncated-schema verdict depends on that). The reader now pulls from decodedBody(res), which undoes the coding lazily, so GRAPHQL_RESPONSE_CAPS (1 MB introspection, 64 KB no-op mutation) bounds both the decoded body and the work to produce it. A body that does not decode fails the read, and the probe returns { sent: true, error }, the same as a network failure.
  • Connection closed after the read: cancelling a body does not close the connection in Bun, which keeps downloading the rest until the request's signal fires (measured in review: an endless answer kept arriving for the full 10 s timeout, about 100 GB over loopback). The probe's signal now includes a controller that is aborted once the probe is done, which closes the connection within milliseconds. Other capped reads in the codebase have the same gap; that is filed as Capped body reads keep downloading after the cap until the request deadline: cancelling the reader does not close Bun's connection #766.
  • Hosted runs: fetchUndecoded calls the global fetch at call time, so a runtime that wraps fetch still sees every probe, and decompress: false reaches it as long as the wrapper passes init through (checked in review).

Acceptance criteria

  • The GraphQL probe request goes out with the runtime's decoding off (fetchUndecoded), and its body is read through the shared decoder no further than GRAPHQL_RESPONSE_CAPS.
  • A probe whose response is a 1 GiB gzip bomb, and a brotli one, is read to its 1 MB cap with under 256 MiB of RSS growth. This uses a real Bun.serve, in packages/rules/tests/graphql-probe-decoding-722.test.ts, modelled on packages/utils/tests/fetch-undecoded.test.ts.
  • A global fetch spy shows the probe request carries decompress: false and the shared Accept-Encoding. A gzip-encoded GraphQL answer parses to the same parseGraphqlBody result as the plain one, and security/graphql-introspection returns identical checks over gzip.
  • security/graphql-introspection and security/graphql-get-mutations findings are unchanged on the existing rule tests (graphql-probe, graphql-introspection, graphql-get-mutations: 54 pass, untouched).

Verification

  • bun test tests/graphql-probe-decoding-722.test.ts in packages/rules: 6 pass, bombs included. The connection test uses a raw Bun.listen server with an endless body and requires the close within 2 s of the probe returning; without the abort the connection was still open at 5 s.
  • Run against main's probe, the spy test fails. I did not run the bomb tests against main on purpose: inflating 2 GiB in full would cost this shared machine 2 to 3 GB.
  • tsgo --noEmit in packages/rules: clean. The new test file passes oxfmt; oxlint is clean on both files. graphql-probe.ts already had oxfmt drift on main, which I left alone.
  • docs/: tangly check --strict passes. Both rule pages now say the read limit counts the decompressed answer.

Agent skill

No change needed.

@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Reviewed the diff and the fetchUndecoded and decodedBody helpers in packages/utils/src/response-body.ts. I found nothing that should block the merge. I did not run the tests.

  • (nit) packages/rules/tests/graphql-probe-decoding-722.test.ts: the two 1 GiB bombs are built in beforeAll, and the compressed buffers are held in memory for the whole file. The RSS assertion is also sensitive to the CI runner. Both are probably fine given the 256 MiB margin and the 60 s timeout. If it turns out flaky, shrink the bomb to 256 MiB and tighten the margin proportionally.
  • (nit) The RSS-growth assertion would also catch a regression where decodedBody is bypassed. A one-line check that response.body is all spaces would not add much beyond the existing trim() check.
  • (nit) sendGraphqlProbe aborts done in finally, after the capped read has finished. I checked that the abort cannot race the read: the abort only fires after readCapped has returned or thrown. The read error path therefore still reports { sent: true, error }.

What I checked:

  • The request now goes through fetchUndecoded, so decompress: false and the shared Accept-Encoding are set.
  • The capped read pulls from decodedBody, so the cap applies to the decoded size.
  • The extra abort controller closes the connection once the probe is done.
  • The CHANGELOG and docs match the behaviour.
  • The tests cover gzip and brotli bombs, the request init, finding parity between gzip and plain, and the connection close.

nc9 added 2 commits October 15, 2026 00:42
…n their cap #722

sendGraphqlProbe called globalThis.fetch, and Bun inflates a
Content-Encoding body in full as it arrives, before the probe's capped
read, so a probed endpoint could answer with a gzip or brotli bomb and
cost the audit gigabytes. The probe now sends through fetchUndecoded
(decompress: false plus the shared Accept-Encoding) and its capped read
pulls from decodedBody, which decodes lazily, so GRAPHQL_RESPONSE_CAPS
bounds the decoded body and the work to produce it. A body that does not
decode fails the probe as an error, as a network failure does.

Tests: 1 GiB gzip and brotli bombs read to the 1 MB cap under 256 MiB of
RSS growth; a fetch spy shows decompress: false and the Accept-Encoding;
a gzip-encoded schema parses, and the introspection rule reaches the same
checks, as with the plain one. Existing GraphQL rule tests unchanged.

Signed-off-by: Nik Cubrilovic <[email protected]>
…722

Cancelling a fetch body does not close the connection in Bun, which keeps
downloading the rest until the request's signal fires, so a capped probe of
an endless answer pulled bytes for the full 10 s timeout. The probe's
signal now includes a controller aborted once the probe is done. A raw
Bun.listen test shows the connection closing within 2 s of the probe
returning (it stayed open past 5 s without the abort). Comments say the
caps count decoded bytes.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/graphql-probe-undecoded-722 branch from 793aa27 to f4d98c5 Compare October 14, 2026 13:42
@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Reviewed the diff and the fetchUndecoded / decodedBody helpers it relies on. The change is a correct, narrowly scoped fix. I found nothing that should block merge.

  • (nit) packages/rules/tests/graphql-probe-decoding-722.test.ts: the bomb tests build two 1 GiB compressed buffers in beforeAll, which takes time and some memory on CI. The 60 s hook timeout is generous, so this is fine, but a smaller bomb (for example 256 MiB with a lower RSS threshold) would make the suite cheaper while still proving the point.
  • (nit) graphql-probe.ts: done.abort() in finally fires on success too. That is intended, and the comment explains it. It would be worth a one-line note that the abort happens after readCapped has returned, so it cannot truncate a body that is still being read.
  • (low) The connection-close test relies on wall-clock bounds (2 s after return, 5 s poll). It could be flaky on a heavily loaded CI runner. Widening the margin a little would help, though the 10 s timeout still leaves a clear gap.

Test coverage is good: bombs for both gzip and brotli, a spy on the fetch init, gzip/plain parity, and the connection close. Docs and changelog are updated.

@nc9
nc9 merged commit 4299a45 into main Oct 14, 2026
15 checks passed
@nc9
nc9 deleted the fix/graphql-probe-undecoded-722 branch October 14, 2026 13:56
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.

GraphQL probes still let the runtime inflate a compressed response before the cap

1 participant