Skip to content

Commit

Permalink
Change adapter nonce API to be more generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlinator committed Sep 11, 2021
1 parent 8efd8af commit c8de01c
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/files/shims.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const generateCspNonce = () => btoa(crypto.getRandomValues(new Uint32Array(4)));
export const generateRandomString = (bytes) => btoa(crypto.getRandomValues(new Uint8Array(bytes)));
2 changes: 1 addition & 1 deletion packages/adapter-netlify/files/shims.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { randomBytes } from 'crypto';
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';

export const generateCspNonce = () => randomBytes(16).toString('base64');
export const generateRandomString = (bytes) => randomBytes(bytes).toString('base64');
2 changes: 1 addition & 1 deletion packages/adapter-node/src/shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRequire } from 'module';
import { randomBytes } from 'crypto';
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';

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

// esbuild automatically renames "require"
// So we still have to use Object.defineProperty here
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-vercel/files/shims.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { randomBytes } from 'crypto';
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';

export const generateCspNonce = () => randomBytes(16).toString('base64');
export const generateRandomString = (bytes) => randomBytes(bytes).toString('base64');
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global generateCspNonce */
/* global generateRandomString */
import { render_endpoint } from './endpoint.js';
import { render_page } from './page/index.js';
import { render_response } from './page/render.js';
Expand Down Expand Up @@ -39,7 +39,7 @@ export async function respond(incoming, options, state = {}) {
let nonce;
if (!state.prerender && options.cspNonce) {
try {
nonce = generateCspNonce();
nonce = generateRandomString(16);
} catch (e) {
if (e instanceof ReferenceError) {
console.warn(
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
declare const generateCspNonce: () => string;
declare const generateRandomString: (bytes: number) => string;

0 comments on commit c8de01c

Please sign in to comment.