-
-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
security(ExternalRedirect): stronger validations (#10627)
- Loading branch information
Showing
4 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,17 +1,86 @@ | ||
import { isTrustedRedirectHost } from '../url-helpers'; | ||
import { isRelativeHref, isTrustedRedirectURL } from '../url-helpers'; | ||
import * as WindowLib from '../window'; | ||
|
||
// Mock the window object to simulate SSR/CSR | ||
jest.mock('../window'); | ||
|
||
describe('isTrustedRedirectHost', () => { | ||
it('returns true for valid domains', () => { | ||
expect(isTrustedRedirectHost('octobox.io')).toBe(true); | ||
expect(isTrustedRedirectHost('opencollective.com')).toBe(true); | ||
expect(isTrustedRedirectHost('docs.opencollective.com')).toBe(true); | ||
expect(isTrustedRedirectHost('app.papertree.earth')).toBe(true); | ||
expect(isTrustedRedirectHost('gatherfor.org')).toBe(true); | ||
expect(isTrustedRedirectURL(new URL('https://octobox.io'))).toBe(true); | ||
expect(isTrustedRedirectURL(new URL('https://opencollective.com'))).toBe(true); | ||
expect(isTrustedRedirectURL(new URL('https://docs.opencollective.com'))).toBe(true); | ||
expect(isTrustedRedirectURL(new URL('https://app.papertree.earth'))).toBe(true); | ||
expect(isTrustedRedirectURL(new URL('https://gatherfor.org'))).toBe(true); | ||
}); | ||
|
||
it('returns false for invalid domains', () => { | ||
expect(isTrustedRedirectHost('wowoctobox.io')).toBe(false); | ||
expect(isTrustedRedirectHost('opencollectivez.com')).toBe(false); | ||
expect(isTrustedRedirectHost('malicious-opencollective.com')).toBe(false); | ||
expect(isTrustedRedirectURL(new URL('https://wowoctobox.io'))).toBe(false); | ||
expect(isTrustedRedirectURL(new URL('https://opencollectivez.com'))).toBe(false); | ||
expect(isTrustedRedirectURL(new URL('https://malicious-opencollective.com'))).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isRelativeHref', () => { | ||
describe('on main website, client-side', () => { | ||
it('returns true for relative URLs', () => { | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('/foo')).toBe(true); | ||
|
||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('/foo/bar')).toBe(true); | ||
|
||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('/foo/bar?baz=1')).toBe(true); | ||
|
||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('/foo/bar#baz')).toBe(true); | ||
}); | ||
|
||
it('returns false for absolute URLs', () => { | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('https://octobox.io')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('http://octobox.io')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('//octobox.io')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('foo')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('foo/bar')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('./foo/bar')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: 'https://opencollective.com' }); | ||
expect(isRelativeHref('../foo/bar')).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('without window object set, server-side', () => { | ||
it('returns true for relative URLs', () => { | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('/foo')).toBe(true); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('/foo/bar')).toBe(true); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('/foo/bar?baz=1')).toBe(true); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('/foo/bar#baz')).toBe(true); | ||
}); | ||
|
||
it('returns false for absolute URLs', () => { | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('https://octobox.io')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('http://octobox.io')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('//octobox.io')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('foo')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('foo/bar')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('./foo/bar')).toBe(false); | ||
WindowLib.getWindowLocation.mockReturnValueOnce({ origin: undefined }); | ||
expect(isRelativeHref('../foo/bar')).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains 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 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,11 @@ | ||
/** | ||
* A helper that returns `window.location` if it exists. | ||
* Two advantages: | ||
* 1. It makes it easier to mock `window.location` in tests. | ||
* 2. It doesn't crash the server-side rendering. | ||
*/ | ||
export const getWindowLocation = (): Location | undefined => { | ||
if (typeof window !== 'undefined') { | ||
return window.location; | ||
} | ||
}; |
This file contains 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