From 0203c2388a2e69cb842e0b6547c9146e577467a8 Mon Sep 17 00:00:00 2001 From: Sebastian Danielsson Date: Sun, 27 Sep 2026 23:50:19 +0200 Subject: [PATCH] fix(random): use crypto.getRandomValues for generated secrets `src/utils/random.ts` was built on Math.random(), and it backs the token generator, htpasswd salts and the passphrase suffixes. The PIN code generator and the database builder's default password called Math.random() directly. All now go through crypto.getRandomValues() with rejection sampling, so indices are unbiased, for ranges up to 2^53. The passphrase generator no longer calls generateSillyPassword(), which also picks words with Math.random(). It uses the package's word lists with the secure helpers, and skips pulling lodash and zxcvbn into the bundle. Behaviour changes that came with it: - randIntFromInterval() now includes its upper bound. The lorem ipsum sliders could never reach their maximum and the passphrase generator never put a number on the last word. The coin flipper used `0..10 % 2`, which would be biased with an inclusive range, so it now draws 0 or 1. - randIntFromInterval() orders reversed bounds, rounds fractional ones inward and returns NaN for non-finite input instead of throwing. - The lorem ipsum generator falls back to its defaults for malformed `paragraphs`, `sentences` and `words` URL values and clamps them to the slider ranges, so a link like `?sentences=[1,4294967297]` cannot freeze the page. - Passphrases whose length equals the min or max length are accepted. - Multi-word list entries ("wide eyed") count as one word, so the word count and number placement match the settings. - Token generator draws each character independently instead of shuffling the alphabet repeated `length` times. Fixes the CodeQL js/insecure-randomness alert in the passphrase generator. Co-Authored-By: Claude Opus 5.5 --- src/tools/coin-flipper/coin-flipper.vue | 2 +- .../database-builder/database-builder.vue | 3 +- .../lorem-ipsum-generator.vue | 26 +++++-- .../passphrase-generator.service.test.ts | 34 +++++++++ .../passphrase-generator.service.ts | 36 +++++++++ .../passphrase-generator.vue | 36 ++------- .../pin-code-generator.service.ts | 5 +- .../token-generator.service.test.ts | 9 ++- .../token-generator.service.ts | 4 +- src/utils/random.test.ts | 73 +++++++++++++++++++ src/utils/random.ts | 62 ++++++++++++++-- 11 files changed, 243 insertions(+), 47 deletions(-) create mode 100644 src/tools/passphrase-generator/passphrase-generator.service.test.ts create mode 100644 src/tools/passphrase-generator/passphrase-generator.service.ts create mode 100644 src/utils/random.test.ts diff --git a/src/tools/coin-flipper/coin-flipper.vue b/src/tools/coin-flipper/coin-flipper.vue index d8f00429..5a4ae89e 100644 --- a/src/tools/coin-flipper/coin-flipper.vue +++ b/src/tools/coin-flipper/coin-flipper.vue @@ -6,7 +6,7 @@ import { randIntFromInterval } from '@/utils/random'; const { t } = useI18n(); const [coinFlip, refreshCoinFlip] = computedRefreshable(() => ({ - coin: randIntFromInterval(0, 10) % 2 === 0 ? 'Heads' : 'Tails', + coin: randIntFromInterval(0, 1) === 0 ? 'Heads' : 'Tails', dt: Date.now(), })); diff --git a/src/tools/database-builder/database-builder.vue b/src/tools/database-builder/database-builder.vue index e8ea29e6..fba004a8 100644 --- a/src/tools/database-builder/database-builder.vue +++ b/src/tools/database-builder/database-builder.vue @@ -1,6 +1,7 @@