Bezpieczenstwo: straz produkcyjna tylko dla sekretow JWT (haslo admina pilnuje seed, restarty bez podwaznego enva)

This commit is contained in:
tisachi
2026-10-11 16:18:47 +02:00
parent 3a6e238612
commit dbd299aa45
2 changed files with 4 additions and 38 deletions

View File

@@ -2,19 +2,17 @@ import 'dotenv/config';
import path from 'node:path';
const DEV_JWT_SECRETS = new Set(['dev-access-secret', 'dev-refresh-secret']);
export const DEFAULT_ADMIN_PASSWORD = 'ChangeMe123!';
export interface SecretsGuardInput {
accessSecret: string;
refreshSecret: string;
firstAdminEmail?: string;
firstAdminPassword?: string;
}
/**
* Straż startowa: w produkcji nie odpalamy z deweloperskimi sekretami JWT
* ani z domyślnym hasłem pierwszego admina. Rzucana wyjątkiem przy imporcie
* (patrz dół pliku) — proces nie wstanie zamiast działać na z góry złamanych sekretach.
* Straż startowa: w produkcji nie odpalamy z deweloperskimi sekretami JWT.
* Rzucana wyjątkiem przy imporcie (patrz dół pliku) — proces nie wstanie
* zamiast działać na z góry złamanych sekretach. Hasło pierwszego admina
* sprawdza seed (jedyny moment, w którym powstaje konto).
*/
export function assertProductionSecrets(cfg: SecretsGuardInput): void {
const problems: string[] = [];
@@ -27,14 +25,6 @@ export function assertProductionSecrets(cfg: SecretsGuardInput): void {
if (cfg.accessSecret && cfg.accessSecret === cfg.refreshSecret) {
problems.push('JWT_ACCESS_SECRET and JWT_REFRESH_SECRET must differ');
}
if (
cfg.firstAdminEmail &&
(!cfg.firstAdminPassword || cfg.firstAdminPassword === DEFAULT_ADMIN_PASSWORD)
) {
problems.push(
'FITOS_FIRST_ADMIN_EMAIL is set but FITOS_FIRST_ADMIN_PASSWORD is missing or default',
);
}
if (problems.length > 0) {
throw new Error(`Unsafe production configuration:\n- ${problems.join('\n- ')}`);
}
@@ -50,8 +40,6 @@ export const config = {
accessTtlSec: 15 * 60,
refreshTtlSec: 7 * 24 * 3600,
},
firstAdminEmail: process.env.FITOS_FIRST_ADMIN_EMAIL,
firstAdminPassword: process.env.FITOS_FIRST_ADMIN_PASSWORD || 'ChangeMe123!',
logLevel: process.env.LOG_LEVEL || 'info',
uploadDir: process.env.UPLOAD_DIR || path.join(process.cwd(), 'uploads'),
// true only when served over HTTPS — over plain HTTP browsers drop Secure cookies (breaks refresh)
@@ -71,7 +59,5 @@ if (config.nodeEnv === 'production') {
assertProductionSecrets({
accessSecret: config.jwt.accessSecret,
refreshSecret: config.jwt.refreshSecret,
firstAdminEmail: config.firstAdminEmail,
firstAdminPassword: process.env.FITOS_FIRST_ADMIN_PASSWORD,
});
}

View File

@@ -4,8 +4,6 @@ import { assertProductionSecrets } from '../../src/config.js';
const good = {
accessSecret: 'a'.repeat(32),
refreshSecret: 'b'.repeat(32),
firstAdminEmail: 'admin@example.com',
firstAdminPassword: 'SilneHaslo123!',
};
describe('assertProductionSecrets', () => {
@@ -30,22 +28,4 @@ describe('assertProductionSecrets', () => {
assertProductionSecrets({ ...good, refreshSecret: good.accessSecret }),
).toThrow(/must differ/);
});
it('rzuca gdy ustawiono email admina bez hasła', () => {
expect(() =>
assertProductionSecrets({ ...good, firstAdminPassword: undefined }),
).toThrow(/FITOS_FIRST_ADMIN_PASSWORD/);
});
it('rzuca gdy hasło admina jest domyślne', () => {
expect(() =>
assertProductionSecrets({ ...good, firstAdminPassword: 'ChangeMe123!' }),
).toThrow(/FITOS_FIRST_ADMIN_PASSWORD/);
});
it('zezwala na brak admina (oba envy puste)', () => {
expect(() =>
assertProductionSecrets({ ...good, firstAdminEmail: undefined, firstAdminPassword: undefined }),
).not.toThrow();
});
});