Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add excluded characters option to password generator settings (#2490)
Leendert de Borstb279992bb05c
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Forms/PasswordConfigForm.tsx b/apps/browser-extension/src/entrypoints/popup/components/Forms/PasswordConfigForm.tsxindex d2f8d6743..73368029d 100644--- a/apps/browser-extension/src/entrypoints/popup/components/Forms/PasswordConfigForm.tsx+++ b/apps/browser-extension/src/entrypoints/popup/components/Forms/PasswordConfigForm.tsx@@ -257,6 +257,23 @@ const PasswordConfigForm: React.FC<IPasswordConfigFormProps> = ({{t('items.avoidAmbiguousCharacters')}</label></div>++ {/* Excluded characters */}+ <div>+ <label htmlFor="excluded-chars" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">+ {t('items.excludedCharsLabel')}+ </label>+ <input+ id="excluded-chars"+ type="text"+ autoComplete="off"+ spellCheck={false}+ value={settings.ExcludedChars ?? ''}+ onChange={(e) => onSettingChange('ExcludedChars', e.target.value)}+ className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md text-gray-900 dark:text-white font-mono focus:ring-primary-500 focus:border-primary-500"+ />+ <p className="mt-1 text-xs text-gray-500 dark:text-gray-400">{t('items.excludedCharsDescription')}</p>+ </div></div>)}diff --git a/apps/mobile-app/app/(tabs)/settings/password-generator.tsx b/apps/mobile-app/app/(tabs)/settings/password-generator.tsxindex b6536b281..ac24e829c 100644--- a/apps/mobile-app/app/(tabs)/settings/password-generator.tsx+++ b/apps/mobile-app/app/(tabs)/settings/password-generator.tsx@@ -1,7 +1,7 @@import { useFocusEffect } from 'expo-router';import { useState, useCallback, useRef } from 'react';import { useTranslation } from 'react-i18next';-import { StyleSheet, View } from 'react-native';+import { KeyboardAvoidingView, Platform, StyleSheet, View } from 'react-native';import { useColors } from '@/hooks/useColorScheme';import { useVaultMutate } from '@/hooks/useVaultMutate';@@ -86,6 +86,9 @@ export default function PasswordGeneratorSettingsScreen(): React.ReactNode {fontSize: 13,marginBottom: 16,},+ keyboardAvoidingView: {+ flex: 1,+ },});if (!settings) {@@ -99,17 +102,19 @@ export default function PasswordGeneratorSettingsScreen(): React.ReactNode {}return (- <ThemedContainer>- <ThemedScrollView>- <ThemedText style={styles.descriptionText}>- {t('settings.passwordGeneratorSettings.description')}- </ThemedText>- <PasswordGeneratorPanel- key={loadKey}- initialSettings={settings}- onSettingsChange={handleSettingsChange}- />- </ThemedScrollView>- </ThemedContainer>+ <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={styles.keyboardAvoidingView}>+ <ThemedContainer>+ <ThemedScrollView>+ <ThemedText style={styles.descriptionText}>+ {t('settings.passwordGeneratorSettings.description')}+ </ThemedText>+ <PasswordGeneratorPanel+ key={loadKey}+ initialSettings={settings}+ onSettingsChange={handleSettingsChange}+ />+ </ThemedScrollView>+ </ThemedContainer>+ </KeyboardAvoidingView>);}diff --git a/apps/mobile-app/components/form/PasswordGeneratorPanel.tsx b/apps/mobile-app/components/form/PasswordGeneratorPanel.tsxindex 133500828..86b7b8e61 100644--- a/apps/mobile-app/components/form/PasswordGeneratorPanel.tsx+++ b/apps/mobile-app/components/form/PasswordGeneratorPanel.tsx@@ -2,9 +2,9 @@ import { Ionicons, MaterialIcons } from '@expo/vector-icons';import Slider from '@react-native-community/slider';import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';-import { Platform, StyleSheet, TouchableOpacity, View } from 'react-native';+import { Platform, StyleSheet, TextInput, TouchableOpacity, View } from 'react-native';-import type { PasswordSettings, DicewareCapitalization, DicewareSeparator, DicewareSalt } from '@aliasvault/models/vault';+import type { PasswordSettings, PasswordGeneratorType, DicewareCapitalization, DicewareSeparator, DicewareSalt } from '@aliasvault/models/vault';import { getLanguageInfo, resolveDefaultLanguage } from '@aliasvault/i18n/languages';import { MIN_WORD_COUNT, MAX_WORD_COUNT, DEFAULT_WORD_COUNT } from '@aliasvault/models/defaults';import { sliderToLength, lengthToSlider, SLIDER_MIN, SLIDER_MAX } from '@aliasvault/client/utilities/PasswordLengthSlider';@@ -137,6 +137,28 @@ export function PasswordGeneratorPanel({ initialSettings, onSettingsChange, onPrjustifyContent: 'space-between',paddingVertical: 12,},+ excludedDescription: {+ color: colors.textMuted,+ fontSize: 13,+ lineHeight: 18,+ marginTop: 8,+ },+ excludedInput: {+ backgroundColor: colors.modalSurface,+ borderColor: colors.accentBorder,+ borderRadius: 6,+ borderWidth: 1,+ color: colors.text,+ fontFamily: Platform.OS === 'ios' ? 'Courier' : 'monospace',+ fontSize: 15,+ marginTop: 8,+ paddingHorizontal: 12,+ paddingVertical: 10,+ },+ excludedSection: {+ paddingBottom: 14,+ paddingTop: 10,+ },languageValue: {alignItems: 'center',flexDirection: 'row',@@ -363,6 +385,21 @@ export function PasswordGeneratorPanel({ initialSettings, onSettingsChange, onPr</TouchableOpacity></View>))}++ {/* Excluded characters. */}+ <View style={[styles.excludedSection, styles.rowDivider]}>+ <ThemedText style={styles.sliderLabel}>{t('items.excludedCharsLabel')}</ThemedText>+ <TextInput+ style={styles.excludedInput}+ value={settings.ExcludedChars ?? ''}+ onChangeText={(text) => applyChange({ ExcludedChars: text })}+ autoCapitalize="none"+ autoCorrect={false}+ spellCheck={false}+ testID="excluded-chars-input"+ />+ <ThemedText style={styles.excludedDescription}>{t('items.excludedCharsDescription')}</ThemedText>+ </View></View>) : (<View style={styles.card}>diff --git a/apps/web/src/components/settings/PasswordSettingsPopup.tsx b/apps/web/src/components/settings/PasswordSettingsPopup.tsxindex 05a08ec34..cd8999fc2 100644--- a/apps/web/src/components/settings/PasswordSettingsPopup.tsx+++ b/apps/web/src/components/settings/PasswordSettingsPopup.tsx@@ -165,6 +165,12 @@ const PasswordSettingsPopup: React.FC<PasswordSettingsPopupProps> = ({ passwordS<input id="use-non-ambiguous" type="checkbox" className="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded dark:bg-gray-700 dark:border-gray-600" checked={settings.UseNonAmbiguousChars} onChange={e => handleSettingChange('UseNonAmbiguousChars', e.target.checked)} /><label htmlFor="use-non-ambiguous" className="ml-2 block text-sm text-gray-700 dark:text-gray-300">{t('items.passwordSettingsPopup.avoidAmbiguousCharsLabel')}</label></div>++ <div>+ <label htmlFor="excluded-chars" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">{t('items.excludedCharsLabel')}</label>+ <input id="excluded-chars" type="text" autoComplete="off" spellCheck={false} className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md text-gray-900 dark:text-white font-mono focus:ring-primary-500 focus:border-primary-500" value={settings.ExcludedChars ?? ''} onChange={e => handleSettingChange('ExcludedChars', e.target.value)} />+ <p className="mt-1 text-xs text-gray-500 dark:text-gray-400">{t('items.excludedCharsDescription')}</p>+ </div></>) : (<>diff --git a/core/i18n/hints.json b/core/i18n/hints.jsonindex cb4ef10ad..a323bc3ce 100644--- a/core/i18n/hints.json+++ b/core/i18n/hints.json@@ -279,6 +279,8 @@"items.delete.yesImSureButton": "Confirmation button text","items.editableFieldLabel.deleteField": "Tooltip for delete field button","items.editableFieldLabel.editLabel": "Tooltip for edit label button",+ "items.excludedCharsDescription": "Help text under the password generator field where the user types characters that must never appear in generated passwords; keep the example characters as-is",+ "items.excludedCharsLabel": "Label of the password generator text field for characters that must never appear in generated passwords","items.fieldHistory.deleteConfirm": "Delete confirmation text","items.folders.deleteModal.deleteFolderAndContentsDescription": "Description for delete folder and contents option. {0} is item count.","items.folders.deleteModal.deleteFolderAndContentsTitle": "Title for delete folder and contents option",diff --git a/core/i18n/locales/en.json b/core/i18n/locales/en.jsonindex 9203a2fba..ed926445a 100644--- a/core/i18n/locales/en.json+++ b/core/i18n/locales/en.json@@ -686,6 +686,8 @@"emailUnexpectedError": "An unexpected error occurred while loading emails. Please try again later.","emptyFolderHint": "This folder is empty. To move items to this folder, edit the item and tap the folder icon in the name field.","errorLoadingItems": "Error loading items",+ "excludedCharsDescription": "Generated passwords will never contain these characters.",+ "excludedCharsLabel": "Exclude characters","fieldHistory": {"deleteConfirm": "Are you sure?"},diff --git a/core/models/src/vault/PasswordSettings.ts b/core/models/src/vault/PasswordSettings.tsindex 02cb557b8..191f7090d 100644--- a/core/models/src/vault/PasswordSettings.ts+++ b/core/models/src/vault/PasswordSettings.ts@@ -55,6 +55,11 @@ export type PasswordSettings = {*/UseNonAmbiguousChars: boolean;+ /**+ * Characters that must never appear in a generated password (basic generator). Empty or absent means no exclusions.+ */+ ExcludedChars?: string;+/*** Which generator to use. Defaults to 'basic' when absent.*/diff --git a/core/rust/src/password_generator/basic.rs b/core/rust/src/password_generator/basic.rsindex 20d89eefe..47bb6ebc9 100644--- a/core/rust/src/password_generator/basic.rs+++ b/core/rust/src/password_generator/basic.rs@@ -62,7 +62,13 @@ fn build_character_set(settings: &PasswordSettings) -> Vec<char> {chars.push_str(LOWERCASE_CHARS);}- safe_character_set(&chars, settings)+ let safe = safe_character_set(&chars, settings);+ if !safe.is_empty() {+ return safe;+ }++ // Every enabled character was excluded: ignore the exclusions rather than produce nothing.+ chars.chars().filter(|c| !settings.use_non_ambiguous_chars || !AMBIGUOUS_CHARS.contains(*c)).collect()}/// Collect one mandatory character per enabled class, so the password contains at least one character from each.@@ -91,9 +97,13 @@ fn push_one<R: Rng + ?Sized>(out: &mut Vec<char>, char_set: &str, settings: &Pas}}-/// Get a character set with ambiguous characters removed if the option is enabled.+/// Get a character set with the user's excluded characters removed, and the ambiguous ones if that option is enabled.fn safe_character_set(char_set: &str, settings: &PasswordSettings) -> Vec<char> {- char_set.chars().filter(|c| !settings.use_non_ambiguous_chars || !AMBIGUOUS_CHARS.contains(*c)).collect()+ char_set+ .chars()+ .filter(|c| !settings.excluded_chars.contains(*c))+ .filter(|c| !settings.use_non_ambiguous_chars || !AMBIGUOUS_CHARS.contains(*c))+ .collect()}/// Shuffle a slice in place with an unbiased Fisher-Yates shuffle, reusing [`unbiased_index`] so a fixed seed stays deterministic.diff --git a/core/rust/src/password_generator/mod.rs b/core/rust/src/password_generator/mod.rsindex a9fa40f61..1ab1d156b 100644--- a/core/rust/src/password_generator/mod.rs+++ b/core/rust/src/password_generator/mod.rs@@ -110,6 +110,9 @@ pub(crate) struct PasswordSettings {/// Whether to exclude ambiguous characters.#[serde(default)]pub use_non_ambiguous_chars: bool,+ /// Characters that must never appear in the password (user supplied).+ #[serde(default)]+ pub excluded_chars: String,/// Number of words in the passphrase.#[serde(default = "default_word_count")]diff --git a/core/rust/src/password_generator/tests.rs b/core/rust/src/password_generator/tests.rsindex 6293cc9aa..a708dc9c8 100644--- a/core/rust/src/password_generator/tests.rs+++ b/core/rust/src/password_generator/tests.rs@@ -91,6 +91,28 @@ fn basic_no_classes_falls_back_to_lowercase() {assert!(pw.chars().all(|c| c.is_ascii_lowercase()));}+#[test]+fn basic_excluded_chars_never_appear() {+ // Dead keys on a US International layout plus a few letters and digits, spanning every class.+ let excluded = "^`'\"~$aE7";+ let json = r#"{"Type":"basic","Length":80,"ExcludedChars":"^`'\"~$aE7"}"#;+ for _ in 0..20 {+ let pw = generate_password(json).unwrap();+ assert!(pw.chars().all(|c| !excluded.contains(c)), "found excluded char in {pw}");+ assert_eq!(pw.chars().count(), 80);+ }+}++#[test]+fn basic_excluding_a_whole_class_still_fills_the_length() {+ // Numbers are enabled but all excluded: the mandatory digit is skipped and the rest fills the length.+ let json = r#"{"Type":"basic","Length":40,"UseLowercase":false,"UseUppercase":false,"UseSpecialChars":false,"UseNumbers":true,"ExcludedChars":"0123456789"}"#;+ let pw = generate_password(json).unwrap();+ assert_eq!(pw.chars().count(), 40);+ // Every enabled character was excluded, so the exclusions are ignored instead of producing nothing.+ assert!(pw.chars().all(|c| c.is_ascii_digit()), "unexpected chars in {pw}");+}+#[test]fn basic_non_ambiguous_excludes_ambiguous_chars() {let ambiguous = "Il1O0oZzSsBbGg2568|[]{}()<>;:,.`'\"_-";