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
123
apps/admin/tests/admin-security.test.ts
Normal file
123
apps/admin/tests/admin-security.test.ts
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
import { NextRequest } from 'next/server'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
isStaticAssetPath,
|
||||
parseOptionalIsoDate,
|
||||
resolveAdminCallbackOrigin,
|
||||
safeAdminRedirectPath,
|
||||
} from '@/lib/admin-url-safety'
|
||||
|
||||
const mockCreateServerClient = vi.fn()
|
||||
const mockExchangeCodeForSession = vi.fn()
|
||||
|
||||
vi.mock('@supabase/ssr', () => ({
|
||||
createServerClient: (...args: unknown[]) => {
|
||||
mockCreateServerClient(...args)
|
||||
return {
|
||||
auth: {
|
||||
exchangeCodeForSession: (code: string) => mockExchangeCodeForSession(code),
|
||||
},
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
||||
describe('Admin 라우트 가드 및 OAuth 콜백 보안', () => {
|
||||
const originalSupabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
const originalSupabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
|
||||
const originalAdminOrigin = process.env.ADMIN_PUBLIC_ORIGIN
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL = originalSupabaseUrl
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = originalSupabaseAnonKey
|
||||
process.env.ADMIN_PUBLIC_ORIGIN = originalAdminOrigin
|
||||
})
|
||||
|
||||
it('proxy_점포함동적경로나API경로미인증접근_정적으로우회하지않고로그인으로차단한다', async () => {
|
||||
const { proxy } = await import('@/proxy')
|
||||
|
||||
const dottedUserRoute = new NextRequest('https://admin.chanpaca.net/users/john.doe')
|
||||
const userRes = await proxy(dottedUserRoute)
|
||||
expect(userRes.status).toBe(307)
|
||||
expect(userRes.headers.get('location')).toBe(
|
||||
'https://admin.chanpaca.net/login?redirect=%2Fusers%2Fjohn.doe',
|
||||
)
|
||||
|
||||
const dottedApiRoute = new NextRequest('https://admin.chanpaca.net/api/audit.export')
|
||||
const apiRes = await proxy(dottedApiRoute)
|
||||
expect(apiRes.status).toBe(307)
|
||||
expect(apiRes.headers.get('location')).toBe(
|
||||
'https://admin.chanpaca.net/login?redirect=%2Fapi%2Faudit.export',
|
||||
)
|
||||
|
||||
const staticAsset = new NextRequest('https://admin.chanpaca.net/static/logo.png')
|
||||
const staticRes = await proxy(staticAsset)
|
||||
expect(staticRes.status).toBe(200)
|
||||
expect(isStaticAssetPath('/api/logo.png')).toBe(false)
|
||||
expect(isStaticAssetPath('/_next/static/chunk.js')).toBe(true)
|
||||
})
|
||||
|
||||
it('safeAdminRedirectPath_외부URL이나프로토콜상대경로_루트로폴백한다', () => {
|
||||
expect(safeAdminRedirectPath('https://evil.example/phish')).toBe('/')
|
||||
expect(safeAdminRedirectPath('//evil.example/phish')).toBe('/')
|
||||
expect(safeAdminRedirectPath('/\\evil.example')).toBe('/')
|
||||
expect(safeAdminRedirectPath('/users\r\n')).toBe('/')
|
||||
expect(safeAdminRedirectPath('/users/123?tab=billing')).toBe('/users/123?tab=billing')
|
||||
})
|
||||
|
||||
it('parseOptionalIsoDate_유효하지않은날짜입력_예외없이오류를반환한다', () => {
|
||||
expect(parseOptionalIsoDate('')).toEqual({ ok: true, iso: undefined })
|
||||
expect(parseOptionalIsoDate('2026-12-31')).toEqual({
|
||||
ok: true,
|
||||
iso: '2026-12-31T00:00:00.000Z',
|
||||
})
|
||||
expect(parseOptionalIsoDate('invalid-date-value')).toEqual({
|
||||
ok: false,
|
||||
message: '올바른 종료일 형식이 아닙니다.',
|
||||
})
|
||||
})
|
||||
|
||||
it('authCallback_GET_Supabase환경변수누락시_빈문자클라이언트생성없이실패닫힘으로리다이렉트한다', async () => {
|
||||
delete process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
delete process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
|
||||
const { GET } = await import('@/app/auth/callback/route')
|
||||
const req = new NextRequest('https://admin.chanpaca.net/auth/callback?code=fake-oauth-code')
|
||||
|
||||
const res = await GET(req)
|
||||
expect(res.status).toBe(307)
|
||||
expect(mockCreateServerClient).not.toHaveBeenCalled()
|
||||
expect(res.headers.get('location')).toBe('https://admin.chanpaca.net/login?error=auth_unavailable')
|
||||
})
|
||||
|
||||
it('authCallback_GET_세션교환실패시_원시에러노출없이고정코드로리다이렉트한다', async () => {
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://example.supabase.co'
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'public-anon-example'
|
||||
mockExchangeCodeForSession.mockResolvedValue({
|
||||
error: { message: 'internal error http://10.0.0.5:5432' },
|
||||
})
|
||||
const { GET } = await import('@/app/auth/callback/route')
|
||||
const req = new NextRequest('https://admin.chanpaca.net/auth/callback?code=fake-oauth-code')
|
||||
|
||||
const res = await GET(req)
|
||||
expect(res.status).toBe(307)
|
||||
const location = res.headers.get('location') ?? ''
|
||||
expect(location).toBe('https://admin.chanpaca.net/login?error=oauth_callback_failed')
|
||||
expect(location).not.toContain('10.0.0.5')
|
||||
})
|
||||
|
||||
it('authCallback_GET_공개origin설정과위험한next파라미터_공개도메인루트로만이동한다', async () => {
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://example.supabase.co'
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'public-anon-example'
|
||||
process.env.ADMIN_PUBLIC_ORIGIN = 'https://admin.chanpaca.net/ignored-path'
|
||||
mockExchangeCodeForSession.mockResolvedValue({ error: null })
|
||||
const { GET } = await import('@/app/auth/callback/route')
|
||||
const req = new NextRequest('http://127.0.0.1:3001/auth/callback?code=ok-code&next=//evil.example')
|
||||
|
||||
const res = await GET(req)
|
||||
expect(res.status).toBe(307)
|
||||
expect(res.headers.get('location')).toBe('https://admin.chanpaca.net/')
|
||||
expect(
|
||||
resolveAdminCallbackOrigin('http://127.0.0.1:3001/auth/callback', 'javascript:alert(1)'),
|
||||
).toBe('http://127.0.0.1:3001')
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue