mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-10-31 16:13:15 +00:00
Users with several accounts can save the list of accounts in their browser (address, server, label, colour) on the webmail server and bring it back on another device. The list is encrypted in the browser with a password the user chooses (PBKDF2-SHA-256, 600,000 iterations, AES-256-GCM, owner bound as additional data); the server stores only the envelope, one file per owner under SETTINGS_DATA_DIR/account-archives. No mailbox passwords are included. Off by default behind the admin feature gate accountArchiveEnabled, pinned off in Lite. Every method of /api/account-archive needs a session cookie of the owner, matched without regard to case, whose credential is checked against the mail server on each call; listing included. Restored accounts this browser lacks are listed as awaiting sign-in: they survive a reload, and opening one leads to the sign-in form with the address filled in. Accounts already present get their archived label and colour.
42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
import { createHash, randomBytes } from 'node:crypto';
|
|
import { mkdir, open, readFile, rename, unlink } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { archiveIdentity, parseArchiveEnvelope, type ArchiveEnvelope, type ArchiveOwner } from './account-archive';
|
|
|
|
/** One file per owner: `SETTINGS_DATA_DIR/account-archives/<sha256 of the owner>.json`. */
|
|
function archiveDir(): string {
|
|
return path.join(process.env.SETTINGS_DATA_DIR || path.join(process.cwd(), 'data', 'settings'), 'account-archives');
|
|
}
|
|
function archivePath(owner: ArchiveOwner): string {
|
|
return path.join(archiveDir(), createHash('sha256').update(archiveIdentity(owner)).digest('hex') + '.json');
|
|
}
|
|
|
|
export async function loadArchive(owner: ArchiveOwner): Promise<ArchiveEnvelope | null> {
|
|
try { return parseArchiveEnvelope(JSON.parse(await readFile(archivePath(owner), 'utf8'))); }
|
|
catch (err) {
|
|
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/** Replaces the owner's archive atomically: a reader sees the old file or the new one, never half of it. */
|
|
export async function saveArchive(owner: ArchiveOwner, envelope: ArchiveEnvelope): Promise<void> {
|
|
const target = archivePath(owner);
|
|
const temporary = `${target}.${randomBytes(8).toString('hex')}.tmp`;
|
|
await mkdir(archiveDir(), { recursive: true, mode: 0o700 });
|
|
try {
|
|
const file = await open(temporary, 'wx', 0o600);
|
|
try { await file.writeFile(JSON.stringify(parseArchiveEnvelope(envelope))); await file.sync(); }
|
|
finally { await file.close(); }
|
|
await rename(temporary, target);
|
|
} finally {
|
|
await unlink(temporary).catch(() => {});
|
|
}
|
|
}
|
|
|
|
export async function deleteArchive(owner: ArchiveOwner): Promise<void> {
|
|
await unlink(archivePath(owner)).catch(err => {
|
|
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err;
|
|
});
|
|
}
|