Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(ui): use secure randomness for regex password generation (#8926)
Andrew Huang244f66f73167
diff --git a/frontend/src/components/v3/platform/PasswordGenerator/PasswordGenerator.tsx b/frontend/src/components/v3/platform/PasswordGenerator/PasswordGenerator.tsxindex f851cfb9c6..5d73d690e9 100644--- a/frontend/src/components/v3/platform/PasswordGenerator/PasswordGenerator.tsx+++ b/frontend/src/components/v3/platform/PasswordGenerator/PasswordGenerator.tsx@@ -7,7 +7,7 @@ import {ShieldCheckIcon,TriangleAlertIcon} from "lucide-react";-import RandExp from "randexp";+import type RandExp from "randexp";import { twMerge } from "tailwind-merge";import { useTimedReset } from "@app/hooks";@@ -27,6 +27,7 @@ import { IconButton } from "../../generic/IconButton";import { Label } from "../../generic/Label";import { Popover, PopoverContent, PopoverTrigger } from "../../generic/Popover";import { Tooltip, TooltipContent, TooltipTrigger } from "../../generic/Tooltip";+import { buildSecureRandExp } from "./secure-randexp";type PasswordOptionsType = {length: number;@@ -67,11 +68,13 @@ const generateFromConstraints = (constraints: TValueConstraints): string => {if (regexValue) {// Regex takes full precedence over min/max length constraints+ let generator: RandExp | undefined;try {- middle = new RandExp(new RegExp(regexValue)).gen();+ generator = buildSecureRandExp(new RegExp(regexValue));} catch {// Fall through to manual generation if regex is invalid}+ middle = generator?.gen();}if (middle === undefined) {diff --git a/frontend/src/components/v3/platform/PasswordGenerator/secure-randexp.test.ts b/frontend/src/components/v3/platform/PasswordGenerator/secure-randexp.test.tsnew file mode 100644index 0000000000..6b6db7a81f--- /dev/null+++ b/frontend/src/components/v3/platform/PasswordGenerator/secure-randexp.test.ts@@ -0,0 +1,68 @@+import { afterEach, describe, expect, it, vi } from "vitest";++import { buildSecureRandExp } from "./secure-randexp";++afterEach(() => {+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+});++describe("secure RandExp", () => {+ it("uses Web Crypto for choices, repetitions and characters, never Math.random", () => {+ const random = vi.spyOn(Math, "random").mockImplementation(() => {+ throw new Error("Insecure random source used");+ });+ const secureRandom = vi.spyOn(globalThis.crypto, "getRandomValues");+ const pattern = /^(ab|cd)[A-Z]{12,24}[0-9]{2}$/i;+ const value = buildSecureRandExp(pattern).gen();++ expect(value).toMatch(pattern);+ expect(secureRandom).toHaveBeenCalled();+ expect(random).not.toHaveBeenCalled();+ });++ it("includes both integer bounds and rejects the biased tail", () => {+ const samples = [0, 5, 2 ** 32 - 5, 2 ** 32 - 4, 2 ** 32 - 1, 2];+ const getRandomValues = vi.fn((array: Uint32Array) => {+ array.set([samples.shift()!]);+ return array;+ });+ vi.stubGlobal("crypto", { getRandomValues });+ const generator = buildSecureRandExp(/[a-f]/);++ expect(generator.randInt(10, 15)).toBe(10);+ expect(generator.randInt(10, 15)).toBe(15);+ expect(generator.randInt(10, 15)).toBe(15);+ expect(generator.randInt(10, 15)).toBe(12);+ expect(getRandomValues).toHaveBeenCalledTimes(6);+ });++ it("handles singleton and full uint32 ranges and refuses invalid bounds", () => {+ vi.stubGlobal("crypto", {+ getRandomValues: (array: Uint32Array) => array.fill(2 ** 32 - 1)+ });+ const generator = buildSecureRandExp(/[a-z]/);++ expect(generator.randInt(7, 7)).toBe(7);+ expect(generator.randInt(0, 2 ** 32 - 1)).toBe(2 ** 32 - 1);+ expect(() => generator.randInt(1, 0)).toThrow(RangeError);+ expect(() => generator.randInt(0, 2 ** 32)).toThrow(RangeError);+ expect(() => generator.randInt(0.5, 2)).toThrow(RangeError);+ expect(() => generator.randInt(0, Infinity)).toThrow(RangeError);+ });++ it("fails without a value when Web Crypto is missing or fails", () => {+ const random = vi.spyOn(Math, "random");+ const generator = buildSecureRandExp(/[A-Z]{16}/);+ vi.stubGlobal("crypto", undefined);+ expect(() => generator.gen()).toThrow();++ vi.stubGlobal("crypto", {+ getRandomValues: () => {+ throw new Error("Secure randomness unavailable");+ }+ });+ expect(() => generator.gen()).toThrow("Secure randomness unavailable");+ expect(random).not.toHaveBeenCalled();+ });+});diff --git a/frontend/src/components/v3/platform/PasswordGenerator/secure-randexp.ts b/frontend/src/components/v3/platform/PasswordGenerator/secure-randexp.tsnew file mode 100644index 0000000000..655268b2e2--- /dev/null+++ b/frontend/src/components/v3/platform/PasswordGenerator/secure-randexp.ts@@ -0,0 +1,26 @@+import RandExp from "randexp";++export const buildSecureRandExp = (pattern: RegExp): RandExp => {+ const instance = new RandExp(pattern);+ instance.randInt = (min, max) => {+ const range = max - min + 1;+ const sampleSpace = 2 ** 32;+ if (+ !Number.isSafeInteger(min) ||+ !Number.isSafeInteger(max) ||+ range < 1 ||+ range > sampleSpace+ ) {+ throw new RangeError("Unsupported password generation bounds");+ }++ const limit = sampleSpace - (sampleSpace % range);+ const sample = new Uint32Array(1);+ do {+ globalThis.crypto.getRandomValues(sample);+ } while (sample[0] >= limit);++ return min + (sample[0] % range);+ };+ return instance;+};