Files
Alaister YoungandAlaister Young 31711c0762 fix(studio): validate edge function test proxy target url (#53256)
The edge function test proxy (`pages/api/edge-functions/test.ts`, also
served by `routes/api/edge-functions/test.ts`) fetched the URL from the
request body after checking it only with the regexes in
`isValidEdgeFunctionURL`, and on self-hosted any
`http(s)://<host>/functions/vN/...` URL passed. It now fetches only a
URL rebuilt from parts it has validated.

**Changed:**
- `apps/studio/lib/api/edgeFunctions.ts` – replaced
`isValidEdgeFunctionURL` with `getEdgeFunctionTestUrl`, which parses the
URL, rejects non-http(s) schemes and credentials, requires a
`/functions/vN/` path, checks the host (platform:
`<ref>.supabase.co|red` over https; the Nimbus apex domain when
`NIMBUS_PROD_PROJECTS_URL` is set; self-hosted: the `SUPABASE_URL` or
`SUPABASE_PUBLIC_URL` host) and returns the fetch target rebuilt from
the validated origin, path and query. Self-hosted requests always go to
the `SUPABASE_URL` origin, replacing the old `SUPABASE_PUBLIC_URL`
string rewrite.
- `apps/studio/pages/api/edge-functions/test.ts` – fetches the URL
returned by `getEdgeFunctionTestUrl` and returns 400 when there is none.
`redirect: 'manual'` is unchanged.

**Added:**
- `apps/studio/lib/api/edgeFunctions.test.ts` – allowed platform,
`.red`, Nimbus and self-hosted URLs, and rejected cases: other hosts,
lookalike hosts, userinfo, ports, non-http schemes, paths outside
`/functions/vN/`.
- `apps/studio/tests/pages/api/edge-functions/test.test.ts` – handler
rejects userinfo and non-functions paths, and on self-hosted calls the
public URL through `SUPABASE_URL` and rejects other hosts.

## To test
- [ ] On platform (including staging `.red`), open an edge function,
click **Test**, and send a request with a query parameter: the response
shows as before.
- [ ] On self-hosted (`docker compose`), do the same from the edge
function tester: the response shows as before.
- [ ] `POST /api/edge-functions/test` with `{ "url":
"https://example.com/functions/v1/hello", "method": "GET" }`: the
response is 400 with "Provided URL is not a valid Supabase edge function
URL".

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-11 16:34:02 +09:00

114 lines
3.9 KiB
TypeScript

import { IS_PLATFORM } from '@/lib/constants'
// Cron jobs and database hooks run inside Postgres, where Kong is available by this network alias.
const SELF_HOSTED_EDGE_FUNCTIONS_URL = 'http://kong:8000/functions/v1'
const PLATFORM_TLDS = ['co', 'red'] as const
export const buildDatabaseEdgeFunctionUrl = (
slug: string,
projectRef: string,
restUrl?: string,
isPlatform = IS_PLATFORM
) => {
if (!isPlatform) return `${SELF_HOSTED_EDGE_FUNCTIONS_URL}/${slug}`
const projectOrigin = restUrl ? new URL(restUrl).origin : `https://${projectRef}.supabase.co`
return `${projectOrigin}/functions/v1/${slug}`
}
export const isEdgeFunctionUrl = (
url: string,
projectRef: string,
restUrl?: string,
isPlatform = IS_PLATFORM
) => {
if (!isPlatform && url.startsWith(`${SELF_HOSTED_EDGE_FUNCTIONS_URL}/`)) return true
const projectOrigin = restUrl ? new URL(restUrl).origin : undefined
if (projectOrigin && url.startsWith(`${projectOrigin}/functions/v1/`)) return true
return PLATFORM_TLDS.some(
(tld) =>
url.startsWith(`https://${projectRef}.functions.supabase.${tld}/`) ||
url.startsWith(`https://${projectRef}.supabase.${tld}/functions/`)
)
}
const PLATFORM_EDGE_FUNCTION_HOSTNAME = /^[a-z]{20}\.supabase\.(co|red)$/
const PROJECT_SUBDOMAIN = /^[a-z]+$/
// Any WHATWG-serialized http(s) pathname under `/functions/vN/`. The character class is spelled
// out instead of `.*` so static analysis (CodeQL) recognizes the test as a sanitizer.
const EDGE_FUNCTION_PATHNAME = /^\/functions\/v[0-9]+\/[\w\-.~!$&'()*+,;=:@%/[\]^|]*$/
interface EdgeFunctionTestUrlOptions {
isPlatform: boolean
/** Wildcard projects URL of a Nimbus deployment, e.g. `https://*.example.com` */
nimbusProjectsUrl?: string
/** Self-hosted API gateway URL the server can reach */
supabaseUrl?: string
/** Self-hosted API gateway URL as seen by the browser */
supabasePublicUrl?: string
}
const parseUrl = (value: unknown) => {
if (typeof value !== 'string') return undefined
try {
return new URL(value)
} catch {
return undefined
}
}
const withQuery = (base: string, search: string) => {
const query = search.slice(1)
return query.length > 0 ? `${base}?${query}` : base
}
/**
* Validates a URL submitted to the edge function test proxy and returns the URL the proxy should
* fetch, rebuilt from the validated parts, or `undefined` if it isn't an edge function URL of a
* Supabase project the proxy may call.
*/
export const getEdgeFunctionTestUrl = (
url: unknown,
{ isPlatform, nimbusProjectsUrl, supabaseUrl, supabasePublicUrl }: EdgeFunctionTestUrlOptions
): string | undefined => {
const parsed = parseUrl(url)
if (parsed === undefined) return undefined
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') return undefined
if (parsed.username !== '' || parsed.password !== '') return undefined
const { hostname, pathname, search } = parsed
if (!EDGE_FUNCTION_PATHNAME.test(pathname)) return undefined
if (!isPlatform) {
const internalUrl = parseUrl(supabaseUrl)
if (internalUrl === undefined) return undefined
const isInternalHost = parsed.host === internalUrl.host
const isPublicHost = parsed.host === parseUrl(supabasePublicUrl)?.host
if (!isInternalHost && !isPublicHost) return undefined
return withQuery(`${internalUrl.origin}${pathname}`, search)
}
if (parsed.protocol !== 'https:' || parsed.port !== '') return undefined
if (nimbusProjectsUrl) {
const apexDomain = parseUrl(nimbusProjectsUrl.replace('https://*.', 'https://'))?.hostname
if (!apexDomain) return undefined
const suffix = `.${apexDomain}`
if (!hostname.endsWith(suffix)) return undefined
const subdomain = hostname.slice(0, -suffix.length)
if (!PROJECT_SUBDOMAIN.test(subdomain)) return undefined
return withQuery(`https://${subdomain}.${apexDomain}${pathname}`, search)
}
if (!PLATFORM_EDGE_FUNCTION_HOSTNAME.test(hostname)) return undefined
return withQuery(`https://${hostname}${pathname}`, search)
}