-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce configured host on user supplied returnTo (#557)
* Enforce configured host on user supplied returnTo * Test improvements per PR suggestions
- Loading branch information
1 parent
129650d
commit 0bbd9f8
Showing
6 changed files
with
571 additions
and
33 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
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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
/** | ||
* Helper which tests if a URL can safely be redirected to. Requires the URL to be relative. | ||
* @param url | ||
* @param dangerousRedirect | ||
* @param safeBaseUrl | ||
*/ | ||
export default function isSafeRedirect(url: string): boolean { | ||
if (typeof url !== 'string') { | ||
throw new TypeError(`Invalid url: ${url}`); | ||
export default function toSafeRedirect(dangerousRedirect: string, safeBaseUrl: URL): string | undefined { | ||
let url: URL; | ||
try { | ||
url = new URL(dangerousRedirect, safeBaseUrl); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
|
||
// Prevent open redirects using the //foo.com format (double forward slash). | ||
if (/\/\//.test(url)) { | ||
return false; | ||
if (url.origin === safeBaseUrl.origin) { | ||
return url.toString(); | ||
} | ||
|
||
return !/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url); | ||
return undefined; | ||
} |
Oops, something went wrong.