-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: validate ORIGIN env var at startup #15045
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 3 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/adapter-node': minor | ||
KiraPC marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| --- | ||
|
|
||
| validate ORIGIN env var at startup | ||
elliott-with-the-longest-name-on-github marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
KiraPC marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,22 @@ import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/nod | |
| import { Server } from 'SERVER'; | ||
| import { manifest, prerendered, base } from 'MANIFEST'; | ||
| import { env } from 'ENV'; | ||
| import { parse_as_bytes } from '../utils.js'; | ||
| import { parse_as_bytes, parse_origin } from '../utils.js'; | ||
|
|
||
| /* global ENV_PREFIX */ | ||
|
|
||
| const server = new Server(manifest); | ||
|
|
||
| const origin = env('ORIGIN', undefined); | ||
| const origin = parse_origin(env('ORIGIN', undefined)); | ||
|
Contributor
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. We've slightly changed how we parse origin stuff (see |
||
|
|
||
| if (origin === undefined && env('ORIGIN', undefined) !== undefined) { | ||
| throw new Error( | ||
| `Invalid ORIGIN: '${env('ORIGIN', undefined)}'. ` + | ||
| `ORIGIN must be a valid URL with http:// or https:// protocol. ` + | ||
| `For example: 'http://localhost:3000' or 'https://my.site'` | ||
| ); | ||
| } | ||
|
|
||
| const xff_depth = parseInt(env('XFF_DEPTH', '1')); | ||
| const address_header = env('ADDRESS_HEADER', '').toLowerCase(); | ||
| const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { expect, test, describe } from 'vitest'; | ||
| import { parse_as_bytes } from '../utils.js'; | ||
| import { parse_as_bytes, parse_origin } from '../utils.js'; | ||
|
|
||
| describe('parse_as_bytes', () => { | ||
| test('parses correctly', () => { | ||
|
|
@@ -19,3 +19,47 @@ describe('parse_as_bytes', () => { | |
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parse_origin', () => { | ||
| test('valid origins return normalized origin', () => { | ||
|
Contributor
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. Let's use |
||
| const testCases = [ | ||
| { input: 'http://localhost:3000', expected: 'http://localhost:3000' }, | ||
| { input: 'https://example.com', expected: 'https://example.com' }, | ||
| { input: 'http://192.168.1.1:8080', expected: 'http://192.168.1.1:8080' }, | ||
| { input: 'https://my-site.com', expected: 'https://my-site.com' }, | ||
| { input: 'http://localhost', expected: 'http://localhost' }, | ||
| { input: 'https://example.com:443', expected: 'https://example.com' }, | ||
| { input: 'http://example.com:80', expected: 'http://example.com' }, | ||
|
Contributor
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. Why are we stripping the port numbers here? This should be valid AFAIK |
||
| { input: undefined, expected: undefined } | ||
| ]; | ||
|
|
||
| testCases.forEach(({ input, expected }) => { | ||
| const actual = parse_origin(input); | ||
| expect(actual, `Testing input '${input}'`).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| test('URLs with path/query/hash are normalized to origin', () => { | ||
|
Contributor
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. These should just be an error. You shouldn't be providing an invalid origin as |
||
| const testCases = [ | ||
| { input: 'http://localhost:3000/path', expected: 'http://localhost:3000' }, | ||
| { input: 'http://localhost:3000?query=1', expected: 'http://localhost:3000' }, | ||
| { input: 'http://localhost:3000#hash', expected: 'http://localhost:3000' }, | ||
| { input: 'https://example.com/path/to/page', expected: 'https://example.com' }, | ||
| { input: 'https://example.com:443/path?query=1#hash', expected: 'https://example.com' } | ||
| ]; | ||
|
|
||
| testCases.forEach(({ input, expected }) => { | ||
| const actual = parse_origin(input); | ||
| expect(actual, `Testing input '${input}'`).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| test('invalid origins return undefined', () => { | ||
| const invalidInputs = ['localhost:3000', 'example.com', '', ' ', 'ftp://localhost:3000']; | ||
|
|
||
| invalidInputs.forEach((input) => { | ||
| const actual = parse_origin(input); | ||
| expect(actual, `Testing input '${input}'`).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.