fix: generate one Content-Security-Policy nonce per render (#1480)
Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
15d8a67a2c
commit
ab45c5c42c
+17
-25
@@ -309,28 +309,27 @@ export async function render<Data>(
|
|||||||
|
|
||||||
bodyHtml = bodyHtml as unknown as string;
|
bodyHtml = bodyHtml as unknown as string;
|
||||||
|
|
||||||
const moduleScripts: [string, string][] = [];
|
let randomNonce: undefined | string;
|
||||||
|
function getRandomNonce(): string {
|
||||||
for (const url of opts.imports) {
|
if (randomNonce === undefined) {
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||||
if (csp) {
|
if (csp) {
|
||||||
csp.directives.scriptSrc = [
|
csp.directives.scriptSrc = [
|
||||||
...csp.directives.scriptSrc ?? [],
|
...csp.directives.scriptSrc ?? [],
|
||||||
nonce(randomNonce),
|
nonce(randomNonce),
|
||||||
];
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
moduleScripts.push([url, randomNonce]);
|
return randomNonce;
|
||||||
|
}
|
||||||
|
|
||||||
|
const moduleScripts: [string, string][] = [];
|
||||||
|
for (const url of opts.imports) {
|
||||||
|
moduleScripts.push([url, getRandomNonce()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const preloadSet = new Set<string>();
|
const preloadSet = new Set<string>();
|
||||||
function addImport(path: string): string {
|
function addImport(path: string): string {
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
||||||
if (csp) {
|
|
||||||
csp.directives.scriptSrc = [
|
|
||||||
...csp.directives.scriptSrc ?? [],
|
|
||||||
nonce(randomNonce),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
const url = bundleAssetUrl(`/${path}`);
|
const url = bundleAssetUrl(`/${path}`);
|
||||||
preloadSet.add(url);
|
preloadSet.add(url);
|
||||||
for (const depPath of opts.dependenciesFn(path)) {
|
for (const depPath of opts.dependenciesFn(path)) {
|
||||||
@@ -412,15 +411,8 @@ export async function render<Data>(
|
|||||||
|
|
||||||
// Append the inline script.
|
// Append the inline script.
|
||||||
if (script !== "") {
|
if (script !== "") {
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
||||||
if (csp) {
|
|
||||||
csp.directives.scriptSrc = [
|
|
||||||
...csp.directives.scriptSrc ?? [],
|
|
||||||
nonce(randomNonce),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
bodyHtml +=
|
bodyHtml +=
|
||||||
`<script type="module" nonce="${randomNonce}">${script}</script>`;
|
`<script type="module" nonce="${getRandomNonce()}">${script}</script>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.styles.length > 0) {
|
if (ctx.styles.length > 0) {
|
||||||
|
|||||||
@@ -896,3 +896,33 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "Generate a single nonce value per page",
|
||||||
|
|
||||||
|
async fn() {
|
||||||
|
await withPageName("./tests/fixture/main.ts", async (page, address) => {
|
||||||
|
await page.goto(address);
|
||||||
|
await page.waitForSelector("p");
|
||||||
|
|
||||||
|
const nonceValues = await page.evaluate(() =>
|
||||||
|
Array.from(
|
||||||
|
new Set(
|
||||||
|
Array.from(document.querySelectorAll("[nonce]")).map((el) =>
|
||||||
|
el.getAttribute("nonce")
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
nonceValues.length,
|
||||||
|
1,
|
||||||
|
`Found more than 1 nonce value per render`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user