AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): keep a non-default port on a host-scoped credential's host (#14710)
Reinier van der Leer1dfc50c119c3
diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/__tests__/helpers.test.tsnew file mode 100644index 0000000000..d25bf28268--- /dev/null+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/__tests__/helpers.test.ts@@ -0,0 +1,54 @@+import { describe, expect, it } from "vitest";++import { findSavedUserCredentialByProviderAndType } from "../helpers";++function providersWithHost(host: string) {+ return {+ http: {+ savedCredentials: [+ { id: "cred-1", provider: "http", type: "host_scoped", host },+ ],+ },+ } as never;+}++function findFor(host: string, url: string) {+ return findSavedUserCredentialByProviderAndType(+ ["http"],+ ["host_scoped"],+ undefined,+ providersWithHost(host),+ [url],+ );+}++describe("findSavedUserCredentialByProviderAndType, host-scoped matching", () => {+ it("offers a credential whose host names the URL's non-default port", () => {+ expect(+ findFor("api.example.com:8443", "https://api.example.com:8443/v1")?.id,+ ).toBe("cred-1");+ expect(+ findFor("api.example.com:8443", "HTTPS://api.example.com:8443/v1")?.id,+ ).toBe("cred-1");+ });++ // The backend refuses a request port outside 80/443 unless the credential+ // names it, so offering either of these would save headers that never send.+ it("does not offer one whose port disagrees with the URL", () => {+ expect(+ findFor("api.example.com", "https://api.example.com:8443/v1"),+ ).toBeUndefined();+ expect(+ findFor("api.example.com:8443", "https://api.example.com/v1"),+ ).toBeUndefined();+ });++ it("still matches when neither names a port", () => {+ expect(findFor("api.example.com", "https://api.example.com/v1")?.id).toBe(+ "cred-1",+ );+ expect(+ findFor("api.example.com", "https://api.example.com:443/v1")?.id,+ ).toBe("cred-1");+ });+});diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/helpers.ts b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/helpers.tsindex 2d8d001a72..d7c36aedaa 100644--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/helpers.ts+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsGroupedView/helpers.ts@@ -1,4 +1,5 @@import { CredentialsProvidersContextType } from "@/providers/agent-credentials/credentials-provider";+import { getHostFromUrl } from "@/lib/utils/url";import { filterSystemCredentials, getSystemCredentials } from "../../helpers";export type CredentialField = [string, any];@@ -38,13 +39,9 @@ function matchesDiscriminatorValues(// Host-scoped credentials match by hostif (credential.type === "host_scoped" && credential.host) {if (!discriminatorValues || discriminatorValues.length === 0) return true;- return discriminatorValues.some((v) => {- try {- return new URL(v).hostname === credential.host;- } catch {- return false;- }- });+ return discriminatorValues.some(+ (v) => getHostFromUrl(v) === credential.host,+ );}return true;}diff --git a/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/ConnectMethodView/__tests__/InlineHostScopedForm.test.tsx b/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/ConnectMethodView/__tests__/InlineHostScopedForm.test.tsxnew file mode 100644index 0000000000..661e26f686--- /dev/null+++ b/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/ConnectMethodView/__tests__/InlineHostScopedForm.test.tsx@@ -0,0 +1,105 @@+import { server } from "@/mocks/mock-server";+import { render, screen, waitFor } from "@/tests/integrations/test-utils";+import userEvent from "@testing-library/user-event";+import { http, HttpResponse } from "msw";+import { describe, expect, it, vi } from "vitest";++vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: vi.fn(),+ useToast: () => ({ toast: vi.fn() }),+ useToastOnFail: () => () => {},+}));++import { InlineHostScopedForm } from "../InlineHostScopedForm";++const CREATE_CREDENTIALS_URL =+ "http://localhost:3000/api/proxy/api/integrations/:provider/credentials";++const PREFILLED_HOST = "api.example.com:8443";++async function findConnectButton() {+ return (await screen.findByRole("button", {+ name: "Connect",+ })) as HTMLButtonElement;+}++async function fillOneHeader() {+ await userEvent.type(+ await screen.findByLabelText("Header name"),+ "Authorization",+ );+ await userEvent.type(+ await screen.findByLabelText("Header value"),+ "Bearer t",+ );+}++describe("InlineHostScopedForm with a prefilled host", () => {+ it("shows the host the block calls, port included, and locks it", async () => {+ render(+ <InlineHostScopedForm+ provider="http"+ host={PREFILLED_HOST}+ onSuccess={vi.fn()}+ />,+ );++ const hostField = (await screen.findByLabelText(+ "Host",+ )) as HTMLInputElement;+ expect(hostField.value).toBe(PREFILLED_HOST);+ expect(hostField.readOnly).toBe(true);+ });++ // The prefilled host is never edited, so a gate reading react-hook-form's+ // isValid — false until the first change — would leave Connect dead.+ it("enables Connect without touching the prefilled host", async () => {+ render(+ <InlineHostScopedForm+ provider="http"+ host={PREFILLED_HOST}+ onSuccess={vi.fn()}+ />,+ );++ expect((await findConnectButton()).disabled).toBe(true);++ await fillOneHeader();++ await waitFor(async () =>+ expect((await findConnectButton()).disabled).toBe(false),+ );+ });++ it("saves the prefilled host verbatim", async () => {+ const posted = vi.fn();+ server.use(+ http.post(CREATE_CREDENTIALS_URL, async ({ request }) => {+ posted(await request.json());+ return HttpResponse.json({+ id: "cred-1",+ provider: "http",+ type: "host_scoped",+ title: PREFILLED_HOST,+ });+ }),+ );+ const onSuccess = vi.fn();++ render(+ <InlineHostScopedForm+ provider="http"+ host={PREFILLED_HOST}+ onSuccess={onSuccess}+ />,+ );++ await fillOneHeader();+ await userEvent.click(await findConnectButton());++ await waitFor(() => expect(onSuccess).toHaveBeenCalled());+ expect(posted).toHaveBeenCalledWith(+ expect.objectContaining({ host: PREFILLED_HOST }),+ );+ });+});diff --git a/autogpt_platform/frontend/src/lib/utils/__tests__/url.test.ts b/autogpt_platform/frontend/src/lib/utils/__tests__/url.test.tsindex 4f71f08c98..bae395c80c 100644--- a/autogpt_platform/frontend/src/lib/utils/__tests__/url.test.ts+++ b/autogpt_platform/frontend/src/lib/utils/__tests__/url.test.ts@@ -22,7 +22,22 @@ describe("getHostFromUrl", () => {it("assumes http for a bare host", () => {expect(getHostFromUrl("api.example.com/v1")).toBe("api.example.com");- expect(getHostFromUrl("api.example.com:8443")).toBe("api.example.com");+ expect(getHostFromUrl("api.example.com:8443")).toBe("api.example.com:8443");+ });++ it("keeps a non-default port", () => {+ expect(getHostFromUrl("https://api.example.com:8443/v1")).toBe(+ "api.example.com:8443",+ );+ expect(getHostFromUrl("http://localhost:8080/api")).toBe("localhost:8080");+ expect(getHostFromUrl("HTTPS://api.example.com:8443/v1")).toBe(+ "api.example.com:8443",+ );+ });++ it("drops a port that is the scheme's default", () => {+ expect(getHostFromUrl("https://x.com:443/")).toBe("x.com");+ expect(getHostFromUrl("http://x.com:80/")).toBe("x.com");});it("returns null for a string it cannot parse", () => {diff --git a/autogpt_platform/frontend/src/lib/utils/url.ts b/autogpt_platform/frontend/src/lib/utils/url.tsindex cc513914ae..142838af29 100644--- a/autogpt_platform/frontend/src/lib/utils/url.ts+++ b/autogpt_platform/frontend/src/lib/utils/url.ts@@ -1,7 +1,7 @@/**- * Extracts the hostname from a URL string.- * @param url - The URL string to extract the hostname from- * @returns The hostname if valid, null if invalid+ * Extracts the host from a URL string.+ * @param url - The URL string to extract the host from+ * @returns The host, including a non-default port, if valid; null if invalid*/export const getHostFromUrl = (url: string): string | null => {try {@@ -9,7 +9,9 @@ export const getHostFromUrl = (url: string): string | null => {url = "http://" + url; // Add a scheme if missing for URL parsing}const urlObj = new URL(url);- return urlObj.hostname;+ // `host`, not `hostname`: the backend refuses a request port outside+ // 80/443 unless the credential's host names it. `URL` drops default ports.+ return urlObj.host;} catch {return null;}