Handle lowercase weak ETag validators consistently

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 04:08:59 +00:00
1 parent 028a1786f7
commit 28ee12ba15
2 files changed
+11 -4

No files matched your search

+3
View File
@@ -980,6 +980,7 @@ describe("ifNoneMatchSatisfied", () => {
it("matches weak and strong forms using weak comparison semantics", () => {
expect(ifNoneMatchSatisfied("W/\"abc\"", "\"abc\"")).toBe(true);
expect(ifNoneMatchSatisfied("\"abc\"", "W/\"abc\"")).toBe(true);
expect(ifNoneMatchSatisfied("w/\"abc\"", "\"abc\"")).toBe(true);
});
it("does not match non-identical etags", () => {
@@ -1008,6 +1009,7 @@ describe("ifMatchSatisfied", () => {
it("rejects weak validators and mismatches", () => {
expect(ifMatchSatisfied("W/\"abc\"", "\"abc\"")).toBe(false);
expect(ifMatchSatisfied("w/\"abc\"", "\"abc\"")).toBe(false);
expect(ifMatchSatisfied("\"xyz\"", "\"abc\"")).toBe(false);
});
@@ -1075,6 +1077,7 @@ describe("ifRangeSatisfied", () => {
expect(ifRangeSatisfied("\"abc\"", "\"abc\"", Date.now())).toBe(true);
expect(ifRangeSatisfied("\"abc\"", "\"xyz\"", Date.now())).toBe(false);
expect(ifRangeSatisfied("W/\"abc\"", "\"abc\"", Date.now())).toBe(false);
expect(ifRangeSatisfied("w/\"abc\"", "\"abc\"", Date.now())).toBe(false);
});
it("rejects wildcard etag values", () => {
+8 -4
View File
@@ -516,9 +516,13 @@ function staticEtagFor(stats: fs.Stats): string {
return `"${stats.size.toString(16)}-${Math.trunc(stats.mtimeMs).toString(16)}"`;
}
function isWeakEtag(value: string): boolean {
return /^w\//i.test(value.trim());
}
function normalizeWeakEtag(etag: string): string {
const trimmed = etag.trim();
return trimmed.startsWith("W/") ? trimmed.slice(2).trim() : trimmed;
return isWeakEtag(trimmed) ? trimmed.slice(2).trim() : trimmed;
}
function normalizeEtagHeaderValue(value: string | string[] | undefined): string[] {
@@ -561,13 +565,13 @@ export function ifMatchSatisfied(ifMatchHeader: string | string[] | undefined, e
}
const normalizedCurrentTag = etag.trim();
if (normalizedCurrentTag.startsWith("W/")) {
if (isWeakEtag(normalizedCurrentTag)) {
return false;
}
return candidates.some((candidate) => {
const normalizedCandidate = candidate.trim();
if (normalizedCandidate.startsWith("W/")) {
if (isWeakEtag(normalizedCandidate)) {
return false;
}
@@ -639,7 +643,7 @@ export function ifRangeSatisfied(
if (trimmed.startsWith("\"")) {
return trimmed === etag;
}
if (trimmed.startsWith("W/")) {
if (isWeakEtag(trimmed)) {
return false;
}