feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
91
server/cloudflare-worker/src/index.test.ts
Normal file
91
server/cloudflare-worker/src/index.test.ts
Normal 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')
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue