Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/validate-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ describe('validateCriticalEnv — AUTH_SECRET errors', () => {
expect(result.valid).toBe(false);
expect(result.errors[0]).toMatch(/31 chars/);
});

it('returns an error when AUTH_SECRET is set to fallback-secret', () => {
const result = validateCriticalEnv(validEnv({ AUTH_SECRET: 'fallback-secret' }));
expect(result.valid).toBe(false);
expect(result.errors[0]).toMatch(
/AUTH_SECRET environment variable must be set to a secure value/
);
});

it('returns an error when AUTH_SECRET contains fallback-secret', () => {
const result = validateCriticalEnv(
validEnv({ AUTH_SECRET: 'this-is-a-long-fallback-secret-string-32chars!' })
);
expect(result.valid).toBe(false);
expect(result.errors[0]).toMatch(
/AUTH_SECRET environment variable must be set to a secure value/
);
});
});

describe('validateCriticalEnv — ENCRYPTION_KEY errors', () => {
Expand Down Expand Up @@ -144,6 +162,12 @@ describe('assertCriticalEnv — throws on bad config', () => {
expect(() => assertCriticalEnv(validEnv({ AUTH_SECRET: undefined }))).toThrow(/AUTH_SECRET/);
});

it('throws when AUTH_SECRET is set to fallback-secret', () => {
expect(() => assertCriticalEnv(validEnv({ AUTH_SECRET: 'fallback-secret' }))).toThrow(
/AUTH_SECRET environment variable must be set to a secure value/
);
});

it('throws when ENCRYPTION_KEY is missing', () => {
expect(() => assertCriticalEnv(validEnv({ ENCRYPTION_KEY: undefined }))).toThrow(
/ENCRYPTION_KEY/
Expand Down
12 changes: 12 additions & 0 deletions lib/validate-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export function validateCriticalEnv(env: NodeJS.ProcessEnv = process.env): EnvVa
'Generate one with: openssl rand -base64 32 ' +
'and add it to your .env.local and deployment environment.'
);
} else if (
authSecret === 'fallback-secret' ||
authSecret.toLowerCase().includes('fallback-secret') ||
['secret', 'changeme', 'change-me', 'default-secret'].includes(authSecret.toLowerCase())
) {
errors.push('AUTH_SECRET environment variable must be set to a secure value');
} else if (authSecret.length < 32) {
errors.push(
`AUTH_SECRET is too short (${authSecret.length} chars). ` +
Expand All @@ -50,6 +56,12 @@ export function validateCriticalEnv(env: NodeJS.ProcessEnv = process.env): EnvVa
'Generate one with: openssl rand -hex 32 ' +
'and add it to your .env.local and deployment environment.'
);
} else if (
encryptionKey === 'fallback-secret' ||
encryptionKey.toLowerCase().includes('fallback-secret') ||
['secret', 'changeme', 'change-me', 'default-secret'].includes(encryptionKey.toLowerCase())
) {
errors.push('ENCRYPTION_KEY environment variable must be set to a secure value');
} else if (encryptionKey.length < 32) {
errors.push(
`ENCRYPTION_KEY is too short (${encryptionKey.length} chars). ` +
Expand Down
Loading