Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(admin): retry webhook DNS failures and allow disabling offline URLs

Tin5c3b62e12f81
diff --git a/apps/api/src/admin/webhooks/update-webhook.ts b/apps/api/src/admin/webhooks/update-webhook.ts
index 23f2f0ea8..76f7574b0 100644
--- a/apps/api/src/admin/webhooks/update-webhook.ts
+++ b/apps/api/src/admin/webhooks/update-webhook.ts
@@ -4,6 +4,7 @@ import db, { schema } from "../../database";
import type { AdminAuditRequest } from "../audit/admin-audit-request";
import { recordAdminAction } from "../audit/record-admin-action";
import { pruneWebhookDeliveries } from "./prune-webhook-deliveries";
+import { requireWebhook } from "./require-webhook";
import { publicWebhook } from "./public-webhook";
import type { WebhookInput } from "./schema";
import { validateWebhookUrl } from "./validate-webhook-url";
@@ -13,7 +14,11 @@ export async function updateWebhook(
input: WebhookInput,
audit: AdminAuditRequest,
) {
- const url = await validateWebhookUrl(input.url);
+ const current = await requireWebhook(id);
+ const url =
+ !input.enabled && input.url === current.url
+ ? current.url
+ : await validateWebhookUrl(input.url);
return db.transaction(async (tx) => {
const [row] = await tx
.update(schema.instanceWebhookTable)
diff --git a/apps/api/src/utils/assert-public-destination.ts b/apps/api/src/utils/assert-public-destination.ts
index b1e8826f7..c6ae8ab39 100644
--- a/apps/api/src/utils/assert-public-destination.ts
+++ b/apps/api/src/utils/assert-public-destination.ts
@@ -1,5 +1,6 @@
import { lookup } from "node:dns/promises";
import net from "node:net";
+import { DestinationLookupError } from "./destination-lookup-error";
function isDisallowedIpv4(ip: string): boolean {
const parts = ip.split(".").map((part) => Number.parseInt(part, 10));
@@ -161,9 +162,13 @@ export async function assertPublicDestination(
// them here, and avoid looking up bracketed IPv6 literals as hostnames.
if (net.isIP(hostname)) return;
- const addresses = await lookup(hostname, { all: true, verbatim: true });
+ const addresses = await lookup(hostname, { all: true, verbatim: true }).catch(
+ () => {
+ throw new DestinationLookupError();
+ },
+ );
if (addresses.length === 0) {
- throw new Error(`${label} destination could not be resolved`);
+ throw new DestinationLookupError();
}
if (addresses.some((entry) => isDisallowedAddress(entry.address))) {
diff --git a/apps/api/src/utils/destination-lookup-error.ts b/apps/api/src/utils/destination-lookup-error.ts
new file mode 100644
index 000000000..861203b53
--- /dev/null
+++ b/apps/api/src/utils/destination-lookup-error.ts
@@ -0,0 +1,5 @@
+export class DestinationLookupError extends Error {
+ constructor() {
+ super("Destination could not be resolved");
+ }
+}
diff --git a/apps/api/src/utils/outbound-request.ts b/apps/api/src/utils/outbound-request.ts
index 920f0c0e1..08341905e 100644
--- a/apps/api/src/utils/outbound-request.ts
+++ b/apps/api/src/utils/outbound-request.ts
@@ -1,5 +1,6 @@
import { destinationFetch } from "../import/destination-fetch";
import { isBlockedDestinationError } from "../import/blocked-destination-error";
+import { DestinationLookupError } from "./destination-lookup-error";
import { assertPublicDestination } from "./assert-public-destination";
import { withoutOutboundTelemetry } from "./sensitive-outbound";
@@ -77,8 +78,10 @@ export async function sendOutboundRequest(
if (options.publicDestination) {
try {
await assertPublicDestination(url, "Notification");
- } catch {
- throw new OutboundRequestError("destination");
+ } catch (error) {
+ throw new OutboundRequestError(
+ error instanceof DestinationLookupError ? "network" : "destination",
+ );
}
}
// DNS validation may finish after the deadline. Never start a late request.
diff --git a/tests/api-integration/admin-instance-webhooks.test.ts b/tests/api-integration/admin-instance-webhooks.test.ts
index 8905c82c9..4cbe64b38 100644
--- a/tests/api-integration/admin-instance-webhooks.test.ts
+++ b/tests/api-integration/admin-instance-webhooks.test.ts
@@ -1,3 +1,4 @@
+import { lookup } from "node:dns/promises";
import { createHmac } from "node:crypto";
import { createServer, type Server, type IncomingHttpHeaders } from "node:http";
import { eq } from "drizzle-orm";
@@ -23,6 +24,11 @@ import {
import { readAuditLog } from "./helpers/admin/read-audit-log";
import { resetTestDatabase } from "./helpers/database";
+vi.mock("node:dns/promises", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("node:dns/promises")>();
+ return { ...actual, lookup: vi.fn(actual.lookup) };
+});
+
let server: Server;
let url: string;
let status: number;
@@ -32,6 +38,7 @@ let app: ReturnType<typeof createApp>["app"];
const table = schema.instanceWebhookDeliveryTable;
beforeEach(async () => {
+ vi.mocked(lookup).mockReset();
await resetTestDatabase();
vi.stubEnv(
"NOTIFICATION_SECRET_ENCRYPTION_KEY",
@@ -150,6 +157,47 @@ describe("instance webhook administration", () => {
).toBe(200);
});
+ it("disables an unchanged destination during DNS failure while still validating changed URLs and re-enabling", async () => {
+ const { webhook } = await create();
+ const brokenUrl = "https://gone.example.test/events";
+ await db
+ .update(schema.instanceWebhookTable)
+ .set({ url: brokenUrl })
+ .where(eq(schema.instanceWebhookTable.id, webhook.id));
+ await record();
+ vi.stubEnv("KANEO_ALLOW_PRIVATE_WEBHOOK_DESTINATIONS", "");
+ vi.mocked(lookup).mockRejectedValue(
+ Object.assign(new Error("DNS failure"), { code: "ENOTFOUND" }),
+ );
+ const disabled = await admin.api(`/webhooks/${webhook.id}`, {
+ method: "PUT",
+ body: { url: brokenUrl, categories: ["user"], enabled: false },
+ });
+ expect(disabled.status).toBe(200);
+ expect(lookup).not.toHaveBeenCalled();
+ expect(
+ await db.select().from(table).where(eq(table.state, "pending")),
+ ).toEqual([]);
+ for (const input of [
+ {
+ url: "https://other.example.test/events",
+ categories: ["user"],
+ enabled: false,
+ },
+ { url: brokenUrl, categories: ["user"], enabled: true },
+ ]) {
+ expect(
+ (
+ await admin.api(`/webhooks/${webhook.id}`, {
+ method: "PUT",
+ body: input,
+ })
+ ).status,
+ ).toBe(400);
+ }
+ expect(lookup).toHaveBeenCalledTimes(2);
+ });
+
it("shows secrets once, encrypts storage, and audits create/update/rotate/delete without secrets", async () => {
const { webhook, secret } = await create();
const [stored] = await db.select().from(schema.instanceWebhookTable);
@@ -313,6 +361,40 @@ describe("instance webhook delivery", () => {
expect(JSON.stringify(entries)).not.toContain(admin.email);
});
+ it("retries temporary DNS lookup failures without sending or exposing their details", async () => {
+ const { webhook } = await create();
+ await db
+ .update(schema.instanceWebhookTable)
+ .set({ url: "https://receiver.example.test/events" })
+ .where(eq(schema.instanceWebhookTable.id, webhook.id));
+ await record();
+ vi.stubEnv("KANEO_ALLOW_PRIVATE_WEBHOOK_DESTINATIONS", "");
+ vi.mocked(lookup).mockRejectedValueOnce(
+ Object.assign(new Error("private lookup details"), { code: "EAI_AGAIN" }),
+ );
+ expect(await sendWebhookDelivery()).toBe(true);
+ expect(requests).toHaveLength(0);
+ expect(await db.select().from(table)).toEqual([
+ expect.objectContaining({
+ state: "pending",
+ errorCode: "network",
+ attempts: 1,
+ }),
+ ]);
+ expect(await sendWebhookDelivery()).toBe(false);
+ await db
+ .update(schema.instanceWebhookTable)
+ .set({ url })
+ .where(eq(schema.instanceWebhookTable.id, webhook.id));
+ vi.stubEnv("KANEO_ALLOW_PRIVATE_WEBHOOK_DESTINATIONS", "true");
+ await due();
+ expect(await sendWebhookDelivery()).toBe(true);
+ expect(requests).toHaveLength(1);
+ expect(await db.select().from(table)).toEqual([
+ expect.objectContaining({ state: "succeeded", attempts: 2 }),
+ ]);
+ });
+
it("checks saved destinations again before delivery", async () => {
await create();
await record();