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
6
server/cloudflare-site-bridge/deno.json
Normal file
6
server/cloudflare-site-bridge/deno.json
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"strict": true
|
||||
},
|
||||
"unstable": ["sloppy-imports"]
|
||||
}
|
||||
127
server/cloudflare-site-bridge/src/index.test.ts
Normal file
127
server/cloudflare-site-bridge/src/index.test.ts
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
import { handleBridgeRequest, resolvePagesTargetUrl, servedByOrigin } 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}`)
|
||||
}
|
||||
|
||||
Deno.test('servedByOrigin_웹앱API경로와랜딩경로_라우팅분기를정확히구분한다', () => {
|
||||
assert(servedByOrigin('/app'), '/app goes to origin')
|
||||
assert(servedByOrigin('/app/login'), '/app/login goes to origin')
|
||||
assert(servedByOrigin('/api/health'), '/api/health goes to origin')
|
||||
assert(servedByOrigin('/health'), '/health goes to origin')
|
||||
assert(!servedByOrigin('/'), '/ goes to pages')
|
||||
assert(!servedByOrigin('/ko/'), '/ko/ goes to pages')
|
||||
assert(!servedByOrigin('/app-info'), '/app-info goes to pages')
|
||||
})
|
||||
|
||||
Deno.test('resolvePagesTargetUrl_경로스머글링_null로차단한다', () => {
|
||||
assertEquals(resolvePagesTargetUrl(new URL('https://d3ro.chanpaca.net//evil.com/path')), null, 'double slash blocked')
|
||||
assertEquals(resolvePagesTargetUrl(new URL('https://d3ro.chanpaca.net/\\evil.com')), null, 'backslash blocked')
|
||||
const valid = resolvePagesTargetUrl(new URL('https://d3ro.chanpaca.net/ko/?utm=1'))
|
||||
assertEquals(valid?.toString(), 'https://d3ro.pages.dev/ko/?utm=1', 'valid pages target')
|
||||
})
|
||||
|
||||
Deno.test('handleBridgeRequest_경로스머글링및루프헤더_요청전달없이실패폐쇄한다', async () => {
|
||||
let called = false
|
||||
const neverFetch: typeof fetch = () => {
|
||||
called = true
|
||||
return Promise.resolve(new Response('unexpected', { status: 200 }))
|
||||
}
|
||||
|
||||
const smuggled = await handleBridgeRequest(new Request('https://d3ro.chanpaca.net//evil.com'), neverFetch)
|
||||
assertEquals(called, false, 'smuggled request must not trigger fetch')
|
||||
assertEquals(smuggled.status, 400, 'smuggled path returns 400')
|
||||
|
||||
const looped = await handleBridgeRequest(
|
||||
new Request('https://d3ro.chanpaca.net/ko/', {
|
||||
headers: { 'X-D3RO-Site-Bridge': '1' },
|
||||
}),
|
||||
neverFetch,
|
||||
)
|
||||
assertEquals(called, false, 'looped request must not trigger fetch')
|
||||
assertEquals(looped.status, 508, 'loop header returns 508')
|
||||
})
|
||||
|
||||
Deno.test('handleBridgeRequest_Pages및원본프록시_홉바이홉헤더제거및타임아웃전달한다', async () => {
|
||||
const seen: Array<{ url: string; init?: RequestInit }> = []
|
||||
const fakeFetch: typeof fetch = (input, init) => {
|
||||
const url = input instanceof Request ? input.url : String(input)
|
||||
const rawHeaders = input instanceof Request
|
||||
? input.headers
|
||||
: (init as { headers?: HeadersInit } | undefined)?.headers
|
||||
const headers = new Headers(rawHeaders)
|
||||
seen.push({
|
||||
url,
|
||||
init: {
|
||||
...(init as RequestInit | undefined),
|
||||
headers,
|
||||
},
|
||||
})
|
||||
return Promise.resolve(
|
||||
new Response('<html>ok</html>', {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'text/html',
|
||||
Connection: 'x-hop-secret',
|
||||
'x-hop-secret': 'drop',
|
||||
},
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const pagesResp = await handleBridgeRequest(
|
||||
new Request('https://d3ro.chanpaca.net/en/', {
|
||||
headers: {
|
||||
Host: 'd3ro.chanpaca.net',
|
||||
Connection: 'keep-alive',
|
||||
Accept: 'text/html',
|
||||
},
|
||||
}),
|
||||
fakeFetch,
|
||||
)
|
||||
assertEquals(pagesResp.status, 200, 'pages status')
|
||||
assertEquals(pagesResp.headers.has('x-hop-secret'), false, 'response hop-by-hop stripped')
|
||||
assertEquals(seen[0].url, 'https://d3ro.pages.dev/en/', 'proxied to pages target')
|
||||
assertEquals(seen[0].init?.redirect, 'manual', 'manual redirect')
|
||||
assert(seen[0].init?.signal instanceof AbortSignal, 'timeout signal attached')
|
||||
const sentHeaders = new Headers(seen[0].init?.headers)
|
||||
assertEquals(sentHeaders.has('host'), false, 'host header stripped')
|
||||
assertEquals(sentHeaders.has('connection'), false, 'connection header stripped')
|
||||
assertEquals(sentHeaders.get('x-d3ro-site-bridge'), '1', 'bridge marker set')
|
||||
await pagesResp.body?.cancel()
|
||||
|
||||
const originResp = await handleBridgeRequest(new Request('https://d3ro.chanpaca.net/app/login'), fakeFetch)
|
||||
assertEquals(originResp.status, 200, 'origin status')
|
||||
assertEquals(seen[1].url, 'https://d3ro.chanpaca.net/app/login', 'origin passthrough url')
|
||||
assertEquals(seen[1].init?.redirect, 'manual', 'origin request disables auto-redirect')
|
||||
assert(seen[1].init?.signal instanceof AbortSignal, 'origin request has timeout signal')
|
||||
await originResp.body?.cancel()
|
||||
})
|
||||
|
||||
Deno.test('handleBridgeRequest_업스트림네트워크오류와타임아웃_502및504로실패폐쇄하고무본문응답을처리한다', async () => {
|
||||
const errResp = await handleBridgeRequest(
|
||||
new Request('https://d3ro.chanpaca.net/'),
|
||||
() => Promise.reject(new TypeError('connect failed to internal host')),
|
||||
)
|
||||
assertEquals(errResp.status, 502, 'network error maps to 502')
|
||||
const errText = await errResp.text()
|
||||
assert(!errText.includes('internal host'), 'must not leak error message')
|
||||
|
||||
const timeoutResp = await handleBridgeRequest(
|
||||
new Request('https://d3ro.chanpaca.net/'),
|
||||
() => Promise.reject(new DOMException('timed out', 'TimeoutError')),
|
||||
)
|
||||
assertEquals(timeoutResp.status, 504, 'timeout maps to 504')
|
||||
|
||||
const noContent = await handleBridgeRequest(
|
||||
new Request('https://d3ro.chanpaca.net/favicon.ico'),
|
||||
() => Promise.resolve(new Response(null, { status: 304, headers: { ETag: '"1"' } })),
|
||||
)
|
||||
assertEquals(noContent.status, 304, '304 status preserved without null-body error')
|
||||
})
|
||||
|
|
@ -13,34 +13,137 @@
|
|||
import { API_PATH_PREFIXES, WEB_APP_BASE_PATH } from '../../../packages/core/src/web-urls'
|
||||
|
||||
const PAGES_ORIGIN = 'https://d3ro.pages.dev'
|
||||
const UPSTREAM_TIMEOUT_MS = 30_000
|
||||
const BRIDGE_HEADER = 'X-D3RO-Site-Bridge'
|
||||
const NULL_BODY_STATUSES = new Set([204, 205, 304])
|
||||
const ORIGIN_PATH_PREFIXES: readonly string[] = [WEB_APP_BASE_PATH, ...API_PATH_PREFIXES]
|
||||
const HOP_BY_HOP_HEADERS = new Set([
|
||||
'connection',
|
||||
'keep-alive',
|
||||
'proxy-authenticate',
|
||||
'proxy-authorization',
|
||||
'te',
|
||||
'trailer',
|
||||
'transfer-encoding',
|
||||
'upgrade',
|
||||
'host',
|
||||
])
|
||||
|
||||
function servedByOrigin(pathname: string): boolean {
|
||||
return ORIGIN_PATH_PREFIXES.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`))
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
async function fromPages(request: Request): Promise<Response> {
|
||||
const target = new URL(request.url)
|
||||
target.protocol = 'https:'
|
||||
target.hostname = new URL(PAGES_ORIGIN).hostname
|
||||
target.port = ''
|
||||
|
||||
const headers = new Headers(request.headers)
|
||||
headers.delete('host')
|
||||
|
||||
const hasBody = request.method !== 'GET' && request.method !== 'HEAD'
|
||||
return fetch(target.toString(), {
|
||||
method: request.method,
|
||||
headers,
|
||||
body: hasBody ? request.body : undefined,
|
||||
redirect: 'manual',
|
||||
function bridgeErrorJson(error: string, message: string, status: number): Response {
|
||||
return new Response(JSON.stringify({ error, message }), {
|
||||
status,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function servedByOrigin(pathname: string): boolean {
|
||||
return ORIGIN_PATH_PREFIXES.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`))
|
||||
}
|
||||
|
||||
export function resolvePagesTargetUrl(requestUrl: URL): URL | null {
|
||||
if (requestUrl.pathname.startsWith('//') || requestUrl.pathname.includes('\\')) {
|
||||
return null
|
||||
}
|
||||
const target = new URL(requestUrl.toString())
|
||||
target.protocol = 'https:'
|
||||
target.hostname = new URL(PAGES_ORIGIN).hostname
|
||||
target.port = ''
|
||||
if (target.origin !== PAGES_ORIGIN) {
|
||||
return null
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
function buildForwardHeaders(requestHeaders: Headers): Headers {
|
||||
const headers = new Headers(requestHeaders)
|
||||
stripHopByHopHeaders(headers)
|
||||
headers.set(BRIDGE_HEADER, '1')
|
||||
return headers
|
||||
}
|
||||
|
||||
async function forwardRequest(
|
||||
request: Request,
|
||||
targetUrl: string,
|
||||
fetchImpl: typeof fetch,
|
||||
): Promise<Response> {
|
||||
const headers = buildForwardHeaders(request.headers)
|
||||
const hasBody = request.method !== 'GET' && request.method !== 'HEAD'
|
||||
|
||||
try {
|
||||
const response = await fetchImpl(targetUrl, {
|
||||
method: request.method,
|
||||
headers,
|
||||
body: hasBody ? request.body : undefined,
|
||||
redirect: 'manual',
|
||||
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
|
||||
})
|
||||
|
||||
const responseHeaders = new Headers(response.headers)
|
||||
stripHopByHopHeaders(responseHeaders)
|
||||
|
||||
let body: ReadableStream<Uint8Array> | null = response.body
|
||||
if (NULL_BODY_STATUSES.has(response.status)) {
|
||||
await response.body?.cancel().catch(() => undefined)
|
||||
body = null
|
||||
}
|
||||
|
||||
return new Response(body, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: responseHeaders,
|
||||
})
|
||||
} catch (err: unknown) {
|
||||
const isTimeout = err instanceof DOMException && err.name === 'TimeoutError'
|
||||
return bridgeErrorJson(
|
||||
isTimeout ? 'SiteBridgeTimeout' : 'SiteBridgeError',
|
||||
isTimeout ? 'Upstream request timed out.' : 'Upstream service unavailable.',
|
||||
isTimeout ? 504 : 502,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleBridgeRequest(
|
||||
request: Request,
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
): Promise<Response> {
|
||||
if (request.headers.get(BRIDGE_HEADER)) {
|
||||
return bridgeErrorJson('SiteBridgeLoopDetected', 'Bridge proxy loop detected.', 508)
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
if (url.pathname.startsWith('//') || url.pathname.includes('\\')) {
|
||||
return bridgeErrorJson('InvalidBridgeRequest', 'Invalid request path.', 400)
|
||||
}
|
||||
|
||||
if (servedByOrigin(url.pathname)) {
|
||||
return forwardRequest(request, request.url, fetchImpl)
|
||||
}
|
||||
|
||||
const pagesTarget = resolvePagesTargetUrl(url)
|
||||
if (!pagesTarget) {
|
||||
return bridgeErrorJson('InvalidBridgeRequest', 'Invalid target URL.', 400)
|
||||
}
|
||||
return forwardRequest(request, pagesTarget.toString(), fetchImpl)
|
||||
}
|
||||
|
||||
export default {
|
||||
async fetch(request: Request): Promise<Response> {
|
||||
const { pathname } = new URL(request.url)
|
||||
if (servedByOrigin(pathname)) return fetch(request)
|
||||
return fromPages(request)
|
||||
fetch(request: Request): Promise<Response> {
|
||||
return handleBridgeRequest(request)
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -9,11 +9,13 @@ function assert(condition: boolean, message: string): asserts condition {
|
|||
type Row = Record<string, unknown>
|
||||
type ServiceClient = ReturnType<typeof createClient>
|
||||
|
||||
type QueryError = { message: string; code?: string } | null
|
||||
|
||||
/** Minimal in-memory stand-in for the supabase-js query builder used by quota.ts. */
|
||||
class FakeQuery implements PromiseLike<{ data: unknown; error: null }> {
|
||||
class FakeQuery implements PromiseLike<{ data: unknown; error: QueryError }> {
|
||||
private filters: Array<(row: Row) => boolean> = []
|
||||
private singleRow = false
|
||||
constructor(private rows: Row[]) {}
|
||||
constructor(private rows: Row[], private error: QueryError = null) {}
|
||||
select(_columns: string): this {
|
||||
return this
|
||||
}
|
||||
|
|
@ -33,10 +35,13 @@ class FakeQuery implements PromiseLike<{ data: unknown; error: null }> {
|
|||
this.singleRow = true
|
||||
return this
|
||||
}
|
||||
then<T1 = { data: unknown; error: null }, T2 = never>(
|
||||
onfulfilled?: ((value: { data: unknown; error: null }) => T1 | PromiseLike<T1>) | null,
|
||||
then<T1 = { data: unknown; error: QueryError }, T2 = never>(
|
||||
onfulfilled?: ((value: { data: unknown; error: QueryError }) => T1 | PromiseLike<T1>) | null,
|
||||
onrejected?: ((reason: unknown) => T2 | PromiseLike<T2>) | null,
|
||||
): PromiseLike<T1 | T2> {
|
||||
if (this.error) {
|
||||
return Promise.resolve({ data: null, error: this.error }).then(onfulfilled, onrejected)
|
||||
}
|
||||
const matched = this.rows.filter((row) => this.filters.every((f) => f(row)))
|
||||
const data = this.singleRow ? (matched[0] ?? null) : matched
|
||||
return Promise.resolve({ data, error: null }).then(onfulfilled, onrejected)
|
||||
|
|
@ -45,6 +50,7 @@ class FakeQuery implements PromiseLike<{ data: unknown; error: null }> {
|
|||
|
||||
class FakeQuotaClient {
|
||||
tables: Record<string, Row[]> = { subscriptions: [], daily_usage: [] }
|
||||
tableErrors: Partial<Record<'subscriptions' | 'daily_usage', QueryError>> = {}
|
||||
rpcCalls: Array<{ name: string; args: Record<string, unknown> }> = []
|
||||
rpcResult: unknown = {
|
||||
allowed: true,
|
||||
|
|
@ -54,7 +60,10 @@ class FakeQuotaClient {
|
|||
consumed_from: 'overage',
|
||||
}
|
||||
from(table: string): FakeQuery {
|
||||
return new FakeQuery(this.tables[table] ?? [])
|
||||
const error = table === 'subscriptions' || table === 'daily_usage'
|
||||
? (this.tableErrors[table] ?? null)
|
||||
: null
|
||||
return new FakeQuery(this.tables[table] ?? [], error)
|
||||
}
|
||||
rpc(name: string, args: Record<string, unknown>): Promise<{ data: unknown; error: null }> {
|
||||
this.rpcCalls.push({ name, args })
|
||||
|
|
@ -149,3 +158,30 @@ Deno.test('RPC response parser rejects malformed payloads', () => {
|
|||
assert(threw, `accepted ${JSON.stringify(bad)}`)
|
||||
}
|
||||
})
|
||||
|
||||
Deno.test('checkQuota_사용량및구독DB오류_0으로허용하지않고예외로실패폐쇄한다', async () => {
|
||||
const usageFailClient = freeUserWithUsage([])
|
||||
usageFailClient.tableErrors.daily_usage = { message: 'connection refused', code: '08006' }
|
||||
let usageThrew = false
|
||||
try {
|
||||
await checkQuota(USER, 'llm_haiku', usageFailClient.asClient(), NOW)
|
||||
} catch {
|
||||
usageThrew = true
|
||||
}
|
||||
assert(usageThrew, 'daily_usage DB error must throw instead of silently returning usage=0')
|
||||
|
||||
const subFailClient = new FakeQuotaClient()
|
||||
subFailClient.tableErrors.subscriptions = { message: 'timeout', code: '57014' }
|
||||
let subThrew = false
|
||||
try {
|
||||
await checkQuota(USER, 'llm_haiku', subFailClient.asClient(), NOW)
|
||||
} catch {
|
||||
subThrew = true
|
||||
}
|
||||
assert(subThrew, 'subscriptions DB error must throw instead of silently falling back')
|
||||
|
||||
const notFoundClient = new FakeQuotaClient()
|
||||
notFoundClient.tableErrors.subscriptions = { message: 'no rows', code: 'PGRST116' }
|
||||
const missingRowCheck = await checkQuota(USER, 'llm_haiku', notFoundClient.asClient(), NOW)
|
||||
assert(missingRowCheck.tier === 'free' && missingRowCheck.overageCredits === 0, 'PGRST116 falls back cleanly to free tier')
|
||||
})
|
||||
|
|
|
|||
|
|
@ -209,11 +209,14 @@ async function readSubscriptionQuotaState(
|
|||
userId: string,
|
||||
serviceRoleClient: ReturnType<typeof createClient>,
|
||||
): Promise<SubscriptionQuotaState> {
|
||||
const { data: sub } = await serviceRoleClient
|
||||
const { data: sub, error } = await serviceRoleClient
|
||||
.from('subscriptions')
|
||||
.select('tier, overage_credits')
|
||||
.eq('user_id', userId)
|
||||
.single()
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
throw new Error(`Failed to read subscription quota state: ${error.message ?? error.code ?? 'unknown'}`)
|
||||
}
|
||||
return {
|
||||
tier: (sub?.tier as Tier) ?? 'free',
|
||||
overageCredits: (sub?.overage_credits as number) ?? 0,
|
||||
|
|
@ -236,13 +239,16 @@ async function readWindowUsage(
|
|||
serviceRoleClient: ReturnType<typeof createClient>,
|
||||
now: Date,
|
||||
): Promise<number> {
|
||||
const { data: rows } = await serviceRoleClient
|
||||
const { data: rows, error } = await serviceRoleClient
|
||||
.from('daily_usage')
|
||||
.select('count')
|
||||
.eq('user_id', userId)
|
||||
.eq('feature', feature)
|
||||
.gte('date', quotaWindowStart(period, now))
|
||||
.lte('date', quotaUsageDate(now))
|
||||
if (error) {
|
||||
throw new Error(`Failed to read quota usage window: ${error.message ?? error.code ?? 'unknown'}`)
|
||||
}
|
||||
const usageRows = (rows ?? []) as Array<{ count: number | null }>
|
||||
return usageRows.reduce((sum, row) => sum + (row.count ?? 0), 0)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,3 +164,41 @@ Deno.test('another user\'s document stays hidden', async () => {
|
|||
const response = await call()
|
||||
assert(response.status === 404 && provider.calls === 0, 'foreign document was embedded')
|
||||
})
|
||||
|
||||
Deno.test('embedChunks_공급자200오응답또는저장실패_청구된유닛은환불하지않는다', async () => {
|
||||
const invalidProvider: EmbeddingProvider = {
|
||||
embed: () => Promise.resolve({ ok: false as const, reason: 'invalid' as const }),
|
||||
}
|
||||
const usageInvalid = new MemoryUsageStore('pro', TODAY)
|
||||
const storeInvalid = new FakeKnowledgeStore(chunks(2))
|
||||
const handlerInvalid = createEmbedChunksHandler({
|
||||
authenticate: () => Promise.resolve({ id: USER }),
|
||||
embeddingProvider: () => invalidProvider,
|
||||
knowledgeStore: () => storeInvalid,
|
||||
usageStore: () => usageInvalid,
|
||||
now: () => NOW,
|
||||
})
|
||||
const invalidResp = await handlerInvalid(new Request('https://edge/embed-chunks', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ document_id: DOC }),
|
||||
}))
|
||||
assert(invalidResp.status === 502, `expected 502, got ${invalidResp.status}`)
|
||||
assert(usageInvalid.rows.get(TODAY) === 2, `invalid 200 response must keep units charged, got ${usageInvalid.rows.get(TODAY)}`)
|
||||
|
||||
const usageDbFail = new MemoryUsageStore('pro', TODAY)
|
||||
const storeDbFail = new FakeKnowledgeStore(chunks(2))
|
||||
storeDbFail.saveEmbedding = () => Promise.resolve(false)
|
||||
const handlerDbFail = createEmbedChunksHandler({
|
||||
authenticate: () => Promise.resolve({ id: USER }),
|
||||
embeddingProvider: () => new FakeProvider(false),
|
||||
knowledgeStore: () => storeDbFail,
|
||||
usageStore: () => usageDbFail,
|
||||
now: () => NOW,
|
||||
})
|
||||
const dbFailResp = await handlerDbFail(new Request('https://edge/embed-chunks', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ document_id: DOC }),
|
||||
}))
|
||||
assert(dbFailResp.status === 502, `expected 502, got ${dbFailResp.status}`)
|
||||
assert(usageDbFail.rows.get(TODAY) === 2, `local DB save failure must not refund billed units, got ${usageDbFail.rows.get(TODAY)}`)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -57,14 +57,24 @@ export interface EmbedChunksDeps {
|
|||
now?(): Date
|
||||
}
|
||||
|
||||
interface EmbedBatchOutcome {
|
||||
saved: number
|
||||
refund: boolean
|
||||
}
|
||||
|
||||
async function embedBatch(
|
||||
provider: EmbeddingProvider,
|
||||
store: KnowledgeIndexStore,
|
||||
documentId: string,
|
||||
batch: KnowledgeChunkRow[],
|
||||
): Promise<number> {
|
||||
): Promise<EmbedBatchOutcome> {
|
||||
const outcome = await provider.embed(batch.map((chunk) => chunk.content))
|
||||
if (!outcome.ok || outcome.data.length !== batch.length) return 0
|
||||
if (!outcome.ok) {
|
||||
return { saved: 0, refund: outcome.reason === 'upstream' }
|
||||
}
|
||||
if (outcome.data.length !== batch.length) {
|
||||
return { saved: 0, refund: false }
|
||||
}
|
||||
|
||||
const received = new Set<number>()
|
||||
let saved = 0
|
||||
|
|
@ -82,7 +92,7 @@ async function embedBatch(
|
|||
received.add(item.index)
|
||||
if (await store.saveEmbedding(documentId, batch[item.index].id, item.embedding)) saved += 1
|
||||
}
|
||||
return saved
|
||||
return { saved, refund: false }
|
||||
}
|
||||
|
||||
export function createEmbedChunksHandler(deps: EmbedChunksDeps): (req: Request) => Promise<Response> {
|
||||
|
|
@ -155,15 +165,15 @@ export function createEmbedChunksHandler(deps: EmbedChunksDeps): (req: Request)
|
|||
let failed = 0
|
||||
let unspentUnits = 0
|
||||
for (const batch of planEmbeddingBatches(chunks)) {
|
||||
let saved = 0
|
||||
let outcome: EmbedBatchOutcome = { saved: 0, refund: false }
|
||||
try {
|
||||
saved = await embedBatch(provider, store, documentId, batch)
|
||||
outcome = await embedBatch(provider, store, documentId, batch)
|
||||
} catch {
|
||||
saved = 0
|
||||
outcome = { saved: 0, refund: false }
|
||||
}
|
||||
embedded += saved
|
||||
failed += batch.length - saved
|
||||
if (saved === 0) unspentUnits += embeddingUnits(batch.map((chunk) => chunk.content))
|
||||
embedded += outcome.saved
|
||||
failed += batch.length - outcome.saved
|
||||
if (outcome.refund) unspentUnits += embeddingUnits(batch.map((chunk) => chunk.content))
|
||||
}
|
||||
await refundEmbeddingUnits(usage, user.id, reservation, unspentUnits, now())
|
||||
|
||||
|
|
|
|||
|
|
@ -259,3 +259,30 @@ Deno.test('non-multipart body fails closed with 400 instead of 500', async () =>
|
|||
assertEquals(await response.json(), { error: 'invalid_multipart_body' }, 'body')
|
||||
assertEquals(h.events, [], 'no quota or provider work')
|
||||
})
|
||||
|
||||
Deno.test('sttProxy_초과ContentLength헤더_본문파싱전에413으로차단한다', async () => {
|
||||
const h = harness([fakeProvider('groq', OK)])
|
||||
let bodyRead = false
|
||||
const req = new Request('http://localhost/stt-proxy', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data; boundary=x',
|
||||
'Content-Length': String(30 * 1024 * 1024),
|
||||
},
|
||||
body: new ReadableStream(
|
||||
{
|
||||
pull() {
|
||||
bodyRead = true
|
||||
return Promise.reject(new Error('body must not be read when Content-Length exceeds cap'))
|
||||
},
|
||||
},
|
||||
new CountQueuingStrategy({ highWaterMark: 0 }),
|
||||
),
|
||||
})
|
||||
const response = await h.handler(req)
|
||||
assertEquals(response.status, 413, 'oversized Content-Length returns 413')
|
||||
assertEquals(await response.json(), { error: 'audio_too_large' }, 'fixed size error code')
|
||||
assertEquals(bodyRead, false, 'request stream must not be pulled')
|
||||
assertEquals(req.bodyUsed, false, 'request body must remain unread')
|
||||
assertEquals(h.events, [], 'no quota or provider work')
|
||||
})
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import { handleCorsPreflightRequest } from '../_shared/cors.ts'
|
|||
import { createStatusJsonResponder } from '../_shared/json-response.ts'
|
||||
import {
|
||||
type DictionaryHints,
|
||||
MAX_STT_AUDIO_BYTES,
|
||||
type NormalizedSttResult,
|
||||
type SttFailureCode,
|
||||
SttInputError,
|
||||
|
|
@ -118,6 +119,7 @@ export async function runSttChain(
|
|||
}
|
||||
|
||||
const json = createStatusJsonResponder()
|
||||
const MAX_STT_MULTIPART_BYTES = MAX_STT_AUDIO_BYTES + 1024 * 1024
|
||||
|
||||
export function createSttProxyHandler(deps: SttProxyDeps): (req: Request) => Promise<Response> {
|
||||
const { quota, logger } = deps
|
||||
|
|
@ -137,7 +139,15 @@ export function createSttProxyHandler(deps: SttProxyDeps): (req: Request) => Pro
|
|||
if (!auth.ok) return auth.response
|
||||
const user = auth.user
|
||||
|
||||
// 2) input
|
||||
// 2) input (reject oversized Content-Length before buffering multipart body)
|
||||
const rawContentLength = req.headers.get('content-length')
|
||||
if (rawContentLength !== null) {
|
||||
const declaredBytes = Number(rawContentLength.trim())
|
||||
if (Number.isFinite(declaredBytes) && declaredBytes > MAX_STT_MULTIPART_BYTES) {
|
||||
return json(413, { error: 'audio_too_large' })
|
||||
}
|
||||
}
|
||||
|
||||
let formData: FormData
|
||||
try {
|
||||
formData = await req.formData()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue