-
Notifications
You must be signed in to change notification settings - Fork 501
fix: localhost not loading #1122
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { shouldAllowInsecureRequest } from './http-security'; | ||
|
|
||
| describe('shouldAllowInsecureRequest', () => { | ||
| describe('localhost HTTP (always allowed)', () => { | ||
| it('should allow http://localhost', () => { | ||
| expect(shouldAllowInsecureRequest('http://localhost')).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://localhost with port', () => { | ||
| expect(shouldAllowInsecureRequest('http://localhost:8080')).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://localhost with path', () => { | ||
| expect(shouldAllowInsecureRequest('http://localhost/api/token')).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://localhost with port and path', () => { | ||
| expect(shouldAllowInsecureRequest('http://localhost:8080/api/token')).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://127.0.0.1', () => { | ||
| expect(shouldAllowInsecureRequest('http://127.0.0.1')).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://127.0.0.1 with port', () => { | ||
| expect(shouldAllowInsecureRequest('http://127.0.0.1:8080')).toBe(true); | ||
| }); | ||
| }); | ||
nams1570 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('localhost HTTPS (allowed, no insecure flag needed)', () => { | ||
| it('should not need insecure flag for https://localhost', () => { | ||
| expect(shouldAllowInsecureRequest('https://localhost')).toBe(false); | ||
| }); | ||
|
|
||
| it('should not need insecure flag for https://127.0.0.1', () => { | ||
| expect(shouldAllowInsecureRequest('https://127.0.0.1:8080')).toBe(false); | ||
| }); | ||
| }); | ||
nams1570 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('non-localhost HTTP (blocked by default)', () => { | ||
| it('should block http://example.com by default', () => { | ||
| expect(shouldAllowInsecureRequest('http://example.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('should block http://test.internal:8080 by default', () => { | ||
| expect(shouldAllowInsecureRequest('http://test.internal:8080')).toBe(false); | ||
| }); | ||
|
|
||
| it('should block http://192.168.1.1 by default (not 127.0.0.1)', () => { | ||
| expect(shouldAllowInsecureRequest('http://192.168.1.1')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('non-localhost HTTP with dangerouslyAllowInsecureHttp', () => { | ||
| it('should allow http://example.com when opted in', () => { | ||
| expect(shouldAllowInsecureRequest('http://example.com', true)).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://test.internal:8080 when opted in', () => { | ||
| expect(shouldAllowInsecureRequest('http://test.internal:8080', true)).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow http://192.168.1.1 when opted in', () => { | ||
| expect(shouldAllowInsecureRequest('http://192.168.1.1:8080/api', true)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('HTTPS (never needs insecure flag)', () => { | ||
| it('should not need insecure flag for https://example.com', () => { | ||
| expect(shouldAllowInsecureRequest('https://example.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('should not need insecure flag for https://example.com even with opt-in', () => { | ||
| expect(shouldAllowInsecureRequest('https://example.com', true)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('edge cases', () => { | ||
| it('should not match localhost in subdomain (http://localhost.evil.com)', () => { | ||
| expect(shouldAllowInsecureRequest('http://localhost.evil.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('should not match 127.0.0.1 prefix (http://127.0.0.100)', () => { | ||
| expect(shouldAllowInsecureRequest('http://127.0.0.100')).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle undefined dangerouslyAllowInsecureHttp', () => { | ||
| expect(shouldAllowInsecureRequest('http://example.com', undefined)).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle false dangerouslyAllowInsecureHttp', () => { | ||
| expect(shouldAllowInsecureRequest('http://example.com', false)).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
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,10 @@ | ||
| /** | ||
| * Determines if insecure HTTP requests should be allowed for a given endpoint. | ||
| * - Localhost HTTP is always allowed (for local development) | ||
| * - Non-localhost HTTP requires explicit opt-in via dangerouslyAllowInsecureHttp | ||
| * - HTTPS never needs this flag | ||
| */ | ||
| export function shouldAllowInsecureRequest(endpoint: string, dangerouslyAllowInsecureHttp?: boolean): boolean { | ||
| const isLocalhostHttp = /^http:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/.test(endpoint); | ||
nams1570 marked this conversation as resolved.
Show resolved
Hide resolved
nams1570 marked this conversation as resolved.
Show resolved
Hide resolved
nams1570 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return isLocalhostHttp || (!!dangerouslyAllowInsecureHttp && endpoint.startsWith('http://')); | ||
| } | ||
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
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.
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.
it's not that dangerous. it would still require us to have an HTTP base url... which we'd never do in production
keep in mind, tokenEndpoint is controlled by us, not an attacker