diff --git a/frontend/src/lib/logoutRedirect.test.ts b/frontend/src/lib/logoutRedirect.test.ts index a42a86e6ef..b58addcf3a 100644 --- a/frontend/src/lib/logoutRedirect.test.ts +++ b/frontend/src/lib/logoutRedirect.test.ts @@ -1,5 +1,20 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' -import { isValidLogoutRedirect, toSameOriginRelativePath } from './logoutRedirect' +import { isValidLogoutRedirect, parseHttpUrl, toSameOriginRelativePath } from './logoutRedirect' + +describe('parseHttpUrl', () => { + it('accepts http and https URLs', () => { + expect(parseHttpUrl('https://example.com/cb')?.href).toBe('https://example.com/cb') + expect(parseHttpUrl('http://localhost:1234')?.port).toBe('1234') + }) + + it('rejects other schemes and malformed input', () => { + expect(parseHttpUrl('javascript:alert(1)//')).toBeUndefined() + expect(parseHttpUrl('javascript://%0aalert(1)//:80')).toBeUndefined() + expect(parseHttpUrl('data:text/html,x')).toBeUndefined() + expect(parseHttpUrl('/relative')).toBeUndefined() + expect(parseHttpUrl(null)).toBeUndefined() + }) +}) describe('isValidLogoutRedirect', () => { beforeEach(() => { diff --git a/frontend/src/lib/logoutRedirect.ts b/frontend/src/lib/logoutRedirect.ts index 8160dfd66d..2fdca2fcb2 100644 --- a/frontend/src/lib/logoutRedirect.ts +++ b/frontend/src/lib/logoutRedirect.ts @@ -61,6 +61,20 @@ export function toSameOriginRelativePath(rd: string | null | undefined): string } } +/** + * Parses a navigation target taken from the page URL, accepting only `http:` and `https:`. + * A target from page input must go through this before it is assigned to `window.location`. + */ +export function parseHttpUrl(raw: string | null | undefined): URL | undefined { + if (!raw) return undefined + try { + const url = new URL(raw) + return url.protocol === 'http:' || url.protocol === 'https:' ? url : undefined + } catch { + return undefined + } +} + function hasControlChar(s: string): boolean { for (let i = 0; i < s.length; i++) { const c = s.charCodeAt(i) diff --git a/frontend/src/routes/(root)/(logged)/svix/create-webhook/+page@(root).svelte b/frontend/src/routes/(root)/(logged)/svix/create-webhook/+page@(root).svelte index a8b6ecbb9b..4913923f88 100644 --- a/frontend/src/routes/(root)/(logged)/svix/create-webhook/+page@(root).svelte +++ b/frontend/src/routes/(root)/(logged)/svix/create-webhook/+page@(root).svelte @@ -2,7 +2,7 @@ import { run } from 'svelte/legacy' import ScriptPicker from '$lib/components/ScriptPicker.svelte' - import { Button } from '$lib/components/common' + import { Alert, Button } from '$lib/components/common' import { page } from '$app/stores' import { apiBaseUrl } from '$lib/apiBaseUrl.svelte' import InstanceUiSync from '$lib/components/InstanceUiSync.svelte' @@ -13,11 +13,12 @@ import Tooltip from '$lib/components/Tooltip.svelte' import WorkspaceMenu from '$lib/components/sidebar/WorkspaceMenu.svelte' import { Menubar } from '$lib/components/meltComponents' + import { parseHttpUrl } from '$lib/logoutRedirect' let itemPath: string | undefined = $state(undefined) let itemKind: 'script' | 'flow' | 'app' = $state('script') let clientName = $page.url.searchParams.get('domain') ?? undefined - let redirectURI = $page.url.searchParams.get('redirect_uri') + const redirectURL = parseHttpUrl($page.url.searchParams.get('redirect_uri')) let userSettings: TokensDrawer | undefined = $state() let token: string | undefined = $state(undefined) @@ -45,15 +46,17 @@ } function redirectSuccess(webhook: string, token: string) { + if (!redirectURL) return const webhook_url = new URL(webhook) webhook_url.searchParams.append('token', token) - const url = new URL(redirectURI!) + const url = new URL(redirectURL) url.searchParams.append('webhook_url', webhook_url.toString()) window.location.href = url.toString() } function redirectCancel(e: string): void { - const url = new URL(redirectURI!) + if (!redirectURL) return + const url = new URL(redirectURL) url.searchParams.append('error', e) window.location.href = url.toString() } @@ -92,6 +95,15 @@ >{`${clientName} wants to create a webhook`} + {#if redirectURL} +

+ The webhook URL and its token will be sent to {redirectURL.host} +

+ {:else} + + This link has no valid http or https redirect address, so the request cannot be completed. + + {/if}
- +
diff --git a/frontend/src/routes/(root)/(logged)/user/cli/+page.svelte b/frontend/src/routes/(root)/(logged)/user/cli/+page.svelte index 5113af02d8..8c9b7d3ade 100644 --- a/frontend/src/routes/(root)/(logged)/user/cli/+page.svelte +++ b/frontend/src/routes/(root)/(logged)/user/cli/+page.svelte @@ -1,20 +1,22 @@ -

- Token will be posted to {host == 'localhost' ? 'your local machine' : host} to port {port} -

+ {#if callbackUrl} +

+ Token will be posted to {callbackUrl.hostname == 'localhost' + ? 'your local machine' + : callbackUrl.hostname} to port {callbackUrl.port || + (callbackUrl.protocol === 'https:' ? 443 : 80)} +

+ {:else} + + This link does not name a valid http or https address to send the token to. + + {/if}
Token expires in