Repository navigation
(janitor/dedupe): consolidate bounded stream body reads in git-token-service - #7239
Conversation
Five near-identical implementations of the bounded JSON-body read (enforce a byte cap, validate Uint8Array chunks, cancel on overflow, strict UTF-8 decode) had drifted across bitbucket-api, bitbucket-code-review-service, gitlab-oauth-credential-refresher, gitlab-runtime-token-resolver, and the request entrypoint. Consolidate the size-limit and chunk-validation logic into a single lib/bounded-read module, leaving each caller's error mapping intact.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (6 files)
Verified the consolidated Reviewed by deepseek-v4.1-flash · Input: 67.3K · Output: 16.9K · Cached: 600.7K Review guidance: REVIEW.md from base branch |
eshurakov
left a comment
There was a problem hiding this comment.
Code Review Summary
Status: No Issues Found | Recommendation: Merge
Executive Summary
Independently reviewed head 19b83ebbae7655a959f220b123ce6c7c09a9fe11 against base 7b6d23c90b7291238dbdced8d444ac08fd91762c, including all changed files and callers. No major correctness, security, or resource-handling issues found: byte limits, overflow cancellation, reader lock release, strict UTF-8/JSON parsing, and caller error sanitization remain fail closed.
Verification: 280 in-memory assertions comparing pinned base/head readers, eight production-cap boundary checks, and mid-read abort handling passed. CI logs confirm 24 test files / 662 tests passed and service typechecking succeeded; lint, format, and security checks are green. Existing review/discussion inspected. Limitation: focused execution and existing unit tests use Node, not Workerd; no local full-suite or deployment validation.
Files Reviewed (6 files)
services/git-token-service/src/lib/bounded-read.tsservices/git-token-service/src/bitbucket-api.tsservices/git-token-service/src/bitbucket-code-review-service.tsservices/git-token-service/src/gitlab-oauth-credential-refresher.tsservices/git-token-service/src/gitlab-runtime-token-resolver.tsservices/git-token-service/src/index.ts
Summary
Five near-identical bounded JSON-body readers in
services/git-token-serviceeach re-implemented the same security-critical logic: enforce a byte cap while reading, validate that every chunk is aUint8Array, cancel the stream and fail closed when the cap is exceeded, and strict UTF-8 decode beforeJSON.parse.src/index.ts—readBoundedInternalJsonRequestsrc/bitbucket-api.ts—readBoundedJsonsrc/bitbucket-code-review-service.ts—readBoundedJsonsrc/gitlab-oauth-credential-refresher.ts—readBoundedJsonsrc/gitlab-runtime-token-resolver.ts—readBoundedProjectIdentityThese copies had already drifted (differing
reader.cancel()error handling, differingUint8Arraychunk guards, differing byte-cap constants and decode paths), so a fix to one would not reliably propagate to the others.Change
Introduce
src/lib/bounded-read.tswith:readBoundedJsonBody(stream, maxBytes)— bounded read + strict UTF-8 decode +JSON.parseResponseTooLargeError— the single signal callers use to distinguish overflow from other failuresEach caller becomes a thin adapter that keeps its own error type (
BitbucketApiError,BitbucketCodeReviewProviderError,Error, or anullreturn) and pre-flightContent-Length/Content-Typechecks. No runtime behavior changes: overflow still fails closed, invalid chunks still reject, and AbortSignal timeouts inbitbucket-apistill map torequest_timed_out.Net: 157 lines deleted, 22 added across callers plus one 51-line module.
Verification
pnpm --filter cloudflare-git-token-service typecheck— cleanpnpm -w exec oxlint ...on changed files — 0 errorspnpm --filter cloudflare-git-token-service test— 24 files, 662 tests passed