Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
chore(deps): keep the URL out of axios's malformed-protocol error in safeRequest
Vlad Ligai3599302407df
diff --git a/backend/src/lib/validator/safe-request.test.ts b/backend/src/lib/validator/safe-request.test.tsindex 95ea89017a..399db1f9be 100644--- a/backend/src/lib/validator/safe-request.test.ts+++ b/backend/src/lib/validator/safe-request.test.ts@@ -2,6 +2,7 @@import type { AgentOptions as HttpAgentOptions } from "node:http";import type { Agent as HttpsAgent } from "node:https";+import { AxiosError } from "axios";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { buildSsrfSafeAgent, getSharedHttpsAgent, safeRequest, validateAndPinUrl } from "./safe-request";@@ -470,6 +471,27 @@ describe("safe-request SSRF helpers", () => {expect(args.httpAgent).toBeDefined();});+ it.each([+ ["post", (url: string) => safeRequest.post(url, {})],+ ["request", (url: string) => safeRequest.request({ method: "POST", url })]+ ])("safeRequest.%s drops the URL from Axios's missing-slashes error, keeping its code", async (_name, send) => {+ const url = "http:example.com/hooks/s3cret";+ requestRequestMock.mockRejectedValueOnce(+ new AxiosError(`Invalid URL ${JSON.stringify(url)}: missing "//" after protocol`, AxiosError.ERR_INVALID_URL)+ );+ const err = (await send(url).catch((e: unknown) => e)) as AxiosError;+ expect(err).toBeInstanceOf(AxiosError);+ expect(err.code).toBe(AxiosError.ERR_INVALID_URL);+ expect(err.message).toBe('Invalid URL: missing "//" after protocol');+ expect(err.stack).not.toContain("s3cret");+ });++ it("rethrows other request errors unchanged", async () => {+ const networkError = new AxiosError("connect ECONNREFUSED", "ECONNREFUSED");+ requestRequestMock.mockRejectedValueOnce(networkError);+ await expect(safeRequest.get("https://example.com")).rejects.toBe(networkError);+ });+it("plumbs validateStatus through dispatch", async () => {requestRequestMock.mockResolvedValueOnce({ data: undefined, status: 401, headers: {} });const validateStatus = (s: number) => s < 500;diff --git a/backend/src/lib/validator/safe-request.ts b/backend/src/lib/validator/safe-request.tsindex 640a26874e..e408e14e1a 100644--- a/backend/src/lib/validator/safe-request.ts+++ b/backend/src/lib/validator/safe-request.ts@@ -4,7 +4,7 @@ import http from "node:http";import https from "node:https";import { LookupFunction } from "node:net";-import { AxiosRequestConfig } from "axios";+import { AxiosError, AxiosRequestConfig, isAxiosError } from "axios";import { isIP } from "net";import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";@@ -386,6 +386,17 @@ const resolveBaseUrl = (url: string, baseURL?: string): string => {return combineURLs(baseURL, url);};+// Axios embeds the requested URL, path included, in this error's message, and a+// path can carry a secret (webhook tokens). Rethrow it without the URL.+const MISSING_SLASHES_SUFFIX = 'missing "//" after protocol';++const withoutUrlInMessage = (err: unknown): unknown => {+ if (isAxiosError(err) && err.code === AxiosError.ERR_INVALID_URL && err.message.endsWith(MISSING_SLASHES_SUFFIX)) {+ return new AxiosError(`Invalid URL: ${MISSING_SLASHES_SUFFIX}`, err.code, err.config, err.request, err.response);+ }+ return err;+};+const logDispatch = (method: string, effectiveUrl: string, validated: TValidatedHost | undefined) => {const pinned = validated ? formatEntries(validated.entries) : "none";logger.debug(@@ -407,18 +418,22 @@ const dispatch = async <T>(const agent = buildPinnedAgent(validated, protocol, { addressFamily, ca, rejectUnauthorized, servername });logDispatch(method, effectiveUrl, validated);- return request.request<T>({- ...axiosOpts,- method,- url,- ...(data !== undefined && { data }),- maxRedirects: 0,- // Force direct egress so an ambient HTTP(S)_PROXY can't route around the- // pinned agent (the proxy would re-resolve the target, defeating the pin).- ...(appCfg.SAFE_REQUEST_FORCE_DIRECT_EGRESS && { proxy: false as const }),- httpAgent: protocol === "http:" ? (agent as http.Agent | undefined) : undefined,- httpsAgent: protocol === "https:" ? (agent as https.Agent | undefined) : undefined- });+ return request+ .request<T>({+ ...axiosOpts,+ method,+ url,+ ...(data !== undefined && { data }),+ maxRedirects: 0,+ // Force direct egress so an ambient HTTP(S)_PROXY can't route around the+ // pinned agent (the proxy would re-resolve the target, defeating the pin).+ ...(appCfg.SAFE_REQUEST_FORCE_DIRECT_EGRESS && { proxy: false as const }),+ httpAgent: protocol === "http:" ? (agent as http.Agent | undefined) : undefined,+ httpsAgent: protocol === "https:" ? (agent as https.Agent | undefined) : undefined+ })+ .catch((err: unknown) => {+ throw withoutUrlInMessage(err);+ });};const dispatchFull = async <T>(config: TSafeRequestFullConfig) => {@@ -430,16 +445,20 @@ const dispatchFull = async <T>(config: TSafeRequestFullConfig) => {const agent = buildPinnedAgent(validated, protocol, { addressFamily, ca, rejectUnauthorized, servername });logDispatch(axiosOpts.method ?? "GET", effectiveUrl, validated);- return request.request<T>({- ...axiosOpts,- url,- maxRedirects: 0,- // Force direct egress so an ambient HTTP(S)_PROXY can't route around the- // pinned agent (the proxy would re-resolve the target, defeating the pin).- ...(appCfg.SAFE_REQUEST_FORCE_DIRECT_EGRESS && { proxy: false as const }),- httpAgent: protocol === "http:" ? (agent as http.Agent | undefined) : undefined,- httpsAgent: protocol === "https:" ? (agent as https.Agent | undefined) : undefined- });+ return request+ .request<T>({+ ...axiosOpts,+ url,+ maxRedirects: 0,+ // Force direct egress so an ambient HTTP(S)_PROXY can't route around the+ // pinned agent (the proxy would re-resolve the target, defeating the pin).+ ...(appCfg.SAFE_REQUEST_FORCE_DIRECT_EGRESS && { proxy: false as const }),+ httpAgent: protocol === "http:" ? (agent as http.Agent | undefined) : undefined,+ httpsAgent: protocol === "https:" ? (agent as https.Agent | undefined) : undefined+ })+ .catch((err: unknown) => {+ throw withoutUrlInMessage(err);+ });};// HTTP client that validates the URL, pins the connection to the validated