Skip to content

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

Greptile comment fixes

Carlos Monastyrskie57039f07352
diff --git a/backend/src/lib/validator/safe-request.socket.test.ts b/backend/src/lib/validator/safe-request.socket.test.ts
index 18c2bb8f5a..6be80efbd6 100644
--- a/backend/src/lib/validator/safe-request.socket.test.ts
+++ b/backend/src/lib/validator/safe-request.socket.test.ts
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
+import type { LookupAllOptions } from "node:dns";
import http from "node:http";
import type { AddressInfo } from "node:net";
@@ -14,7 +15,8 @@ import { buildSsrfSafeAgent } from "./safe-request";
// or Docker, and never opens a socket to anything other than 127.0.0.1 (the
// control test does a single DNS lookup of an RFC 6761 `.invalid` name, which
// resolves nowhere and contacts no real external host).
-const { configState } = vi.hoisted(() => ({
+const { configState, validationAnswer } = vi.hoisted(() => ({
+ validationAnswer: { address: "127.0.0.1" },
configState: {
isDevelopmentMode: false,
// Allow the loopback address to pass validation so we can pin to it.
@@ -26,6 +28,15 @@ const { configState } = vi.hoisted(() => ({
}
}));
+vi.mock("node:dns/promises", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("node:dns/promises")>();
+ const lookup = (hostname: string, options: LookupAllOptions) =>
+ hostname === "pinned-rebind.invalid"
+ ? Promise.resolve([{ address: validationAnswer.address, family: 4 }])
+ : actual.lookup(hostname, options);
+ return { ...actual, lookup, default: { ...actual, lookup } };
+});
+
vi.mock("@app/lib/config/env", () => ({
getConfig: () => configState
}));
@@ -96,18 +107,23 @@ describe("safe-request real-socket pinning", () => {
});
});
- it("pins the connection to the validated IP even when the connect-time hostname differs (anti-rebinding)", async () => {
- // The agent was validated/pinned against 127.0.0.1. We then issue a request
- // to a different, unresolvable hostname reusing that agent, simulating a
- // DNS record that flipped between validation and connect. Because the pinned
- // lookup ignores connect-time DNS, the socket still lands on the loopback
- // server. If pinning were bypassed, this would fail with ENOTFOUND (proven
- // by the control test above).
- const agent = await buildSsrfSafeAgent(`http://127.0.0.1:${port}`, { keepAlive: false });
+ it("pins the connection to the validated IP when connect-time DNS for the same hostname differs (anti-rebinding)", async () => {
+ validationAnswer.address = "127.0.0.1";
+ const agent = await buildSsrfSafeAgent(`http://pinned-rebind.invalid:${port}`, { keepAlive: false });
const res = await httpGet({ hostname: "pinned-rebind.invalid", port, path: "/rebind", agent });
expect(res.status).toBe(200);
expect(receivedPaths).toContain("/rebind");
});
+
+ it("resolves hostnames other than the validated one normally, so a forward proxy is reached at its own address", async () => {
+ validationAnswer.address = "192.0.2.1";
+ const agent = await buildSsrfSafeAgent(`http://pinned-rebind.invalid:${port}`, { keepAlive: false });
+
+ const res = await httpGet({ hostname: "localhost", port, path: "/proxy-host", agent });
+
+ expect(res.status).toBe(200);
+ expect(receivedPaths).toContain("/proxy-host");
+ });
});
diff --git a/backend/src/lib/validator/safe-request.test.ts b/backend/src/lib/validator/safe-request.test.ts
index 17f54096a6..95ea89017a 100644
--- a/backend/src/lib/validator/safe-request.test.ts
+++ b/backend/src/lib/validator/safe-request.test.ts
@@ -132,6 +132,16 @@ describe("safe-request SSRF helpers", () => {
expect(lookupMock).not.toHaveBeenCalled();
});
+ it("allows literal local hostnames when ALLOW_INTERNAL_IP_CONNECTIONS is set", async () => {
+ configState.ALLOW_INTERNAL_IP_CONNECTIONS = true;
+ setLookup([{ address: LOOPBACK_IP_V4, family: 4 }]);
+
+ await expect(validateAndPinUrl("http://localhost:8080")).resolves.toEqual({
+ hostname: "localhost",
+ entries: [{ address: LOOPBACK_IP_V4, family: 4 }]
+ });
+ });
+
it("rejects RFC1918 / loopback IPs by default", async () => {
setLookup([{ address: PRIVATE_IP_V4, family: 4 }]);
await expect(validateAndPinUrl("https://example.com")).rejects.toThrow(/Local IPs not allowed/i);
diff --git a/backend/src/lib/validator/safe-request.ts b/backend/src/lib/validator/safe-request.ts
index faa792ee67..640a26874e 100644
--- a/backend/src/lib/validator/safe-request.ts
+++ b/backend/src/lib/validator/safe-request.ts
@@ -1,4 +1,4 @@
-import { LookupAddress } from "node:dns";
+import { lookup as dnsLookup, LookupAddress } from "node:dns";
import dns from "node:dns/promises";
import http from "node:http";
import https from "node:https";
@@ -64,7 +64,7 @@ export const validateAndPinUrl = async (
if (isIP(rawHost)) {
entries = [{ address: rawHost, family: isIP(rawHost) }];
} else {
- if (rawHost === "localhost" || rawHost === "host.docker.internal") {
+ if (!appCfg.ALLOW_INTERNAL_IP_CONNECTIONS && (rawHost === "localhost" || rawHost === "host.docker.internal")) {
logger.warn(`safeRequest: rejecting literal local hostname [hostname=${rawHost}]`);
throw new BadRequestError({ message: "Local IPs not allowed as URL" });
}
@@ -118,8 +118,12 @@ const pickPreferredEntry = (entries: LookupAddress[]): LookupAddress =>
// dns.lookup-shaped function that always returns the pre-validated IPs.
// Installed on an http(s).Agent so connect-time DNS cannot land on a different
// IP than the one validation approved.
-const makePinnedLookup = (entries: LookupAddress[]): LookupFunction =>
+const makePinnedLookup = ({ hostname: validatedHostname, entries }: TValidatedHost): LookupFunction =>
((hostname: string, optionsOrCb: unknown, maybeCb?: unknown) => {
+ if ((hostname.endsWith(".") ? hostname.slice(0, -1) : hostname) !== validatedHostname) {
+ (dnsLookup as (...args: unknown[]) => void)(hostname, optionsOrCb, maybeCb);
+ return;
+ }
if (typeof optionsOrCb === "function") {
const first = pickPreferredEntry(entries);
(optionsOrCb as TLookupOneCallback)(null, first.address, first.family);
@@ -241,7 +245,7 @@ const constructAgent = (
opts: TBuildAgentOptions
): http.Agent | https.Agent => {
const isHttps = protocol === "https:";
- const lookup = validated ? makePinnedLookup(validated.entries) : undefined;
+ const lookup = validated ? makePinnedLookup(validated) : undefined;
// Default keepAlive: true so cached agents reuse connections.
const baseOpts: http.AgentOptions = {
keepAlive: opts.keepAlive ?? true,