fix(cloud): harden edge gateways, api-server ssrf, supabase fail-closed quota, and web/admin lifecycle
This commit is contained in:
parent
2a009e421a
commit
c68e81e0aa
46 changed files with 2553 additions and 294 deletions
|
|
@ -11,7 +11,7 @@ function assertEquals(actual: unknown, expected: unknown, message: string): void
|
|||
}
|
||||
|
||||
const TEST_ENV: Env = {
|
||||
BACKEND_ORIGIN: 'http://192.168.0.39:5050',
|
||||
BACKEND_ORIGIN: 'https://api.d3ro.example',
|
||||
SERVICE_NAME: 'D3RO Voice Cloud Gateway',
|
||||
}
|
||||
|
||||
|
|
@ -19,7 +19,7 @@ Deno.test('resolveBackendTargetUrl blocks protocol-relative SSRF and preserves q
|
|||
assertEquals(resolveBackendTargetUrl(new URL('https://edge.test//evil.com/steal'), TEST_ENV.BACKEND_ORIGIN), null, 'protocol-relative path must fail closed')
|
||||
assertEquals(resolveBackendTargetUrl(new URL('https://edge.test/\\evil.com/steal'), TEST_ENV.BACKEND_ORIGIN), null, 'backslash path must fail closed')
|
||||
const valid = resolveBackendTargetUrl(new URL('https://edge.test/api/stt/internal/transcribe?lang=ko'), TEST_ENV.BACKEND_ORIGIN)
|
||||
assertEquals(valid?.toString(), 'http://192.168.0.39:5050/api/stt/internal/transcribe?lang=ko', 'valid upstream url')
|
||||
assertEquals(valid?.toString(), 'https://api.d3ro.example/api/stt/internal/transcribe?lang=ko', 'valid upstream url')
|
||||
})
|
||||
|
||||
Deno.test('worker health check omits internal BACKEND_ORIGIN', async () => {
|
||||
|
|
@ -58,7 +58,7 @@ Deno.test('proxy strips hop-by-hop/host headers, disables auto-redirect, and att
|
|||
fetchImpl,
|
||||
)
|
||||
|
||||
assertEquals(seenUrl, 'http://192.168.0.39:5050/api/health', 'target url')
|
||||
assertEquals(seenUrl, 'https://api.d3ro.example/api/health', 'target url')
|
||||
assertEquals(seenInit?.redirect, 'manual', 'must not follow redirects automatically')
|
||||
assert(seenInit?.signal instanceof AbortSignal, 'must pass timeout AbortSignal')
|
||||
const forwarded = new Headers(seenInit?.headers)
|
||||
|
|
@ -73,10 +73,11 @@ Deno.test('upstream network errors and timeouts fail closed without leaking orig
|
|||
const networkFail = await handleWorkerRequest(
|
||||
new Request('https://edge.test/api/test'),
|
||||
TEST_ENV,
|
||||
() => Promise.reject(new TypeError('connect ECONNREFUSED 192.168.0.39:5050')),
|
||||
() => Promise.reject(new TypeError('connect ECONNREFUSED api.d3ro.example:443')),
|
||||
)
|
||||
assertEquals(networkFail.status, 502, 'network error maps to 502')
|
||||
const networkText = await networkFail.text()
|
||||
assert(!networkText.includes('api.d3ro.example'), 'must not leak upstream hostname')
|
||||
assert(!networkText.includes('192.168.0.39'), 'must not leak internal IP')
|
||||
assert(!networkText.includes('NAS backend'), 'must not leak internal topology wording')
|
||||
assertEquals(JSON.parse(networkText).error, 'EdgeGatewayError', 'fixed error code')
|
||||
|
|
@ -89,3 +90,84 @@ Deno.test('upstream network errors and timeouts fail closed without leaking orig
|
|||
assertEquals(timeoutFail.status, 504, 'timeout maps to 504')
|
||||
assertEquals((await timeoutFail.json()).error, 'EdgeGatewayTimeout', 'fixed timeout code')
|
||||
})
|
||||
|
||||
Deno.test('resolveBackendTargetUrl_IP리터럴과비HTTPS및루프오리진_차단한다', () => {
|
||||
const invalidOrigins = [
|
||||
'http://192.168.0.39:5050',
|
||||
'https://192.168.0.39:5050',
|
||||
'https://127.0.0.1:5050',
|
||||
'https://169.254.169.254',
|
||||
'https://[::1]:5050',
|
||||
'https://2130706433',
|
||||
'http://d3ro.chanpaca.net',
|
||||
'https://localhost:5050',
|
||||
'https://api.internal',
|
||||
'https://api.local',
|
||||
'https://api.d3ro.example/prefix',
|
||||
'https://api.d3ro.example?leak=1',
|
||||
'https://user:pass@api.d3ro.example',
|
||||
]
|
||||
for (const origin of invalidOrigins) {
|
||||
assertEquals(
|
||||
resolveBackendTargetUrl(new URL('https://edge.test/health'), origin),
|
||||
null,
|
||||
`invalid origin must fail closed: ${origin}`,
|
||||
)
|
||||
}
|
||||
assertEquals(
|
||||
resolveBackendTargetUrl(new URL('https://api.d3ro.example/health'), 'https://api.d3ro.example'),
|
||||
null,
|
||||
'self-proxy origin loop must fail closed',
|
||||
)
|
||||
})
|
||||
|
||||
Deno.test('handleWorkerRequest_오리진미설정IP및프록시루프_요청전송없이실패폐쇄한다', async () => {
|
||||
let fetched = false
|
||||
const neverFetch: typeof fetch = () => {
|
||||
fetched = true
|
||||
return Promise.resolve(new Response('unexpected', { status: 200 }))
|
||||
}
|
||||
|
||||
const badOriginResp = await handleWorkerRequest(
|
||||
new Request('https://d3ro-voice-api.yunchan8804.workers.dev/health'),
|
||||
{ BACKEND_ORIGIN: 'http://192.168.0.39:5050', SERVICE_NAME: 'test' },
|
||||
neverFetch,
|
||||
)
|
||||
assertEquals(fetched, false, 'must not call fetch when BACKEND_ORIGIN is an IP literal')
|
||||
assertEquals(badOriginResp.status, 503, 'misconfigured backend origin maps to 503')
|
||||
const badOriginText = await badOriginResp.text()
|
||||
assert(!badOriginText.includes('192.168.0.39'), 'must not echo configured IP')
|
||||
assertEquals(JSON.parse(badOriginText).error, 'EdgeGatewayMisconfigured', 'fixed misconfiguration code')
|
||||
|
||||
const loopResp = await handleWorkerRequest(
|
||||
new Request('https://edge.test/health', {
|
||||
headers: { 'X-D3RO-Edge-Proxy': 'Cloudflare-Worker' },
|
||||
}),
|
||||
TEST_ENV,
|
||||
neverFetch,
|
||||
)
|
||||
assertEquals(fetched, false, 'must not proxy when edge loop header is already set')
|
||||
assertEquals(loopResp.status, 508, 'loop header maps to 508 Loop Detected')
|
||||
assertEquals((await loopResp.json()).error, 'EdgeGatewayLoopDetected', 'fixed loop error code')
|
||||
})
|
||||
|
||||
Deno.test('handleWorkerRequest_204와304무본문응답_예외없이전달한다', async () => {
|
||||
for (const status of [204, 304]) {
|
||||
const resp = await handleWorkerRequest(
|
||||
new Request('https://edge.test/api/ping'),
|
||||
TEST_ENV,
|
||||
() => Promise.resolve(new Response(null, { status, headers: { 'X-Trace': 'ok' } })),
|
||||
)
|
||||
assertEquals(resp.status, status, `status ${status} preserved`)
|
||||
assertEquals(resp.headers.get('Access-Control-Allow-Origin'), '*', 'CORS header attached')
|
||||
assertEquals(resp.headers.get('X-Trace'), 'ok', 'upstream header preserved')
|
||||
}
|
||||
})
|
||||
|
||||
Deno.test('wranglerConfig_BACKENDORIGIN_IP리터럴이아닌공개HTTPS호스트다', async () => {
|
||||
const tomlText = await Deno.readTextFile(new URL('../wrangler.toml', import.meta.url))
|
||||
const match = tomlText.match(/^\s*BACKEND_ORIGIN\s*=\s*"([^"]+)"/m)
|
||||
assert(match !== null, 'BACKEND_ORIGIN must be configured in wrangler.toml')
|
||||
const resolved = resolveBackendTargetUrl(new URL('https://d3ro-voice-api.yunchan8804.workers.dev/health'), match[1])
|
||||
assertEquals(resolved?.toString(), 'https://d3ro.chanpaca.net/health', 'wrangler BACKEND_ORIGIN must resolve cleanly')
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// server/cloudflare-worker/src/index.ts
|
||||
// D3RO Voice — Global Cloudflare Edge Gateway & Proxy Worker
|
||||
|
||||
import { hasSmuggledRequestPath, parsePublicHttpsOrigin } from './origin-policy.ts'
|
||||
import { drainPushDispatches } from './push-drain.ts'
|
||||
|
||||
export interface Env {
|
||||
|
|
@ -60,17 +61,14 @@ function workerJson(body: Record<string, unknown>, status = 200, noStore = false
|
|||
return new Response(JSON.stringify(body), { status, headers })
|
||||
}
|
||||
|
||||
const NULL_BODY_STATUSES = new Set([204, 205, 304])
|
||||
|
||||
export function resolveBackendTargetUrl(requestUrl: URL, rawOrigin: string): URL | null {
|
||||
if (requestUrl.pathname.startsWith('//') || requestUrl.pathname.includes('\\')) {
|
||||
if (hasSmuggledRequestPath(requestUrl.pathname)) {
|
||||
return null
|
||||
}
|
||||
let originUrl: URL
|
||||
try {
|
||||
originUrl = new URL(rawOrigin)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if ((originUrl.protocol !== 'http:' && originUrl.protocol !== 'https:') || originUrl.username || originUrl.password) {
|
||||
const originUrl = parsePublicHttpsOrigin(rawOrigin)
|
||||
if (!originUrl || originUrl.origin === requestUrl.origin) {
|
||||
return null
|
||||
}
|
||||
const targetUrl = new URL(originUrl.origin)
|
||||
|
|
@ -107,14 +105,36 @@ export async function handleWorkerRequest(
|
|||
})
|
||||
}
|
||||
|
||||
// 3. Construct Target Backend URL (fail closed on path/origin smuggling)
|
||||
// 3. Reject proxy loops and verify backend origin configuration (prevents CF 1003 direct-IP errors)
|
||||
if (request.headers.get('X-D3RO-Edge-Proxy')) {
|
||||
return workerJson(
|
||||
{
|
||||
error: 'EdgeGatewayLoopDetected',
|
||||
message: 'Proxy loop detected.',
|
||||
},
|
||||
508,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
const rawOrigin = (env.BACKEND_ORIGIN || 'https://d3ro.chanpaca.net').trim()
|
||||
if (!parsePublicHttpsOrigin(rawOrigin)) {
|
||||
return workerJson(
|
||||
{
|
||||
error: 'EdgeGatewayMisconfigured',
|
||||
message: 'Upstream gateway origin is invalid or not configured.',
|
||||
},
|
||||
503,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
const targetUrl = resolveBackendTargetUrl(url, rawOrigin)
|
||||
if (!targetUrl) {
|
||||
return workerJson(
|
||||
{
|
||||
error: 'InvalidGatewayRequest',
|
||||
message: 'Invalid request path or backend origin configuration.',
|
||||
message: 'Invalid request path or proxy target.',
|
||||
},
|
||||
400,
|
||||
true,
|
||||
|
|
@ -144,7 +164,13 @@ export async function handleWorkerRequest(
|
|||
newHeaders.set(key, value)
|
||||
}
|
||||
|
||||
return new Response(response.body, {
|
||||
let responseBody: ReadableStream<Uint8Array> | null = response.body
|
||||
if (NULL_BODY_STATUSES.has(response.status)) {
|
||||
await response.body?.cancel().catch(() => undefined)
|
||||
responseBody = null
|
||||
}
|
||||
|
||||
return new Response(responseBody, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: newHeaders,
|
||||
|
|
|
|||
45
server/cloudflare-worker/src/origin-policy.ts
Normal file
45
server/cloudflare-worker/src/origin-policy.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// server/cloudflare-worker/src/origin-policy.ts
|
||||
// Shared upstream origin validation helpers for the Cloudflare Edge Worker.
|
||||
|
||||
const IPV4_PATTERN = /^\d{1,3}(?:\.\d{1,3}){3}$/
|
||||
const INTEGER_OR_HEX_HOST_PATTERN = /^(?:0x[\da-f]+|\d+)$/i
|
||||
const BLOCKED_HOST_SUFFIXES = ['.localhost', '.local', '.internal', '.home.arpa', '.localdomain']
|
||||
|
||||
export function hasSmuggledRequestPath(pathname: string): boolean {
|
||||
return pathname.startsWith('//') || pathname.includes('\\')
|
||||
}
|
||||
|
||||
export function isDisallowedPublicHost(rawHostname: string): boolean {
|
||||
const trimmed = rawHostname.trim().toLowerCase()
|
||||
if (!trimmed || trimmed.endsWith('.')) return true
|
||||
if (trimmed === 'localhost') return true
|
||||
if (BLOCKED_HOST_SUFFIXES.some((suffix) => trimmed.endsWith(suffix))) return true
|
||||
if (trimmed.startsWith('[') || trimmed.includes(':')) return true
|
||||
if (IPV4_PATTERN.test(trimmed) || INTEGER_OR_HEX_HOST_PATTERN.test(trimmed)) return true
|
||||
if (!trimmed.includes('.')) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that a configured upstream origin is strictly a clean `https://` public hostname
|
||||
* with no credentials, path prefix, query string, fragment, or IP/internal literal.
|
||||
* Prevents Cloudflare Error 1003 ("Direct IP access not allowed") and internal SSRF.
|
||||
*/
|
||||
export function parsePublicHttpsOrigin(rawOrigin: string): URL | null {
|
||||
let originUrl: URL
|
||||
try {
|
||||
originUrl = new URL(rawOrigin.trim())
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (originUrl.protocol !== 'https:' || originUrl.username || originUrl.password) {
|
||||
return null
|
||||
}
|
||||
if (originUrl.pathname !== '/' || originUrl.search !== '' || originUrl.hash !== '') {
|
||||
return null
|
||||
}
|
||||
if (isDisallowedPublicHost(originUrl.hostname)) {
|
||||
return null
|
||||
}
|
||||
return originUrl
|
||||
}
|
||||
|
|
@ -67,3 +67,39 @@ Deno.test('drain tolerates a malformed success body', async () => {
|
|||
assert(result.ok === true, 'ok')
|
||||
assert(result.claimed === undefined, 'summary left undefined')
|
||||
})
|
||||
|
||||
Deno.test('drainPushDispatches_잘못된SupabaseURL또는내부호스트_요청전송없이실패폐쇄한다', async () => {
|
||||
let called = false
|
||||
const neverFetch = (() => {
|
||||
called = true
|
||||
return Promise.resolve(jsonResponse(200, {}))
|
||||
}) as unknown as typeof fetch
|
||||
|
||||
for (const badUrl of [
|
||||
'http://example.supabase.co',
|
||||
'https://127.0.0.1',
|
||||
'https://192.168.0.1',
|
||||
'https://[::1]',
|
||||
'https://localhost',
|
||||
'https://supabase.internal',
|
||||
'https://user:pass@example.supabase.co',
|
||||
'https://example.supabase.co/unexpected-path',
|
||||
]) {
|
||||
const res = await drainPushDispatches({ ...env, SUPABASE_URL: badUrl }, neverFetch)
|
||||
assert(called === false, `fetch must not run for invalid SUPABASE_URL: ${badUrl}`)
|
||||
assert(res.ok === false && res.status === 503, `expected fail-closed 503 for ${badUrl}`)
|
||||
}
|
||||
})
|
||||
|
||||
Deno.test('drainPushDispatches_안전한요청옵션_리다이렉트차단과타임아웃시그널을전달한다', async () => {
|
||||
let seenInit: RequestInit | undefined
|
||||
const fetchImpl = ((_input: string | URL | Request, init?: RequestInit) => {
|
||||
seenInit = init
|
||||
return Promise.resolve(jsonResponse(200, { claimed: 0, completed: 0, pending: 0 }))
|
||||
}) as unknown as typeof fetch
|
||||
|
||||
const res = await drainPushDispatches(env, fetchImpl)
|
||||
assert(res.ok === true, 'ok')
|
||||
assert(seenInit?.redirect === 'manual', 'redirect must be manual')
|
||||
assert(seenInit?.signal instanceof AbortSignal, 'timeout AbortSignal must be attached')
|
||||
})
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
// (team invites, transcription completion, billing) never left the queue. This
|
||||
// worker drains them on a cron schedule.
|
||||
|
||||
import { parsePublicHttpsOrigin } from './origin-policy.ts'
|
||||
|
||||
export interface PushDrainEnv {
|
||||
SUPABASE_URL?: string
|
||||
SUPABASE_SERVICE_ROLE_KEY?: string
|
||||
|
|
@ -35,17 +37,21 @@ export async function drainPushDispatches(
|
|||
env: PushDrainEnv,
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
): Promise<PushDrainResult> {
|
||||
const baseUrl = env.SUPABASE_URL?.trim().replace(/\/+$/, '') ?? ''
|
||||
const rawUrl = env.SUPABASE_URL?.trim().replace(/\/+$/, '') ?? ''
|
||||
const serviceRoleKey = env.SUPABASE_SERVICE_ROLE_KEY?.trim() ?? ''
|
||||
if (!baseUrl || serviceRoleKey.length < 20) {
|
||||
if (!rawUrl || serviceRoleKey.length < 20) {
|
||||
return { ok: false, status: 503, error: 'push_drain_not_configured' }
|
||||
}
|
||||
const parsedBaseUrl = parsePublicHttpsOrigin(rawUrl)
|
||||
if (!parsedBaseUrl) {
|
||||
return { ok: false, status: 503, error: 'push_drain_invalid_config' }
|
||||
}
|
||||
|
||||
const limit = resolveBatchLimit(env.PUSH_DRAIN_BATCH_LIMIT)
|
||||
let response: Response
|
||||
try {
|
||||
response = await fetchImpl(
|
||||
`${baseUrl}/functions/v1/send-push?mode=drain&limit=${limit}`,
|
||||
`${parsedBaseUrl.origin}/functions/v1/send-push?mode=drain&limit=${limit}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
|
|
@ -53,6 +59,7 @@ export async function drainPushDispatches(
|
|||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: '{}',
|
||||
redirect: 'manual',
|
||||
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ main = "src/index.ts"
|
|||
compatibility_date = "2024-04-01"
|
||||
|
||||
[vars]
|
||||
BACKEND_ORIGIN = "http://192.168.0.39:5050"
|
||||
BACKEND_ORIGIN = "https://d3ro.chanpaca.net"
|
||||
SERVICE_NAME = "D3RO Voice Cloud Gateway"
|
||||
# Supabase project base URL for the scheduled push-outbox drain.
|
||||
# SUPABASE_URL = "https://<project-ref>.supabase.co"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue