feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes

This commit is contained in:
Yun Chan 2026-11-14 11:51:41 +09:00
parent 01ef3293cf
commit 66b6b45ba9
399 changed files with 17828 additions and 11330 deletions

View file

@ -0,0 +1,91 @@
import { type Env, handleWorkerRequest, resolveBackendTargetUrl } from './index.ts'
function assert(condition: boolean, message: string): asserts condition {
if (!condition) throw new Error(message)
}
function assertEquals(actual: unknown, expected: unknown, message: string): void {
const a = JSON.stringify(actual)
const e = JSON.stringify(expected)
if (a !== e) throw new Error(`${message}: expected ${e}, got ${a}`)
}
const TEST_ENV: Env = {
BACKEND_ORIGIN: 'http://192.168.0.39:5050',
SERVICE_NAME: 'D3RO Voice Cloud Gateway',
}
Deno.test('resolveBackendTargetUrl blocks protocol-relative SSRF and preserves query/path', () => {
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')
})
Deno.test('worker health check omits internal BACKEND_ORIGIN', async () => {
const resp = await handleWorkerRequest(new Request('https://edge.test/worker-health'), TEST_ENV)
assertEquals(resp.status, 200, 'status')
const body = await resp.json() as Record<string, unknown>
assertEquals(body.status, 'Healthy', 'health status')
assertEquals('backendOrigin' in body, false, 'must not leak internal backend origin')
})
Deno.test('proxy strips hop-by-hop/host headers, disables auto-redirect, and attaches AbortSignal timeout', async () => {
let seenInit: RequestInit | undefined
let seenUrl = ''
const fetchImpl: typeof fetch = (input, init) => {
seenUrl = String(input)
seenInit = init
return Promise.resolve(new Response('{"ok":true}', {
status: 200,
headers: {
'Content-Type': 'application/json',
Connection: 'x-custom-hop',
'x-custom-hop': 'strip-me',
},
}))
}
const resp = await handleWorkerRequest(
new Request('https://edge.test/api/health', {
headers: {
Host: 'edge.test',
Connection: 'keep-alive',
Authorization: 'Bearer test-token',
},
}),
TEST_ENV,
fetchImpl,
)
assertEquals(seenUrl, 'http://192.168.0.39:5050/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)
assertEquals(forwarded.has('host'), false, 'host must be stripped')
assertEquals(forwarded.has('connection'), false, 'connection must be stripped')
assertEquals(forwarded.get('authorization'), 'Bearer test-token', 'auth preserved')
assertEquals(resp.headers.has('x-custom-hop'), false, 'hop-by-hop response header stripped')
await resp.body?.cancel()
})
Deno.test('upstream network errors and timeouts fail closed without leaking origin details', async () => {
const networkFail = await handleWorkerRequest(
new Request('https://edge.test/api/test'),
TEST_ENV,
() => Promise.reject(new TypeError('connect ECONNREFUSED 192.168.0.39:5050')),
)
assertEquals(networkFail.status, 502, 'network error maps to 502')
const networkText = await networkFail.text()
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')
const timeoutFail = await handleWorkerRequest(
new Request('https://edge.test/api/test'),
TEST_ENV,
() => Promise.reject(new DOMException('timed out', 'TimeoutError')),
)
assertEquals(timeoutFail.status, 504, 'timeout maps to 504')
assertEquals((await timeoutFail.json()).error, 'EdgeGatewayTimeout', 'fixed timeout code')
})

View file

@ -14,6 +14,8 @@ export interface Env {
PUSH_DRAIN_BATCH_LIMIT?: string
}
const UPSTREAM_TIMEOUT_MS = 30_000
const CORS_HEADERS: Record<string, string> = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
@ -22,102 +24,167 @@ const CORS_HEADERS: Record<string, string> = {
'Access-Control-Max-Age': '86400',
}
const HOP_BY_HOP_HEADERS = new Set([
'connection',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'te',
'trailer',
'transfer-encoding',
'upgrade',
'host',
])
type WorkerRequest = Request & { cf?: { colo?: string } }
function stripHopByHopHeaders(headers: Headers): void {
const connectionValue = headers.get('connection')
if (connectionValue) {
for (const token of connectionValue.split(',')) {
const name = token.trim().toLowerCase()
if (name) headers.delete(name)
}
}
for (const name of HOP_BY_HOP_HEADERS) {
headers.delete(name)
}
}
function workerJson(body: Record<string, unknown>, status = 200, noStore = false): Response {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
...CORS_HEADERS,
}
if (noStore) headers['Cache-Control'] = 'no-store'
return new Response(JSON.stringify(body), { status, headers })
}
export function resolveBackendTargetUrl(requestUrl: URL, rawOrigin: string): URL | null {
if (requestUrl.pathname.startsWith('//') || requestUrl.pathname.includes('\\')) {
return null
}
let originUrl: URL
try {
originUrl = new URL(rawOrigin)
} catch {
return null
}
if ((originUrl.protocol !== 'http:' && originUrl.protocol !== 'https:') || originUrl.username || originUrl.password) {
return null
}
const targetUrl = new URL(originUrl.origin)
targetUrl.pathname = requestUrl.pathname
targetUrl.search = requestUrl.search
if (targetUrl.origin !== originUrl.origin) {
return null
}
return targetUrl
}
export async function handleWorkerRequest(
request: Request,
env: Env,
fetchImpl: typeof fetch = fetch,
): Promise<Response> {
const url = new URL(request.url)
// 1. Handle CORS Preflight (OPTIONS)
if (request.method === 'OPTIONS') {
return new Response(null, {
status: 204,
headers: CORS_HEADERS,
})
}
// 2. Edge Health Check Ping (/worker-health)
if (url.pathname === '/worker-health') {
return workerJson({
status: 'Healthy',
worker: env.SERVICE_NAME ?? 'D3RO Voice Edge Worker',
timestamp: new Date().toISOString(),
region: (request as WorkerRequest).cf?.colo ?? 'global',
})
}
// 3. Construct Target Backend URL (fail closed on path/origin smuggling)
const rawOrigin = (env.BACKEND_ORIGIN || 'https://d3ro.chanpaca.net').trim()
const targetUrl = resolveBackendTargetUrl(url, rawOrigin)
if (!targetUrl) {
return workerJson(
{
error: 'InvalidGatewayRequest',
message: 'Invalid request path or backend origin configuration.',
},
400,
true,
)
}
// Forward Headers without hop-by-hop smuggling or mismatched Host
const forwardHeaders = new Headers(request.headers)
stripHopByHopHeaders(forwardHeaders)
forwardHeaders.set('X-Forwarded-Host', url.host)
forwardHeaders.set('X-Forwarded-Proto', url.protocol.replace(':', ''))
forwardHeaders.set('X-D3RO-Edge-Proxy', 'Cloudflare-Worker')
try {
const response = await fetchImpl(targetUrl.toString(), {
method: request.method,
headers: forwardHeaders,
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined,
redirect: 'manual',
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
})
// Clone response headers, strip hop-by-hop, and attach CORS
const newHeaders = new Headers(response.headers)
stripHopByHopHeaders(newHeaders)
for (const [key, value] of Object.entries(CORS_HEADERS)) {
newHeaders.set(key, value)
}
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
})
} catch (err: unknown) {
const isTimeout = err instanceof DOMException && err.name === 'TimeoutError'
return workerJson(
{
error: isTimeout ? 'EdgeGatewayTimeout' : 'EdgeGatewayError',
message: isTimeout ? 'Upstream request timed out.' : 'Upstream service unavailable.',
timestamp: new Date().toISOString(),
},
isTimeout ? 504 : 502,
true,
)
}
}
export default {
async fetch(request: Request, env: Env, _ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url)
// 1. Handle CORS Preflight (OPTIONS)
if (request.method === 'OPTIONS') {
return new Response(null, {
status: 204,
headers: CORS_HEADERS,
})
}
// 2. Edge Health Check Ping (/worker-health)
if (url.pathname === '/worker-health') {
return new Response(
JSON.stringify({
status: 'Healthy',
worker: env.SERVICE_NAME ?? 'D3RO Voice Edge Worker',
backendOrigin: env.BACKEND_ORIGIN,
timestamp: new Date().toISOString(),
region: request.cf?.colo ?? 'global',
}),
{
headers: {
'Content-Type': 'application/json',
...CORS_HEADERS,
},
}
)
}
// 3. Construct Target Backend URL
const backendOrigin = (env.BACKEND_ORIGIN || 'https://d3ro.chanpaca.net').replace(/\/$/, '')
const targetUrl = new URL(`${url.pathname}${url.search}`, backendOrigin)
// Forward Headers
const forwardHeaders = new Headers(request.headers)
forwardHeaders.set('X-Forwarded-Host', url.host)
forwardHeaders.set('X-Forwarded-Proto', url.protocol.replace(':', ''))
forwardHeaders.set('X-D3RO-Edge-Proxy', 'Cloudflare-Worker')
try {
const response = await fetch(targetUrl.toString(), {
method: request.method,
headers: forwardHeaders,
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined,
redirect: 'follow',
})
// Clone response headers and attach CORS
const newHeaders = new Headers(response.headers)
for (const [key, value] of Object.entries(CORS_HEADERS)) {
newHeaders.set(key, value)
}
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
})
} catch (err: unknown) {
const errorMessage = err instanceof Error ? err.message : String(err)
return new Response(
JSON.stringify({
error: 'EdgeGatewayError',
message: `Unable to connect to origin NAS backend: ${errorMessage}`,
targetUrl: targetUrl.toString(),
timestamp: new Date().toISOString(),
}),
{
status: 502,
headers: {
'Content-Type': 'application/json',
...CORS_HEADERS,
},
}
)
}
fetch(request: Request, env: Env, _ctx: { waitUntil(promise: Promise<unknown>): void }): Promise<Response> {
return handleWorkerRequest(request, env)
},
// Cron trigger: drain the Supabase push outbox. Nothing else in the
// repository scheduled this, so transactionally enqueued notifications
// (team invites, transcription completion, billing) never left the queue.
async scheduled(
scheduled(
_event: unknown,
env: Env,
ctx: { waitUntil(promise: Promise<unknown>): void },
): Promise<void> {
ctx.waitUntil(drainAndLog(env))
return Promise.resolve()
},
}
async function drainAndLog(env: Env): Promise<void> {
const result = await drainPushDispatches(env)
if (result.ok) {
console.log(
console.info(
`[push-drain] claimed=${result.claimed ?? 0} completed=${result.completed ?? 0} pending=${result.pending ?? 0}`,
)
} else {

View file

@ -61,6 +61,7 @@ export async function drainPushDispatches(
}
if (!response.ok) {
await response.body?.cancel().catch(() => undefined)
return { ok: false, status: response.status, error: `push_drain_http_${response.status}` }
}