Skip to content
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

fix(flat-pages): avoid JavaScript protocol in redirect URL #2135

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/flat-pages/src/LoginPage/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ export function useMachine(): [any, (type: ToggleEventsType) => void] {
return [currentState, setCurrentState];
}

const isJavaScriptProtocol =
/^[\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;

function sanitizeURL(url: string | null): string | null {
if (url && isJavaScriptProtocol.test(url)) {
console.warn("Refuse to redirect to", url);
return null;
}
return url;
}

export interface LoginState {
currentState: any;
setCurrentState: (type: ToggleEventsType) => void;
Expand All @@ -55,7 +66,7 @@ export function useLoginState(): LoginState {
const [loginResult, setLoginResult] = useState<LoginProcessResult | null>(null);

const [redirectURL] = useState(() =>
new URLSearchParams(window.location.search).get("redirect"),
sanitizeURL(new URLSearchParams(window.location.search).get("redirect")),
);

const [currentState, setCurrentState] = useMachine();
Expand Down
Loading