-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(csp): create hashes of tracked scripts and hashes #13675
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
Parent:
feat: experimental CSP
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions
5
packages/astro/src/core/csp-hashes.js → ...es/astro/src/core/astro-islands-hashes.ts
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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| // This file is code-generated, please don't change it manually | ||
| export default [ | ||
| export const ASTRO_ISLAND_HASHES = [ | ||
| "GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=", | ||
| "HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=", | ||
| "XN6a2Vn8uvpBr/WhdYPdK0jVeCzlcOD2XYaP10veV4Y=", | ||
| "ZR0ZAU8UNTzLmo/ApeWH0y1mVLT+XtFkvZ5nw32W8jI=", | ||
| "cSNmhdbFlyTDRozeu9HPjo+B2S4QAeMp0RO41PqgAcA=", | ||
| "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=", | ||
| "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=" | ||
| "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=", | ||
| "s81ZcLcyAa7P/Jh5M5hUxYthTGwW+iZY3e6aHrQ8H9E=" | ||
| ]; |
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,40 @@ | ||
| import type { AstroConfig } from '../../types/public/index.js'; | ||
| import type { BuildInternals } from '../build/internal.js'; | ||
| import crypto from 'node:crypto'; | ||
| import type { AstroSettings } from '../../types/astro.js'; | ||
|
|
||
| export function shouldTrackCspHashes(config: AstroConfig): boolean { | ||
| return config.experimental?.csp === true; | ||
| } | ||
|
|
||
| export function trackStyleHashes(internals: BuildInternals): string[] { | ||
| const clientStyleHashes: string[] = []; | ||
| for (const [_, page] of internals.pagesByViteID.entries()) { | ||
| for (const style of page.styles) { | ||
| if (style.sheet.type === 'inline') { | ||
| clientStyleHashes.push( | ||
| crypto.createHash('sha256').update(style.sheet.content).digest('base64'), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return clientStyleHashes; | ||
| } | ||
|
|
||
| export function trackScriptHashes(internals: BuildInternals, settings: AstroSettings): string[] { | ||
| const clientScriptHashes: string[] = []; | ||
|
|
||
| for (const script of internals.inlinedScripts.values()) { | ||
| clientScriptHashes.push(crypto.createHash('sha256').update(script).digest('base64')); | ||
| } | ||
|
|
||
| for (const script of settings.scripts) { | ||
| const { content, stage } = script; | ||
| if (stage === 'head-inline' || stage === 'before-hydration') { | ||
| clientScriptHashes.push(crypto.createHash('sha256').update(content).digest('base64')); | ||
| } | ||
| } | ||
|
|
||
| return clientScriptHashes; | ||
| } | ||
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,2 @@ | ||
| export const ISLAND_STYLES = | ||
| '<style>astro-island,astro-slot,astro-static-slot{display:contents}</style>'; |
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
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,42 @@ | ||
| import { before, describe, it } from 'node:test'; | ||
| import { loadFixture } from './test-utils.js'; | ||
| import testAdapter from './test-adapter.js'; | ||
| import assert from 'node:assert/strict'; | ||
| import * as cheerio from 'cheerio'; | ||
|
|
||
| describe('CSP', () => { | ||
| let app; | ||
| /** | ||
| * @type {import('../dist/core/build/types.js').SSGManifest} | ||
| */ | ||
| let manifest; | ||
| /** @type {import('./test-utils.js').Fixture} */ | ||
| let fixture; | ||
| before(async () => { | ||
| fixture = await loadFixture({ | ||
| root: './fixtures/csp/', | ||
| adapter: testAdapter({ | ||
| setManifest(_manifest) { | ||
| manifest = _manifest; | ||
| }, | ||
| }), | ||
| }); | ||
| await fixture.build(); | ||
| app = await fixture.loadTestAdapterApp(); | ||
| }); | ||
|
|
||
| it('should contain the meta style hashes when CSS is imported from Astro component', async () => { | ||
| if (manifest) { | ||
| const request = new Request('http://example.com/index.html'); | ||
| const response = await app.render(request); | ||
| const $ = cheerio.load(await response.text()); | ||
|
|
||
| for (const hash of manifest.clientStyleHashes) { | ||
| let meta = $('meta[http-equiv="Content-Security-Policy"][content="' + hash + '"]'); | ||
| assert.equal(meta.length, 1, `Should have a CSP meta tag for ${hash}`); | ||
| } | ||
| } else { | ||
| assert.fail('Should have the manifest'); | ||
| } | ||
| }); | ||
| }); |
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,8 @@ | ||
| import { defineConfig } from 'astro/config'; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| csp: true, | ||
| } | ||
| }); | ||
|
|
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.