Files
HealthLog/src/app/api/notifications/web-push/route.ts
T
Marc-André Bombeck d6bc858899 fix: refuse an unrecognised request field where the caller can act on it
A `z.object()` without `.strict()` strips an unknown key and parses
clean, so a caller that misspells a field name gets a 200 and loses the
value with nothing to tell it. The worst of them was the Coach
question-dismiss endpoint, where an omitted `question` means "dismiss
all": a typo in that key wiped every pending question and answered as
though one had been dismissed.

Twenty-four inline schemas across twenty-two endpoints are strict now —
the settings toggles, the restore and bulk-delete endpoints, message
feedback, the admin user update, the onboarding acknowledgements, the
provider chain, the chart overlay preferences, the medication extractor
and the two client-side reporters.

The strip stays where it carries weight. Sync ingest, device
registration and the layout PUTs are built out of fields that shipped as
additive and optional, on the promise that a server which does not know
one ignores it; an app running ahead of the install it talks to is
routine here rather than exceptional, and refusing would turn a dropped
field into a refused batch — a year of history lost to one row, a
dashboard that will not save, a phone that quietly stops registering for
push. A read filter discards nothing when a stray query parameter rides
along, the OAuth endpoints are required by RFC 6749 and RFC 7591 to
ignore parameters they do not recognise, and an import file comes from
another version or another tool by definition. Each is registered with
its reason in a new inventory test, so a schema cannot join them without
someone making the call. That test parses the tree rather than grepping
it: fourteen of these schemas are written `z\n  .object({…})` and a
`z.object(` sweep never saw them.

`sanitiseZodIssues` renders `unrecognized_keys` instead of passing it
through. Zod leaves `path` empty and puts the names in `issue.keys`, so
the sanitised issue named nothing at all and the caller was told only
that its body had failed. The names now ride a bounded `keys` field and
the message is rebuilt from them, capped at ten keys of 64 characters
and restricted to identifier characters, so a caller-supplied key cannot
echo back verbatim through the error path or into the audit ledger. The
names survive `stripValuesFromMessage` because a field name is
structure, not user-typed content.
2026-09-03 05:49:13 +02:00

150 lines
4.9 KiB
TypeScript

import { NextRequest } from "next/server";
import { prisma } from "@/lib/db";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { isChannelGloballyEnabled } from "@/lib/app-settings";
import { annotate } from "@/lib/logging/context";
import { apiSuccess, apiError, returnAllZodIssues } 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(),
})
.strict();
/*
* Both parses below answer with the multi-issue envelope, like every other
* body-parsing route in the tree. They used to answer a flat
* `apiError("Invalid data", 422)`, which made a non-HTTPS endpoint, an
* endpoint pointing at an internal host, an over-long one and a missing
* subscription key byte-identical to each other — a client could see that its
* subscription was refused and had no way to learn which rule it broke.
*
* Echoing the issues is safe here, and it is worth writing down why rather
* than leaving it to be re-derived. The endpoint is a routing secret and the
* keys are subscription crypto material, so the refusal must not carry them
* back. It does not: `sanitiseZodIssues` emits `path`, `code` and `message`
* only — `issue.params`, which is where Zod keeps a rejected value, stays
* server-side — and every message this schema can produce is a fixed string
* or a length/format default. None of them interpolates the value.
*/
/**
* 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" } });
// Subscribing IS the enable step for this channel — it creates the
// WEB_PUSH `NotificationChannel` row. An operator who switched Web Push
// off instance-wide (`AppSettings.webPushGlobal`) gets a refusal that says
// so rather than a subscription that stores fine and delivers nothing.
if (!(await isChannelGloballyEnabled("WEB_PUSH"))) {
annotate({ meta: { refused: "globally_disabled" } });
return apiError(
"Web Push is disabled on this instance by the operator",
403,
);
}
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 returnAllZodIssues(parsed.error, 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 returnAllZodIssues(parsed.error, 422);
await prisma.pushSubscription.deleteMany({
where: {
userId: user.id,
endpoint: parsed.data.endpoint,
},
});
return apiSuccess({ unsubscribed: true });
});