Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
return 401 in case of revoked session
Thiago Araujo da Silva837d43982cc5
diff --git a/backend/e2e-test/routes/v1/oauth-scope.spec.ts b/backend/e2e-test/routes/v1/oauth-scope.spec.tsindex 94e415a851..62ef1d330f 100644--- a/backend/e2e-test/routes/v1/oauth-scope.spec.ts+++ b/backend/e2e-test/routes/v1/oauth-scope.spec.ts@@ -6,12 +6,12 @@ import { AuthMethod, AuthTokenType } from "@app/services/auth/auth-type";// Mints a delegated OAuth access token for the seed user (an org admin) with the given scopes. It// reuses the same session (tokenVersionId/accessVersion) as the working jwtAuthToken, so it passes// signature + session validation and only differs by the oauthClientId/scopes claims.-const signOauthToken = (scopes: string[]) =>+const signOauthToken = (scopes: string[], tokenVersionId = seedData1.token.id) =>jwt.sign({authTokenType: AuthTokenType.ACCESS_TOKEN,userId: seedData1.id,- tokenVersionId: seedData1.token.id,+ tokenVersionId,authMethod: AuthMethod.EMAIL,organizationId: seedData1.organization.id,accessVersion: 1,@@ -109,4 +109,30 @@ describe("OAuth delegated token scope enforcement", async () => {// 200 (configured) or 404 (no TOTP set up) — the point is it is NOT 403/forbidden by auth mode.expect(res.statusCode).not.toBe(403);});++ const validate = async (token: string) =>+ testServer.inject({+ method: "GET",+ url: `/api/v1/oauth/validate`,+ headers: { authorization: `Bearer ${token}` }+ });++ test("validate confirms a live delegated token", async () => {+ const res = await validate(signOauthToken(["secrets:read"]));+ expect(res.statusCode).toBe(200);+ expect(JSON.parse(res.payload)).toEqual({ active: true });+ });++ // Revocation deletes the session row, so the token's session lookup misses. Middleware reads this+ // endpoint to decide whether to obtain a fresh token, and a 404 there is indistinguishable from a+ // wrong URL, so the dead-session case has to answer 401.+ test("validate answers 401, not 404, for a revoked delegated token", async () => {+ const res = await validate(signOauthToken(["secrets:read"], "00000000-0000-0000-0000-000000000000"));+ expect(res.statusCode).toBe(401);+ });++ test("a revoked delegated token is 401 on a data route too", async () => {+ const res = await readSecrets(signOauthToken(["secrets:read"], "00000000-0000-0000-0000-000000000000"));+ expect(res.statusCode).toBe(401);+ });});diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.tsindex 38ab2d40f4..4982b61165 100644--- a/backend/src/server/plugins/auth/inject-identity.ts+++ b/backend/src/server/plugins/auth/inject-identity.ts@@ -7,7 +7,7 @@ import { TServiceTokens, TUsers } from "@app/db/schemas";import { TScimTokenJwtPayload } from "@app/ee/services/scim/scim-types";import { getConfig } from "@app/lib/config/env";import { crypto } from "@app/lib/crypto";-import { BadRequestError, UnauthorizedError } from "@app/lib/errors";+import { BadRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";import { RequestContextKey } from "@app/lib/request-context/request-context-keys";import {ActorType,@@ -311,8 +311,17 @@ export const injectIdentity = fp(break;}case AuthMode.OAUTH: {- const { user, tokenVersionId, orgId, orgName, rootOrgId, parentOrgId } =- await server.services.authToken.fnValidateJwtIdentity(token);+ const { user, tokenVersionId, orgId, orgName, rootOrgId, parentOrgId } = await server.services.authToken+ .fnValidateJwtIdentity(token)+ .catch((err) => {+ if (err instanceof NotFoundError) {+ throw new UnauthorizedError({+ name: "InvalidToken",+ message: "Access token is no longer valid. Obtain a new one."+ });+ }+ throw err;+ });requestContext.set(RequestContextKey.OrgId, orgId);requestContext.set(RequestContextKey.OrgName, orgName);if (token.delegation !== OauthDelegationMode.Full) {diff --git a/docs/documentation/platform/oauth-applications/authorization-code.mdx b/docs/documentation/platform/oauth-applications/authorization-code.mdxindex 010334e849..0d33ad42cd 100644--- a/docs/documentation/platform/oauth-applications/authorization-code.mdx+++ b/docs/documentation/platform/oauth-applications/authorization-code.mdx@@ -149,6 +149,8 @@ GET /api/v1/oauth/validateA lightweight introspection endpoint. Send the delegated access token as a Bearer token; a `200` with `{ "active": true }` confirms the token is valid.+Any other status means the token is not usable. A `401` covers an expired, revoked, or otherwise dead token, including one whose application was deleted. Middleware should treat any 4xx from this endpoint as a signal to obtain a fresh token rather than to retry.+## Accessing the APIUse the access token as a standard Bearer token on the supported read-only secret endpoints:diff --git a/docs/documentation/platform/oauth-applications/token-exchange.mdx b/docs/documentation/platform/oauth-applications/token-exchange.mdxindex 37156349de..033c65ce3e 100644--- a/docs/documentation/platform/oauth-applications/token-exchange.mdx+++ b/docs/documentation/platform/oauth-applications/token-exchange.mdx@@ -199,6 +199,7 @@ Exchanging per request makes your identity provider a hard dependency of every I- Refresh slightly early, say 60 seconds before `expires_in` elapses, so an in-flight request never carries a token that expires mid-call.- Exchange again when it is gone. No refresh token is issued.- Drop the entry when the user's session at your identity provider ends, so their Infisical access does not outlive it.+- Exchange again on any 4xx from `GET /api/v1/oauth/validate` or from an API call made with the cached token. A cached token can die before its expiry, most often because the application was deleted, and retrying with it never succeeds.<Note>On Infisical Cloud the token endpoint is rate limited per source IP, so every instance of your middleware behind one egress address shares a budget. Caching correctly means roughly one exchange per user per token lifetime, which stays well inside it. A self-hosted instance applies no limit of its own, so set one at your ingress.