Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: enhance safeRequest functionality and improve URL validation

Victor Hugo dos Santos7eeb90370f41
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 80726957a9..3900236fd9 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -265,9 +265,9 @@ Migration tip: when refactoring code that built a custom `https.Agent`, drop the
**Opt-outs that skip pinning** (in all three, `safeRequest` falls back to Axios's default agent and there is no rebinding protection):
- `NODE_ENV=development` — validation is bypassed entirely.
- Calling `blockLocalAndPrivateIpAddresses(url, isGateway = true)` — gateway-routed traffic doesn't go through Axios DNS, so pinning would do nothing.
-- `allowPrivateIps: true` option — caller skips the private-IP refusal but **still** pins DNS to the validated IPs (rebinding protection is preserved).
+- `allowPrivateIps: true` option — caller skips the private-IP refusal **and** DNS pinning **and** the Infisical internal-infrastructure blocklist (`verifyHostInputValidity`). `validateSsrfUrl` short-circuits when this flag is set, so no DNS resolution happens at validation time and Node's default resolver is used at connect time.
-**`allowPrivateIps: true` is only acceptable when there is a stronger runtime trust boundary than the IP class.** Existing legitimate callers:
+**`allowPrivateIps: true` is only acceptable when there is a stronger runtime trust boundary than the IP class** (e.g., TLS cert verification + cluster token, or save-time host validation). The flag does not preserve rebinding protection or the internal-infra blocklist on its own.
**Gateway-aware patterns**
diff --git a/backend/src/lib/validator/validate-url.test.ts b/backend/src/lib/validator/validate-url.test.ts
index f9fab6f3a0..fbf05f5705 100644
--- a/backend/src/lib/validator/validate-url.test.ts
+++ b/backend/src/lib/validator/validate-url.test.ts
@@ -341,6 +341,40 @@ describe("validate-url SSRF helpers", () => {
expect(lastRequestArgs().method).toBe("DELETE");
});
+ it("safeRequest.delete preserves options.data (Axios DELETE-with-body)", async () => {
+ requestRequestMock.mockResolvedValue({ data: undefined, status: 200, headers: {} });
+ const body = { workspaceId: "w-1", environment: "prod", secretPath: "/", secrets: [{ secretKey: "K" }] };
+ await safeRequest.delete("https://example.com/v3/secrets/batch/raw", {
+ headers: { Authorization: "Bearer t" },
+ data: body
+ });
+ const args = lastRequestArgs();
+ expect(args.method).toBe("DELETE");
+ expect(args.data).toEqual(body);
+ expect(args.headers).toEqual({ Authorization: "Bearer t" });
+ });
+
+ it("safeRequest.get preserves options.data (Axios GET-with-body, unusual but legal)", async () => {
+ requestRequestMock.mockResolvedValue({ data: undefined, status: 200, headers: {} });
+ const body = { query: "x" };
+ await safeRequest.get("https://example.com/search", { data: body });
+ const args = lastRequestArgs();
+ expect(args.method).toBe("GET");
+ expect(args.data).toEqual(body);
+ });
+
+ it("positional body wins over options.data for post/put/patch (matches Axios)", async () => {
+ requestRequestMock.mockResolvedValue({ data: undefined, status: 200, headers: {} });
+ const positional = { positional: true };
+ const optionsData = { positional: false };
+ await safeRequest.post("https://example.com", positional, { data: optionsData } as any);
+ expect(lastRequestArgs().data).toEqual(positional);
+ await safeRequest.put("https://example.com", positional, { data: optionsData } as any);
+ expect(lastRequestArgs().data).toEqual(positional);
+ await safeRequest.patch("https://example.com", positional, { data: optionsData } as any);
+ expect(lastRequestArgs().data).toEqual(positional);
+ });
+
it("safeRequest.request validates absolute `url` even when baseURL is set (Axios-compatible)", async () => {
requestRequestMock.mockResolvedValueOnce({ data: undefined, status: 200, headers: {} });
await safeRequest.request({
diff --git a/backend/src/lib/validator/validate-url.ts b/backend/src/lib/validator/validate-url.ts
index d3a9006717..9adc02c0bc 100644
--- a/backend/src/lib/validator/validate-url.ts
+++ b/backend/src/lib/validator/validate-url.ts
@@ -432,7 +432,7 @@ const dispatch = async <T>(
...axiosOpts,
method,
url,
- data,
+ ...(data !== undefined && { data }),
maxRedirects: 0,
httpAgent: protocol === "http:" ? (agent as http.Agent | undefined) : undefined,
httpsAgent: protocol === "https:" ? (agent as https.Agent | undefined) : undefined
diff --git a/backend/src/services/external-migration/external-migration-fns/vault.ts b/backend/src/services/external-migration/external-migration-fns/vault.ts
index 89eec5c5ff..a9da5610c5 100644
--- a/backend/src/services/external-migration/external-migration-fns/vault.ts
+++ b/backend/src/services/external-migration/external-migration-fns/vault.ts
@@ -1,3 +1,4 @@
+import http from "node:http";
import https from "node:https";
import axios, { AxiosInstance, isAxiosError } from "axios";
@@ -31,7 +32,12 @@ const vaultFactory = (
targetHostname: string;
targetPort: number;
},
- gatewayCallback: (host: string, port: number, httpsAgent?: https.Agent, hostHeader?: string) => Promise<T>
+ gatewayCallback: (
+ host: string,
+ port: number,
+ agents?: { httpsAgent?: https.Agent; httpAgent?: http.Agent },
+ hostHeader?: string
+ ) => Promise<T>
): Promise<T> => {
const { gatewayId, targetProtocol, targetHostname, targetPort } = inputs;
@@ -46,7 +52,7 @@ const vaultFactory = (
const httpsAgent = isHttps ? new https.Agent({ servername: targetHostname }) : undefined;
return withGatewayV2Proxy(
- async (port) => gatewayCallback(`${targetProtocol}://localhost`, port, httpsAgent, targetHostname),
+ async (port) => gatewayCallback(`${targetProtocol}://localhost`, port, { httpsAgent }, targetHostname),
{
protocol: GatewayProxyProtocol.Tcp,
relayHost: gatewayV2Details.relayHost,
@@ -58,7 +64,7 @@ const vaultFactory = (
const relayDetails = await gatewayService.fnGetGatewayClientTlsByGatewayId(gatewayId);
- return withGatewayProxy(async (port, httpsAgent) => gatewayCallback("http://localhost", port, httpsAgent), {
+ return withGatewayProxy(async (port, httpsAgent) => gatewayCallback("http://localhost", port, { httpsAgent }), {
protocol: GatewayProxyProtocol.Http,
targetHost: `${targetProtocol}://${targetHostname}`,
targetPort,
@@ -229,7 +235,12 @@ const vaultFactory = (
accessToken: string;
gatewayId?: string;
}): Promise<VaultData[]> {
- const getData = async (host: string, port?: number, httpsAgent?: https.Agent, hostHeader?: string) => {
+ const getData = async (
+ host: string,
+ port?: number,
+ agents?: { httpsAgent?: https.Agent; httpAgent?: http.Agent },
+ hostHeader?: string
+ ) => {
const allData: VaultData[] = [];
const request = axios.create({
@@ -240,7 +251,8 @@ const vaultFactory = (
...(hostHeader ? { Host: hostHeader } : {})
},
maxRedirects: 0,
- httpsAgent
+ httpsAgent: agents?.httpsAgent,
+ httpAgent: agents?.httpAgent
});
// Get all mounts in this namespace
@@ -311,7 +323,10 @@ const vaultFactory = (
} else {
const pinnedAgent = await buildSsrfSafeAgent(baseUrl, { rejectUnauthorized: true });
const isHttps = new URL(baseUrl).protocol === "https:";
- data = await getData(baseUrl, undefined, isHttps ? (pinnedAgent as https.Agent | undefined) : undefined);
+ const agents = isHttps
+ ? { httpsAgent: pinnedAgent as https.Agent | undefined }
+ : { httpAgent: pinnedAgent as http.Agent | undefined };
+ data = await getData(baseUrl, undefined, agents);
}
return data;