Skip to content

Commit 3224637

Browse files
ematipicoascorbic
andauthored
fix(csp): CSP headers in development server (#14283)
* fix(csp): CSP headers in development server * Update packages/astro/src/core/render-context.ts Co-authored-by: Matt Kane <m@mk.gg> --------- Co-authored-by: Matt Kane <m@mk.gg>
1 parent dfd88de commit 3224637

5 files changed

Lines changed: 29 additions & 28 deletions

File tree

.changeset/curly-laws-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an issue where CSP headers were incorrectly injected in the development server.

packages/astro/src/core/render-context.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class RenderContext {
5959
protected url = new URL(request.url),
6060
public props: Props = {},
6161
public partial: undefined | boolean = undefined,
62+
public shouldInjectCspMetaTags = !!pipeline.manifest.csp,
6263
public session: AstroSession | undefined = pipeline.manifest.sessionConfig
6364
? new AstroSession(cookies, pipeline.manifest.sessionConfig, pipeline.runtimeMode)
6465
: undefined,
@@ -87,9 +88,19 @@ export class RenderContext {
8788
props,
8889
partial = undefined,
8990
actions,
91+
shouldInjectCspMetaTags,
9092
}: Pick<RenderContext, 'pathname' | 'pipeline' | 'request' | 'routeData' | 'clientAddress'> &
9193
Partial<
92-
Pick<RenderContext, 'locals' | 'middleware' | 'status' | 'props' | 'partial' | 'actions'>
94+
Pick<
95+
RenderContext,
96+
| 'locals'
97+
| 'middleware'
98+
| 'status'
99+
| 'props'
100+
| 'partial'
101+
| 'actions'
102+
| 'shouldInjectCspMetaTags'
103+
>
93104
>): Promise<RenderContext> {
94105
const pipelineMiddleware = await pipeline.getMiddleware();
95106
const pipelineActions = actions ?? (await pipeline.getActions());
@@ -114,6 +125,7 @@ export class RenderContext {
114125
undefined,
115126
props,
116127
partial,
128+
shouldInjectCspMetaTags ?? !!pipeline.manifest.csp,
117129
);
118130
}
119131
/**
@@ -461,7 +473,7 @@ export class RenderContext {
461473

462474
const extraStyleHashes = [];
463475
const extraScriptHashes = [];
464-
const shouldInjectCspMetaTags = !!manifest.csp;
476+
const shouldInjectCspMetaTags = this.shouldInjectCspMetaTags;
465477
const cspAlgorithm = manifest.csp?.algorithm ?? 'SHA-256';
466478
if (shouldInjectCspMetaTags) {
467479
for (const style of styles) {

packages/astro/src/vite-plugin-astro-server/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export async function handleRoute({
196196
routeData: route,
197197
clientAddress: incomingRequest.socket.remoteAddress,
198198
actions,
199+
shouldInjectCspMetaTags: false,
199200
});
200201

201202
let response;
@@ -221,6 +222,7 @@ export async function handleRoute({
221222
routeData: route,
222223
clientAddress: incomingRequest.socket.remoteAddress,
223224
actions,
225+
shouldInjectCspMetaTags: false,
224226
});
225227
renderContext.props.error = err;
226228
const _response = await renderContext.render(preloaded500Component);
@@ -297,6 +299,7 @@ export async function handleRoute({
297299
request,
298300
routeData: fourOhFourRoute.route,
299301
clientAddress: incomingRequest.socket.remoteAddress,
302+
shouldInjectCspMetaTags: false,
300303
});
301304
response = await renderContext.render(fourOhFourRoute.preloadedComponent);
302305
}

packages/astro/test/fixtures/ssr-trailing-slash/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"start": "node dist/server/entry.mjs"
88
},
99
"dependencies": {
10-
"astro": "file:../../",
11-
"@astrojs/node": "^8.0.0"
10+
"astro": "workspace:*",
11+
"@astrojs/node": "workspace:*"
1212
}
13-
}
13+
}

pnpm-lock.yaml

Lines changed: 4 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)