fix(core): unable to redirect to same origin paths (#6586)

This commit is contained in:
forehalo
2024-04-17 03:25:31 +00:00
parent 83d8587a45
commit 66a272fb8b
2 changed files with 20 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
import { DebugLogger } from '@affine/debug';
import { type LoaderFunction, Navigate, useLoaderData } from 'react-router-dom'; import { type LoaderFunction, Navigate, useLoaderData } from 'react-router-dom';
const trustedDomain = [ const trustedDomain = [
@@ -10,6 +11,8 @@ const trustedDomain = [
'reddit.com', 'reddit.com',
]; ];
const logger = new DebugLogger('redirect_proxy');
export const loader: LoaderFunction = async ({ request }) => { export const loader: LoaderFunction = async ({ request }) => {
const url = new URL(request.url); const url = new URL(request.url);
const searchParams = url.searchParams; const searchParams = url.searchParams;
@@ -19,14 +22,21 @@ export const loader: LoaderFunction = async ({ request }) => {
return { allow: false }; return { allow: false };
} }
const target = new URL(redirectUri); try {
const target = new URL(redirectUri);
if ( if (
trustedDomain.some(domain => target.hostname === window.location.hostname ||
new RegExp(`.?${domain}$`).test(target.hostname) trustedDomain.some(domain =>
) new RegExp(`.?${domain}$`).test(target.hostname)
) { )
location.href = redirectUri; ) {
location.href = redirectUri;
return { allow: true };
}
} catch (e) {
logger.error('Failed to parse redirect uri', e);
return { allow: false };
} }
return { allow: true }; return { allow: true };

View File

@@ -1,5 +1,8 @@
export function popupWindow(target: string) { export function popupWindow(target: string) {
const url = new URL(runtimeConfig.serverUrlPrefix + '/redirect-proxy'); const url = new URL(runtimeConfig.serverUrlPrefix + '/redirect-proxy');
target = /^https?:\/\//.test(target)
? target
: runtimeConfig.serverUrlPrefix + target;
url.searchParams.set('redirect_uri', target); url.searchParams.set('redirect_uri', target);
return window.open(url, '_blank', `noreferrer noopener`); return window.open(url, '_blank', `noreferrer noopener`);