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
|
|
@ -1,6 +1,7 @@
|
|||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { AdminBackendError, fetchAdminBackend } from '@/lib/backend-session'
|
||||
import type { AdminRole } from '@/lib/admin-session'
|
||||
import { isSameOriginRequest, readBoundedTextBody } from '@/lib/request-security'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
|
||||
|
|
@ -40,26 +41,6 @@ function requiredRole(method: string, path: string): AdminRole | null {
|
|||
return null
|
||||
}
|
||||
|
||||
function isSameOrigin(request: NextRequest): boolean {
|
||||
const origin = request.headers.get('origin')
|
||||
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
|
||||
try {
|
||||
const requestOrigin = new URL(origin).origin
|
||||
if (requestOrigin === request.nextUrl.origin) return true
|
||||
|
||||
const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase()
|
||||
const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim()
|
||||
?? request.headers.get('host')?.trim()
|
||||
if ((forwardedProto !== 'http' && forwardedProto !== 'https') ||
|
||||
!forwardedHost || !/^[A-Za-z0-9.:[\]-]+$/.test(forwardedHost)) {
|
||||
return false
|
||||
}
|
||||
return requestOrigin === `${forwardedProto}://${forwardedHost}`
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function proxy(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ segments: string[] }> }
|
||||
|
|
@ -68,15 +49,19 @@ async function proxy(
|
|||
const path = pathFor(segments)
|
||||
const role = path ? requiredRole(request.method, path) : null
|
||||
if (!path || !role) return NextResponse.json({ error: 'admin_route_not_allowed' }, { status: 404 })
|
||||
if (request.method !== 'GET' && !isSameOrigin(request)) {
|
||||
if (request.method !== 'GET' && !isSameOriginRequest(request)) {
|
||||
return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 })
|
||||
}
|
||||
|
||||
let body: string | undefined
|
||||
if (request.method !== 'GET') {
|
||||
body = await request.text()
|
||||
if (new TextEncoder().encode(body).byteLength > 64 * 1024) {
|
||||
return NextResponse.json({ error: 'request_too_large' }, { status: 413 })
|
||||
try {
|
||||
body = await readBoundedTextBody(request, 64 * 1024)
|
||||
} catch (error) {
|
||||
if (error instanceof AdminBackendError) {
|
||||
return NextResponse.json({ error: error.message }, { status: error.status })
|
||||
}
|
||||
return NextResponse.json({ error: 'invalid_request' }, { status: 400 })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
|||
import { issueSignedLicenseKey, type SignedLicensePayload } from '@d3ro/core/utils/crypto-license'
|
||||
import type { LicenseTier } from '@d3ro/core/types'
|
||||
import { AdminBackendError, fetchAdminBackend, requireVerifiedBackendSession } from '@/lib/backend-session'
|
||||
import { isSameOriginRequest, readBoundedJsonObject } from '@/lib/request-security'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
|
||||
|
|
@ -32,19 +33,12 @@ function resolveSigningKey(): string | null {
|
|||
return null
|
||||
}
|
||||
|
||||
function isSameOrigin(request: NextRequest): boolean {
|
||||
const origin = request.headers.get('origin')
|
||||
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
|
||||
try {
|
||||
return new URL(origin).origin === request.nextUrl.origin
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest): Promise<NextResponse> {
|
||||
if (!isSameOrigin(request)) {
|
||||
return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 })
|
||||
if (!isSameOriginRequest(request)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'cross_site_request_rejected' },
|
||||
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -52,20 +46,24 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
|
|||
} catch (error) {
|
||||
const status = error instanceof AdminBackendError ? error.status : 401
|
||||
const message = error instanceof AdminBackendError ? error.message : 'admin_session_invalid'
|
||||
return NextResponse.json({ error: message }, { status })
|
||||
return NextResponse.json({ error: message }, { status, headers: { 'Cache-Control': 'no-store' } })
|
||||
}
|
||||
|
||||
let body: unknown
|
||||
let candidate: Record<string, unknown>
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'invalid_request' }, { status: 400 })
|
||||
candidate = await readBoundedJsonObject(request, 16 * 1024)
|
||||
} catch (error) {
|
||||
if (error instanceof AdminBackendError) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: error.status, headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: 'invalid_request' },
|
||||
{ status: 400, headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
}
|
||||
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
||||
return NextResponse.json({ error: 'invalid_request' }, { status: 400 })
|
||||
}
|
||||
|
||||
const candidate = body as Record<string, unknown>
|
||||
const customerEmail = typeof candidate.customerEmail === 'string' ? candidate.customerEmail.trim().toLowerCase() : ''
|
||||
const tier = candidate.tier
|
||||
const validity = typeof candidate.validity === 'string' ? candidate.validity : ''
|
||||
|
|
@ -139,5 +137,5 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
|
|||
licenseId: payload.licenseId,
|
||||
expiresAt: payload.expiresAt,
|
||||
auditRecorded
|
||||
})
|
||||
}, { headers: { 'Cache-Control': 'no-store' } })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,46 +1,17 @@
|
|||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { AdminBackendError, requireVerifiedBackendSession } from '@/lib/backend-session'
|
||||
import { ADMIN_UUID_PATTERN, isSameOriginRequest, readBoundedJsonObject } from '@/lib/request-security'
|
||||
import { getSupabaseAdminClient } from '@/lib/supabase-admin'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
const UUID = ADMIN_UUID_PATTERN
|
||||
const ROLES = new Set(['user', 'manager', 'admin', 'super_admin'])
|
||||
const TIERS = new Set(['free', 'pro', 'pro_plus'])
|
||||
const STATUSES = new Set(['active', 'canceled', 'past_due', 'expired'])
|
||||
|
||||
function sameOrigin(request: NextRequest): boolean {
|
||||
const origin = request.headers.get('origin')
|
||||
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
|
||||
try {
|
||||
const requestOrigin = new URL(origin).origin
|
||||
if (requestOrigin === request.nextUrl.origin) return true
|
||||
const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase()
|
||||
const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim()
|
||||
?? request.headers.get('host')?.trim()
|
||||
if ((forwardedProto !== 'http' && forwardedProto !== 'https') ||
|
||||
!forwardedHost || !/^[A-Za-z0-9.:[\]-]+$/.test(forwardedHost)) {
|
||||
return false
|
||||
}
|
||||
return requestOrigin === `${forwardedProto}://${forwardedHost}`
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function bodyObject(request: NextRequest): Promise<Record<string, unknown>> {
|
||||
if (request.headers.get('content-type')?.split(';', 1)[0]?.trim().toLowerCase() !== 'application/json') {
|
||||
throw new AdminBackendError('application_json_required', 415)
|
||||
}
|
||||
const text = await request.text()
|
||||
if (new TextEncoder().encode(text).byteLength > 32 * 1024) throw new AdminBackendError('request_too_large', 413)
|
||||
try {
|
||||
const parsed = JSON.parse(text) as unknown
|
||||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error()
|
||||
return parsed as Record<string, unknown>
|
||||
} catch {
|
||||
throw new AdminBackendError('invalid_request', 400)
|
||||
}
|
||||
return await readBoundedJsonObject(request, 32 * 1024, { requireJsonContentType: true })
|
||||
}
|
||||
|
||||
function onlyKeys(body: Record<string, unknown>, allowed: readonly string[]): void {
|
||||
|
|
@ -220,7 +191,7 @@ async function handlePayments(request: NextRequest): Promise<NextResponse> {
|
|||
}
|
||||
|
||||
async function route(request: NextRequest, context: { params: Promise<{ operation: string }> }): Promise<NextResponse> {
|
||||
if (request.method !== 'GET' && !sameOrigin(request)) {
|
||||
if (request.method !== 'GET' && !isSameOriginRequest(request)) {
|
||||
return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 })
|
||||
}
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
import { NextResponse } from 'next/server'
|
||||
import { AdminBackendError } from '@/lib/backend-session'
|
||||
import { readBoundedJsonObject } from '@/lib/request-security'
|
||||
import {
|
||||
checkRateLimit,
|
||||
recordFailedAttempt,
|
||||
|
|
@ -52,23 +54,24 @@ function failure(key: string, status: number, retryAfterSeconds?: number): NextR
|
|||
},
|
||||
{
|
||||
status,
|
||||
headers: retryAfterSeconds ? { 'Retry-After': String(retryAfterSeconds) } : undefined
|
||||
headers: {
|
||||
'Cache-Control': 'no-store',
|
||||
...(retryAfterSeconds ? { 'Retry-After': String(retryAfterSeconds) } : {})
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function POST(request: Request): Promise<NextResponse> {
|
||||
let body: unknown
|
||||
let candidate: Record<string, unknown>
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
candidate = await readBoundedJsonObject(request, 16 * 1024)
|
||||
} catch (error) {
|
||||
if (error instanceof AdminBackendError) {
|
||||
return failure(error.message, error.status)
|
||||
}
|
||||
return failure('invalid_request', 400)
|
||||
}
|
||||
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
||||
return failure('invalid_request', 400)
|
||||
}
|
||||
|
||||
const candidate = body as Record<string, unknown>
|
||||
if (candidate.trap) return failure('access_denied', 403)
|
||||
const email =
|
||||
typeof candidate.usernameOrEmail === 'string'
|
||||
|
|
@ -132,10 +135,13 @@ export async function POST(request: Request): Promise<NextResponse> {
|
|||
expiresAt
|
||||
})
|
||||
|
||||
const response = NextResponse.json({
|
||||
success: true,
|
||||
user: { email: authenticatedEmail, role }
|
||||
})
|
||||
const response = NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
user: { email: authenticatedEmail, role }
|
||||
},
|
||||
{ headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
response.cookies.set({
|
||||
name: 'd3ro_admin_session',
|
||||
value: signedCookieValue,
|
||||
|
|
|
|||
|
|
@ -1,40 +1,49 @@
|
|||
// apps/admin/src/app/api/auth/logout/route.ts
|
||||
// D3RO Voice — Admin Session Logout Handler
|
||||
|
||||
import { NextResponse } from 'next/server'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { adminCookieSecure } from '@/lib/admin-session'
|
||||
import { isSameOriginRequest } from '@/lib/request-security'
|
||||
|
||||
export async function POST(): Promise<NextResponse> {
|
||||
const response = NextResponse.json({ success: true, message: 'Logged out successfully' })
|
||||
|
||||
function clearAdminSessionCookie(response: NextResponse): NextResponse {
|
||||
response.cookies.set({
|
||||
name: 'd3ro_admin_session',
|
||||
value: '',
|
||||
httpOnly: true,
|
||||
secure: adminCookieSecure(),
|
||||
sameSite: 'lax',
|
||||
sameSite: 'strict',
|
||||
path: '/',
|
||||
maxAge: 0,
|
||||
expires: new Date(0),
|
||||
})
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export async function GET(request: Request): Promise<NextResponse> {
|
||||
export async function POST(request: NextRequest): Promise<NextResponse> {
|
||||
if (!isSameOriginRequest(request)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'cross_site_request_rejected' },
|
||||
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
}
|
||||
|
||||
const response = NextResponse.json(
|
||||
{ success: true, message: 'Logged out successfully' },
|
||||
{ headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
return clearAdminSessionCookie(response)
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
if (request.headers.get('sec-fetch-site') === 'cross-site') {
|
||||
return NextResponse.json(
|
||||
{ error: 'cross_site_request_rejected' },
|
||||
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
|
||||
)
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
const response = NextResponse.redirect(new URL('/login', url.origin))
|
||||
|
||||
response.cookies.set({
|
||||
name: 'd3ro_admin_session',
|
||||
value: '',
|
||||
httpOnly: true,
|
||||
secure: adminCookieSecure(),
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 0,
|
||||
expires: new Date(0),
|
||||
})
|
||||
|
||||
return response
|
||||
response.headers.set('Cache-Control', 'no-store')
|
||||
return clearAdminSessionCookie(response)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue