Skip to content

Commit 66f3255

Browse files
tweak
1 parent 6669b9a commit 66f3255

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
lines changed

packages/svelte/src/internal/server/renderer.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ export class Renderer {
382382
static render(component, options = {}) {
383383
/** @type {AccumulatedContent | undefined} */
384384
let sync;
385-
/** @type {Promise<AccumulatedContent> | undefined} */
385+
/** @type {Promise<AccumulatedContent & { hashes: { script: string[] } }> | undefined} */
386386
let async;
387387

388388
const result = /** @type {RenderOutput} */ ({});
@@ -426,7 +426,7 @@ export class Renderer {
426426
head: result.head,
427427
body: result.body,
428428
html: result.body,
429-
hashes: { script: '' }
429+
hashes: { script: [] }
430430
});
431431
return Promise.resolve(user_result);
432432
}
@@ -521,7 +521,7 @@ export class Renderer {
521521
* @template {Record<string, any>} Props
522522
* @param {Component<Props>} component
523523
* @param {{ props?: Omit<Props, '$$slots' | '$$events'>; context?: Map<any, any>; idPrefix?: string; csp?: CspInternal }} options
524-
* @returns {Promise<AccumulatedContent & { hashes: { script: string } }>}
524+
* @returns {Promise<AccumulatedContent & { hashes: { script: string[] } }>}
525525
*/
526526
static async #render_async(component, options) {
527527
const previous_context = ssr_context;
@@ -629,7 +629,7 @@ export class Renderer {
629629
/**
630630
* @param {AccumulatedContent} content
631631
* @param {Renderer} renderer
632-
* @returns {AccumulatedContent & { hashes: { script: string } }}
632+
* @returns {AccumulatedContent & { hashes: { script: string[] } }}
633633
*/
634634
static #close_render(content, renderer) {
635635
for (const cleanup of renderer.#collect_on_destroy()) {
@@ -647,7 +647,7 @@ export class Renderer {
647647
head,
648648
body,
649649
hashes: {
650-
script: renderer.global.csp.script_hashes.map((hash) => `'${hash}'`).join(' ')
650+
script: renderer.global.csp.script_hashes
651651
}
652652
};
653653
}
@@ -694,8 +694,11 @@ export class Renderer {
694694
if (this.global.csp.nonce) {
695695
csp_attr = ` nonce="${this.global.csp.nonce}"`;
696696
} else if (this.global.csp.hash) {
697+
// note to future selves: this doesn't need to be optimized with a Map<body, hash>
698+
// because the it's impossible for identical data to occur multiple times in a single render
699+
// (this would require the same hydratable key:value pair to be serialized multiple times)
697700
const hash = await sha256(body);
698-
this.global.csp.script_hashes.push(hash);
701+
this.global.csp.script_hashes.push(`sha256-${hash}`);
699702
}
700703

701704
return `<script${csp_attr}>${body}</script>`;

packages/svelte/src/internal/server/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface SyncRenderOutput {
4545
/** HTML that goes somewhere into the `<body>` */
4646
body: string;
4747
hashes: {
48-
script: string;
48+
script: string[];
4949
};
5050
}
5151

packages/svelte/tests/server-side-rendering/samples/csp-hash/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { test } from '../../test';
33
export default test({
44
mode: ['async'],
55
csp: { hash: true },
6-
script_hashes: "'TbJbBVc8UZ9yf9YRlCZCPBDitel+IaSg0BXUsnAx0cA='"
6+
script_hashes: ['sha256-TbJbBVc8UZ9yf9YRlCZCPBDitel+IaSg0BXUsnAx0cA=']
77
});

packages/svelte/tests/server-side-rendering/samples/csp-nonce-precedence/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { test } from '../../test';
33
export default test({
44
mode: ['async'],
55
csp: { hash: true, nonce: 'test-nonce' },
6-
script_hashes: ''
6+
script_hashes: []
77
});

packages/svelte/tests/server-side-rendering/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface SSRTest extends BaseTest {
2222
withoutNormalizeHtml?: boolean;
2323
error?: string;
2424
csp?: { nonce: string } | { hash: true };
25-
script_hashes?: string;
25+
script_hashes?: string[];
2626
}
2727

2828
// TODO remove this shim when we can
@@ -145,7 +145,7 @@ const { test, run } = suite_with_variants<SSRTest, 'sync' | 'async', CompileOpti
145145
}
146146

147147
if (config.script_hashes !== undefined) {
148-
assert.equal(hashes.script, config.script_hashes);
148+
assert.deepEqual(hashes.script, config.script_hashes);
149149
}
150150
}
151151
);

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ declare module 'svelte/server' {
25762576
/** HTML that goes somewhere into the `<body>` */
25772577
body: string;
25782578
hashes: {
2579-
script: string;
2579+
script: string[];
25802580
};
25812581
}
25822582

0 commit comments

Comments
 (0)