|
| 1 | +import { NextRequest } from "next/server"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { rateLimit } from "@/lib/rate-limit"; |
| 4 | +import { clientIp } from "@/lib/client-ip"; |
| 5 | + |
| 6 | +/* G-02 — `/api/contact` POST endpoint. |
| 7 | + * |
| 8 | + * Sink for the ContactForm primitive. Rate-limited 3 / 60s per IP |
| 9 | + * to keep form-spam at bay without blocking legitimate retries. |
| 10 | + * Body: name + email + topic + message. Validated via zod. |
| 11 | + * |
| 12 | + * Sink behavior: log to console + (when CONTACT_WEBHOOK_URL is |
| 13 | + * configured) POST to the configured Slack/Discord/email webhook. |
| 14 | + * Both paths are best-effort — a webhook 5xx still returns ok:true |
| 15 | + * to the client because the message has already been logged. |
| 16 | + * |
| 17 | + * No DB write — submissions are intentionally ephemeral. The |
| 18 | + * production sink is whatever endpoint CONTACT_WEBHOOK_URL points |
| 19 | + * at (PR-P-2 ships with no default URL set; ops adds the secret |
| 20 | + * post-deploy). |
| 21 | + */ |
| 22 | + |
| 23 | +const BodySchema = z.object({ |
| 24 | + name: z.string().min(1).max(120), |
| 25 | + email: z.string().email().max(200), |
| 26 | + topic: z.enum(["general", "bug", "school", "press", "privacy"]), |
| 27 | + message: z.string().min(10).max(5000), |
| 28 | +}); |
| 29 | + |
| 30 | +const RATE_LIMIT_PER_WINDOW = 3; |
| 31 | +const RATE_LIMIT_WINDOW_MS = 60_000; |
| 32 | + |
| 33 | +export async function POST(req: NextRequest) { |
| 34 | + const ip = clientIp(req); |
| 35 | + const rl = await rateLimit( |
| 36 | + `contact:${ip}`, |
| 37 | + RATE_LIMIT_PER_WINDOW, |
| 38 | + RATE_LIMIT_WINDOW_MS, |
| 39 | + ); |
| 40 | + if (!rl.ok) { |
| 41 | + return Response.json( |
| 42 | + { ok: false, error: "rate-limited", resetAt: rl.resetAt }, |
| 43 | + { status: 429 }, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + let parsed; |
| 48 | + try { |
| 49 | + const body = await req.json(); |
| 50 | + parsed = BodySchema.safeParse(body); |
| 51 | + } catch { |
| 52 | + return Response.json({ ok: false, error: "invalid-json" }, { status: 400 }); |
| 53 | + } |
| 54 | + if (!parsed.success) { |
| 55 | + return Response.json( |
| 56 | + { ok: false, error: parsed.error.issues[0]?.message ?? "invalid" }, |
| 57 | + { status: 400 }, |
| 58 | + ); |
| 59 | + } |
| 60 | + const { name, email, topic, message } = parsed.data; |
| 61 | + |
| 62 | + // Always log; the build pipeline scrubs prod console output by default, |
| 63 | + // and the runtime captures it in Vercel logs for ops triage. |
| 64 | + console.log("[contact] submission", { topic, email, name, ip }); |
| 65 | + |
| 66 | + const webhookUrl = process.env.CONTACT_WEBHOOK_URL; |
| 67 | + if (webhookUrl) { |
| 68 | + try { |
| 69 | + await fetch(webhookUrl, { |
| 70 | + method: "POST", |
| 71 | + headers: { "Content-Type": "application/json" }, |
| 72 | + body: JSON.stringify({ |
| 73 | + text: `📨 [${topic}] from ${name} <${email}>\n\n${message}`, |
| 74 | + // Slack-compatible payload; Discord ignores extra keys. |
| 75 | + name, |
| 76 | + email, |
| 77 | + topic, |
| 78 | + message, |
| 79 | + }), |
| 80 | + }); |
| 81 | + } catch (err) { |
| 82 | + // Webhook failure must not surface as a user-visible error — |
| 83 | + // the message is already logged above. Ops surfaces webhook |
| 84 | + // health separately. |
| 85 | + console.error("[contact] webhook delivery failed", err); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return Response.json({ ok: true }); |
| 90 | +} |
0 commit comments