mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
fix(api): name the private-origin refusal on webhook and ntfy routes
The save routes evaluate the same policy the senders do. A target on a private network the operator has not listed is a 422 with meta.errorCode = private_origin_not_approved and a message that names NOTIFICATION_PRIVATE_ORIGINS, instead of the generic "Invalid data"; a listed private address saves although the plain public floor would have refused it. The settings schemas take the target predicate as a parameter so the validator module never reads the environment; the exported constants keep the public floor for the OpenAPI contract. The test routes forward the sender's refusal as the same 422 and code instead of a bare 500, so the card can say why the test failed rather than sending the operator to the wide-event log. A delivery fault is still a 500. Both test routes and the dispatcher reach the same sender function, and a structural test holds them there. The OpenAPI descriptions name the code on all four routes; the test button's error map gains the code in every locale. Refs #947
This commit is contained in:
1 parent
6c75c4f39f
commit
984a75d036
17 files changed
+614
-56
No files matched your search
@@ -2,22 +2,42 @@ import { prisma } from "@/lib/db";
|
||||
import { apiSuccess, apiError, safeJson } from "@/lib/api-response";
|
||||
import {
|
||||
notificationChannelEnabledSchema,
|
||||
webhookSettingsSchema,
|
||||
webhookSettingsSchemaWith,
|
||||
} from "@/lib/validations/notifications";
|
||||
import {
|
||||
evaluateNotificationTarget,
|
||||
isAllowedNotificationTarget,
|
||||
PRIVATE_ORIGIN_NOT_APPROVED,
|
||||
} from "@/lib/notifications/egress-policy";
|
||||
import { encrypt, decrypt } from "@/lib/crypto";
|
||||
import { NextRequest } from "next/server";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
|
||||
/**
|
||||
* The save-time schema evaluates the same policy the sender does: the public
|
||||
* floor, plus the operator's exact-origin grant (#947). A listed private
|
||||
* address therefore saves; an unlisted one is refused here with the reason,
|
||||
* before anything is encrypted onto the row.
|
||||
*/
|
||||
const webhookSettingsSchema = webhookSettingsSchemaWith(
|
||||
isAllowedNotificationTarget,
|
||||
);
|
||||
|
||||
const PRIVATE_ORIGIN_REFUSAL =
|
||||
"This address is on a private network. The operator has to list its exact origin (scheme://host:port) in NOTIFICATION_PRIVATE_ORIGINS before HealthLog can send to it.";
|
||||
|
||||
/**
|
||||
* Generic-webhook channel config (v1.17.1).
|
||||
* GET: current config (header value redacted — only presence flagged).
|
||||
* PUT: upsert config.
|
||||
*
|
||||
* Covers Gotify / Discord / Slack / Matrix-bridge / Home Assistant in one
|
||||
* channel: the user supplies a public URL and an optional shared-secret
|
||||
* header. SSRF is enforced at input time (`isPublicUrl` in the schema) and
|
||||
* again at dispatch time (`safeFetch({ requirePublicHost: true })`).
|
||||
* channel: the user supplies a URL and an optional shared-secret header. SSRF
|
||||
* is enforced at input time (the schema's target predicate) and again at
|
||||
* dispatch time (`safeFetch` with the connect-time pin); a private origin
|
||||
* passes both only when the operator listed it in
|
||||
* `NOTIFICATION_PRIVATE_ORIGINS`.
|
||||
*/
|
||||
export const GET = apiHandler(async () => {
|
||||
const { user } = await requireAuth();
|
||||
@@ -108,7 +128,23 @@ export const PUT = apiHandler(async (request: NextRequest) => {
|
||||
}
|
||||
|
||||
const parsed = webhookSettingsSchema.safeParse(body);
|
||||
if (!parsed.success) return apiError("Invalid data", 422);
|
||||
if (!parsed.success) {
|
||||
const candidate = (body as { url?: unknown } | null)?.url;
|
||||
if (
|
||||
typeof candidate === "string" &&
|
||||
evaluateNotificationTarget(candidate).reasonCode ===
|
||||
PRIVATE_ORIGIN_NOT_APPROVED
|
||||
) {
|
||||
annotate({
|
||||
action: { name: "settings.webhook.update" },
|
||||
meta: { refused: PRIVATE_ORIGIN_NOT_APPROVED },
|
||||
});
|
||||
return apiError(PRIVATE_ORIGIN_REFUSAL, 422, {
|
||||
errorCode: PRIVATE_ORIGIN_NOT_APPROVED,
|
||||
});
|
||||
}
|
||||
return apiError("Invalid data", 422);
|
||||
}
|
||||
|
||||
const { url, headerName, headerValue, enabled } = parsed.data;
|
||||
|
||||
|
||||
@@ -41,6 +41,20 @@ export const POST = apiHandler(async () => {
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
// A private-origin refusal is a policy decision, not a delivery fault:
|
||||
// say so, with the code the card translates, instead of a bare 500 that
|
||||
// sends the operator to the wide-event log to learn why (#947).
|
||||
if (result.errorCode) {
|
||||
annotate({
|
||||
action: { name: "settings.webhook.test" },
|
||||
meta: { success: false, refused: result.errorCode },
|
||||
});
|
||||
return apiError(
|
||||
"The target is on a private network the operator has not approved. List its exact origin (scheme://host:port) in NOTIFICATION_PRIVATE_ORIGINS on the server.",
|
||||
422,
|
||||
{ errorCode: result.errorCode },
|
||||
);
|
||||
}
|
||||
return apiError("Failed to send test message", 500);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user