Skip to content

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

reject ttl greater than instance max TTL

Thiago Araujo da Silva810ea92bd4a8
diff --git a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts
index cc14a3dbf4..d1d061d7ad 100644
--- a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts
+++ b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts
@@ -498,15 +498,30 @@ describe("Access token TTL is per application", async () => {
await deleteClient(client.id);
});
- // The application's TTL is a ceiling, not an override, so asking for longer than the instance
- // allows must not widen it past JWT_AUTH_LIFETIME.
- test("a TTL longer than the instance lifetime is capped, not honoured", async () => {
- const { client, token } = await issueAccessToken(90 * 24 * 60 * 60);
+ // A TTL the instance can never honour is a configuration mistake, so it is rejected at the write
+ // rather than stored and quietly shortened at every issuance.
+ test("rejects a TTL longer than the instance lifetime", async () => {
+ const res = await createClient({
+ name: "e2e-access-token-ttl-over-instance-lifetime",
+ redirectUris: [REDIRECT_URI],
+ accessTokenTTL: 90 * 24 * 60 * 60
+ });
- expect(client.accessTokenTTL).toBe(90 * 24 * 60 * 60);
- expect(token.statusCode).toBe(200);
- const { expires_in: expiresIn } = JSON.parse(token.payload) as { expires_in: number };
- expect(expiresIn).toBeLessThan(client.accessTokenTTL);
+ expect(res.statusCode).toBe(400);
+ expect(res.payload).toContain("The access token lifetime cannot exceed");
+ });
+
+ test("rejects an update that raises the TTL past the instance lifetime", async () => {
+ const { client } = await issueAccessToken(3600);
+
+ const res = await testServer.inject({
+ method: "PATCH",
+ url: `/api/v1/oauth/clients/${client.id}`,
+ headers: { authorization: `Bearer ${jwtAuthToken}` },
+ body: { accessTokenTTL: 90 * 24 * 60 * 60 }
+ });
+
+ expect(res.statusCode).toBe(400);
await deleteClient(client.id);
});
diff --git a/backend/src/server/routes/v1/oauth-router.ts b/backend/src/server/routes/v1/oauth-router.ts
index 588806cdd0..9a5e9a353b 100644
--- a/backend/src/server/routes/v1/oauth-router.ts
+++ b/backend/src/server/routes/v1/oauth-router.ts
@@ -53,7 +53,7 @@ const accessTokenTtlSchema = z
.min(MIN_OAUTH_ACCESS_TOKEN_TTL_SECONDS)
.max(MAX_OAUTH_ACCESS_TOKEN_TTL_SECONDS)
.describe(
- "How long, in seconds, the access tokens this application issues stay valid. A ceiling rather than an override: the instance and organization token lifetimes still apply, so the issued token's 'expires_in' is the shortest of the three."
+ "How long, in seconds, the access tokens this application issues stay valid. Cannot exceed the instance's login session lifetime. The organization's session length still applies on top, so the issued token's 'expires_in' is the shorter of the two."
);
const tokenExchangeIdpSatisfiesMfaSchema = z
diff --git a/backend/src/services/oauth-client/oauth-client-service.ts b/backend/src/services/oauth-client/oauth-client-service.ts
index b1b28a750a..d1bb44b097 100644
--- a/backend/src/services/oauth-client/oauth-client-service.ts
+++ b/backend/src/services/oauth-client/oauth-client-service.ts
@@ -78,13 +78,22 @@ const sanitizeOauthClient = (client: TOauthClients) => {
return rest;
};
-// The column is a plain text[] in Postgres, so the generated schema types it as `string[]`. Every write
-// goes through the router's `grantTypesSchema`, so the narrowing holds.
const getGrantTypes = (client: TOauthClients) => client.grantTypes as OauthGrantType[];
const expiresInToSeconds = (expiresIn: string | number) =>
typeof expiresIn === "number" ? expiresIn : Math.floor(ms(expiresIn) / 1000);
+const assertAccessTokenTtlWithinInstanceLimit = (accessTokenTTL?: number) => {
+ if (accessTokenTTL === undefined) return;
+
+ const instanceLimit = expiresInToSeconds(getConfig().JWT_AUTH_LIFETIME);
+ if (accessTokenTTL > instanceLimit) {
+ throw new BadRequestError({
+ message: `The access token lifetime cannot exceed ${instanceLimit} seconds.`
+ });
+ }
+};
+
type TOauthTokenClaims = {
authMethod: AuthMethod;
userId: string;
@@ -296,6 +305,8 @@ export const oauthClientServiceFactory = ({
const createOauthClient = async (dto: TCreateOauthClientDTO, actor: OrgServiceActor) => {
await checkOauthClientPermission(actor, OrgPermissionActions.Create);
+ assertAccessTokenTtlWithinInstanceLimit(dto.accessTokenTTL);
+
const { grantTypes, isTokenExchangeEnabled, redirectUris, tokenExchangeAudience, tokenExchangeIdpSatisfiesMfa } =
await resolveGrantConfig({
dto,
@@ -345,6 +356,8 @@ export const oauthClientServiceFactory = ({
const updateOauthClient = async (dto: TUpdateOauthClientDTO, actor: OrgServiceActor) => {
await checkOauthClientPermission(actor, OrgPermissionActions.Edit);
+ assertAccessTokenTtlWithinInstanceLimit(dto.accessTokenTTL);
+
const client = await getOrgClientOrThrow(dto.clientDbId, actor.orgId);
const {
@@ -590,9 +603,9 @@ export const oauthClientServiceFactory = ({
return client;
};
- // The application's own TTL is a ceiling, not an override: the instance's JWT_AUTH_LIFETIME and the
- // org's session length still apply, so an application cannot hand out a token that outlives the org's
- // policy. Refresh tokens follow the org alone, since they exist to outlive individual access tokens.
+ // Writes reject a TTL above JWT_AUTH_LIFETIME, but both it and the org's session length can be lowered
+ // after the fact, so issuance still caps rather than trusting the stored value. Refresh tokens follow
+ // the org alone, since they exist to outlive individual access tokens.
const resolveTokenLifetimes = (client: Pick<TOauthClients, "accessTokenTTL">, org?: TOrganizations) => {
const appCfg = getConfig();
let accessTokenExpiresIn: string | number = getMinExpiresIn(appCfg.JWT_AUTH_LIFETIME, client.accessTokenTTL);