fix: clear csp during suspense (#73)

This commit is contained in:
Luca Casonato
2021-11-01 13:06:26 +01:00
committed by GitHub
parent 44a0d490fe
commit 6ecd7a680b
2 changed files with 14 additions and 6 deletions
-2
View File
@@ -48,5 +48,3 @@ export const handler = {
return Response.redirect(ctx.req.url, 303); return Response.redirect(ctx.req.url, 303);
}, },
}; };
export const config: PageConfig = { csp: true };
+14 -4
View File
@@ -82,6 +82,13 @@ export class RenderContext {
const MAX_SUSPENSE_DEPTH = 10; const MAX_SUSPENSE_DEPTH = 10;
function defaultCsp() {
return {
directives: { defaultSrc: [NONE], styleSrc: [UNSAFE_INLINE] },
reportOnly: false,
};
}
/** /**
* This function renders out a page. Rendering is asynchronous, and streaming. * This function renders out a page. Rendering is asynchronous, and streaming.
* Rendering happens in multiple steps, because of the need to handle suspense. * Rendering happens in multiple steps, because of the need to handle suspense.
@@ -110,10 +117,7 @@ export async function* render(
const props = { params: opts.params, url: opts.url, route: opts.page.route }; const props = { params: opts.params, url: opts.url, route: opts.page.route };
const csp: ContentSecurityPolicy | undefined = opts.page.csp const csp: ContentSecurityPolicy | undefined = opts.page.csp
? { ? defaultCsp()
directives: { defaultSrc: [NONE], styleSrc: [UNSAFE_INLINE] },
reportOnly: false,
}
: undefined; : undefined;
const dataCache = new Map(); const dataCache = new Map();
const suspenseQueue: ComponentChildren[] = []; const suspenseQueue: ComponentChildren[] = [];
@@ -137,6 +141,12 @@ export async function* render(
let suspended = 0; let suspended = 0;
const renderWithRenderer = (): string | Promise<string> => { const renderWithRenderer = (): string | Promise<string> => {
if (csp) {
// Clear the csp
const newCsp = defaultCsp();
csp.directives = newCsp.directives;
csp.reportOnly = newCsp.reportOnly;
}
// Clear the suspense queue // Clear the suspense queue
suspenseQueue.splice(0, suspenseQueue.length); suspenseQueue.splice(0, suspenseQueue.length);
// Clear the head components // Clear the head components