fix(deploy): isolate application from hosting owner credentials
This commit is contained in:
@@ -5,3 +5,4 @@ mkdirSync('.next/standalone/scripts', { recursive: true });
|
||||
cpSync('scripts/start-production.sh', '.next/standalone/scripts/start-production.sh');
|
||||
cpSync('scripts/notdienst-alarm-worker.mjs', '.next/standalone/scripts/notdienst-alarm-worker.mjs');
|
||||
cpSync('scripts/runtime-database-url.mjs', '.next/standalone/scripts/runtime-database-url.mjs');
|
||||
cpSync('scripts/verify-runtime-isolation.mjs', '.next/standalone/scripts/verify-runtime-isolation.mjs');
|
||||
|
||||
@@ -5,6 +5,7 @@ if [ -n "${APP_DATABASE_PASSWORD_FILE:-}" ]; then
|
||||
DATABASE_URL="$(node scripts/runtime-database-url.mjs)"
|
||||
export DATABASE_URL
|
||||
fi
|
||||
unset POSTGRES_PASSWORD PGADMIN_DEFAULT_PASSWORD APP_DATABASE_PASSWORD MIGRATION_DATABASE_URL
|
||||
# Schema changes are applied by the maintenance image before runtime startup.
|
||||
if [ -n "${NOTDIENST_JOB_SECRET:-}" ]; then
|
||||
node scripts/notdienst-alarm-worker.mjs &
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
let prisma;
|
||||
try {
|
||||
const main = Object.fromEntries(readFileSync('/proc/1/environ', 'utf8').split('\0').filter(Boolean).map(line => {
|
||||
const index = line.indexOf('='); return [line.slice(0, index), line.slice(index + 1)];
|
||||
}));
|
||||
for (const key of ['POSTGRES_PASSWORD', 'PGADMIN_DEFAULT_PASSWORD', 'APP_DATABASE_PASSWORD', 'MIGRATION_DATABASE_URL']) {
|
||||
if (process.env[key] || main[key]) throw new Error('Administrative credential reached the app.');
|
||||
}
|
||||
// Configured global/legacy URLs must not expose an administrative login either.
|
||||
if (process.env.DATABASE_URL) throw new Error('Hosting injected a legacy database URL.');
|
||||
if (new URL(main.DATABASE_URL).username !== 'kita_runtime') throw new Error('Wrong application database role.');
|
||||
prisma = new PrismaClient({ datasourceUrl: main.DATABASE_URL });
|
||||
const [row] = await prisma.$queryRawUnsafe('SELECT current_user');
|
||||
if (row.current_user !== 'kita_runtime') throw new Error('Wrong effective database role.');
|
||||
console.log('Runtime isolation verified: no administrative credentials in container/app environment; current_user=kita_runtime.');
|
||||
} catch {
|
||||
console.error('Runtime isolation verification failed. Credential values were not logged.');
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await prisma?.$disconnect();
|
||||
}
|
||||
Reference in New Issue
Block a user