Skip to content

Commit 4fab8e8

Browse files
authored
Login: Filter redirect URLs (#88726)
* Login: Filter redirect URLs * Remove safeProtocolUrl() usage
1 parent 3fcd217 commit 4fab8e8

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

client/login/redirect-logged-in/index.web.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
import safeProtocolUrl from 'calypso/lib/safe-protocol-url';
21
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
32

3+
/**
4+
* For this context, we consider external URLs that are NOT:
5+
* - Relative paths (`/test`)
6+
* - Absolute URLs on https://wordpress.com/*
7+
* @param {string} url URL to check
8+
* @returns {boolean}
9+
*/
10+
function isExternalUrl( url ) {
11+
if ( url.startsWith( '/' ) ) {
12+
return false;
13+
}
14+
15+
try {
16+
const urlObject = new URL( url );
17+
if ( urlObject.hostname === 'wordpress.com' && urlObject.protocol === 'https:' ) {
18+
return false;
19+
}
20+
} catch {
21+
return true;
22+
}
23+
24+
return true;
25+
}
26+
427
export default function redirectLoggedIn( context, next ) {
528
const userLoggedIn = isUserLoggedIn( context.store.getState() );
629

730
if ( userLoggedIn ) {
831
// force full page reload to avoid SSR hydration issues.
932
// Redirect parameters should have higher priority.
10-
let url = safeProtocolUrl( context?.query?.redirect_to );
11-
if ( ! url || url === 'http:' ) {
33+
let url = context?.query?.redirect_to;
34+
if ( ! url || isExternalUrl( url ) ) {
1235
url = '/';
1336
}
1437
window.location = url;

0 commit comments

Comments
 (0)