-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Generate valid secrets in the CLI bootstrap #3619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { execSync, spawn } from 'child_process' | ||
| import { randomBytes } from 'crypto' | ||
| import { existsSync, mkdirSync } from 'fs' | ||
| import { homedir } from 'os' | ||
| import { join } from 'path' | ||
|
|
@@ -15,6 +16,10 @@ const REALTIME_CONTAINER = 'simstudio-realtime' | |
| const APP_CONTAINER = 'simstudio-app' | ||
| const DEFAULT_PORT = '3000' | ||
|
|
||
| function generateHexSecret(bytes = 32): string { | ||
| return randomBytes(bytes).toString('hex') | ||
| } | ||
|
|
||
| const program = new Command() | ||
|
|
||
| program.name('simstudio').description('Run Sim using Docker').version('0.1.0') | ||
|
|
@@ -84,6 +89,10 @@ async function cleanupExistingContainers(): Promise<void> { | |
|
|
||
| async function main() { | ||
| const options = program.parse().opts() | ||
| const betterAuthSecret = generateHexSecret() | ||
| const encryptionKey = generateHexSecret() | ||
| const internalApiSecret = generateHexSecret() | ||
| const apiEncryptionKey = generateHexSecret() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Secrets regenerated on restart, corrupting persisted encrypted dataHigh Severity
Additional Locations (1) |
||
|
|
||
| console.log(chalk.blue('🚀 Starting Sim...')) | ||
|
|
||
|
|
@@ -215,7 +224,9 @@ async function main() { | |
| '-e', | ||
| `NEXT_PUBLIC_APP_URL=http://localhost:${port}`, | ||
| '-e', | ||
| 'BETTER_AUTH_SECRET=your_auth_secret_here', | ||
| `BETTER_AUTH_SECRET=${betterAuthSecret}`, | ||
| '-e', | ||
| `INTERNAL_API_SECRET=${internalApiSecret}`, | ||
| 'ghcr.io/simstudioai/realtime:latest', | ||
| ]) | ||
|
|
||
|
|
@@ -243,9 +254,13 @@ async function main() { | |
| '-e', | ||
| `NEXT_PUBLIC_APP_URL=http://localhost:${port}`, | ||
| '-e', | ||
| 'BETTER_AUTH_SECRET=your_auth_secret_here', | ||
| `BETTER_AUTH_SECRET=${betterAuthSecret}`, | ||
| '-e', | ||
| `ENCRYPTION_KEY=${encryptionKey}`, | ||
| '-e', | ||
| `INTERNAL_API_SECRET=${internalApiSecret}`, | ||
| '-e', | ||
| 'ENCRYPTION_KEY=your_encryption_key_here', | ||
| `API_ENCRYPTION_KEY=${apiEncryptionKey}`, | ||
| 'ghcr.io/simstudioai/simstudio:latest', | ||
| ]) | ||
|
|
||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New secrets are generated on every invocation of
npx simstudio. BecausecleanupExistingContainers()stops and removes the old containers but the PostgreSQL data volume at~/.simstudio/data/postgresis persisted on disk, the next startup uses a differentENCRYPTION_KEYandAPI_ENCRYPTION_KEYagainst a database that already has data encrypted under the previous keys. This will silently make any previously-encrypted credential/secret rows unreadable.Similarly,
BETTER_AUTH_SECRETchanging on every restart will invalidate all existing user sessions, forcing re-authentication every time the CLI is re-run.The fix is to persist the generated secrets to disk the first time they are created and reload them on subsequent runs. A simple approach:
Key requirements:
0o600) to avoid leaking secrets to other users on the same machine~/.simstudio/directory already exists at this point in the flow (thedataDirmkdirruns earlier), so writing there is safe