Skip to content

Commit c8de01c

Browse files
committed
Change adapter nonce API to be more generic.
1 parent 8efd8af commit c8de01c

File tree

6 files changed

+7
-7
lines changed

6 files changed

+7
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const generateCspNonce = () => btoa(crypto.getRandomValues(new Uint32Array(4)));
1+
export const generateRandomString = (bytes) => btoa(crypto.getRandomValues(new Uint8Array(bytes)));
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { randomBytes } from 'crypto';
22
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';
33

4-
export const generateCspNonce = () => randomBytes(16).toString('base64');
4+
export const generateRandomString = (bytes) => randomBytes(bytes).toString('base64');

packages/adapter-node/src/shims.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createRequire } from 'module';
22
import { randomBytes } from 'crypto';
33
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';
44

5-
export const generateCspNonce = () => randomBytes(16).toString('base64');
5+
export const generateRandomString = (bytes) => randomBytes(bytes).toString('base64');
66

77
// esbuild automatically renames "require"
88
// So we still have to use Object.defineProperty here
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { randomBytes } from 'crypto';
22
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';
33

4-
export const generateCspNonce = () => randomBytes(16).toString('base64');
4+
export const generateRandomString = (bytes) => randomBytes(bytes).toString('base64');

packages/kit/src/runtime/server/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global generateCspNonce */
1+
/* global generateRandomString */
22
import { render_endpoint } from './endpoint.js';
33
import { render_page } from './page/index.js';
44
import { render_response } from './page/render.js';
@@ -39,7 +39,7 @@ export async function respond(incoming, options, state = {}) {
3939
let nonce;
4040
if (!state.prerender && options.cspNonce) {
4141
try {
42-
nonce = generateCspNonce();
42+
nonce = generateRandomString(16);
4343
} catch (e) {
4444
if (e instanceof ReferenceError) {
4545
console.warn(

packages/kit/types/globals.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
declare const generateCspNonce: () => string;
1+
declare const generateRandomString: (bytes: number) => string;

0 commit comments

Comments
 (0)