Skip to content

Commit 216c931

Browse files
authored
fix(flat-pages): avoid JavaScript protocol in redirect URL (#2135)
1 parent 68a4710 commit 216c931

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

  • packages/flat-pages/src/LoginPage/utils

packages/flat-pages/src/LoginPage/utils/state.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ export function useMachine(): [any, (type: ToggleEventsType) => void] {
3636
return [currentState, setCurrentState];
3737
}
3838

39+
const isJavaScriptProtocol =
40+
/^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*\:/i;
41+
42+
function sanitizeURL(url: string | null): string | null {
43+
if (url && isJavaScriptProtocol.test(url)) {
44+
console.warn("Refuse to redirect to", url);
45+
return null;
46+
}
47+
return url;
48+
}
49+
3950
export interface LoginState {
4051
currentState: any;
4152
setCurrentState: (type: ToggleEventsType) => void;
@@ -55,7 +66,7 @@ export function useLoginState(): LoginState {
5566
const [loginResult, setLoginResult] = useState<LoginProcessResult | null>(null);
5667

5768
const [redirectURL] = useState(() =>
58-
new URLSearchParams(window.location.search).get("redirect"),
69+
sanitizeURL(new URLSearchParams(window.location.search).get("redirect")),
5970
);
6071

6172
const [currentState, setCurrentState] = useMachine();

0 commit comments

Comments
 (0)