Skip to content

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

fix: harden template-sourced reviewer JWT against connection repointing

Scott Wilson13b92cb1ea88
diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts
index 28f383f4f6..45b3d5dda9 100644
--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts
+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts
@@ -152,17 +152,20 @@ describe("identityAuthTemplateServiceFactory kubernetes host validation", () =>
);
});
- it("still permits unrelated edits when a private host sits behind a deleted gateway", async () => {
- // rotating the reviewer JWT cannot repoint the dial target, so it must not be blocked by a
- // host that is only private because it was always reached through the now-deleted gateway
+ it("blocks even an unrelated edit while a private host sits behind a deleted gateway", async () => {
+ // ON DELETE SET NULL can strip the gateway out-of-band, leaving a direct-dial private host.
+ // login dials that host with no address block of its own, so any patch that would propagate
+ // it must re-vet the host, not just the ones that touch a dial field. the template is not
+ // stranded: re-adding a gateway or repointing to a public host in the same patch clears it
const { service, identityKubernetesAuthDAL } = createService({
blobFields: { ...baseBlobFields, kubernetesHost: PRIVATE_HOST },
gatewayColumns: NO_GATEWAY
});
- await patchTemplate(service, { tokenReviewerJwt: "rotated-jwt" });
-
- expect(identityKubernetesAuthDAL.updateByTemplateId).toHaveBeenCalled();
+ await expect(patchTemplate(service, { tokenReviewerJwt: "rotated-jwt" })).rejects.toThrow(
+ "Local IPs not allowed as URL"
+ );
+ expect(identityKubernetesAuthDAL.updateByTemplateId).not.toHaveBeenCalled();
});
});
diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts
index ec9cd39764..a07920e5d3 100644
--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts
+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts
@@ -448,14 +448,15 @@ export const identityAuthTemplateServiceFactory = ({
gatewayPoolId: resolvedGatewayPoolId
};
}
- // the merged host propagates to every linked identity and is dialed by the backend at
- // login, so it gets the same local/private-address block as the identity attach flow.
- // only patches that can change where we dial are checked, so losing a gateway to a
- // deletion does not strand the template: its host is legitimately private, and an edit
- // that cannot repoint the dial target has no new address to vet
- const patchAffectsDialing = patchTouchesGateway || "kubernetesHost" in patch || "tokenReviewMode" in patch;
+ // the merged host propagates to every linked identity on this patch and is dialed
+ // directly by the backend at login (which runs no address block of its own), so a
+ // private host reachable only through a gateway becomes an SSRF vector the moment the
+ // gateway is gone. the gateway FK is ON DELETE SET NULL, so a gateway can be deleted
+ // out-of-band and leave a private-host/no-gateway config behind without this code ever
+ // running; checking only patches that touch dial fields would then let an unrelated edit
+ // propagate that config unvalidated. so vet any direct-dial (API-mode, host, no gateway)
+ // merge on every propagation, not just the ones that repoint it here
if (
- patchAffectsDialing &&
merged.tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api &&
merged.kubernetesHost &&
!merged.gatewayId &&
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 0f1b5c9466..6c287a0e0a 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
@@ -1265,23 +1265,33 @@ export const identityKubernetesAuthServiceFactory = ({
}
}
- // a template-sourced reviewer JWT stays write-only to the caller, so it must only ever be
- // sent to the host the template chose. unlinking deliberately leaves the credential in
- // place to keep login working, which would otherwise let an actor with EditAuth alone
- // repoint the host and have the unauthenticated login endpoint deliver the JWT to a server
- // they control. keyed on the provenance flag rather than on the unlink itself, so a repoint
- // in a later request is caught too
- if (
- !template &&
- identityKubernetesAuth.isTokenReviewerJwtTemplateSourced &&
- tokenReviewerJwt === undefined &&
- kubernetesHost !== undefined &&
- kubernetesHost !== identityKubernetesAuth.kubernetesHost
- ) {
- throw new BadRequestError({
- message:
- "This identity still uses the token reviewer JWT copied from its auth template, so its Kubernetes host cannot be changed. Supply your own tokenReviewerJwt with this change, or set tokenReviewerJwt to null to remove the stored one."
- });
+ // a template-sourced reviewer JWT stays write-only to the caller, so it must only ever
+ // travel to the destination, and over the trust, that the template chose. unlinking
+ // deliberately leaves the credential in place to keep login working, which would otherwise
+ // let an actor with identity EditAuth (plus AttachGateways, for the gateway fields) repoint
+ // the connection and have the unauthenticated login endpoint deliver the JWT to a server or
+ // gateway they control, or downgrade TLS trust to intercept it in transit. keyed on the
+ // provenance flag rather than on the unlink itself, so a repoint in a later request is caught
+ // too. every field below decides where or how the JWT is sent; the host alone is not enough
+ // (a gateway tunnels it in API mode, and CA/verify settings guard it on the wire)
+ if (!template && identityKubernetesAuth.isTokenReviewerJwtTemplateSourced && tokenReviewerJwt === undefined) {
+ const storedGatewayId = identityKubernetesAuth.gatewayV2Id ?? identityKubernetesAuth.gatewayId ?? null;
+ const repointsConnection =
+ (kubernetesHost !== undefined && kubernetesHost !== identityKubernetesAuth.kubernetesHost) ||
+ (gatewayId !== undefined && (gatewayId ?? null) !== storedGatewayId) ||
+ (gatewayPoolId !== undefined && (gatewayPoolId ?? null) !== (identityKubernetesAuth.gatewayPoolId ?? null)) ||
+ (verifyTlsCertificate !== undefined && verifyTlsCertificate !== identityKubernetesAuth.verifyTlsCertificate) ||
+ (caCert !== undefined &&
+ caCert !==
+ (identityKubernetesAuth.encryptedKubernetesCaCertificate
+ ? decryptor({ cipherTextBlob: identityKubernetesAuth.encryptedKubernetesCaCertificate }).toString()
+ : ""));
+ if (repointsConnection) {
+ throw new BadRequestError({
+ message:
+ "This identity still uses the token reviewer JWT copied from its auth template, so its Kubernetes connection settings (host, gateway, gateway pool, CA certificate, or TLS verification) cannot be changed. Supply your own tokenReviewerJwt with this change, or set tokenReviewerJwt to null to remove the stored one."
+ });
+ }
}
const reformattedAccessTokenTrustedIps = accessTokenTrustedIps?.map((accessTokenTrustedIp) => {