-
Notifications
You must be signed in to change notification settings - Fork 0
Enforce production auth cookie origin config #18
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
Merged
+280
−8
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
93a2c10
harden(auth): enforce production cookie origin config
3163f06
fix(auth): normalize production origin guard
84d206c
fix(auth): align standalone CORS origin fallback
9f44ee6
test(auth): restore node env safely
730a6bf
fix(auth): normalize production CORS middleware origin
488a487
fix(auth): prefer explicit secure cookie config
e87acc8
fix(auth): normalize CORS origin consistently
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| const { | ||
| assertProductionAuthConfig, | ||
| buildServices, | ||
| createApp, | ||
| normalizeProductionCorsOrigin, | ||
| } = require('../lib/appFactory'); | ||
| const { openDatabase } = require('../lib/db'); | ||
| const { createMailer } = require('../lib/mailer'); | ||
| const { _resetPepperForTests } = require('../lib/kdf'); | ||
| const request = require('supertest'); | ||
|
|
||
| describe('appFactory production auth config', () => { | ||
| const originalNodeEnv = process.env.NODE_ENV; | ||
| const originalSecureCookies = process.env.SYSNODE_SECURE_COOKIES; | ||
| const originalCorsOrigin = process.env.CORS_ORIGIN; | ||
| const originalFrontendUrl = process.env.FRONTEND_URL; | ||
|
|
||
| beforeEach(() => { | ||
| _resetPepperForTests(); | ||
| process.env.SYSNODE_AUTH_PEPPER = 'f'.repeat(64); | ||
| delete process.env.SYSNODE_SECURE_COOKIES; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalNodeEnv === undefined) { | ||
| delete process.env.NODE_ENV; | ||
| } else { | ||
| process.env.NODE_ENV = originalNodeEnv; | ||
| } | ||
| if (originalSecureCookies === undefined) { | ||
| delete process.env.SYSNODE_SECURE_COOKIES; | ||
| } else { | ||
| process.env.SYSNODE_SECURE_COOKIES = originalSecureCookies; | ||
| } | ||
| if (originalCorsOrigin === undefined) { | ||
| delete process.env.CORS_ORIGIN; | ||
| } else { | ||
| process.env.CORS_ORIGIN = originalCorsOrigin; | ||
| } | ||
| if (originalFrontendUrl === undefined) { | ||
| delete process.env.FRONTEND_URL; | ||
| } else { | ||
| process.env.FRONTEND_URL = originalFrontendUrl; | ||
| } | ||
| }); | ||
|
|
||
| test('production refuses non-secure cookies', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| expect(() => | ||
| assertProductionAuthConfig({ | ||
| secureCookies: false, | ||
| corsOrigin: 'https://sysnode.info', | ||
| frontendUrl: 'https://sysnode.info', | ||
| }) | ||
| ).toThrow('secure_cookies_required_in_production'); | ||
| }); | ||
|
|
||
| test('production requires same frontend and credentialed CORS origin', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| expect(() => | ||
| assertProductionAuthConfig({ | ||
| secureCookies: true, | ||
| corsOrigin: 'https://api.sysnode.info', | ||
| frontendUrl: 'https://sysnode.info', | ||
| }) | ||
| ).toThrow('same_origin_cors_required_in_production'); | ||
| }); | ||
|
|
||
| test('production accepts equivalent same-origin URLs after normalization', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| expect(() => | ||
| assertProductionAuthConfig({ | ||
| secureCookies: true, | ||
| corsOrigin: 'https://sysnode.info/', | ||
| frontendUrl: 'https://sysnode.info/path-that-is-not-used', | ||
| }) | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| test('production requires an https frontend origin', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| expect(() => | ||
| assertProductionAuthConfig({ | ||
| secureCookies: true, | ||
| corsOrigin: 'http://sysnode.info', | ||
| frontendUrl: 'http://sysnode.info', | ||
| }) | ||
| ).toThrow('frontend_https_url_required_in_production'); | ||
| }); | ||
|
|
||
| test('SYSNODE_SECURE_COOKIES=false is rejected in production', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| process.env.SYSNODE_SECURE_COOKIES = 'false'; | ||
| const db = openDatabase(':memory:'); | ||
| try { | ||
| expect(() => buildServices({ db })).toThrow( | ||
| 'secure_cookies_required_in_production' | ||
| ); | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test('explicit secureCookies option ignores malformed env override', () => { | ||
| process.env.NODE_ENV = 'test'; | ||
| process.env.SYSNODE_SECURE_COOKIES = '0'; | ||
| const db = openDatabase(':memory:'); | ||
| try { | ||
| expect(() => buildServices({ db, secureCookies: false })).not.toThrow(); | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test('standalone createApp accepts FRONTEND_URL as production CORS origin fallback', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| delete process.env.CORS_ORIGIN; | ||
| process.env.FRONTEND_URL = 'https://sysnode.info'; | ||
| const db = openDatabase(':memory:'); | ||
| const mailer = createMailer({ transport: 'memory', from: 't@x.com' }); | ||
| try { | ||
| expect(() => createApp({ db, mailer })).not.toThrow(); | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test('standalone createApp emits normalized production CORS origin', async () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| const db = openDatabase(':memory:'); | ||
| const mailer = createMailer({ transport: 'memory', from: 't@x.com' }); | ||
| const { app } = createApp({ | ||
| db, | ||
| mailer, | ||
| corsOrigin: 'https://sysnode.info/', | ||
| frontendUrl: 'https://sysnode.info/path-that-is-not-used', | ||
| }); | ||
| try { | ||
| const res = await request(app) | ||
| .get('/health') | ||
| .set('Origin', 'https://sysnode.info'); | ||
| expect(res.headers['access-control-allow-origin']).toBe( | ||
| 'https://sysnode.info' | ||
| ); | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test('normalizes production CORS origins and preserves development values', () => { | ||
| process.env.NODE_ENV = 'production'; | ||
| expect(normalizeProductionCorsOrigin('https://sysnode.info/')).toBe( | ||
| 'https://sysnode.info' | ||
| ); | ||
| process.env.NODE_ENV = 'development'; | ||
| expect(normalizeProductionCorsOrigin('http://localhost:3000/app')).toBe( | ||
| 'http://localhost:3000' | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.