mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-04 00:06:39 +00:00
Shared helper for redirect targets across login, logout and related pages, plus a small notice on the CLI login page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
// Store-free on purpose: the opaque public-app viewer imports this, and its bundle must not
|
|
// statically pull in `$lib/stores` or the generated client (see publicAppApi.ts).
|
|
|
|
/**
|
|
* Whether a URL path addresses the backend (`/api/...`) rather than a page of the app.
|
|
*
|
|
* A navigation the app starts itself reaches the backend as same-origin, which is what its
|
|
* cross-site GET checks trust, so a redirect target taken from a link must never be one. The
|
|
* path is decoded and re-normalized until stable, so `/%61pi`, `/./api`, `//api` and `/API`
|
|
* are caught whichever way the server ends up reading them.
|
|
*/
|
|
export function isBackendApiPath(pathname: string): boolean {
|
|
let p = pathname
|
|
for (let i = 0; i < 8; i++) {
|
|
let decoded: string
|
|
try {
|
|
decoded = decodeURIComponent(p)
|
|
} catch {
|
|
return true
|
|
}
|
|
const collapsed = decoded.replace(/[\\/]+/g, '/')
|
|
let next: string
|
|
try {
|
|
next = new URL(collapsed.startsWith('/') ? collapsed : '/' + collapsed, 'http://n').pathname
|
|
} catch {
|
|
return true
|
|
}
|
|
next = next.toLowerCase()
|
|
if (next === p) break
|
|
p = next
|
|
}
|
|
return p === '/api' || p.startsWith('/api/')
|
|
}
|
|
|
|
/** Whether `url` is a backend route of the instance this page is served from. */
|
|
export function isSameOriginBackendUrl(url: URL): boolean {
|
|
return (
|
|
typeof window !== 'undefined' &&
|
|
url.origin === window.location?.origin &&
|
|
isBackendApiPath(url.pathname)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Reduces a redirect target to a safe same-origin relative path.
|
|
*
|
|
* Returns the path (`/foo?bar#baz`) when the input is either:
|
|
* - already an absolute-but-relative path (`/foo`) that isn't protocol-relative
|
|
* (`//evil.com`) or a back-slash trick (`/\\evil.com`), or
|
|
* - a full URL whose origin matches the current page's origin,
|
|
* and the path is not a backend route ({@link isBackendApiPath}).
|
|
*
|
|
* Returns `null` for anything else (cross-origin URLs, protocol-relative paths,
|
|
* malformed input). Used to sanitize values flowing into `RelayState`,
|
|
* `localStorage.rd`, and post-login redirects so we don't either lose a valid
|
|
* same-origin deep link or open-redirect into a foreign origin.
|
|
*/
|
|
export function toSameOriginRelativePath(rd: string | null | undefined): string | null {
|
|
if (!rd) return null
|
|
if (rd.length > 2048) return null
|
|
if (hasControlChar(rd)) return null
|
|
if (rd.startsWith('/')) {
|
|
if (rd.startsWith('//') || rd.startsWith('/\\')) return null
|
|
try {
|
|
if (isBackendApiPath(new URL(rd, 'http://n').pathname)) return null
|
|
} catch {
|
|
return null
|
|
}
|
|
return rd
|
|
}
|
|
if (typeof window === 'undefined') return null
|
|
try {
|
|
const url = new URL(rd, window.location.origin)
|
|
// A `blob:` URL takes the origin of the URL it wraps, which its pathname then holds.
|
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
|
|
if (url.origin !== window.location.origin) return null
|
|
if (isBackendApiPath(url.pathname)) return null
|
|
// `https://<this host>//evil.example` has the pathname `//evil.example`, which on its own
|
|
// is a protocol-relative URL to another host.
|
|
const pathname = url.pathname.replace(/^[/\\]+/, '/')
|
|
return pathname + url.search + url.hash
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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`.
|
|
* A backend route of this instance is refused too (see {@link isBackendApiPath}).
|
|
*/
|
|
export function parseHttpUrl(raw: string | null | undefined): URL | undefined {
|
|
if (!raw) return undefined
|
|
try {
|
|
const url = new URL(raw)
|
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') return undefined
|
|
return isSameOriginBackendUrl(url) ? undefined : url
|
|
} catch {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
export function hasControlChar(s: string): boolean {
|
|
for (let i = 0; i < s.length; i++) {
|
|
const c = s.charCodeAt(i)
|
|
if (c < 0x20 || c === 0x7f) return true
|
|
}
|
|
return false
|
|
}
|