fix: validate return addresses on the webhook and cli sign-in pages (#12719)

* fix: validate the address the webhook and cli sign-in pages send back to

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: show the port the cli sign-in page actually sends to

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-28 17:15:17 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 0c63dfe18c
commit dce05fdc3b
4 changed files with 79 additions and 31 deletions
+16 -1
View File
@@ -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(() => {
+14
View File
@@ -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)
@@ -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`}</h1
>
</div>
{#if redirectURL}
<p class="text-center text-sm text-secondary">
The webhook URL and its token will be sent to <b>{redirectURL.host}</b>
</p>
{:else}
<Alert type="error" title="Invalid redirect address">
This link has no valid http or https redirect address, so the request cannot be completed.
</Alert>
{/if}
<div class="flex flex-row gap-3 items-center">
<div
class="flex items-center justify-center min-w-10 w-10 h-10 border-2 border-blue-500 text-blue-500 font-bold rounded-full"
@@ -161,12 +173,16 @@
</div>
<div class="flex flex-row gap-2 mt-10">
<Button
disabled={!itemPath || !token || token === ''}
disabled={!redirectURL || !itemPath || !token || token === ''}
btnClasses="whitespace-normal break-words flex flex-wrap"
on:click={() => itemPath && token && createWebhook(itemPath, itemKind, token)}
>Approve and share with {clientName}</Button
>
<Button color="red" on:click={() => redirectCancel('user_canceled')}>Cancel</Button>
<Button
color="red"
disabled={!redirectURL}
on:click={() => redirectCancel('user_canceled')}>Cancel</Button
>
</div>
</div>
</div>
@@ -1,20 +1,22 @@
<script lang="ts">
import { onMount, untrack } from 'svelte'
import { onMount } from 'svelte'
import { SettingService, UserService } from '$lib/gen'
import { page } from '$app/state'
import CenteredModal from '$lib/components/CenteredModal.svelte'
import { Button } from '$lib/components/common'
import { Alert, Button } from '$lib/components/common'
import Select from '$lib/components/select/Select.svelte'
import Toggle from '$lib/components/Toggle.svelte'
import ToggleButtonGroup from '$lib/components/common/toggleButton-v2/ToggleButtonGroup.svelte'
import ToggleButton from '$lib/components/common/toggleButton-v2/ToggleButton.svelte'
import { superadmin, userStore, usersWorkspaceStore, workspaceStore } from '$lib/stores'
import { parseMaxTokenExpirationDays } from '$lib/tokenExpiration'
import { parseHttpUrl } from '$lib/logoutRedirect'
let port = $state(Number(page.url.searchParams.get('port')))
let host: string = page.url.searchParams.get('host') || 'localhost'
let scheme: string = page.url.searchParams.get('scheme') || 'http'
port = untrack(() => port) == 0 || Number.isNaN(untrack(() => port)) ? 80 : untrack(() => port)
const rawPort = Number(page.url.searchParams.get('port'))
const port = rawPort == 0 || Number.isNaN(rawPort) ? 80 : rawPort
const host = page.url.searchParams.get('host') || 'localhost'
const scheme = page.url.searchParams.get('scheme') || 'http'
const callbackUrl = parseHttpUrl(`${scheme}://${host}:${port}`)
const DEFAULT_DAYS = 30
const CHOICES_DAYS = [1, 7, 30, 90, 365]
@@ -70,6 +72,7 @@
const isAdminSomewhere = $derived(!!$userStore?.is_admin || !!$superadmin)
async function authorizeToken(): Promise<void> {
if (!callbackUrl) return
const username = $userStore?.username
// `cli-login:` is reserved in `is_user_token` (windmill-common): the CLI signs in again on
// its own once this token expires, so it must not email the user about it.
@@ -88,24 +91,26 @@
}
})
const url =
scheme +
'://' +
host +
':' +
port +
'?token=' +
newToken +
'&workspace=' +
(cliWorkspace ?? $workspaceStore)
window.location.href = url
const url = new URL(callbackUrl)
url.searchParams.set('token', newToken)
url.searchParams.set('workspace', cliWorkspace ?? $workspaceStore ?? '')
window.location.href = url.toString()
}
</script>
<CenteredModal title="Authorize login request" inPage>
<p class="text-center text-lg mb-6">
Token will be posted to {host == 'localhost' ? 'your local machine' : host} to port {port}
</p>
{#if callbackUrl}
<p class="text-center text-lg mb-6">
Token will be posted to {callbackUrl.hostname == 'localhost'
? 'your local machine'
: callbackUrl.hostname} to port {callbackUrl.port ||
(callbackUrl.protocol === 'https:' ? 443 : 80)}
</p>
{:else}
<Alert type="error" title="Invalid login request" class="mb-6">
This link does not name a valid http or https address to send the token to.
</Alert>
{/if}
<div class="flex flex-col gap-1">
<span class="text-xs font-semibold text-emphasis">Token expires in</span>
<Select
@@ -168,10 +173,8 @@
{/if}
<div class="flex flex-row justify-around pt-4 gap-x-1">
<Button variant="default" destructive unifiedSize="lg" href="/">Decline</Button>
<Button
variant="accent"
unifiedSize="lg"
onClick={authorizeToken}>Authorize</Button
<Button variant="accent" unifiedSize="lg" disabled={!callbackUrl} onClick={authorizeToken}
>Authorize</Button
>
</div>
</CenteredModal>