fix: keep dashboard usable when stored secrets cannot be decrypted
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SfihHazrSHXgndJtSNLubJ
This commit is contained in:
parent
5611f8c183
commit
530c09a769
2 changed files with 31 additions and 6 deletions
|
|
@ -7,6 +7,13 @@ export const repoDir = (cfg, id) => join(cfg.dataDir, 'repos', String(id));
|
||||||
|
|
||||||
const parse = (s) => (s ? JSON.parse(s) : null);
|
const parse = (s) => (s ? JSON.parse(s) : null);
|
||||||
|
|
||||||
|
// Secret illisible (SECRET_KEY changée, donnée abîmée) : on le traite comme absent pour que le
|
||||||
|
// panel reste utilisable, il suffira de le ressaisir.
|
||||||
|
function readSecret(key, payload, what) {
|
||||||
|
try { return decryptJson(key, payload); }
|
||||||
|
catch { console.warn(`[secrets] ${what} illisible (SECRET_KEY changée ?), à ressaisir`); return null; }
|
||||||
|
}
|
||||||
|
|
||||||
export function getProject(app, id, { secrets = false } = {}) {
|
export function getProject(app, id, { secrets = false } = {}) {
|
||||||
const p = app.db.prepare('SELECT * FROM projects WHERE id = ?').get(Number(id));
|
const p = app.db.prepare('SELECT * FROM projects WHERE id = ?').get(Number(id));
|
||||||
if (!p) throw new HttpError(404, 'Projet introuvable');
|
if (!p) throw new HttpError(404, 'Projet introuvable');
|
||||||
|
|
@ -20,9 +27,9 @@ export function getProject(app, id, { secrets = false } = {}) {
|
||||||
followupEnabled: !!p.followup_enabled, followupDays: p.followup_days,
|
followupEnabled: !!p.followup_enabled, followupDays: p.followup_days,
|
||||||
};
|
};
|
||||||
if (secrets) {
|
if (secrets) {
|
||||||
out.smtp = decryptJson(app.cfg.secretKey, p.smtp_enc);
|
out.smtp = readSecret(app.cfg.secretKey, p.smtp_enc, `SMTP du projet ${p.id}`);
|
||||||
out.dbCfg = decryptJson(app.cfg.secretKey, p.db_enc);
|
out.dbCfg = readSecret(app.cfg.secretKey, p.db_enc, `accès BDD du projet ${p.id}`);
|
||||||
out.imapCfg = decryptJson(app.cfg.secretKey, p.imap_enc);
|
out.imapCfg = readSecret(app.cfg.secretKey, p.imap_enc, `IMAP du projet ${p.id}`);
|
||||||
out.imap = imapCredentials(out.imapCfg, out.smtp);
|
out.imap = imapCredentials(out.imapCfg, out.smtp);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
|
@ -40,7 +47,7 @@ export function createProject(app, { name, gitUrl }) {
|
||||||
// Les secrets vides dans le formulaire = on garde l'ancien (le formulaire ne les réaffiche jamais)
|
// Les secrets vides dans le formulaire = on garde l'ancien (le formulaire ne les réaffiche jamais)
|
||||||
export function saveSecretConfig(app, id, column, incoming, secretField) {
|
export function saveSecretConfig(app, id, column, incoming, secretField) {
|
||||||
if (!['smtp_enc', 'db_enc', 'imap_enc'].includes(column)) throw new Error('Colonne invalide');
|
if (!['smtp_enc', 'db_enc', 'imap_enc'].includes(column)) throw new Error('Colonne invalide');
|
||||||
const current = decryptJson(app.cfg.secretKey, app.db.prepare(`SELECT ${column} c FROM projects WHERE id = ?`).get(id)?.c) || {};
|
const current = readSecret(app.cfg.secretKey, app.db.prepare(`SELECT ${column} c FROM projects WHERE id = ?`).get(id)?.c, column) || {};
|
||||||
const next = { ...current, ...incoming };
|
const next = { ...current, ...incoming };
|
||||||
if (!incoming[secretField]) next[secretField] = current[secretField];
|
if (!incoming[secretField]) next[secretField] = current[secretField];
|
||||||
app.db.prepare(`UPDATE projects SET ${column} = ? WHERE id = ?`).run(encryptJson(app.cfg.secretKey, next), id);
|
app.db.prepare(`UPDATE projects SET ${column} = ? WHERE id = ?`).run(encryptJson(app.cfg.secretKey, next), id);
|
||||||
|
|
@ -54,11 +61,12 @@ export function publicConfig(cfg, secretField) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function forgejoToken(app) {
|
export function forgejoToken(app) {
|
||||||
return decrypt(app.cfg.secretKey, getSetting(app.db, 'forgejo_token_enc'));
|
try { return decrypt(app.cfg.secretKey, getSetting(app.db, 'forgejo_token_enc')); }
|
||||||
|
catch { console.warn('[secrets] token Forgejo illisible, à ressaisir'); return null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function globalSmtp(app) {
|
export function globalSmtp(app) {
|
||||||
return decryptJson(app.cfg.secretKey, getSetting(app.db, 'smtp_enc'));
|
return readSecret(app.cfg.secretKey, getSetting(app.db, 'smtp_enc'), 'SMTP global');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prospectStats(db, projectId) {
|
export function prospectStats(db, projectId) {
|
||||||
|
|
|
||||||
|
|
@ -144,3 +144,20 @@ test('désinscription : GET confirme, POST blackliste, token forgé refusé', as
|
||||||
r = await post(`/u/${encodeURIComponent(email)}/faux`, {}, { auth: false, origin: null });
|
r = await post(`/u/${encodeURIComponent(email)}/faux`, {}, { auth: false, origin: null });
|
||||||
assert.equal(r.status, 400);
|
assert.equal(r.status, 400);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('SECRET_KEY changée : le panel reste utilisable, les secrets sont à ressaisir', async () => {
|
||||||
|
const id = app.db.prepare('SELECT id FROM projects LIMIT 1').get().id;
|
||||||
|
app.db.prepare('UPDATE projects SET smtp_enc = ? WHERE id = ?').run('v1:AAAA:BBBB:CCCC', id);
|
||||||
|
const warn = console.warn; console.warn = () => {};
|
||||||
|
try {
|
||||||
|
let r = await get('/');
|
||||||
|
assert.equal(r.status, 200);
|
||||||
|
r = await get('/settings');
|
||||||
|
assert.equal(r.status, 200);
|
||||||
|
r = await get(`/api/projects/${id}/status`);
|
||||||
|
assert.equal(r.status, 200);
|
||||||
|
assert.equal((await r.json()).hasSmtp, true, 'la colonne existe toujours');
|
||||||
|
r = await post(`/api/projects/${id}/smtp`, { host: 'smtp.x', user: 'u', pass: 'nouveau', fromEmail: 'a@x.fr' });
|
||||||
|
assert.equal(r.status, 200, 'on peut ressaisir');
|
||||||
|
} finally { console.warn = warn; }
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue