-
Notifications
You must be signed in to change notification settings - Fork 367
fix(server): honor static freshness validators #2715
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 9 commits into
cloudflare:main
from
Boyeep:fix/static-conditional-requests
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d70435b
fix(server): honor static freshness validators
james-elicx a361cfa
fix(server): preserve content type on cached 416 responses
james-elicx 8b41878
fix(cloudflare): preserve partial asset response status
james-elicx 1086a35
docs(server): clarify static range representation
james-elicx 1dee6f8
fix(server): align cached precondition headers
james-elicx 9734c04
fix(server): align slow-path not-modified headers
james-elicx 87d8bd3
fix(server): adjust RFC 850 leap-day centuries
james-elicx 3cb7903
fix(server): align static range and method handling
james-elicx b997294
fix(server): match weak static etags for If-Match
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
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,135 @@ | ||
| const IMF_FIXDATE_RE = | ||
| /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT$/; | ||
| const RFC850_DATE_RE = | ||
| /^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{2}):(\d{2}):(\d{2}) GMT$/; | ||
| const ASCTIME_DATE_RE = | ||
| /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?: (\d)|(\d{2})) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/; | ||
|
james-elicx marked this conversation as resolved.
|
||
|
|
||
| const WEEKDAY_INDEX: Record<string, number> = { | ||
| Sun: 0, | ||
| Sunday: 0, | ||
| Mon: 1, | ||
| Monday: 1, | ||
| Tue: 2, | ||
| Tuesday: 2, | ||
| Wed: 3, | ||
| Wednesday: 3, | ||
| Thu: 4, | ||
| Thursday: 4, | ||
| Fri: 5, | ||
| Friday: 5, | ||
| Sat: 6, | ||
| Saturday: 6, | ||
| }; | ||
| const MONTH_INDEX: Record<string, number> = { | ||
| Jan: 0, | ||
| Feb: 1, | ||
| Mar: 2, | ||
| Apr: 3, | ||
| May: 4, | ||
| Jun: 5, | ||
| Jul: 6, | ||
| Aug: 7, | ||
| Sep: 8, | ||
| Oct: 9, | ||
| Nov: 10, | ||
| Dec: 11, | ||
| }; | ||
|
|
||
| /** Parse only the three HTTP-date wire formats accepted by RFC 9110. */ | ||
| export function parseHttpDate(value: string): number { | ||
| const imf = IMF_FIXDATE_RE.exec(value); | ||
| if (imf) { | ||
| return timestampFromHttpDateParts(imf[1], imf[2], imf[3], imf[4], imf[5], imf[6], imf[7]); | ||
| } | ||
|
|
||
| const rfc850 = RFC850_DATE_RE.exec(value); | ||
| if (rfc850) { | ||
| const now = new Date(); | ||
| let year = now.getUTCFullYear() - (now.getUTCFullYear() % 100) + Number(rfc850[4]); | ||
| const candidateTimestamp = timestampForRfc850YearWindow( | ||
| rfc850[2], | ||
| rfc850[3], | ||
| String(year), | ||
| rfc850[5], | ||
| rfc850[6], | ||
| rfc850[7], | ||
| ); | ||
| const fiftyYearsFromNow = new Date(now); | ||
| fiftyYearsFromNow.setUTCFullYear(now.getUTCFullYear() + 50); | ||
| if (candidateTimestamp > fiftyYearsFromNow.getTime()) year -= 100; | ||
|
james-elicx marked this conversation as resolved.
james-elicx marked this conversation as resolved.
|
||
| return timestampFromHttpDateParts( | ||
| rfc850[1], | ||
| rfc850[2], | ||
| rfc850[3], | ||
| String(year), | ||
| rfc850[5], | ||
| rfc850[6], | ||
| rfc850[7], | ||
| ); | ||
| } | ||
|
|
||
| const asctime = ASCTIME_DATE_RE.exec(value); | ||
| if (asctime) { | ||
| return timestampFromHttpDateParts( | ||
| asctime[1], | ||
| asctime[3] || asctime[4], | ||
| asctime[2], | ||
| asctime[8], | ||
| asctime[5], | ||
| asctime[6], | ||
| asctime[7], | ||
| ); | ||
| } | ||
|
|
||
| return Number.NaN; | ||
| } | ||
|
|
||
| function timestampForRfc850YearWindow( | ||
| dayValue: string, | ||
| monthValue: string, | ||
| yearValue: string, | ||
| hourValue: string, | ||
| minuteValue: string, | ||
| secondValue: string, | ||
| ): number { | ||
| // Normalize solely for the 50-year comparison. Final calendar and weekday | ||
| // validation happens after the RFC 850 century adjustment. | ||
| const date = new Date(0); | ||
| date.setUTCFullYear(Number(yearValue), MONTH_INDEX[monthValue], Number(dayValue)); | ||
| date.setUTCHours(Number(hourValue), Number(minuteValue), Number(secondValue), 0); | ||
| return date.getTime(); | ||
| } | ||
|
|
||
| function timestampFromHttpDateParts( | ||
| weekday: string | undefined, | ||
| dayValue: string, | ||
| monthValue: string, | ||
| yearValue: string, | ||
| hourValue: string, | ||
| minuteValue: string, | ||
| secondValue: string, | ||
| ): number { | ||
| const day = Number(dayValue); | ||
| const month = MONTH_INDEX[monthValue]; | ||
| const year = Number(yearValue); | ||
| const hour = Number(hourValue); | ||
| const minute = Number(minuteValue); | ||
| const second = Number(secondValue); | ||
|
|
||
| const date = new Date(0); | ||
| date.setUTCFullYear(year, month, day); | ||
| date.setUTCHours(hour, minute, second, 0); | ||
| if ( | ||
| date.getUTCFullYear() !== year || | ||
| date.getUTCMonth() !== month || | ||
| date.getUTCDate() !== day || | ||
| date.getUTCHours() !== hour || | ||
| date.getUTCMinutes() !== minute || | ||
| date.getUTCSeconds() !== second || | ||
| (weekday !== undefined && date.getUTCDay() !== WEEKDAY_INDEX[weekday]) | ||
| ) { | ||
| return Number.NaN; | ||
| } | ||
| return date.getTime(); | ||
| } | ||
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,80 @@ | ||
| import { parseHttpDate } from "./http-date.js"; | ||
|
|
||
| export type ByteRange = | ||
| | { kind: "ignore" } | ||
| | { kind: "unsatisfiable" } | ||
| | { kind: "range"; start: number; end: number }; | ||
|
|
||
| /** | ||
| * Parse a single bytes range. Unsupported range units, malformed values, and | ||
| * multipart ranges are ignored, which RFC 9110 permits servers to do. | ||
| */ | ||
| export function parseByteRange(value: string | undefined, size: number): ByteRange { | ||
| if (!value || value.slice(0, 6).toLowerCase() !== "bytes=") return { kind: "ignore" }; | ||
|
|
||
| const spec = value.slice("bytes=".length).trim(); | ||
| if (spec.includes(",")) return { kind: "ignore" }; | ||
|
|
||
| const match = /^(\d*)-(\d*)$/.exec(spec); | ||
| if (!match || (!match[1] && !match[2])) return { kind: "ignore" }; | ||
|
|
||
| if (!match[1]) { | ||
| const suffixLength = parseDecimalInteger(match[2]); | ||
| if (suffixLength === "overflow") { | ||
| if (size === 0) return { kind: "unsatisfiable" }; | ||
| return { kind: "range", start: 0, end: size - 1 }; | ||
| } | ||
| if (suffixLength === 0 || size === 0) return { kind: "unsatisfiable" }; | ||
| return { | ||
| kind: "range", | ||
| start: Math.max(size - suffixLength, 0), | ||
| end: size - 1, | ||
| }; | ||
| } | ||
|
|
||
| const start = parseDecimalInteger(match[1]); | ||
| const requestedEnd = match[2] ? parseDecimalInteger(match[2]) : size - 1; | ||
| // The wire grammar has no JavaScript-safe-integer limit. A start beyond | ||
| // that limit is necessarily beyond any file size Node can address, while an | ||
| // overflowing end still denotes the remainder of the representation. | ||
| if (start === "overflow") return { kind: "unsatisfiable" }; | ||
| if (requestedEnd === "overflow") { | ||
| if (start >= size) return { kind: "unsatisfiable" }; | ||
| return { kind: "range", start, end: size - 1 }; | ||
| } | ||
| if (start >= size || requestedEnd < start) return { kind: "unsatisfiable" }; | ||
|
|
||
| return { | ||
| kind: "range", | ||
| start, | ||
| end: Math.min(requestedEnd, size - 1), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * If-Range requires strong entity-tag comparison. Date validators are matched | ||
| * at HTTP-date (whole-second) precision because filesystem mtimes are finer, | ||
| * and allow a range when the representation has not changed since that date. | ||
| * Vinext's static ETags are currently weak, so its emitted Last-Modified value | ||
| * is the validator that can enable a range response on a later request. | ||
| */ | ||
| export function ifRangeAllowsRange( | ||
| value: string | undefined, | ||
| etag: string, | ||
| mtimeMs: number, | ||
| ): boolean { | ||
| if (!value) return true; | ||
|
|
||
| const trimmed = value.trim(); | ||
| if (trimmed.startsWith('"') || trimmed.startsWith("W/")) { | ||
| return !trimmed.startsWith("W/") && !etag.startsWith("W/") && trimmed === etag; | ||
|
james-elicx marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const timestamp = parseHttpDate(trimmed); | ||
| return Number.isFinite(timestamp) && Math.floor(mtimeMs / 1000) * 1000 <= timestamp; | ||
|
james-elicx marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function parseDecimalInteger(value: string): number | "overflow" { | ||
| const parsed = Number(value); | ||
| return Number.isSafeInteger(parsed) ? parsed : "overflow"; | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.