Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

GDGT-2564 Webhook destination cannot be saved from the UI (#78334)

Kamil Mielnik9896c4aee5a1
diff --git a/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.tsx b/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.tsx
index d41294c83af..585da0edae3 100644
--- a/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.tsx
+++ b/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.tsx
@@ -1,5 +1,5 @@
import type { FormikHelpers } from "formik";
-import { useMemo } from "react";
+import { useMemo, useState } from "react";
import { c, jt, t } from "ttag";
import * as Yup from "yup";
@@ -35,24 +35,43 @@ import {
import { buildAuthInfo } from "./utils";
-const validationSchema = Yup.object({
- url: Yup.string()
- // eslint-disable-next-line ttag/no-module-declaration -- see metabase#55045
- .url(t`Please enter a correctly formatted URL`)
- // eslint-disable-next-line ttag/no-module-declaration -- see metabase#55045
- .required(t`Please enter a correctly formatted URL`),
- // eslint-disable-next-line ttag/no-module-declaration -- see metabase#55045
- name: Yup.string().required(t`Please add a name`),
- // eslint-disable-next-line ttag/no-module-declaration -- see metabase#55045
- description: Yup.string().required(t`Please add a description`),
- "auth-method": Yup.string()
- .required()
- .equals(["none", "header", "query-param", "request-body"]),
- "fe-form-type": Yup.string()
- .required()
- .equals(["none", "basic", "bearer", "api-key"]),
- "auth-info": Yup.object(),
-});
+function getValidationSchema() {
+ return Yup.object({
+ url: Yup.string()
+ .required(t`Please enter a correctly formatted URL`)
+ .test(
+ "is-http-url",
+ t`Please enter a correctly formatted URL`,
+ (value) => value != null && isValidWebhookUrl(value),
+ ),
+ name: Yup.string().required(t`Please add a name`),
+ description: Yup.string().required(t`Please add a description`),
+ "auth-method": Yup.string()
+ .required()
+ .equals(["none", "header", "query-param", "request-body"]),
+ "fe-form-type": Yup.string()
+ .required()
+ .equals(["none", "basic", "bearer", "api-key"]),
+ "auth-info": Yup.object(),
+ });
+}
+
+/**
+ * Approximates the backend validator (metabase.util/url?), which unlike Yup's
+ * .url() accepts bare hostnames such as http://webhook-tester:8080/
+ */
+function isValidWebhookUrl(value: string): boolean {
+ if (value !== value.trim() || !value.includes("://")) {
+ return false;
+ }
+
+ try {
+ const { protocol } = new URL(value);
+ return protocol === "http:" || protocol === "https:";
+ } catch {
+ return false;
+ }
+}
const styles = {
wrapperProps: {
@@ -177,6 +196,7 @@ export const WebhookForm = ({
const { label: testButtonLabel, setLabel: setTestButtonLabel } =
useActionButtonLabel({ defaultLabel: t`Send a test` });
const [testChannel, { error }] = useTestChannelMutation();
+ const [validationSchema] = useState(getValidationSchema());
const errorData = useMemo(() => {
if (isNotificationChannelTestErrorResponse(error)) {
diff --git a/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.unit.spec.tsx
index dce385a8492..82360132b35 100644
--- a/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.unit.spec.tsx
+++ b/frontend/src/metabase/admin/settings/components/widgets/Notifications/WebhookForm.unit.spec.tsx
@@ -54,15 +54,42 @@ describe("WebhookForm", () => {
jest.useRealTimers();
});
- it("should error when an invalid url is given", async () => {
- await setup();
+ it.each(["A-bad-url", "webhook-tester:8080", " http://padded.example.com/"])(
+ "should error when an invalid url is given (%s)",
+ async (url) => {
+ await setup();
+ await userEvent.type(
+ await screen.findByLabelText("Webhook URL"),
+ `${url}{tab}`,
+ );
+ expect(
+ await screen.findByText("Please enter a correctly formatted URL"),
+ ).toBeInTheDocument();
+ },
+ );
+
+ it("should accept bare-hostname URLs (metabase#74812)", async () => {
+ const { onSubmit } = await setup();
await userEvent.type(
await screen.findByLabelText("Webhook URL"),
- "A-bad-url{tab}",
+ "http://webhook-tester:8080/",
+ );
+ await userEvent.type(screen.getByLabelText("Give it a name"), "Local hook");
+ await userEvent.type(
+ screen.getByLabelText("Description"),
+ "Docker network hook",
+ );
+ await userEvent.click(
+ screen.getByRole("button", { name: "Create destination" }),
);
+
expect(
- await screen.findByText("Please enter a correctly formatted URL"),
- ).toBeInTheDocument();
+ screen.queryByText("Please enter a correctly formatted URL"),
+ ).not.toBeInTheDocument();
+ expect(onSubmit).toHaveBeenCalledWith(
+ expect.objectContaining({ url: "http://webhook-tester:8080/" }),
+ expect.anything(),
+ );
});
it("should error when no name is provided", async () => {