fix: sort response headers for deterministic order#1646
Open
kitsuyui wants to merge 1 commit into
Open
Conversation
Add `sortHeaders` to `src/lib/headers.ts` and apply it in `renderToResponse` after `appendVaryHeader`. This eliminates non-determinism introduced by Playwright's `response.headers()` enumeration order, which varies by browser and upstream server. Adds unit tests for `sortHeaders` covering: empty input, lexicographic ordering, value preservation, and input immutability.
gh-counterPR gate
Repo dashboard
Reported by gh-counter |
Code Metrics Report
Details | | main (de5f702) | #1646 (b9cc2fe) | +/- |
|---------------------|----------------|-----------------|-------|
+ | Coverage | 86.9% | 87.2% | +0.2% |
| Files | 11 | 11 | 0 |
| Lines | 207 | 211 | +4 |
+ | Covered | 180 | 184 | +4 |
+ | Code to Test Ratio | 1:0.5 | 1:0.5 | +0.0 |
| Code | 1648 | 1694 | +46 |
+ | Test | 961 | 994 | +33 |
- | Test Execution Time | 6s | 7s | +1s |Code coverage of files in pull request scope (86.0% → 86.6%)
Reported by octocov |
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.
Summary
Response headers forwarded by the proxy were enumerated in a non-deterministic
order.
Playwright'sresponse.headers()returns a plain object whose keyordering depends on the browser engine and the upstream server's transmission
order. As a result, identical requests could produce different header orderings
across browser types or runs.
This change adds a
sortHeadersutility that sorts header keyslexicographically and applies it in
renderToResponseafterappendVaryHeader,so the proxy always emits headers in a stable alphabetical order regardless of
the upstream or browser source.
Changes
src/lib/headers.ts: add and exportsortHeaders(headers: Headers): Headerssrc/lib/headers.spec.ts: unit tests forsortHeaders(empty input,lexicographic order, value preservation, input immutability)
src/server/index.ts: wrapappendVaryHeader(...)result withsortHeadersin
renderToResponseVerification
bun run lint: cleanbun run typecheck: cleanbun run vitest run src/lib/headers.spec.ts: 15/15 passedTrade-offs
Sorting adds one O(n log n) pass over the header set per response, where n is
the number of forwarded headers (typically < 20). The overhead is negligible
compared to the browser render time.