Skip to content

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

improvement(auth): explain an access token from another instance (#8781)

Saif Ur Rahmanee5ca258c0dc
diff --git a/backend/src/server/plugins/auth/inject-identity.test.ts b/backend/src/server/plugins/auth/inject-identity.test.ts
index 3b316d1a5e..dac15e16fd 100644
--- a/backend/src/server/plugins/auth/inject-identity.test.ts
+++ b/backend/src/server/plugins/auth/inject-identity.test.ts
@@ -1,4 +1,5 @@
import type { FastifyRequest } from "fastify";
+import jwt from "jsonwebtoken";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { crypto } from "@app/lib/crypto";
@@ -10,6 +11,11 @@ vi.mock("@app/lib/logger", () => ({
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() }
}));
+vi.mock("@app/lib/config/env", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@app/lib/config/env")>()),
+ getConfig: () => ({ INFISICAL_CLOUD: true, INTERNAL_REGION: "us" })
+}));
+
const AUTH_SECRET = "test-secret-for-extract-auth-unit-tests";
const baseAccessClaims = {
@@ -75,3 +81,29 @@ describe("extractAuth — OAuth access token classification", () => {
expect(result.authMode).toBeNull();
});
});
+
+describe("extractAuth — token from another instance", () => {
+ beforeAll(async () => {
+ process.env.FIPS_ENABLED = "false";
+ await crypto.initialize({} as never, {} as never, {} as never);
+ });
+
+ afterAll(() => {
+ delete process.env.FIPS_ENABLED;
+ });
+
+ test("a token signed with another instance's secret names the likely cause", async () => {
+ const token = crypto.jwt().sign(baseAccessClaims, "another-instance-secret", { expiresIn: "1h" });
+ const attempt = extractAuth(makeReq(`Bearer ${token}`), AUTH_SECRET);
+
+ // Still a JsonWebTokenError, so the error handler answers with the same 403 TokenError as before.
+ await expect(attempt).rejects.toBeInstanceOf(jwt.JsonWebTokenError);
+ await expect(attempt).rejects.toThrow("may have been issued by a different Infisical instance: this is US Cloud");
+ });
+
+ test("other token errors keep the library message", async () => {
+ const attempt = extractAuth(makeReq("Bearer not-a-jwt"), AUTH_SECRET);
+
+ await expect(attempt).rejects.toThrow("jwt malformed");
+ });
+});
diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.ts
index 9650b310a4..d39caaebaa 100644
--- a/backend/src/server/plugins/auth/inject-identity.ts
+++ b/backend/src/server/plugins/auth/inject-identity.ts
@@ -1,7 +1,7 @@
import { requestContext, RequestContextData } from "@fastify/request-context";
import { FastifyRequest } from "fastify";
import fp from "fastify-plugin";
-import type { JwtPayload } from "jsonwebtoken";
+import jwt, { type JwtPayload } from "jsonwebtoken";
import { TServiceTokens, TUsers } from "@app/db/schemas";
import { TScimTokenJwtPayload } from "@app/ee/services/scim/scim-types";
@@ -25,6 +25,8 @@ import { TIdentityAccessTokenJwtPayload } from "@app/services/identity-access-to
import { OauthDelegationMode } from "@app/services/oauth-client/oauth-client-types";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
+import { invalidSignatureMessage } from "./invalid-signature-message";
+
export type TAuthMode =
| {
authMode: AuthMode.JWT | AuthMode.OAUTH;
@@ -150,7 +152,16 @@ export const extractAuth = async (req: FastifyRequest, jwtSecret: string) => {
} as const;
}
- const decodedToken = crypto.jwt().verify(authTokenValue, jwtSecret) as JwtPayload;
+ let decodedToken: JwtPayload;
+ try {
+ decodedToken = crypto.jwt().verify(authTokenValue, jwtSecret) as JwtPayload;
+ } catch (error) {
+ // Rethrown as the same error class so the response keeps its 403 TokenError shape.
+ if (error instanceof jwt.JsonWebTokenError && error.message === "invalid signature") {
+ throw new jwt.JsonWebTokenError(invalidSignatureMessage(getConfig()));
+ }
+ throw error;
+ }
switch (decodedToken.authTokenType) {
case AuthTokenType.ACCESS_TOKEN: {
diff --git a/backend/src/server/plugins/auth/invalid-signature-message.test.ts b/backend/src/server/plugins/auth/invalid-signature-message.test.ts
new file mode 100644
index 0000000000..b4e42473a8
--- /dev/null
+++ b/backend/src/server/plugins/auth/invalid-signature-message.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, test } from "vitest";
+
+import { invalidSignatureMessage } from "./invalid-signature-message";
+
+describe("invalidSignatureMessage", () => {
+ test("US Cloud points at EU Cloud", () => {
+ expect(invalidSignatureMessage({ INFISICAL_CLOUD: true, INTERNAL_REGION: "us" })).toBe(
+ "The access token's signature could not be verified. It may have been issued by a different Infisical instance: this is US Cloud (https://app.infisical.com); if your organization is on EU Cloud, use https://eu.infisical.com."
+ );
+ });
+
+ test("EU Cloud points at US Cloud", () => {
+ expect(invalidSignatureMessage({ INFISICAL_CLOUD: true, INTERNAL_REGION: "eu" })).toBe(
+ "The access token's signature could not be verified. It may have been issued by a different Infisical instance: this is EU Cloud (https://eu.infisical.com); if your organization is on US Cloud, use https://app.infisical.com."
+ );
+ });
+
+ test("Cloud without a region lists both regions", () => {
+ expect(invalidSignatureMessage({ INFISICAL_CLOUD: true, INTERNAL_REGION: undefined })).toBe(
+ "The access token's signature could not be verified. It may have been issued by a different Infisical instance: if your organization is on Infisical Cloud, use the region that issued it (US: https://app.infisical.com, EU: https://eu.infisical.com)."
+ );
+ });
+
+ test("self-hosted gets no Cloud region advice", () => {
+ const message = invalidSignatureMessage({ INFISICAL_CLOUD: false, INTERNAL_REGION: undefined });
+ expect(message).toBe(
+ "The access token's signature could not be verified. It may have been issued by a different Infisical instance."
+ );
+ expect(message).not.toContain("infisical.com");
+ });
+});
diff --git a/backend/src/server/plugins/auth/invalid-signature-message.ts b/backend/src/server/plugins/auth/invalid-signature-message.ts
new file mode 100644
index 0000000000..744db5f340
--- /dev/null
+++ b/backend/src/server/plugins/auth/invalid-signature-message.ts
@@ -0,0 +1,28 @@
+import type { TEnvConfig } from "@app/lib/config/env";
+
+const CLOUD_REGIONS = {
+ us: { name: "US Cloud", url: "https://app.infisical.com" },
+ eu: { name: "EU Cloud", url: "https://eu.infisical.com" }
+} as const;
+
+// jsonwebtoken reports a token signed with another key as a bare "invalid signature". On a Bearer token that
+// is most often a token from another instance, usually the other Infisical Cloud region, and the bare message
+// gives no hint of that. It can also be a token signed before this instance's secret changed, or an edited one,
+// so the message says "may". Self-hosted and dedicated instances get no region advice, since it would send their
+// users somewhere their tokens never came from.
+export const invalidSignatureMessage = (cfg: Pick<TEnvConfig, "INFISICAL_CLOUD" | "INTERNAL_REGION">) => {
+ const base =
+ "The access token's signature could not be verified. It may have been issued by a different Infisical instance";
+
+ if (!cfg.INFISICAL_CLOUD) {
+ return `${base}.`;
+ }
+
+ if (cfg.INTERNAL_REGION) {
+ const here = CLOUD_REGIONS[cfg.INTERNAL_REGION];
+ const other = CLOUD_REGIONS[cfg.INTERNAL_REGION === "us" ? "eu" : "us"];
+ return `${base}: this is ${here.name} (${here.url}); if your organization is on ${other.name}, use ${other.url}.`;
+ }
+
+ return `${base}: if your organization is on Infisical Cloud, use the region that issued it (US: ${CLOUD_REGIONS.us.url}, EU: ${CLOUD_REGIONS.eu.url}).`;
+};