fix(startup): fail closed on key inspection errors

Propagate database inspection failures instead of treating them as
missing encrypted credentials.

This keeps startup from generating a fresh encryption key when an
existing database cannot be inspected and adds a regression test for
that path.
This commit is contained in:
Kfir Amar
2026-03-14 21:23:07 +02:00
parent 88cc53a4b0
commit da39e1485f
3 changed files with 17 additions and 4 deletions

View File

@@ -116,8 +116,9 @@ function hasEncryptedCredentials(dbPath) {
} finally {
db.close();
}
} catch {
return false;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Unable to inspect existing database at ${dbPath}: ${message}`);
}
}

View File

@@ -87,8 +87,9 @@ function hasEncryptedCredentials(dataDir) {
} finally {
db.close();
}
} catch {
return false;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Unable to inspect existing database at ${dbPath}: ${message}`);
}
}

View File

@@ -89,3 +89,14 @@ test("bootstrapEnv refuses to generate a new key over encrypted data", () => {
);
});
});
test("bootstrapEnv fails closed when existing database cannot be inspected", () => {
withTempEnv(({ dataDir }) => {
fs.mkdirSync(path.join(dataDir, "storage.sqlite"), { recursive: true });
assert.throws(
() => bootstrapEnv({ quiet: true }),
/Unable to inspect existing database/
);
});
});