Skip to content

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

Update TOTP import conversion (#2492)

Leendert de Borstd848987d97a6
diff --git a/core/client/src/transfer/import/writers/ItemConverter.ts b/core/client/src/transfer/import/writers/ItemConverter.ts
index ce85ad973..d3ad99845 100644
--- a/core/client/src/transfer/import/writers/ItemConverter.ts
+++ b/core/client/src/transfer/import/writers/ItemConverter.ts
@@ -126,7 +126,7 @@ export function convertToItem(credential: ImportedCredential, folderPathToId: Ma
function buildTotpCode(secret: string, itemId: string, row: Pick<TotpCodeEntity, 'CreatedAt' | 'UpdatedAt' | 'IsDeleted'>): TotpCodeEntity | null {
try {
const sanitized = sanitizeTotpSecretKey(secret);
- return { Id: crypto.randomUUID(), ItemId: itemId, Name: sanitized.name ?? '', SecretKey: sanitized.secretKey, Algorithm: sanitized.algorithm, Digits: sanitized.digits, Period: sanitized.period, ...row };
+ return { Id: crypto.randomUUID(), ItemId: itemId, Name: sanitized.name, SecretKey: sanitized.secretKey, Algorithm: sanitized.algorithm, Digits: sanitized.digits, Period: sanitized.period, ...row };
} catch (error) {
logExpected('Error importing TOTP code', error);
return null;
diff --git a/core/client/src/transfer/import/writers/__tests__/ItemConverter.test.ts b/core/client/src/transfer/import/writers/__tests__/ItemConverter.test.ts
index 79d5059c5..afd6bf210 100644
--- a/core/client/src/transfer/import/writers/__tests__/ItemConverter.test.ts
+++ b/core/client/src/transfer/import/writers/__tests__/ItemConverter.test.ts
@@ -10,6 +10,7 @@ describe('convertToItem', () => {
const credentials: ImportedCredential[] = [
{ ServiceName: 'Sha512Service', TwoFactorSecret: 'otpauth://totp/Sha512Service:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Sha512Service&algorithm=SHA512&digits=8&period=60' },
{ ServiceName: 'PlainService', TwoFactorSecret: 'JBSWY3DPEHPK3PXP' },
+ { ServiceName: 'UppercaseScheme', TwoFactorSecret: 'OTPAUTH://TOTP/Example:me?secret=JBSWY3DPEHPK3PXP&issuer=Example&algorithm=SHA256' },
];
const items = convertToItems(credentials);
@@ -22,6 +23,7 @@ describe('convertToItem', () => {
expect(withDefaults.Algorithm).toBe(TOTP_DEFAULT_ALGORITHM);
expect(withDefaults.Digits).toBe(TOTP_DEFAULT_DIGITS);
expect(withDefaults.Period).toBe(TOTP_DEFAULT_PERIOD);
+ expect(itemByName(items, 'UppercaseScheme').TotpCodes[0]).toMatchObject({ Name: 'Example: me', SecretKey: 'JBSWY3DPEHPK3PXP', Algorithm: 'SHA256' });
});
it('assigns folders when converting to items', () => {
diff --git a/core/models/src/vault/TotpCode.ts b/core/models/src/vault/TotpCode.ts
index d443775ed..219590cce 100644
--- a/core/models/src/vault/TotpCode.ts
+++ b/core/models/src/vault/TotpCode.ts
@@ -39,6 +39,9 @@ export const TOTP_DEFAULT_PERIOD = 30;
/** The HMAC algorithms the TOTP generators implement. Anything else falls back to {@link TOTP_DEFAULT_ALGORITHM}. */
export const TOTP_SUPPORTED_ALGORITHMS = ['SHA1', 'SHA256', 'SHA512'] as const;
+/** The code lengths the TOTP generators accept. Anything else falls back to {@link TOTP_DEFAULT_DIGITS}. */
+export const TOTP_SUPPORTED_DIGITS = [6, 7, 8] as const;
+
/**
* Normalizes a raw `algorithm` value (from an otpauth:// URI or an older vault row) to one of
* {@link TOTP_SUPPORTED_ALGORITHMS}, falling back to {@link TOTP_DEFAULT_ALGORITHM}.
@@ -62,7 +65,7 @@ export function normalizeTotpAlgorithm(value: string | null | undefined): string
*/
export function normalizeTotpDigits(value: string | number | null | undefined): number {
const parsed = typeof value === 'number' ? value : parseInt(String(value ?? ''), 10);
- return Number.isInteger(parsed) && parsed >= 6 && parsed <= 8 ? parsed : TOTP_DEFAULT_DIGITS;
+ return (TOTP_SUPPORTED_DIGITS as readonly number[]).includes(parsed) ? parsed : TOTP_DEFAULT_DIGITS;
}
/**