-
Notifications
You must be signed in to change notification settings - Fork 7
Keep the document security headers off hashed build assets #1603
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // @vitest-environment node | ||
| import path from "path"; | ||
| import { getPathMatch } from "next/dist/shared/lib/router/utils/path-match"; | ||
|
|
||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| /** | ||
| * The security headers (CSP, CSP-Report-Only, Permissions-Policy, X-Frame-Options, | ||
| * Referrer-Policy) are document headers. They used to ride on every hashed asset | ||
| * because the block matched `/:path*`, which put ~4.7 KB of headers on each of the | ||
| * ~130 `/_next/static` responses a page makes. Lighthouse counts header bytes in | ||
| * transfer size, so a feed page carried ~600 KB of header weight in PageSpeed's | ||
| * model (#1592). Only that prefix is excluded: a miss there is a text/plain 404, | ||
| * while a miss under /assets or /scripts renders the HTML not-found page, which | ||
| * must keep its clickjacking protection. | ||
| * | ||
| * This evaluates the real `headers()` from next.config.js with Next's own path | ||
| * matcher so the split cannot silently regress. | ||
| */ | ||
| type HeaderRule = { source: string; headers: { key: string; value: string }[] }; | ||
|
|
||
| const CONFIG = path.resolve(__dirname, "../../../next.config.js"); | ||
| const SECURITY = [ | ||
| "Content-Security-Policy", | ||
| "Content-Security-Policy-Report-Only", | ||
| "Permissions-Policy", | ||
| "X-Frame-Options", | ||
| "Referrer-Policy" | ||
| ]; | ||
|
|
||
| let rules: HeaderRule[]; | ||
|
|
||
| beforeAll(async () => { | ||
| // next.config.js is CommonJS; both its production and development exports | ||
| // wrap the same base config, whose headers() is what ships. | ||
| const config = require(CONFIG) as { headers: () => Promise<HeaderRule[]> }; | ||
| rules = await config.headers(); | ||
| }); | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
|
|
||
| // Same matcher options the production server uses for the headers manifest | ||
| // (next/dist/server/lib/router-utils/filesystem.js). | ||
| const MATCH = { strict: true, removeUnnamedParams: true, sensitive: false }; | ||
|
|
||
| function headersFor(pathname: string): string[] { | ||
| return rules | ||
| .filter((rule) => getPathMatch(rule.source, MATCH)(pathname)) | ||
| .flatMap((rule) => rule.headers.map((h) => h.key)); | ||
| } | ||
|
|
||
| describe("security headers stay on documents (#1592)", () => { | ||
| it.each([ | ||
| "/", | ||
| "/trending", | ||
| "/@good-karma", | ||
| "/hive-125125", | ||
| "/@ecency/some-post", | ||
| "/api/csp-report", | ||
| "/sw.js", | ||
| "/firebase-messaging-sw.js", | ||
| "/manifest.json", | ||
| "/favicon.ico", | ||
| // Public files keep the block: a miss under these prefixes renders the HTML | ||
| // not-found page, which needs its clickjacking protection. | ||
| "/assets/img/logo-circle.svg", | ||
| "/assets/nope.png", | ||
| "/scripts/x.js", | ||
| "/geo/cities.json", | ||
| // Only the exact prefix is excluded, not routes that merely start with | ||
| // the same letters. | ||
| "/_next/staticx", | ||
| "/_next/static" | ||
| ])("%s carries the full security block", (pathname) => { | ||
| const keys = headersFor(pathname); | ||
| for (const key of SECURITY) expect(keys).toContain(key); | ||
| expect(keys).toContain("X-Content-Type-Options"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("static responses carry only nosniff + cache headers (#1592)", () => { | ||
| it.each([ | ||
| "/_next/static/css/8c78658f5066068d.css", | ||
| "/_next/static/chunks/49542-f010dbacf7effe19.js", | ||
| "/_next/static/media/e4af272ccee01ff0-s.p.woff2", | ||
| "/_next/static/chunks/nope.js" | ||
| ])("%s", (pathname) => { | ||
| const keys = headersFor(pathname); | ||
| for (const key of SECURITY) expect(keys).not.toContain(key); | ||
| expect(keys).toContain("X-Content-Type-Options"); | ||
| expect(keys).toContain("Cache-Control"); | ||
| }); | ||
|
|
||
| it("keeps the immutable cache policy on hashed assets", () => { | ||
| const rule = rules.find((r) => r.source === "/_next/static/:path*"); | ||
| expect(rule).toBeDefined(); | ||
| expect(rule!.headers.find((h) => h.key === "Cache-Control")?.value).toBe( | ||
| "public, max-age=31536000, immutable" | ||
| ); | ||
| }); | ||
| }); | ||
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.