Harden private media and backup recovery; add V2 deployment and migration guides

This commit is contained in:
Elijah 2026-11-11 19:31:32 -07:00
parent c549c34f60
commit ffaa1f6afa
19 changed files with 623 additions and 14 deletions

View file

@ -31,16 +31,21 @@ function bool(v: string | undefined, fallback: boolean): boolean {
}
function loadSecret(dataDir: string, fromEnv: string | undefined): Buffer {
if (fromEnv && fromEnv.trim().length >= 16) {
if (fromEnv && fromEnv.trim().length < 16) throw new Error('APP_SECRET must contain at least 16 characters.');
if (fromEnv) {
return crypto.createHash('sha256').update(`studydesk:${fromEnv.trim()}`).digest();
}
const file = path.join(dataDir, 'secret.key');
if (fs.existsSync(file)) {
const raw = Buffer.from(fs.readFileSync(file, 'utf8').trim(), 'base64');
if (raw.length === 32) return raw;
throw new Error('secret.key is damaged. Restore the original key from your backup; refusing to replace it.');
}
if (fs.existsSync(path.join(dataDir, 'study.db'))) {
throw new Error('Existing database has no secret.key. Restore its original key or set the original APP_SECRET; refusing to generate a replacement.');
}
const key = crypto.randomBytes(32);
fs.writeFileSync(file, key.toString('base64'), { mode: 0o600 });
fs.writeFileSync(file, key.toString('base64'), { mode: 0o600, flag: 'wx' });
return key;
}