-
Notifications
You must be signed in to change notification settings - Fork 365
fix(server): use weak comparison for If-None-Match #2710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
james-elicx
merged 27 commits into
cloudflare:main
from
Boyeep:fix/weak-etag-validation
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d2e8e57
fix(server): use weak comparison for If-None-Match
Boyeep 932bfc8
chore: retrigger CI
Boyeep 9ce704a
fix(server): validate conditional static requests
james-elicx f11c2f9
fix(server): tighten entity-tag parsing
james-elicx 1c0b44e
test(server): document entity-tag comma behavior
james-elicx 1e2d403
fix(dev): weak-match public asset etags
james-elicx 233099b
fix(dev): normalize public etag lookup paths
james-elicx 4c95813
fix(dev): canonicalize conditional public assets
james-elicx 510f8e0
fix(dev): honor Vite public file identity
james-elicx 1b3c835
fix(dev): mirror Vite public filesystem semantics
james-elicx b395c75
fix(dev): refresh public filesystem capabilities
james-elicx b4dfe41
fix(dev): verify public path aliases
james-elicx ecc9fad
fix(dev): index verified public request aliases
james-elicx 0db6645
fix(dev): compose verified public path aliases
james-elicx 99babf4
fix(dev): resolve Unicode public path classes
james-elicx bdb00b8
fix(dev): use Unicode simple case matching
james-elicx a8df551
fix(dev): match Unicode expansion tokens
james-elicx 29d95eb
Merge remote-tracking branch 'origin/main' into codex/pr2710-ready
james-elicx 4175bc8
fix(dev): keep public path node private
james-elicx aee5021
Merge remote-tracking branch 'origin/main' into codex/pr2710-ready
james-elicx 14ada51
Merge remote-tracking branch 'origin/main' into codex/pr2710-ready
james-elicx 26f315d
fix(dev): resolve public-root symlink redirects
james-elicx d0d7dbb
refactor(dev): leave public etag handling to Vite
james-elicx 068bc78
Merge remote-tracking branch 'origin/main' into codex/pr2710-clean
james-elicx 03998b5
test(server): reject malformed etag list tails
james-elicx 6b9e0e1
fix(pages): share conditional etag matching
james-elicx a699cec
refactor(pages): remove reversed etag matcher
james-elicx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * Test an If-None-Match field value against a current entity tag. | ||
| * | ||
| * RFC 9110 requires weak comparison for If-None-Match: a weak and strong | ||
| * validator with the same opaque tag compare equal. | ||
| */ | ||
| export function matchesIfNoneMatch(ifNoneMatch: string | undefined, etag: string): boolean { | ||
| if (!ifNoneMatch) return false; | ||
|
|
||
| const current = parseEntityTag(etag, 0); | ||
| if (!current || current.end !== etag.length) return false; | ||
| const currentOpaqueTag = etag.slice(current.opaqueStart, current.opaqueEnd); | ||
|
|
||
| let index = skipOptionalWhitespace(ifNoneMatch, 0); | ||
| if (ifNoneMatch[index] === "*") { | ||
| return skipOptionalWhitespace(ifNoneMatch, index + 1) === ifNoneMatch.length; | ||
| } | ||
|
|
||
| let matched = false; | ||
| let sawTag = false; | ||
| while (index < ifNoneMatch.length) { | ||
| // RFC 9110's list extension allows recipients to ignore empty members. | ||
| if (ifNoneMatch[index] === ",") { | ||
| index = skipOptionalWhitespace(ifNoneMatch, index + 1); | ||
| continue; | ||
| } | ||
|
|
||
| const candidate = parseEntityTag(ifNoneMatch, index); | ||
| if (!candidate) return false; | ||
| sawTag = true; | ||
| matched ||= ifNoneMatch.slice(candidate.opaqueStart, candidate.opaqueEnd) === currentOpaqueTag; | ||
|
james-elicx marked this conversation as resolved.
|
||
|
|
||
| index = skipOptionalWhitespace(ifNoneMatch, candidate.end); | ||
| if (index === ifNoneMatch.length) break; | ||
| if (ifNoneMatch[index] !== ",") return false; | ||
| index = skipOptionalWhitespace(ifNoneMatch, index + 1); | ||
| } | ||
|
|
||
| return sawTag && matched; | ||
| } | ||
|
|
||
| type ParsedEntityTag = { | ||
| opaqueStart: number; | ||
| opaqueEnd: number; | ||
| end: number; | ||
| }; | ||
|
|
||
| function parseEntityTag(value: string, start: number): ParsedEntityTag | null { | ||
| let index = start; | ||
| if (value.startsWith("W/", index)) index += 2; | ||
| if (value[index] !== '"') return null; | ||
|
|
||
| const opaqueStart = ++index; | ||
| while (index < value.length && value[index] !== '"') { | ||
| const code = value.charCodeAt(index); | ||
| // etagc = %x21 / %x23-7E / obs-text. A backslash is an ordinary | ||
| // character here; entity tags do not use quoted-string escaping. | ||
| const isEtagCharacter = | ||
| code === 0x21 || (code >= 0x23 && code <= 0x7e) || (code >= 0x80 && code <= 0xff); | ||
| if (!isEtagCharacter) return null; | ||
| index++; | ||
| } | ||
| if (value[index] !== '"') return null; | ||
|
|
||
| return { opaqueStart, opaqueEnd: index, end: index + 1 }; | ||
| } | ||
|
|
||
| function skipOptionalWhitespace(value: string, start: number): number { | ||
| let index = start; | ||
| while (value[index] === " " || value[index] === "\t") index++; | ||
| return index; | ||
| } | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { matchesIfNoneMatch } from "../packages/vinext/src/server/http-conditional.js"; | ||
|
|
||
| describe("matchesIfNoneMatch", () => { | ||
| it("matches an identical entity tag", () => { | ||
| expect(matchesIfNoneMatch('"asset"', '"asset"')).toBe(true); | ||
| }); | ||
|
|
||
| it("uses weak comparison in either direction", () => { | ||
| expect(matchesIfNoneMatch('"asset"', 'W/"asset"')).toBe(true); | ||
| expect(matchesIfNoneMatch('W/"asset"', '"asset"')).toBe(true); | ||
| }); | ||
|
|
||
| it("matches a validator within a comma-separated field value", () => { | ||
| expect(matchesIfNoneMatch('"other", W/"asset", "last"', '"asset"')).toBe(true); | ||
| }); | ||
|
|
||
| it("does not split commas inside an opaque tag", () => { | ||
| // RFC 9110 permits commas in opaque tags. This is intentionally stricter | ||
| // than Next.js 16.2.7's compiled `fresh` parser, which splits every comma. | ||
| expect(matchesIfNoneMatch('"other", W/"asset,part"', '"asset,part"')).toBe(true); | ||
| expect(matchesIfNoneMatch('"asset,part"', '"asset"')).toBe(false); | ||
| }); | ||
|
|
||
| it("treats backslashes as opaque characters rather than escapes", () => { | ||
| expect(matchesIfNoneMatch('W/"asset\\part"', '"asset\\part"')).toBe(true); | ||
| }); | ||
|
|
||
| it("matches a wildcard with optional whitespace", () => { | ||
| expect(matchesIfNoneMatch(" \t * \t ", 'W/"asset"')).toBe(true); | ||
| }); | ||
|
|
||
| it("rejects a wildcard mixed into an entity-tag list", () => { | ||
| expect(matchesIfNoneMatch('*, "asset"', '"asset"')).toBe(false); | ||
| expect(matchesIfNoneMatch('"other", *', '"asset"')).toBe(false); | ||
| }); | ||
|
|
||
| it("does not match different opaque tags or case", () => { | ||
| expect(matchesIfNoneMatch('"other"', '"asset"')).toBe(false); | ||
| expect(matchesIfNoneMatch('"ASSET"', '"asset"')).toBe(false); | ||
| }); | ||
|
|
||
| it("does not treat a lowercase weak prefix as W/", () => { | ||
| expect(matchesIfNoneMatch('w/"asset"', '"asset"')).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects malformed entity tags", () => { | ||
| expect(matchesIfNoneMatch('asset, "match"', '"match"')).toBe(false); | ||
| expect(matchesIfNoneMatch('"match", garbage', '"match"')).toBe(false); | ||
| expect(matchesIfNoneMatch('"unterminated', '"unterminated"')).toBe(false); | ||
| expect(matchesIfNoneMatch('W/ "asset"', '"asset"')).toBe(false); | ||
| expect(matchesIfNoneMatch('"asset" suffix', '"asset"')).toBe(false); | ||
| expect(matchesIfNoneMatch('"asset\x7fpart"', '"asset\x7fpart"')).toBe(false); | ||
| }); | ||
|
|
||
| it("ignores empty list members", () => { | ||
| expect(matchesIfNoneMatch(' , W/"asset", ', '"asset"')).toBe(true); | ||
| }); | ||
|
|
||
| it("rejects an invalid current entity tag", () => { | ||
| expect(matchesIfNoneMatch('"asset"', "asset")).toBe(false); | ||
| expect(matchesIfNoneMatch('"asset"', 'W/ "asset"')).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects an absent or empty field value", () => { | ||
| expect(matchesIfNoneMatch(undefined, '"asset"')).toBe(false); | ||
| expect(matchesIfNoneMatch("", '"asset"')).toBe(false); | ||
| }); | ||
| }); |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subtle semantic narrowing vs. the old
matchesIfNoneMatchHeader: the wildcard branch is now gated behind the current-ETag validation on lines 10-11, soIf-None-Match: *returnsfalsewhenetagfailsparseEntityTag. RFC 9110 defines*as matching whenever a current representation exists, independent of the ETag's exact form, and the old code returnedtruefor*unconditionally.This is not a live bug — every caller passes a well-formed
W/"..."or"..."ETag (verified all producers), so the wildcard path is never reached with an invalidetag. But if a future caller ever passes a non-quoted or malformed ETag,*would silently stop matching. A one-line comment noting that*intentionally requires a valid current ETag here (or hoisting the*check above the current-tag parse) would preempt the question.