-
Notifications
You must be signed in to change notification settings - Fork 3
CDM-350 Add external URL redirect whitelist for nextRequest #252
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
+269
−12
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d778ff7
Add external URL redirect whitelist for nextRequest
dauglyon bb42042
Add tests for redirect validation utilities
dauglyon d9766dc
Make isExternalUrl case-insensitive for protocol matching
dauglyon 3bb3d54
Simplify redirect whitelist validation per PR review
dauglyon 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,174 @@ | ||
| import { | ||
| isExternalUrl, | ||
| matchesWildcard, | ||
| getRedirectWhitelist, | ||
| isWhitelistedExternalUrl, | ||
| } from './redirectValidation'; | ||
|
|
||
| describe('isExternalUrl', () => { | ||
| test('returns true for https URLs', () => { | ||
| expect(isExternalUrl('https://example.com')).toBe(true); | ||
| expect(isExternalUrl('https://hub.berdl.kbase.us/path')).toBe(true); | ||
| }); | ||
|
|
||
| test('returns true for http URLs', () => { | ||
| expect(isExternalUrl('http://example.com')).toBe(true); | ||
| }); | ||
|
|
||
| test('handles case variations in protocol', () => { | ||
| expect(isExternalUrl('HTTPS://example.com')).toBe(true); | ||
| expect(isExternalUrl('HTTP://example.com')).toBe(true); | ||
| expect(isExternalUrl('Https://example.com')).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false for JSON-encoded paths', () => { | ||
| expect(isExternalUrl('{"pathname":"/narratives"}')).toBe(false); | ||
| expect(isExternalUrl('"/profile"')).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false for plain paths', () => { | ||
| expect(isExternalUrl('/narratives')).toBe(false); | ||
| expect(isExternalUrl('narratives')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('matchesWildcard', () => { | ||
| test('matches exact domains', () => { | ||
| expect(matchesWildcard('example.com', 'example.com')).toBe(true); | ||
| expect(matchesWildcard('hub.berdl.kbase.us', 'hub.berdl.kbase.us')).toBe( | ||
| true | ||
| ); | ||
| }); | ||
|
|
||
| test('does not match different exact domains', () => { | ||
| expect(matchesWildcard('other.com', 'example.com')).toBe(false); | ||
| }); | ||
|
|
||
| test('matches wildcard subdomains', () => { | ||
| expect(matchesWildcard('hub.berdl.kbase.us', '*.berdl.kbase.us')).toBe( | ||
| true | ||
| ); | ||
| expect(matchesWildcard('hub.dev.berdl.kbase.us', '*.berdl.kbase.us')).toBe( | ||
| true | ||
| ); | ||
| expect(matchesWildcard('anything.kbase.us', '*.kbase.us')).toBe(true); | ||
| }); | ||
|
|
||
| test('matches wildcard base domain', () => { | ||
| expect(matchesWildcard('berdl.kbase.us', '*.berdl.kbase.us')).toBe(true); | ||
| }); | ||
|
|
||
| test('does not match unrelated domains with wildcard', () => { | ||
| expect(matchesWildcard('hub.other.com', '*.berdl.kbase.us')).toBe(false); | ||
| expect(matchesWildcard('kbase.us', '*.berdl.kbase.us')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getRedirectWhitelist', () => { | ||
| const originalEnv = process.env.REACT_APP_REDIRECT_WHITELIST; | ||
|
|
||
| afterEach(() => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = originalEnv; | ||
| }); | ||
|
|
||
| test('returns empty array when not set', () => { | ||
| delete process.env.REACT_APP_REDIRECT_WHITELIST; | ||
| expect(getRedirectWhitelist()).toEqual([]); | ||
| }); | ||
|
|
||
| test('returns empty array for empty string', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = ''; | ||
| expect(getRedirectWhitelist()).toEqual([]); | ||
| }); | ||
|
|
||
| test('parses single domain', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*.berdl.kbase.us'; | ||
| expect(getRedirectWhitelist()).toEqual(['*.berdl.kbase.us']); | ||
| }); | ||
|
|
||
| test('parses comma-separated domains', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = | ||
| '*.berdl.kbase.us,*.other.kbase.us'; | ||
| expect(getRedirectWhitelist()).toEqual([ | ||
| '*.berdl.kbase.us', | ||
| '*.other.kbase.us', | ||
| ]); | ||
| }); | ||
|
|
||
| test('trims whitespace', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = | ||
| ' *.berdl.kbase.us , *.other.kbase.us '; | ||
| expect(getRedirectWhitelist()).toEqual([ | ||
| '*.berdl.kbase.us', | ||
| '*.other.kbase.us', | ||
| ]); | ||
| }); | ||
|
|
||
| test('filters empty entries', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = | ||
| '*.berdl.kbase.us,,*.other.kbase.us,'; | ||
| expect(getRedirectWhitelist()).toEqual([ | ||
| '*.berdl.kbase.us', | ||
| '*.other.kbase.us', | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isWhitelistedExternalUrl', () => { | ||
| const originalEnv = process.env.REACT_APP_REDIRECT_WHITELIST; | ||
|
|
||
| afterEach(() => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = originalEnv; | ||
| }); | ||
|
|
||
| test('returns false when whitelist is empty', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = ''; | ||
| expect(isWhitelistedExternalUrl('https://hub.berdl.kbase.us')).toBe(false); | ||
| }); | ||
|
|
||
| test('returns true for whitelisted domain', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*.berdl.kbase.us'; | ||
| expect(isWhitelistedExternalUrl('https://hub.berdl.kbase.us')).toBe(true); | ||
| expect(isWhitelistedExternalUrl('https://hub.berdl.kbase.us/path')).toBe( | ||
| true | ||
| ); | ||
| expect( | ||
| isWhitelistedExternalUrl('https://hub.dev.berdl.kbase.us/path?query=1') | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false for non-whitelisted domain', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*.berdl.kbase.us'; | ||
| expect(isWhitelistedExternalUrl('https://evil.com')).toBe(false); | ||
| expect(isWhitelistedExternalUrl('https://other.kbase.us')).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false for HTTP URLs', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*.berdl.kbase.us'; | ||
| expect(isWhitelistedExternalUrl('http://hub.berdl.kbase.us')).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false for invalid URLs', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*.berdl.kbase.us'; | ||
| expect(isWhitelistedExternalUrl('not-a-url')).toBe(false); | ||
| expect(isWhitelistedExternalUrl('')).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false when whitelist contains literal asterisk', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*'; | ||
| expect(isWhitelistedExternalUrl('https://anything.com')).toBe(false); | ||
| }); | ||
|
|
||
| test('allows broad wildcards like *.com when explicitly configured', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = '*.com'; | ||
| expect(isWhitelistedExternalUrl('https://anything.com')).toBe(true); | ||
| }); | ||
|
|
||
| test('works with multiple whitelist entries', () => { | ||
| process.env.REACT_APP_REDIRECT_WHITELIST = | ||
| '*.berdl.kbase.us,exact.example.com'; | ||
| expect(isWhitelistedExternalUrl('https://hub.berdl.kbase.us')).toBe(true); | ||
| expect(isWhitelistedExternalUrl('https://exact.example.com')).toBe(true); | ||
| expect(isWhitelistedExternalUrl('https://other.example.com')).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,59 @@ | ||
| /** | ||
| * Utilities for validating external redirect URLs. | ||
| * | ||
| * External URLs must be HTTPS and match a whitelisted domain pattern. | ||
| * Wildcards like *.berdl.kbase.us are supported. Literal "*" is rejected. | ||
| */ | ||
|
|
||
| export const getRedirectWhitelist = (): string[] => { | ||
| const whitelist = process.env.REACT_APP_REDIRECT_WHITELIST || ''; | ||
| return whitelist | ||
| .split(',') | ||
| .map((d) => d.trim()) | ||
| .filter(Boolean); | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if a hostname matches a domain pattern. | ||
| * Supports wildcards: *.berdl.kbase.us matches hub.berdl.kbase.us | ||
| */ | ||
| export const matchesWildcard = (hostname: string, pattern: string): boolean => { | ||
| if (pattern.startsWith('*.')) { | ||
| const suffix = pattern.slice(1); // ".berdl.kbase.us" | ||
| return hostname.endsWith(suffix) || hostname === pattern.slice(2); | ||
| } | ||
| return hostname === pattern; | ||
| }; | ||
|
|
||
| /** | ||
| * Validates if a URL is whitelisted for external redirect. | ||
| * - Must be HTTPS | ||
| * - Must match a pattern in the whitelist | ||
| * - Rejects if whitelist contains literal "*" | ||
| */ | ||
| export const isWhitelistedExternalUrl = (url: string): boolean => { | ||
| try { | ||
| const parsed = new URL(url); | ||
|
|
||
| // Must be HTTPS | ||
| if (parsed.protocol !== 'https:') return false; | ||
|
|
||
| const whitelist = getRedirectWhitelist(); | ||
|
|
||
| if (whitelist.includes('*')) return false; | ||
|
|
||
| return whitelist.some((pattern) => | ||
| matchesWildcard(parsed.hostname, pattern) | ||
| ); | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if a string looks like an external URL (starts with http:// or https://) | ||
| */ | ||
| export const isExternalUrl = (value: string): boolean => { | ||
| const lower = value.toLowerCase(); | ||
| return lower.startsWith('https://') || lower.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
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.