Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Improve password generator non-ambigious chars option (#1398)

Leendert de Borst62cc0e7c2bb5
diff --git a/shared/password-generator/src/__tests__/PasswordGenerator.test.ts b/shared/password-generator/src/__tests__/PasswordGenerator.test.ts
index 59ada1fce..099dc79aa 100644
--- a/shared/password-generator/src/__tests__/PasswordGenerator.test.ts
+++ b/shared/password-generator/src/__tests__/PasswordGenerator.test.ts
@@ -103,4 +103,69 @@ describe('PasswordGenerator', () => {
expect(result).toBe(generator);
});
+
+ it('removes ambiguous characters when useNonAmbiguousCharacters is enabled', () => {
+ /**
+ * Generate 50 passwords to ensure statistical confidence
+ * Check that none of the ambiguous characters appear:
+ * - I, l, 1, | - vertical line lookalikes
+ * - O, 0, o - circle lookalikes
+ * - Z, z, 2 - similar appearance
+ * - S, s, 5 - similar appearance
+ * - B, b, 8 - similar appearance
+ * - G, g, 6 - similar appearance
+ * - Brackets, braces, parentheses
+ * - Quotes
+ * - Punctuation pairs
+ * - Dashes
+ */
+ for (let i = 0; i < 50; i++) {
+ const password = generator
+ .setLength(32)
+ .useNonAmbiguousCharacters(true)
+ .generateRandomPassword();
+
+ expect(password).not.toMatch(/[Il1|]/);
+ expect(password).not.toMatch(/[O0o]/);
+ expect(password).not.toMatch(/[Zz2]/);
+ expect(password).not.toMatch(/[Ss5]/);
+ expect(password).not.toMatch(/[Bb8]/);
+ expect(password).not.toMatch(/[Gg6]/);
+ expect(password).not.toMatch(/[[\]{}()<>]/);
+ expect(password).not.toMatch(/['"`]/);
+ expect(password).not.toMatch(/[;:,.]/);
+ expect(password).not.toMatch(/[_-]/);
+ }
+ });
+
+ it('still generates valid passwords with non-ambiguous characters enabled', () => {
+ const password = generator
+ .setLength(20)
+ .useNonAmbiguousCharacters(true)
+ .generateRandomPassword();
+
+ expect(password.length).toBe(20);
+ // Should still contain allowed characters
+ expect(password.length).toBeGreaterThan(0);
+ });
+
+ it('includes ambiguous characters when useNonAmbiguousCharacters is disabled', () => {
+ // Generate multiple passwords and check if at least one contains ambiguous characters
+ let foundAmbiguous = false;
+
+ for (let i = 0; i < 100; i++) {
+ const password = generator
+ .setLength(32)
+ .useNonAmbiguousCharacters(false)
+ .generateRandomPassword();
+
+ // Check if any ambiguous characters are present
+ if (/[Il1O0oZzSsBbGg2568|[\]{}()<>;:,.`'"_-]/.test(password)) {
+ foundAmbiguous = true;
+ break;
+ }
+ }
+
+ expect(foundAmbiguous).toBe(true);
+ });
});
\ No newline at end of file
diff --git a/shared/password-generator/src/utils/PasswordGenerator.ts b/shared/password-generator/src/utils/PasswordGenerator.ts
index 330c52051..117e09c8a 100644
--- a/shared/password-generator/src/utils/PasswordGenerator.ts
+++ b/shared/password-generator/src/utils/PasswordGenerator.ts
@@ -8,7 +8,21 @@ export class PasswordGenerator {
private readonly uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
private readonly numberChars = '0123456789';
private readonly specialChars = '!@#$%^&*()_+-=[]{}|;:,.<>?';
- private readonly ambiguousChars = 'Il1O0o';
+ /**
+ * Ambiguous characters that look similar and are easy to confuse when typing:
+ * - I, l, 1, | (pipe) - all look like vertical lines
+ * - O, 0, o - all look like circles
+ * - Z, 2 - similar appearance
+ * - S, 5 - similar appearance
+ * - B, 8 - similar appearance
+ * - G, 6 - similar appearance
+ * - Brackets, braces, parentheses: [], {}, ()
+ * - Quotes: ', ", `
+ * - Punctuation pairs: ;:, .,
+ * - Dashes: -, _
+ * - Angle brackets: <>
+ */
+ private readonly ambiguousChars = 'Il1O0oZzSsBbGg2568|[]{}()<>;:,.`\'"_-';
private length: number = 18;
private useLowercase: boolean = true;