Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/security/src/app/extend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { debuglog } from 'node:util';

import Tokens from 'csrf';
import { Context } from 'egg';
import { nanoid } from 'nanoid/non-secure';
import { nanoid } from 'nanoid';

import type { SecurityConfig } from '../../config/config.default.ts';
import type { HttpClientRequestURL, HttpClientOptions, HttpClientResponse } from '../../lib/extend/safe_curl.ts';
Expand Down
20 changes: 20 additions & 0 deletions plugins/security/test/csp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ describe.skipIf(process.platform === 'win32')('test/csp.test.ts', () => {
const nonce = res.text;
expect(res.headers['x-csp-nonce']).toBe(nonce);
});

it('should generate unpredictable nonce even when Math.random is fixed', async () => {
// The CSP nonce must come from a cryptographically secure RNG.
// `nanoid/non-secure` derives every char from `Math.random()`, so pinning
// `Math.random` to a constant would make the nonce fully predictable.
// A CSPRNG-backed nonce stays random regardless of `Math.random`.
mm(Math, 'random', () => 0);
try {
const res1 = await app.httpRequest().get('/testcsp').expect(200);
const res2 = await app.httpRequest().get('/testcsp').expect(200);
expect(res1.text.length).toBe(16);
expect(res2.text.length).toBe(16);
// not constant across requests
expect(res1.text).not.toBe(res2.text);
// not the predictable value Math.random()===0 would produce
expect(res1.text).not.toBe('u'.repeat(16));
} finally {
mm.restore();
}
});
});

it('should ignore path', async () => {
Expand Down
Loading