Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor math.random (#541)
Leendert de Borst9de7f81053d0
diff --git a/browser-extensions/chrome/src/contentScript/Popup.ts b/browser-extensions/chrome/src/contentScript/Popup.tsindex a1d927624..042e9ee0c 100644--- a/browser-extensions/chrome/src/contentScript/Popup.ts+++ b/browser-extensions/chrome/src/contentScript/Popup.ts@@ -231,11 +231,11 @@ export function createAutofillPopup(input: HTMLInputElement, credentials: Creden// Determine service name based on conditionslet suggestedName = document.title;- // First try to extract the last part after common divider characters- const dividerMatch = document.title.match(/[\s]*[|\-–—/\\]+[\s]*([^|\-–—/\\]+)$/);- if (dividerMatch && dividerMatch[1].trim().split(/\s+/).length === 1) {+ // First try to extract the last part after common divider characters using a safe pattern+ const dividerMatch = document.title.match(/[|\-–—/\\][^|\-–—/\\]*$/);+ if (dividerMatch && dividerMatch[0].trim().split(/\s+/).length === 1) {// If we found a match and it's a single word, use it- suggestedName = dividerMatch[1].trim();+ suggestedName = dividerMatch[0].trim();} else {// Fall back to previous logic for long titlesconst wordCount = document.title.trim().split(/\s+/).length;diff --git a/browser-extensions/chrome/src/shared/generators/Identity/UsernameEmailGenerator.ts b/browser-extensions/chrome/src/shared/generators/Identity/UsernameEmailGenerator.tsindex a498e35fd..e2ac86b75 100644--- a/browser-extensions/chrome/src/shared/generators/Identity/UsernameEmailGenerator.ts+++ b/browser-extensions/chrome/src/shared/generators/Identity/UsernameEmailGenerator.ts@@ -32,7 +32,7 @@ export class UsernameEmailGenerator {public generateEmailPrefix(identity: Identity): string {const parts: string[] = [];- switch (Math.floor(Math.random() * 4)) {+ switch (this.getSecureRandom(4)) {case 0:// First initial + last nameparts.push(identity.firstName.substring(0, 1).toLowerCase() + identity.lastName.toLowerCase());@@ -52,8 +52,8 @@ export class UsernameEmailGenerator {}// Add birth year variations- if (Math.floor(Math.random() * 3) !== 0) {- switch (Math.floor(Math.random() * 2)) {+ if (this.getSecureRandom(3) !== 0) {+ switch (this.getSecureRandom(2)) {case 0:parts.push(identity.birthDate.getFullYear().toString().substring(2));break;@@ -61,17 +61,17 @@ export class UsernameEmailGenerator {parts.push(identity.birthDate.getFullYear().toString());break;}- } else if (Math.floor(Math.random() * 2) === 0) {+ } else if (this.getSecureRandom(2) === 0) {// Add random numbers for more uniqueness- parts.push((Math.floor(Math.random() * 990) + 10).toString());+ parts.push((this.getSecureRandom(990) + 10).toString());}// Join parts with random symbols, possibly multiplelet emailPrefix = parts.join(this.getRandomSymbol());// Add extra random symbol at random position- if (Math.floor(Math.random() * 2) === 0) {- const position = Math.floor(Math.random() * emailPrefix.length);+ if (this.getSecureRandom(2) === 0) {+ const position = this.getSecureRandom(emailPrefix.length);emailPrefix = emailPrefix.slice(0, position) + this.getRandomSymbol() + emailPrefix.slice(position);}@@ -97,8 +97,9 @@ export class UsernameEmailGenerator {// Remove consecutive dots, underscores, or hyphenssanitized = sanitized.replace(/[-_.]{2,}/g, (match) => match[0]);- // Ensure it doesn't start or end with a dot, underscore, or hyphen- sanitized = sanitized.replace(/^[-._]+|[-._]+$/g, '');+ // Remove leading and trailing dots, underscores, or hyphens+ sanitized = sanitized.replace(/^[-._]+/, ''); // Remove from start+ sanitized = sanitized.replace(/[-._]+$/, ''); // Remove from endreturn sanitized;}@@ -107,7 +108,7 @@ export class UsernameEmailGenerator {* Get a random symbol.*/private getRandomSymbol(): string {- return Math.floor(Math.random() * 3) === 0 ? this.symbols[Math.floor(Math.random() * this.symbols.length)] : '';+ return this.getSecureRandom(3) === 0 ? this.symbols[this.getSecureRandom(this.symbols.length)] : '';}/**@@ -115,6 +116,15 @@ export class UsernameEmailGenerator {*/private generateRandomString(length: number): string {const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';- return Array.from({ length }, () => chars.charAt(Math.floor(Math.random() * chars.length))).join('');+ return Array.from({ length }, () => chars.charAt(this.getSecureRandom(chars.length))).join('');+ }++ /**+ * Generate a secure random integer between 0 (inclusive) and max (exclusive)+ */+ private getSecureRandom(max: number): number {+ const array = new Uint32Array(1);+ crypto.getRandomValues(array);+ return array[0] % max;}}