mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
fix(notifications): name the never-grantable refusal and redact the grant log
A loopback, link-local or metadata target answered private_origin_not_approved with a message telling the user to have the operator list it, and the parser refuses such an entry forever: a dead end the route test even pinned. Those targets now answer private_origin_not_grantable (catalogued, on all four routes, translated in seven locales) with a message that says no grant can open them and to use the relay's LAN address. Both senders carry the reason into the outcome so the test button forwards the same code. The malformed-entry warning printed the entry verbatim, which is where a pasted token or userinfo sits; the parser now hands callers scheme and host only, plus the reason in words. The Nightscout list throws a named error with that redacted entry and reason, and the connect route answers it as a 422 with the message instead of a bare 500, so a self-host that listed http://localhost:1337 reads what to change. The two settings cards translate a coded save refusal like the test button does; the docs and the changelog name the sub-host and sibling- port narrowing; the dispatcher floor tests assert the policy marker so a real NXDOMAIN cannot pass for a refusal. Refs #947
This commit is contained in:
1 parent
71175290af
commit
9c306f04e7
30 files changed
+436
-85
No files matched your search
@@ -26,6 +26,7 @@ import {
|
||||
configuredNightscoutPrivateOrigins,
|
||||
evaluateNightscoutOrigin,
|
||||
nightscoutConnectSchema,
|
||||
NightscoutOriginConfigError,
|
||||
} from "@/lib/validations/nightscout";
|
||||
|
||||
/**
|
||||
@@ -67,10 +68,22 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
}
|
||||
|
||||
const { url, token } = result.data;
|
||||
const policy = evaluateNightscoutOrigin(
|
||||
url,
|
||||
configuredNightscoutPrivateOrigins(),
|
||||
);
|
||||
// An operator list that holds a non-grant is an operator problem, not a
|
||||
// user one: say which entry and why, rather than a 500 with no words.
|
||||
let privateOrigins: ReadonlySet<string>;
|
||||
try {
|
||||
privateOrigins = configuredNightscoutPrivateOrigins();
|
||||
} catch (err) {
|
||||
if (err instanceof NightscoutOriginConfigError) {
|
||||
annotate({
|
||||
action: { name: "nightscout.connect" },
|
||||
meta: { refused: "operator_origin_list_invalid" },
|
||||
});
|
||||
return apiError(err.message, 422);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
const policy = evaluateNightscoutOrigin(url, privateOrigins);
|
||||
|
||||
// SSRF floor at INPUT time, not merely as a side effect of the live probe
|
||||
// below. `fetchSgvEntries` does route through `safeFetch` with the same
|
||||
|
||||
@@ -179,10 +179,10 @@ describe("PUT /api/settings/webhook — private targets", () => {
|
||||
expect(json.meta).toBeUndefined();
|
||||
});
|
||||
|
||||
it("refuses a loopback grant at save time: listing it does not open it", async () => {
|
||||
// The parser drops a loopback or localhost grant with a warning, so the
|
||||
// save is refused with the reason and the operator learns that the list
|
||||
// is not the way to reach the container itself.
|
||||
it("answers a loopback target with the not-grantable code, listed or not (M1)", async () => {
|
||||
// This used to pin a dead end: the route said "list it", the parser
|
||||
// dropped the listing, and the next save said "list it" again. A target
|
||||
// no grant can open gets its own code and a message that says so.
|
||||
vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
process.env.NOTIFICATION_PRIVATE_ORIGINS =
|
||||
"http://127.0.0.1:8080, http://localhost:8080";
|
||||
@@ -190,11 +190,16 @@ describe("PUT /api/settings/webhook — private targets", () => {
|
||||
for (const url of [
|
||||
"http://127.0.0.1:8080/message",
|
||||
"http://localhost:8080/message",
|
||||
"http://169.254.169.254/latest",
|
||||
"http://[::1]:8080/message",
|
||||
]) {
|
||||
const response = await put("webhook", { url, enabled: true });
|
||||
expect(response.status).toBe(422);
|
||||
const json = await response.json();
|
||||
expect(json.meta).toEqual({ errorCode: "private_origin_not_approved" });
|
||||
expect(json.meta).toEqual({ errorCode: "private_origin_not_grantable" });
|
||||
expect(json.error).toMatch(/LAN address/);
|
||||
expect(json.error).not.toContain("NOTIFICATION_PRIVATE_ORIGINS");
|
||||
expect(json.details.issues.length).toBeGreaterThan(0);
|
||||
}
|
||||
expect(prisma.notificationChannel.upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -298,6 +303,29 @@ describe("POST /api/settings/{webhook,ntfy}/test — the refusal names itself",
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["webhook", sendViaWebhookMock],
|
||||
["ntfy", sendViaNtfyMock],
|
||||
])(
|
||||
"%s: forwards the not-grantable code with a message that does not say list it (M1)",
|
||||
async (path, sender) => {
|
||||
sender.mockResolvedValue({
|
||||
ok: false,
|
||||
hardReject: false,
|
||||
reason: `${path}_private_origin_refused`,
|
||||
errorCode: "private_origin_not_grantable",
|
||||
});
|
||||
|
||||
const response = await post(path);
|
||||
|
||||
expect(response.status).toBe(422);
|
||||
const json = await response.json();
|
||||
expect(json.meta).toEqual({ errorCode: "private_origin_not_grantable" });
|
||||
expect(json.error).toMatch(/LAN address/);
|
||||
expect(json.error).not.toContain("NOTIFICATION_PRIVATE_ORIGINS");
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["webhook", sendViaWebhookMock],
|
||||
["ntfy", sendViaNtfyMock],
|
||||
|
||||
@@ -13,7 +13,8 @@ import {
|
||||
import {
|
||||
evaluateNotificationTarget,
|
||||
isAllowedNotificationTarget,
|
||||
PRIVATE_ORIGIN_NOT_APPROVED,
|
||||
PRIVATE_ORIGIN_NOT_APPROVED_CODE,
|
||||
PRIVATE_ORIGIN_NOT_GRANTABLE_CODE,
|
||||
} from "@/lib/notifications/egress-policy";
|
||||
import { encrypt, decrypt } from "@/lib/crypto";
|
||||
import { NextRequest } from "next/server";
|
||||
@@ -30,6 +31,8 @@ const ntfySettingsSchema = ntfySettingsSchemaWith(isAllowedNotificationTarget);
|
||||
|
||||
const PRIVATE_ORIGIN_REFUSAL =
|
||||
"This server 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.";
|
||||
const NOT_GRANTABLE_REFUSAL =
|
||||
"This server is loopback, link-local or a metadata address, which no operator grant can open. Use the relay's LAN address instead.";
|
||||
|
||||
/**
|
||||
* Refusal for an enable attempt on a channel the operator switched off
|
||||
@@ -145,18 +148,26 @@ export const PUT = apiHandler(async (request: NextRequest) => {
|
||||
// adds the code and a message naming the operator's lever (#947).
|
||||
const issues = sanitiseZodIssues(parsed.error.issues);
|
||||
const candidate = (body as { serverUrl?: unknown } | null)?.serverUrl;
|
||||
const reason =
|
||||
typeof candidate === "string"
|
||||
? evaluateNotificationTarget(candidate).reasonCode
|
||||
: null;
|
||||
if (
|
||||
typeof candidate === "string" &&
|
||||
evaluateNotificationTarget(candidate).reasonCode ===
|
||||
PRIVATE_ORIGIN_NOT_APPROVED
|
||||
reason === PRIVATE_ORIGIN_NOT_APPROVED_CODE ||
|
||||
reason === PRIVATE_ORIGIN_NOT_GRANTABLE_CODE
|
||||
) {
|
||||
annotate({
|
||||
action: { name: "settings.ntfy.update" },
|
||||
meta: { refused: PRIVATE_ORIGIN_NOT_APPROVED },
|
||||
});
|
||||
return apiValidationError(PRIVATE_ORIGIN_REFUSAL, issues, 422, {
|
||||
errorCode: PRIVATE_ORIGIN_NOT_APPROVED,
|
||||
meta: { refused: reason },
|
||||
});
|
||||
return apiValidationError(
|
||||
reason === PRIVATE_ORIGIN_NOT_GRANTABLE_CODE
|
||||
? NOT_GRANTABLE_REFUSAL
|
||||
: PRIVATE_ORIGIN_REFUSAL,
|
||||
issues,
|
||||
422,
|
||||
{ errorCode: reason },
|
||||
);
|
||||
}
|
||||
return apiValidationError("Invalid data", issues, 422);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,9 @@ export const POST = apiHandler(async () => {
|
||||
meta: { success: false, refused: result.errorCode },
|
||||
});
|
||||
return apiError(
|
||||
"The server is on a private network the operator has not approved. List its exact origin (scheme://host:port) in NOTIFICATION_PRIVATE_ORIGINS on the server.",
|
||||
result.errorCode === "private_origin_not_grantable"
|
||||
? "The server is loopback, link-local or a metadata address, which no operator grant can open. Use the relay's LAN address instead."
|
||||
: "The server 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 },
|
||||
);
|
||||
|
||||
@@ -13,7 +13,8 @@ import {
|
||||
import {
|
||||
evaluateNotificationTarget,
|
||||
isAllowedNotificationTarget,
|
||||
PRIVATE_ORIGIN_NOT_APPROVED,
|
||||
PRIVATE_ORIGIN_NOT_APPROVED_CODE,
|
||||
PRIVATE_ORIGIN_NOT_GRANTABLE_CODE,
|
||||
} from "@/lib/notifications/egress-policy";
|
||||
import { encrypt, decrypt } from "@/lib/crypto";
|
||||
import { NextRequest } from "next/server";
|
||||
@@ -32,6 +33,8 @@ const webhookSettingsSchema = webhookSettingsSchemaWith(
|
||||
|
||||
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.";
|
||||
const NOT_GRANTABLE_REFUSAL =
|
||||
"This address is loopback, link-local or a metadata address, which no operator grant can open. Use the relay's LAN address instead.";
|
||||
|
||||
/**
|
||||
* Generic-webhook channel config (v1.17.1).
|
||||
@@ -139,18 +142,26 @@ export const PUT = apiHandler(async (request: NextRequest) => {
|
||||
// adds the code and a message naming the operator's lever (#947).
|
||||
const issues = sanitiseZodIssues(parsed.error.issues);
|
||||
const candidate = (body as { url?: unknown } | null)?.url;
|
||||
const reason =
|
||||
typeof candidate === "string"
|
||||
? evaluateNotificationTarget(candidate).reasonCode
|
||||
: null;
|
||||
if (
|
||||
typeof candidate === "string" &&
|
||||
evaluateNotificationTarget(candidate).reasonCode ===
|
||||
PRIVATE_ORIGIN_NOT_APPROVED
|
||||
reason === PRIVATE_ORIGIN_NOT_APPROVED_CODE ||
|
||||
reason === PRIVATE_ORIGIN_NOT_GRANTABLE_CODE
|
||||
) {
|
||||
annotate({
|
||||
action: { name: "settings.webhook.update" },
|
||||
meta: { refused: PRIVATE_ORIGIN_NOT_APPROVED },
|
||||
});
|
||||
return apiValidationError(PRIVATE_ORIGIN_REFUSAL, issues, 422, {
|
||||
errorCode: PRIVATE_ORIGIN_NOT_APPROVED,
|
||||
meta: { refused: reason },
|
||||
});
|
||||
return apiValidationError(
|
||||
reason === PRIVATE_ORIGIN_NOT_GRANTABLE_CODE
|
||||
? NOT_GRANTABLE_REFUSAL
|
||||
: PRIVATE_ORIGIN_REFUSAL,
|
||||
issues,
|
||||
422,
|
||||
{ errorCode: reason },
|
||||
);
|
||||
}
|
||||
return apiValidationError("Invalid data", issues, 422);
|
||||
}
|
||||
|
||||
@@ -50,7 +50,9 @@ export const POST = apiHandler(async () => {
|
||||
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.",
|
||||
result.errorCode === "private_origin_not_grantable"
|
||||
? "The target is loopback, link-local or a metadata address, which no operator grant can open. Use the relay's LAN address instead."
|
||||
: "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 },
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user