Changeset 09ee2bd in Klonkt for src/server.js
- Timestamp:
- 06/23/2026 09:25:42 PM (3 months ago)
- Branches:
- main
- Children:
- f99bbe8
- Parents:
- 45271b7
- File:
-
- 1 edited
-
src/server.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/server.js
r45271b7 r09ee2bd 13 13 import path from 'path'; 14 14 import fs from 'fs'; 15 import crypto from 'crypto'; 15 16 import { fileURLToPath } from 'url'; 16 17 import http from 'http'; … … 63 64 import changelogRoutes from './routes/changelog.js'; 64 65 66 // SESSION_SECRET: use the env var if set. Otherwise auto-generate a strong one 67 // and persist it next to the database, so it stays stable across restarts and 68 // updates. This lets Docker / bare-Node installs run with zero manual config. 65 69 if (!process.env.SESSION_SECRET) { 66 console.error('❌ FATAL: SESSION_SECRET is required'); 67 process.exit(1); 70 const dataDir = path.dirname(process.env.DATABASE_PATH || './storage/database.sqlite'); 71 const secretFile = path.join(dataDir, '.session-secret'); 72 try { process.env.SESSION_SECRET = fs.readFileSync(secretFile, 'utf8').trim(); } catch { /* not yet generated */ } 73 if (!process.env.SESSION_SECRET) { 74 fs.mkdirSync(dataDir, { recursive: true }); 75 process.env.SESSION_SECRET = crypto.randomBytes(32).toString('hex'); 76 fs.writeFileSync(secretFile, process.env.SESSION_SECRET, { mode: 0o600 }); 77 console.log(`🔑 Generated a SESSION_SECRET (stored in ${secretFile})`); 78 } 68 79 } 69 80 81 // A SESSION_SECRET that was explicitly set in the env must still be strong in prod. 70 82 if (process.env.NODE_ENV === 'production' && process.env.SESSION_SECRET.length < 32) { 71 console.error('❌ FATAL: SESSION_SECRET too weak for production');83 console.error('❌ FATAL: SESSION_SECRET is too weak for production (set a longer, random one in .env)'); 72 84 process.exit(1); 73 85 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)