Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

New password generation for users (#70646)

Nick Fitzpatrickba8074bb87e4
diff --git a/bun.lock b/bun.lock
index faa4af5a836..d0e9c837d57 100644
--- a/bun.lock
+++ b/bun.lock
@@ -122,7 +122,6 @@
"mustache": "^2.3.2",
"normalizr": "^3.0.2",
"ora": "^8.0.1",
- "password-generator": "2.3.2",
"prettier": "^3.3.3",
"process": "^0.11.10",
"prop-types": "^15.5.7",
@@ -4164,8 +4163,6 @@
"pascal-case": ["[email protected]", "", { "dependencies": { "no-case": "^3.0.4", "tslib": "^2.0.3" } }, "sha512-uWlGT3YSnK9x3BQJaOdcZwrnV6hPpd8jFH1/ucpiLRPh/2zCVJKS19E4GvYHvaCcACn3foXZ0cLB9Wrx1KGe5g=="],
- "password-generator": ["[email protected]", "", { "bin": { "password-generator": "bin/password-generator" } }, "sha512-kJWUrdveSAqHCeWJWnv5vNc89hFHM5au+pvKja5+xCTxlRF3zQaecJlR6hSoOotAJtQ3otQq4/Q4iWc/TxsXhA=="],
-
"patch-console": ["[email protected]", "", {}, "sha512-nxl9nrnLQmh64iTzMfyylSlRozL7kAXIaxw1fVcLYdyhNkJCRUzirRZTikXGJsg+hc4fqpneTK6iU2H1Q8THSA=="],
"patch-package": ["[email protected]", "", { "dependencies": { "@yarnpkg/lockfile": "^1.1.0", "chalk": "^4.1.2", "ci-info": "^3.7.0", "cross-spawn": "^7.0.3", "find-yarn-workspace-root": "^2.0.0", "fs-extra": "^9.0.0", "json-stable-stringify": "^1.0.2", "klaw-sync": "^6.0.0", "minimist": "^1.2.6", "open": "^7.4.2", "rimraf": "^2.6.3", "semver": "^7.5.3", "slash": "^2.0.0", "tmp": "^0.0.33", "yaml": "^2.2.2" }, "bin": { "patch-package": "index.js" } }, "sha512-da8BVIhzjtgScwDJ2TtKsfT5JFWz1hYoBl9rUQ1f38MC2HwnEIkK8VN3dKMKcP7P7bvvgzNDbfNHtx3MsQb5vA=="],
diff --git a/frontend/src/metabase/lib/security.ts b/frontend/src/metabase/lib/security.ts
index 09d2d402bdc..b6b4fce50d8 100644
--- a/frontend/src/metabase/lib/security.ts
+++ b/frontend/src/metabase/lib/security.ts
@@ -1,40 +1,60 @@
-import passwordGenerator from "password-generator";
-
import MetabaseSettings from "metabase/lib/settings";
import type { PasswordComplexity } from "metabase-types/api";
+const LOWER = "abcdefghijklmnopqrstuvwxyz";
+const UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+const DIGITS = "0123456789";
+const SPECIAL = "!@#$%^&*()-_+=";
+const ALL = LOWER + UPPER + DIGITS + SPECIAL;
+
+function randomIndex(max: number): number {
+ const array = new Uint32Array(1);
+ crypto.getRandomValues(array);
+ return array[0] % max;
+}
+
+function randomChar(charset: string): string {
+ return charset[randomIndex(charset.length)];
+}
+
// generate a password that satisfies `complexity` requirements, by default the ones that come back in the
-// `password-complexity` Setting; must be a map like {total: 6, number: 1}
-export const generatePassword = (complexityParam?: PasswordComplexity) => {
+// `password-complexity` Setting; must be a map like {total: 6, digit: 1}
+export const generatePassword = (
+ complexityParam?: PasswordComplexity,
+): string => {
const complexity =
complexityParam || MetabaseSettings.passwordComplexityRequirements() || {};
+
// generated password must be at least `complexity.total`, but can be longer
// so hard code a minimum of 14
const len = Math.max(complexity.total || 0, 14);
- let password = "";
- let tries = 0;
- while (!isStrongEnough(password) && tries < 100) {
- password = passwordGenerator(len, false, /[\w\d\?\-]/);
- tries++;
+ const chars: string[] = [];
+
+ // seed required characters from each category to guarantee requirements are met
+ for (let i = 0; i < (complexity.lower || 0); i++) {
+ chars.push(randomChar(LOWER));
+ }
+ for (let i = 0; i < (complexity.upper || 0); i++) {
+ chars.push(randomChar(UPPER));
}
- return password;
-
- function isStrongEnough(password: string) {
- const uc = password.match(/([A-Z])/g);
- const lc = password.match(/([a-z])/g);
- const di = password.match(/([\d])/g);
- const sc = password.match(/([!@#\$%\^\&*\)\(+=._-{}])/g);
-
- return (
- uc &&
- uc.length >= (complexity.upper || 0) &&
- lc &&
- lc.length >= (complexity.lower || 0) &&
- di &&
- di.length >= (complexity.digit || 0) &&
- sc &&
- sc.length >= (complexity.special || 0)
- );
+ for (let i = 0; i < (complexity.digit || 0); i++) {
+ chars.push(randomChar(DIGITS));
}
+ for (let i = 0; i < (complexity.special || 0); i++) {
+ chars.push(randomChar(SPECIAL));
+ }
+
+ // fill remaining length with random characters from full charset
+ while (chars.length < len) {
+ chars.push(randomChar(ALL));
+ }
+
+ // Fisher-Yates shuffle to avoid required chars clustering at the start
+ for (let i = chars.length - 1; i > 0; i--) {
+ const j = randomIndex(i + 1);
+ [chars[i], chars[j]] = [chars[j], chars[i]];
+ }
+
+ return chars.join("");
};
diff --git a/frontend/src/metabase/lib/security.unit.spec.js b/frontend/src/metabase/lib/security.unit.spec.js
deleted file mode 100644
index 96880cf98ca..00000000000
--- a/frontend/src/metabase/lib/security.unit.spec.js
+++ /dev/null
@@ -1,49 +0,0 @@
-import MetabaseSettings from "metabase/lib/settings";
-
-import { generatePassword } from "./security";
-
-describe("generatePassword", () => {
- it("defaults to at least 14 characters even if password-complexity requirements are lower", () => {
- MetabaseSettings.set("password-complexity", { total: 10 });
- expect(generatePassword().length).toBe(14);
- });
-
- it("defaults to complexity requirements if greater than 14", () => {
- MetabaseSettings.set("password-complexity", { total: 20 });
- expect(generatePassword().length).toBe(20);
- });
-
- it("falls back to length 14 passwords", () => {
- expect(generatePassword({}).length).toBe(14);
- });
-
- it("creates passwords for the length we specify", () => {
- expect(generatePassword({ total: 25 }).length).toBe(25);
- });
-
- it("can enforce", () => {
- expect(
- generatePassword({ total: 14, digit: 2 }).match(/([\d])/g).length >= 2,
- ).toBe(true);
- });
-
- it("can enforce digit requirements", () => {
- expect(
- generatePassword({ total: 14, digit: 2 }).match(/([\d])/g).length >= 2,
- ).toBe(true);
- });
-
- it("can enforce uppercase requirements", () => {
- expect(
- generatePassword({ total: 14, upper: 2 }).match(/([A-Z])/g).length >= 2,
- ).toBe(true);
- });
-
- it("can enforce special character requirements", () => {
- expect(
- generatePassword({ total: 14, special: 2 }).match(
- /([!@#\$%\^\&*\)\(+=._-{}])/g,
- ).length >= 2,
- ).toBe(true);
- });
-});
diff --git a/frontend/src/metabase/lib/security.unit.spec.ts b/frontend/src/metabase/lib/security.unit.spec.ts
new file mode 100644
index 00000000000..b76faf4954d
--- /dev/null
+++ b/frontend/src/metabase/lib/security.unit.spec.ts
@@ -0,0 +1,70 @@
+import MetabaseSettings from "metabase/lib/settings";
+
+import { generatePassword } from "./security";
+
+describe("generatePassword", () => {
+ it("defaults to at least 14 characters even if password-complexity requirements are lower", () => {
+ MetabaseSettings.set("password-complexity", { total: 10 });
+ expect(generatePassword().length).toBe(14);
+ });
+
+ it("defaults to complexity requirements if greater than 14", () => {
+ MetabaseSettings.set("password-complexity", { total: 20 });
+ expect(generatePassword().length).toBe(20);
+ });
+
+ it("falls back to length 14 passwords", () => {
+ expect(generatePassword({}).length).toBe(14);
+ });
+
+ it("creates passwords for the length we specify", () => {
+ expect(generatePassword({ total: 25 }).length).toBe(25);
+ });
+
+ it("can enforce digit requirements", () => {
+ expect(
+ generatePassword({ total: 14, digit: 2 }).match(/\d/g)!.length,
+ ).toBeGreaterThanOrEqual(2);
+ });
+
+ it("can enforce uppercase requirements", () => {
+ expect(
+ generatePassword({ total: 14, upper: 2 }).match(/[A-Z]/g)!.length,
+ ).toBeGreaterThanOrEqual(2);
+ });
+
+ it("can enforce lowercase requirements", () => {
+ expect(
+ generatePassword({ total: 14, lower: 3 }).match(/[a-z]/g)!.length,
+ ).toBeGreaterThanOrEqual(3);
+ });
+
+ it("can enforce special character requirements", () => {
+ expect(
+ generatePassword({ total: 14, special: 2 }).match(/[!@#$%^&*()\-_+=]/g)!
+ .length,
+ ).toBeGreaterThanOrEqual(2);
+ });
+
+ it("always meets strong complexity requirements", () => {
+ const strongComplexity = {
+ total: 10,
+ lower: 2,
+ upper: 2,
+ digit: 1,
+ special: 1,
+ };
+
+ // run 100 iterations to verify deterministic compliance
+ for (let i = 0; i < 100; i++) {
+ const password = generatePassword(strongComplexity);
+ expect(password.length).toBeGreaterThanOrEqual(14);
+ expect(password.match(/[a-z]/g)!.length).toBeGreaterThanOrEqual(2);
+ expect(password.match(/[A-Z]/g)!.length).toBeGreaterThanOrEqual(2);
+ expect(password.match(/\d/g)!.length).toBeGreaterThanOrEqual(1);
+ expect(
+ password.match(/[!@#$%^&*()\-_+=]/g)!.length,
+ ).toBeGreaterThanOrEqual(1);
+ }
+ });
+});
diff --git a/package.json b/package.json
index 32fdc666b90..734f8a66312 100644
--- a/package.json
+++ b/package.json
@@ -127,7 +127,6 @@
"mustache": "^2.3.2",
"normalizr": "^3.0.2",
"ora": "^8.0.1",
- "password-generator": "2.3.2",
"prettier": "^3.3.3",
"process": "^0.11.10",
"prop-types": "^15.5.7",
diff --git a/patches/password-generator+2.3.2.patch b/patches/password-generator+2.3.2.patch
deleted file mode 100644
index e77ee860f23..00000000000
--- a/patches/password-generator+2.3.2.patch
+++ /dev/null
@@ -1,23 +0,0 @@
-diff --git a/node_modules/password-generator/lib/password-generator.js b/node_modules/password-generator/lib/password-generator.js
-index 4048bc1..e5d478b 100644
---- a/node_modules/password-generator/lib/password-generator.js
-+++ b/node_modules/password-generator/lib/password-generator.js
-@@ -91,13 +91,15 @@
-
-
- getRandomValues = function (buf) {
-+ // override root to be able to run in both node and browser env
-+ root = globalThis;
- if (root.crypto && root.crypto.getRandomValues) {
- root.crypto.getRandomValues(buf);
- } else if (typeof root.msCrypto === "object" && typeof root.msCrypto.getRandomValues === 'function') {
- root.msCrypto.getRandomValues(buf);
-- } else if (module.exports === password && typeof require !== "undefined") {
-- var bytes = require("crypto").randomBytes(buf.length);
-- buf.set(bytes);
-+ // require('crypto') fails due to bundler's static analysis
-+ // both node and browsers have getRandomValues nowadays,
-+ // so require is removed
- } else {
- throw new Error("No secure random number generator available.");
- }