mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
fix(security): refuse an ENCRYPTION_KEY that disagrees with the key file
An env value different from data/.encryption_key used to overwrite the file on boot, destroying the only copy of the key the stored secrets were encrypted with. The server now refuses to start and names the rotation script, which re-encrypts the AI Parsing addon key as well and moves the key file to the new key. Under test the suite's fixed key no longer overwrites the developer's key file.
This commit is contained in:
+34
-4
@@ -23,15 +23,45 @@ const jwtSecretFile = path.join(dataDir, '.jwt_secret');
|
||||
const encKeyFile = path.join(dataDir, '.encryption_key');
|
||||
let _encryptionKey: string = process.env.ENCRYPTION_KEY || '';
|
||||
|
||||
if (_encryptionKey) {
|
||||
// Env var is set explicitly — persist it to file so the value survives
|
||||
// container restarts even if the env var is later removed.
|
||||
// Under test the suite sets its own fixed ENCRYPTION_KEY. Comparing it with, or
|
||||
// writing it over, the developer's own data/.encryption_key would either stop
|
||||
// every test run or quietly replace the key a local dev server encrypts with.
|
||||
const keyFileFollowsEnv = process.env.NODE_ENV !== 'test';
|
||||
|
||||
/**
|
||||
* Persist an explicit ENCRYPTION_KEY to the key file, so the value survives
|
||||
* container restarts even if the env var is later removed.
|
||||
*
|
||||
* A key file that disagrees with the env var means every stored secret was
|
||||
* encrypted with the file's key. Booting would make them unreadable and the
|
||||
* write would destroy the only copy of that key, which is exactly what a
|
||||
* regenerated Helm secret or a typo in the variable used to do in silence. The
|
||||
* rotation script updates the file, so a real rotation never lands here.
|
||||
*/
|
||||
function persistEnvKey(envKey: string): void {
|
||||
let storedKey = '';
|
||||
try {
|
||||
storedKey = fs.readFileSync(encKeyFile, 'utf8').trim();
|
||||
} catch {
|
||||
// No readable file yet: nothing to protect, the env value becomes the file.
|
||||
}
|
||||
if (storedKey && storedKey !== envKey.trim()) {
|
||||
console.error(`FATAL: ENCRYPTION_KEY does not match ${encKeyFile}.`);
|
||||
console.error('The stored secrets are encrypted with the key in that file, so TREK will not start with a different one.');
|
||||
console.error('To change the key, run scripts/migrate-encryption.ts first; it re-encrypts the secrets and updates the file.');
|
||||
console.error('Otherwise set ENCRYPTION_KEY back to the value in the file, or remove the variable.');
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
|
||||
fs.writeFileSync(encKeyFile, _encryptionKey, { mode: 0o600 });
|
||||
fs.writeFileSync(encKeyFile, envKey, { mode: 0o600 });
|
||||
} catch {
|
||||
// Non-fatal: env var is the source of truth when set.
|
||||
}
|
||||
}
|
||||
|
||||
if (_encryptionKey) {
|
||||
if (keyFileFollowsEnv) persistEnvKey(_encryptionKey);
|
||||
} else {
|
||||
// Try the dedicated key file first (covers all installs after first start).
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user