Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/early-bees-write.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where `report-uri` wasn't available in `experimental.csp.directives`, causing a typing error and a runtime validation error.
5 changes: 5 additions & 0 deletions .changeset/shaggy-singers-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a type error for the CSP directives `upgrade-insecure-requests`, `sandbox`, and `trusted-type`.
3 changes: 2 additions & 1 deletion packages/astro/src/core/csp/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ const ALLOWED_DIRECTIVES = [
'object-src',
'referrer',
'report-to',
'report-uri',
'require-trusted-types-for',
'sandbox',
'trusted-types',
'upgrade-insecure-requests',
'worker-src',
] as const;
type AllowedDirectives = (typeof ALLOWED_DIRECTIVES)[number];
export type CspDirective = `${AllowedDirectives} ${string}`;
export type CspDirective = `${AllowedDirectives}${string | undefined}`;

export const allowedDirectivesSchema = z.custom<CspDirective>((value) => {
if (typeof value !== 'string') {
Expand Down
25 changes: 25 additions & 0 deletions packages/astro/test/csp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,31 @@ describe('CSP', () => {
assert.ok(meta.attr('content').toString().includes("'strict-dynamic';"));
});

it("allows the use of directives that don't require values, and deprecated ones", async () => {
fixture = await loadFixture({
root: './fixtures/csp/',
experimental: {
csp: {
directives: [
'upgrade-insecure-requests',
'sandbox',
'trusted-types',
'report-uri https://endpoint.example.com',
],
},
},
});
await fixture.build();
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

const meta = $('meta[http-equiv="Content-Security-Policy"]');
assert.ok(meta.attr('content').toString().includes('upgrade-insecure-requests'));
assert.ok(meta.attr('content').toString().includes('sandbox'));
assert.ok(meta.attr('content').toString().includes('trusted-types'));
assert.ok(meta.attr('content').toString().includes('report-uri https://endpoint.example.com'));
});

it('should serve hashes via headers for dynamic pages, when the strategy is "auto"', async () => {
fixture = await loadFixture({
root: './fixtures/csp-adapter/',
Expand Down