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
88
apps/web/src/lib/edge-fetch.ts
Normal file
88
apps/web/src/lib/edge-fetch.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
export interface TimerRefHandle {
|
||||
current: ReturnType<typeof setTimeout> | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Supabase Edge Function URL을 검증·생성한다.
|
||||
* HTTPS 또는 로컬 loopback HTTP만 허용하고, credentials 포함 URL을 거부한다.
|
||||
*/
|
||||
export function resolveSupabaseFunctionUrl(
|
||||
functionName: string,
|
||||
rawBaseUrl: string | undefined = process.env.NEXT_PUBLIC_SUPABASE_URL,
|
||||
): string {
|
||||
const baseUrl = rawBaseUrl?.trim()
|
||||
if (!baseUrl) {
|
||||
throw new Error('서버 설정이 완료되지 않았습니다.')
|
||||
}
|
||||
const normalizedName = functionName.trim().replace(/^\/+/, '')
|
||||
if (!normalizedName || normalizedName.includes('/') || normalizedName.includes('..')) {
|
||||
throw new Error('잘못된 함수 경로입니다.')
|
||||
}
|
||||
try {
|
||||
const url = new URL(`/functions/v1/${encodeURIComponent(normalizedName)}`, baseUrl)
|
||||
const isLocalHttp =
|
||||
url.protocol === 'http:' && (url.hostname === 'localhost' || url.hostname === '127.0.0.1')
|
||||
if ((url.protocol !== 'https:' && !isLocalHttp) || url.username || url.password) {
|
||||
throw new Error('invalid_origin')
|
||||
}
|
||||
return url.toString()
|
||||
} catch {
|
||||
throw new Error('서버 주소 설정이 올바르지 않습니다.')
|
||||
}
|
||||
}
|
||||
|
||||
export interface TimedAbortHandle {
|
||||
controller: AbortController
|
||||
didTimeout: () => boolean
|
||||
dispose: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 타임아웃으로 자동 중단되며, 완료/언마운트 시 타이머와 참조를 정리할 수 있는 AbortController 핸들.
|
||||
*/
|
||||
export function createTimedAbortSignal(
|
||||
timeoutMs: number,
|
||||
activeRef?: { current: AbortController | null },
|
||||
): TimedAbortHandle {
|
||||
const controller = new AbortController()
|
||||
let timedOut = false
|
||||
if (activeRef) {
|
||||
activeRef.current?.abort()
|
||||
activeRef.current = controller
|
||||
}
|
||||
|
||||
const timer = globalThis.setTimeout(() => {
|
||||
timedOut = true
|
||||
controller.abort()
|
||||
}, timeoutMs)
|
||||
|
||||
return {
|
||||
controller,
|
||||
didTimeout: () => timedOut,
|
||||
dispose: () => {
|
||||
globalThis.clearTimeout(timer)
|
||||
if (activeRef && activeRef.current === controller) {
|
||||
activeRef.current = null
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function clearTrackedTimeout(timerRef: TimerRefHandle): void {
|
||||
if (timerRef.current !== null) {
|
||||
globalThis.clearTimeout(timerRef.current)
|
||||
timerRef.current = null
|
||||
}
|
||||
}
|
||||
|
||||
export function scheduleTrackedTimeout(
|
||||
timerRef: TimerRefHandle,
|
||||
callback: () => void,
|
||||
delayMs: number,
|
||||
): void {
|
||||
clearTrackedTimeout(timerRef)
|
||||
timerRef.current = globalThis.setTimeout(() => {
|
||||
timerRef.current = null
|
||||
callback()
|
||||
}, delayMs)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue