Files
Tom MoorandClaude Opus 4.8 8183c8e032 fix: Allow webhook urls without a top-level domain when self-hosted (#12728)
* fix: Allow webhook urls without a top-level domain when self-hosted

Webhook URL validation required a top-level domain, rejecting valid
internal hostnames such as http://webhook on self-hosted installations.
The TLD requirement is now only enforced when cloud hosted.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor: Extract webhook url validation into reusable IsUrl decorator

Move the conditional top-level domain validation into a reusable IsUrl
validator alongside the other model validators, and restore mocks
between create tests to keep hosting mode isolated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test: Move webhook url validation tests onto IsUrl validator

The top-level domain validation is owned by the IsUrl decorator, so test
it directly via a collocated unit test rather than through the
webhookSubscriptions API.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* PR feedback

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 20:29:03 -04:00

48 lines
1.4 KiB
TypeScript

import env from "@server/env";
import { WebhookSubscription } from "@server/models";
describe("IsUrl", () => {
afterEach(() => {
vi.restoreAllMocks();
});
const validateUrl = (url: string) =>
WebhookSubscription.build({ url }).validate({ fields: ["url"] });
describe("when cloud hosted", () => {
beforeEach(() => {
vi.spyOn(env, "isCloudHosted", "get").mockReturnValue(true);
});
it("should allow urls with a top-level domain", async () => {
await expect(validateUrl("https://example.com")).resolves.toBeTruthy();
});
it("should reject urls without a top-level domain", async () => {
await expect(validateUrl("https://webhook")).rejects.toThrow();
});
});
describe("self hosted", () => {
beforeEach(() => {
vi.spyOn(env, "isCloudHosted", "get").mockReturnValue(false);
});
it("should allow urls with a top-level domain", async () => {
await expect(validateUrl("https://example.com")).resolves.toBeTruthy();
});
it("should allow urls without a top-level domain", async () => {
await expect(validateUrl("http://webhook")).resolves.toBeTruthy();
});
});
it("should reject urls without a protocol", async () => {
await expect(validateUrl("example.com")).rejects.toThrow();
});
it("should reject non-http protocols", async () => {
await expect(validateUrl("ftp://example.com")).rejects.toThrow();
});
});