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
|
|
@ -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