Skip to content

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

fix: allow for empty target URLs

Daniel Hougaarda6271a618751
diff --git a/backend/src/lib/gateway/gateway.ts b/backend/src/lib/gateway/gateway.ts
index 179c29fc80..46481a0496 100644
--- a/backend/src/lib/gateway/gateway.ts
+++ b/backend/src/lib/gateway/gateway.ts
@@ -149,8 +149,8 @@ const setupProxyServer = async ({
protocol = GatewayProxyProtocol.Tcp,
httpsAgent
}: {
- targetHost: string;
- targetPort: number;
+ targetHost?: string;
+ targetPort?: number;
relayPort: number;
relayHost: string;
tlsOptions: TGatewayTlsOptions;
@@ -183,27 +183,44 @@ const setupProxyServer = async ({
let command: string;
if (protocol === GatewayProxyProtocol.Http) {
- const targetUrl = `${targetHost}:${targetPort}`; // note(daniel): targetHost MUST include the scheme (https|http)
- command = `FORWARD-HTTP ${targetUrl}`;
- logger.debug(`Using HTTP proxy mode: ${command.trim()}`);
-
- // extract ca certificate from httpsAgent if present
- if (httpsAgent && targetHost.startsWith("https://")) {
- const agentOptions = httpsAgent.options;
- if (agentOptions && agentOptions.ca) {
- const caCert = Array.isArray(agentOptions.ca) ? agentOptions.ca.join("\n") : agentOptions.ca;
- const caB64 = Buffer.from(caCert as string).toString("base64");
- command += ` ca=${caB64}`;
-
- const rejectUnauthorized = agentOptions.rejectUnauthorized !== false;
- command += ` verify=${rejectUnauthorized}`;
-
- logger.debug(`Using HTTP proxy mode [command=${command.trim()}]`);
+ if (!targetHost && !targetPort) {
+ command = `FORWARD-HTTP`;
+ logger.debug(`Using HTTP proxy mode, no target URL provided [command=${command.trim()}]`);
+ } else {
+ if (!targetHost || targetPort === undefined) {
+ throw new BadRequestError({
+ message: `Target host and port are required for HTTP proxy mode with custom target`
+ });
+ }
+
+ const targetUrl = `${targetHost}:${targetPort}`; // note(daniel): targetHost MUST include the scheme (https|http)
+ command = `FORWARD-HTTP ${targetUrl}`;
+ logger.debug(`Using HTTP proxy mode, custom target URL provided [command=${command.trim()}]`);
+
+ // extract ca certificate from httpsAgent if present
+ if (httpsAgent && targetHost.startsWith("https://")) {
+ const agentOptions = httpsAgent.options;
+ if (agentOptions && agentOptions.ca) {
+ const caCert = Array.isArray(agentOptions.ca) ? agentOptions.ca.join("\n") : agentOptions.ca;
+ const caB64 = Buffer.from(caCert as string).toString("base64");
+ command += ` ca=${caB64}`;
+
+ const rejectUnauthorized = agentOptions.rejectUnauthorized !== false;
+ command += ` verify=${rejectUnauthorized}`;
+
+ logger.debug(`Using HTTP proxy mode, custom target URL provided [command=${command.trim()}]`);
+ }
}
}
command += "\n";
} else if (protocol === GatewayProxyProtocol.Tcp) {
+ if (!targetHost || !targetPort) {
+ throw new BadRequestError({
+ message: `Target host and port are required for TCP proxy mode`
+ });
+ }
+
// For TCP mode, send FORWARD-TCP with host:port
command = `FORWARD-TCP ${targetHost}:${targetPort}\n`;
logger.debug(`Using TCP proxy mode: ${command.trim()}`);
diff --git a/backend/src/lib/gateway/types.ts b/backend/src/lib/gateway/types.ts
index fdb13d2562..8552fbf544 100644
--- a/backend/src/lib/gateway/types.ts
+++ b/backend/src/lib/gateway/types.ts
@@ -15,8 +15,8 @@ export enum GatewayHttpProxyActions {
}
export interface IGatewayProxyOptions {
- targetHost: string;
- targetPort: number;
+ targetHost?: string;
+ targetPort?: number;
relayHost: string;
relayPort: number;
tlsOptions: TGatewayTlsOptions;
diff --git a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts
index b599c4ff95..1f89745a97 100644
--- a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts
+++ b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts
@@ -72,8 +72,8 @@ export const identityKubernetesAuthServiceFactory = ({
const $gatewayProxyWrapper = async <T>(
inputs: {
gatewayId: string;
- targetHost: string;
- targetPort: number;
+ targetHost?: string;
+ targetPort?: number;
caCert?: string;
reviewTokenThroughGateway: boolean;
},
@@ -286,8 +286,6 @@ export const identityKubernetesAuthServiceFactory = ({
data = await $gatewayProxyWrapper(
{
gatewayId: identityKubernetesAuth.gatewayId,
- targetHost: `/`, // note(daniel): the targetURL will be constructed as `/:0`, which the gateway will handle as a special case, by replacing the /:0, with the internal kubernetes base URL (only when the action header is set to `GatewayHttpProxyActions.UseGatewayK8sServiceAccount`)
- targetPort: 0,
reviewTokenThroughGateway: true
},
tokenReviewCallbackThroughGateway
diff --git a/cli/packages/gateway/connection.go b/cli/packages/gateway/connection.go
index 460956d3e4..980137374d 100644
--- a/cli/packages/gateway/connection.go
+++ b/cli/packages/gateway/connection.go
@@ -108,19 +108,17 @@ func handleStream(stream quic.Stream, quicConn quic.Connection) {
return
case "FORWARD-HTTP":
+ targetURL := ""
argParts := bytes.Split(args, []byte(" "))
- if len(argParts) == 0 {
- log.Error().Msg("FORWARD-HTTP requires target URL")
- return
- }
-
- targetURL := string(argParts[0])
- // ? note(daniel): special case: if the target URL is "/:0", we don't validate it.
- // ? the reason for this is because we want to be able to send requests to the gateway without knowing the actual target URL, and instead let the gateway construct the target URL.
- if targetURL != "/:0" && !isValidURL(targetURL) {
- log.Error().Msgf("Invalid target URL: %s", targetURL)
- return
+ if len(argParts) == 0 || len(argParts[0]) == 0 {
+ log.Warn().Msg("FORWARD-HTTP used without a target URL.")
+ } else {
+ targetURL = string(argParts[0])
+ if !isValidURL(targetURL) {
+ log.Error().Msgf("Invalid target URL: %s", targetURL)
+ return
+ }
}
// Parse optional parameters
@@ -208,8 +206,7 @@ func handleHTTPProxy(stream quic.Stream, reader *bufio.Reader, targetURL string,
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", string(token)))
log.Info().Msgf("Injected gateway k8s SA auth token in request to %s", targetURL)
- } else if actionHeader == HttpProxyActionUseGatewayK8sServiceAccount {
-
+ } else if actionHeader == HttpProxyActionUseGatewayK8sServiceAccount { // will work without a target URL set
// set the ca cert to the pod's k8s service account ca cert:
caCert, err := os.ReadFile(KUBERNETES_SERVICE_ACCOUNT_CA_CERT_PATH)
if err != nil {
@@ -218,9 +215,7 @@ func handleHTTPProxy(stream quic.Stream, reader *bufio.Reader, targetURL string,
}
caCertPool := x509.NewCertPool()
- appendSuccess := caCertPool.AppendCertsFromPEM(caCert)
-
- if !appendSuccess {
+ if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
stream.Write([]byte(buildHttpInternalServerError("failed to parse k8s sa ca cert")))
continue
}