Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import { options as optionService } from '@triliumnext/core';
import crypto from 'crypto';
import sql from '../sql.js';
import { constantTimeCompare } from '../utils.js';
/**
* What a recovery code looks like: 22 characters and the base64 padding.
*
* Deliberately without the `g` flag the inline copies of this pattern carried. A `g` regex kept in a
* variable remembers where it last matched, so `test` on a shared one answers correctly only every
* other call.
*/
const RECOVERY_CODE_SHAPE = /^.{22}==$/;
function isRecoveryCodeSet() {
return optionService.getOptionBool('encryptedRecoveryCodes');
}
function setRecoveryCodes(recoveryCodes: string) {
const iv = crypto.randomBytes(16);
const securityKey = crypto.randomBytes(32);
const cipher = crypto.createCipheriv('aes-256-cbc', securityKey, iv);
const encryptedRecoveryCodes = cipher.update(recoveryCodes, 'utf-8', 'hex');
sql.transactional(() => {
optionService.setOption('recoveryCodeInitialVector', iv.toString('hex'));
optionService.setOption('recoveryCodeSecurityKey', securityKey.toString('hex'));
optionService.setOption('recoveryCodesEncrypted', encryptedRecoveryCodes + cipher.final('hex'));
optionService.setOption('encryptedRecoveryCodes', 'true');
return true;
});
return false;
}
/** Generates a fresh set of recovery codes WITHOUT persisting them (the caller decides when to commit). */
function createRecoveryCodes(): string[] {
return Array.from({ length: 8 }, () => crypto.randomBytes(16).toString('base64'));
}
function clearRecoveryCodes() {
sql.transactional(() => {
optionService.setOption('recoveryCodeInitialVector', '');
optionService.setOption('recoveryCodeSecurityKey', '');
optionService.setOption('recoveryCodesEncrypted', '');
optionService.setOption('encryptedRecoveryCodes', 'false');
return true;
});
}
function getRecoveryCodes() {
if (!isRecoveryCodeSet()) {
return []
}
return sql.transactional<string[]>(() => {
const iv = Buffer.from(optionService.getOption('recoveryCodeInitialVector'), 'hex');
const securityKey = Buffer.from(optionService.getOption('recoveryCodeSecurityKey'), 'hex');
const encryptedRecoveryCodes = optionService.getOption('recoveryCodesEncrypted');
const decipher = crypto.createDecipheriv('aes-256-cbc', securityKey, iv);
const decryptedData = decipher.update(encryptedRecoveryCodes, 'hex', 'utf-8');
const decryptedString = decryptedData + decipher.final('utf-8');
return decryptedString.split(',');
});
}
function removeRecoveryCode(usedCode: string) {
const oldCodes = getRecoveryCodes();
const today = new Date();
oldCodes[oldCodes.indexOf(usedCode)] = today.toJSON().replace(/-/g, '/');
setRecoveryCodes(oldCodes.toString());
}
/**
* Whether `recoveryCodeGuess` is one of the codes, leaving it where it is.
*
* The consuming check below is the one login uses, and the one to reach for by default. This exists
* for the setup wizard, which runs against a database that is attached but not migrated, with becca
* unloaded: `setOption` there would create a duplicate row rather than update one, and a code spent
* in that state would still read as unspent everywhere else in a sync cluster. So the wizard trades
* the single use for being able to check at all — see `setup_auth` in core.
*/
function matchesRecoveryCode(recoveryCodeGuess: string) {
if (!RECOVERY_CODE_SHAPE.test(recoveryCodeGuess)) {
return false;
}
// Every code is compared even once one has matched, so the time taken says nothing about which.
return getRecoveryCodes().reduce(
(matched, recoveryCode) => constantTimeCompare(recoveryCodeGuess, recoveryCode) || matched,
false
);
}
function verifyRecoveryCode(recoveryCodeGuess: string) {
if (!RECOVERY_CODE_SHAPE.test(recoveryCodeGuess)) {
return false;
}
const recoveryCodes = getRecoveryCodes();
let loginSuccess = false;
let matchedCode: string | null = null;
// Check ALL codes to prevent timing attacks - do not short-circuit
for (const recoveryCode of recoveryCodes) {
if (constantTimeCompare(recoveryCodeGuess, recoveryCode)) {
matchedCode = recoveryCode;
loginSuccess = true;
// Continue checking all codes to maintain constant time
}
}
// Remove the matched code only after checking all codes
if (matchedCode) {
removeRecoveryCode(matchedCode);
}
return loginSuccess;
}
export default {
setRecoveryCodes,
createRecoveryCodes,
clearRecoveryCodes,
getRecoveryCodes,
matchesRecoveryCode,
verifyRecoveryCode,
isRecoveryCodeSet
};