Snapshot of uncommitted work that had accumulated in the tree alongside the Turnstile changes: - marketing pages, SEO helpers (lib/seo.ts, lib/marketing/) and structured data - admin billing actions and a per-user portfolio view, plus an admin error boundary - rate limiting (lib/rate-limit.ts) applied across the /api/v1 surface - CSP and proxy adjustments, accounting/webhook lib updates - Playwright config and an e2e/unit test suite - next bumped to ^16.3.4 with the lockfile regenerated - generated AGENTS.md / CLAUDE.md Authored by other sessions working in this tree; committed here so the Turnstile work could be pushed without leaving the tree dirty. Typecheck passes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
92 lines
3.4 KiB
TypeScript
92 lines
3.4 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { desc, eq } from "drizzle-orm"
|
|
import { db } from "@/lib/db"
|
|
import { webhook_endpoints } from "@/lib/db/schema"
|
|
import { resolveApiRequest } from "@/lib/api-auth"
|
|
import { enforceRateLimit } from "@/lib/rate-limit"
|
|
import { webhookEndpointSchema } from "@/lib/validations"
|
|
import { isWebhookEvent } from "@/lib/webhooks/events"
|
|
import { assertSafeWebhookUrl, WebhookUrlError } from "@/lib/webhooks/ssrf"
|
|
import { generateWebhookSecret } from "@/lib/webhooks/deliver"
|
|
|
|
// Public REST API (v1) — outbound webhook subscriptions. Bearer API-key auth.
|
|
// This is the surface Zapier's REST Hooks use: POST here to subscribe, DELETE
|
|
// /:id to unsubscribe. Scoped by the resolved account owner id.
|
|
|
|
const unauthorized = () =>
|
|
NextResponse.json({ error: { code: 401, message: "Unauthorized" } }, { status: 401 })
|
|
const forbidden = () =>
|
|
NextResponse.json({ error: { code: 403, message: "Forbidden" } }, { status: 403 })
|
|
const badRequest = (message: unknown) =>
|
|
NextResponse.json({ error: { code: 400, message } }, { status: 400 })
|
|
|
|
// The signing secret is not returned on list/read (only at creation & rotation).
|
|
const PUBLIC_COLUMNS = {
|
|
id: webhook_endpoints.id,
|
|
url: webhook_endpoints.url,
|
|
description: webhook_endpoints.description,
|
|
events: webhook_endpoints.events,
|
|
status: webhook_endpoints.status,
|
|
source: webhook_endpoints.source,
|
|
last_success_at: webhook_endpoints.last_success_at,
|
|
last_error_at: webhook_endpoints.last_error_at,
|
|
failure_count: webhook_endpoints.failure_count,
|
|
created_at: webhook_endpoints.created_at,
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const ctx = await resolveApiRequest(request)
|
|
if (!ctx) return unauthorized()
|
|
|
|
// Public API budget: 120 requests/minute per key owner.
|
|
const limited = enforceRateLimit(`v1:${ctx.userId}`, 120, 60)
|
|
if (limited) return limited
|
|
|
|
const data = await db
|
|
.select(PUBLIC_COLUMNS)
|
|
.from(webhook_endpoints)
|
|
.where(eq(webhook_endpoints.user_id, ctx.ownerId))
|
|
.orderBy(desc(webhook_endpoints.created_at))
|
|
|
|
return NextResponse.json({ data, count: data.length })
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const ctx = await resolveApiRequest(request)
|
|
if (!ctx) return unauthorized()
|
|
|
|
// Public API budget: 120 requests/minute per key owner.
|
|
const limited = enforceRateLimit(`v1:${ctx.userId}`, 120, 60)
|
|
if (limited) return limited
|
|
if (!ctx.canWrite) return forbidden()
|
|
|
|
const body = await request.json().catch(() => null)
|
|
const parsed = webhookEndpointSchema.safeParse(body)
|
|
if (!parsed.success) return badRequest(parsed.error.flatten())
|
|
|
|
try {
|
|
await assertSafeWebhookUrl(parsed.data.url)
|
|
} catch (e) {
|
|
return badRequest(e instanceof WebhookUrlError ? e.message : "Invalid webhook URL")
|
|
}
|
|
|
|
// Requests coming through Zapier's REST-hook subscribe carry a Zapier UA.
|
|
const ua = request.headers.get("user-agent") ?? ""
|
|
const source = /zapier/i.test(ua) ? "zapier" : "api"
|
|
|
|
const [data] = await db
|
|
.insert(webhook_endpoints)
|
|
.values({
|
|
user_id: ctx.ownerId,
|
|
url: parsed.data.url,
|
|
description: parsed.data.description || null,
|
|
events: Array.from(new Set(parsed.data.events.filter(isWebhookEvent))),
|
|
secret: generateWebhookSecret(),
|
|
source,
|
|
})
|
|
.returning()
|
|
|
|
// Return the secret exactly once, at creation, so the subscriber can verify signatures.
|
|
return NextResponse.json({ data }, { status: 201 })
|
|
}
|