Skip to content

fix(tlock): bound Quicknet beacon response size - #694

Open
Hamda-gbade wants to merge 1 commit into
Sub-Rosa-Issue:mainfrom
Hamda-gbade:fix/tlock-bound-quicknet-response
Open

Hamda-gbade wants to merge 1 commit into
Sub-Rosa-Issue:mainfrom
Hamda-gbade:fix/tlock-bound-quicknet-response

Conversation

@Hamda-gbade

Copy link
Copy Markdown

Summary

Timelock sealing fetched Quicknet chain-info and beacon responses through drand-client, which reads the whole response body and calls JSON.parse without any size ceiling. The beacon endpoint is public, so a proxy or replica could return an arbitrarily large body and force the sealer, keeper, or web app to buffer it in memory.

This PR routes every Quicknet HTTP request through a bounded JSON fetch:

  • Documented ceiling: MAX_DRAND_RESPONSE_BYTES = 64 KiB (packages/tlock/src/http.ts), exported from @sub-rosa/tlock and noted in docs/LIMITATIONS.md. A normal quicknet beacon is a few hundred bytes, so 64 KiB is a generous cap.
  • Abort before parsing: Content-Length is checked before reading when present; otherwise the body is read as a stream and cancelled as soon as the accumulated byte count exceeds the limit. JSON.parse only runs after the full body fits.
  • Existing behavior preserved: chain-info and beacon shape validation (assertChainInfo / assertBeacon), round binding, BLS signature verification, and non-2xx failure behavior are unchanged. No timeout was removed or shortened.
  • Injectable transport: quicknet(fetchImpl = fetch) keeps the same public API while allowing offline boundary tests. The returned client implements drand-client's ChainClient interface (chain(), get(), latest(), options), so fetchBeacon / tlock-js continue to work.

Files changed

File Change
packages/tlock/src/http.ts New bounded fetchDrandJson, MAX_DRAND_RESPONSE_BYTES, DrandResponseTooLargeError, FetchLike
packages/tlock/src/quicknet.ts Quicknet client now uses the bounded fetch layer; DrandClient aliases drand-client's ChainClient
packages/tlock/src/index.ts Exports the new public constant/error/type
packages/tlock/src/http.test.ts Fake-fetch boundary tests
packages/tlock/package.json Runs the new test file in test, test:unit, and test:quicknet
docs/LIMITATIONS.md Documents the 64 KiB operational limit

Boundary tests

All tests use a fake fetch; no network access:

  • Body exactly at MAX_DRAND_RESPONSE_BYTES is accepted and parsed.
  • Body one byte over is rejected with DrandResponseTooLargeError.
  • Oversized Content-Length is rejected before a body is read.
  • Chunked bodies crossing the limit are cancelled before JSON parsing.
  • Non-2xx responses keep failing.
  • quicknet(fakeFetch) applies the same cap to chain-info.
  • Chain-info validation still runs after the size check.
  • Beacon requests still use /<chain-hash>/public/<round>.

Verification

pnpm --filter @sub-rosa/tlock test:quicknet
# 18 tests, 18 pass

cd packages/tlock
pnpm exec tsc --ignoreConfig --noEmit --strict --skipLibCheck \
  --module esnext --moduleResolution bundler --target es2022 \
  --types node src/http.ts src/quicknet.ts src/http.test.ts src/quicknet.test.ts
# clean

# Diff review
git diff upstream/main...HEAD

pnpm --filter @sub-rosa/tlock test and the workspace typechecks also exercise unrelated files that are already failing on upstream/main (for example packages/tlock/src/seal.ts references an unimported assertSealRoundWindow). The new tests pass in isolation; those pre-existing failures are outside this change.

Supply-chain review

  • No package scripts or dependencies changed. (The existing test script lists the new test file.)
  • No .vscode/, workflow, or other editor/config file changes.
  • No lockfile changes.
  • No binary or font asset changes.
  • No automatic install task (preinstall / postinstall) added.
  • No hidden executable payload added.

Closes #664

Note: #662 (drand-tools: verify configured chain hash at startup) is a separate task in a different package and is intentionally not closed by this PR.

@drips-wave

drips-wave Bot commented Nov 25, 2026

Copy link
Copy Markdown

@Hamda-gbade Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Quicknet responses were parsed without a byte limit because drand-client
reads response bodies in full before JSON parsing. Route chain-info and
beacon requests through a streaming 64 KiB cap so an oversized response is
aborted before parsing, while keeping the existing response validation and
cryptographic checks unchanged.

Adds offline boundary tests with an injected fake fetch for exactly-at-limit,
one-byte-over, declared Content-Length, and chunked bodies.

Closes Sub-Rosa-Issue#664

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@Hamda-gbade

Copy link
Copy Markdown
Author

CI note for reviewers: the failing checks also fail on main at the PR base commit 939e22e, before this change. Matching main runs:

This PR only touches packages/tlock plus one documentation line. Its targeted checks pass locally:

pnpm --filter @sub-rosa/tlock test:quicknet   # 18/18 pass

The unrelated failures include the pre-existing assertSealRoundWindow reference in packages/tlock/src/seal.ts, the agent/keeper merge breakage, and the logging check in scripts/check-logging.test.mjs. I left those out of this PR to keep the security fix reviewable.

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.

tlock(high/easy): bound Quicknet beacon response size

2 participants