mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
Raising the middleware body cap to 512 MB for archive imports removed the implicit ceiling that protected JSON routes, so any bulk endpoint would buffer an arbitrarily large body before validation ran. Every JSON body read under /api now carries an explicit byte cap sized to its legitimate payload (64 KB for single-record routes up to 16 MB for the measurement import), webhooks cap before signature work, and oversized bodies return 413.
118 lines
3.3 KiB
TypeScript
118 lines
3.3 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { apiSuccess, apiError } from "@/lib/api-response";
|
|
import { encrypt } from "@/lib/crypto";
|
|
import { webPushSubscriptionSchema } from "@/lib/validations/notifications";
|
|
import { z } from "zod/v4";
|
|
|
|
// The endpoint is later dialled by `web-push`, which does its own internal
|
|
// fetch that bypasses safeFetch — so the SSRF floor has to be enforced here
|
|
// at input time. `webPushSubscriptionSchema` requires https + isPublicUrl
|
|
// (blocks loopback/link-local/metadata/internal hosts). The sender applies
|
|
// the same check again at egress time as defence-in-depth.
|
|
const subscribeSchema = webPushSubscriptionSchema;
|
|
|
|
const unsubscribeSchema = z.object({
|
|
endpoint: z.string().url(),
|
|
});
|
|
|
|
/**
|
|
* POST /api/notifications/web-push
|
|
* Save a Web Push subscription for the current user.
|
|
*/
|
|
export const POST = apiHandler(async (request: NextRequest) => {
|
|
const { user } = await requireAuth();
|
|
annotate({ action: { name: "notifications.web-push.subscribe" } });
|
|
|
|
let body: unknown;
|
|
try {
|
|
const raw = await request.text();
|
|
if (raw.length > 64 * 1024) {
|
|
return apiError(`Request body exceeds ${64 * 1024} bytes`, 413);
|
|
}
|
|
body = JSON.parse(raw);
|
|
} catch {
|
|
return apiError("Invalid JSON data", 422);
|
|
}
|
|
|
|
const parsed = subscribeSchema.safeParse(body);
|
|
if (!parsed.success) return apiError("Invalid data", 422);
|
|
|
|
const { endpoint, keys } = parsed.data;
|
|
const userAgent = request.headers.get("user-agent") ?? undefined;
|
|
|
|
// Upsert subscription (encrypt sensitive keys)
|
|
await prisma.pushSubscription.upsert({
|
|
where: {
|
|
userId_endpoint: {
|
|
userId: user.id,
|
|
endpoint,
|
|
},
|
|
},
|
|
create: {
|
|
userId: user.id,
|
|
endpoint,
|
|
p256dh: encrypt(keys.p256dh),
|
|
auth: encrypt(keys.auth),
|
|
userAgent,
|
|
},
|
|
update: {
|
|
p256dh: encrypt(keys.p256dh),
|
|
auth: encrypt(keys.auth),
|
|
userAgent,
|
|
},
|
|
});
|
|
|
|
// Ensure a WEB_PUSH notification channel exists for this user
|
|
const existingChannel = await prisma.notificationChannel.findFirst({
|
|
where: { userId: user.id, type: "WEB_PUSH" },
|
|
});
|
|
|
|
if (!existingChannel) {
|
|
await prisma.notificationChannel.create({
|
|
data: {
|
|
userId: user.id,
|
|
type: "WEB_PUSH",
|
|
enabled: true,
|
|
config: encrypt(JSON.stringify({})),
|
|
},
|
|
});
|
|
}
|
|
|
|
return apiSuccess({ subscribed: true });
|
|
});
|
|
|
|
/**
|
|
* DELETE /api/notifications/web-push
|
|
* Remove a Web Push subscription.
|
|
*/
|
|
export const DELETE = apiHandler(async (request: NextRequest) => {
|
|
const { user } = await requireAuth();
|
|
annotate({ action: { name: "notifications.web-push.unsubscribe" } });
|
|
|
|
let body: unknown;
|
|
try {
|
|
const raw = await request.text();
|
|
if (raw.length > 64 * 1024) {
|
|
return apiError(`Request body exceeds ${64 * 1024} bytes`, 413);
|
|
}
|
|
body = JSON.parse(raw);
|
|
} catch {
|
|
return apiError("Invalid JSON data", 422);
|
|
}
|
|
|
|
const parsed = unsubscribeSchema.safeParse(body);
|
|
if (!parsed.success) return apiError("Invalid data", 422);
|
|
|
|
await prisma.pushSubscription.deleteMany({
|
|
where: {
|
|
userId: user.id,
|
|
endpoint: parsed.data.endpoint,
|
|
},
|
|
});
|
|
|
|
return apiSuccess({ unsubscribed: true });
|
|
});
|