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
feat: adds scope org id column and fixes inject identity middleware
Piyush Guptab94b1d480f9e
diff --git a/backend/src/db/migrations/20251127192155_adds-scope-org-id-to-identity-access-tokens.ts b/backend/src/db/migrations/20251127192155_adds-scope-org-id-to-identity-access-tokens.tsnew file mode 100644index 0000000000..cc55c1fb6b--- /dev/null+++ b/backend/src/db/migrations/20251127192155_adds-scope-org-id-to-identity-access-tokens.ts@@ -0,0 +1,16 @@+import { Knex } from "knex";++import { TableName } from "../schemas";++export async function up(knex: Knex): Promise<void> {+ await knex.schema.alterTable(TableName.IdentityAccessToken, (t) => {+ t.uuid("scopeOrgId").notNullable();+ t.foreign("scopeOrgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");+ });+}++export async function down(knex: Knex): Promise<void> {+ await knex.schema.alterTable(TableName.IdentityAccessToken, (t) => {+ t.dropColumn("scopeOrgId");+ });+}diff --git a/backend/src/db/schemas/identity-access-tokens.ts b/backend/src/db/schemas/identity-access-tokens.tsindex 8f2b8b73bf..dfa962439e 100644--- a/backend/src/db/schemas/identity-access-tokens.ts+++ b/backend/src/db/schemas/identity-access-tokens.ts@@ -22,7 +22,8 @@ export const IdentityAccessTokensSchema = z.object({updatedAt: z.date(),name: z.string().nullable().optional(),authMethod: z.string(),- accessTokenPeriod: z.coerce.number().default(0)+ accessTokenPeriod: z.coerce.number().default(0),+ scopeOrgId: z.string().uuid().nullable().optional()});export type TIdentityAccessTokens = z.infer<typeof IdentityAccessTokensSchema>;diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.tsindex e6ba2eec74..a25430d99d 100644--- a/backend/src/server/plugins/auth/inject-identity.ts+++ b/backend/src/server/plugins/auth/inject-identity.ts@@ -8,7 +8,6 @@ import { TScimTokenJwtPayload } from "@app/ee/services/scim/scim-types";import { getConfig } from "@app/lib/config/env";import { crypto } from "@app/lib/crypto";import { BadRequestError } from "@app/lib/errors";-import { slugSchema } from "@app/server/lib/schemas";import { ActorType, AuthMethod, AuthMode, AuthModeJwtTokenPayload, AuthTokenType } from "@app/services/auth/auth-type";import { TIdentityAccessTokenJwtPayload } from "@app/services/identity-access-token/identity-access-token-types";import { getServerCfg } from "@app/services/super-admin/super-admin-service";@@ -152,15 +151,10 @@ export const injectIdentity = fp(if (!authMode) return;- const subOrganizationSelector = req.headers?.["x-infisical-org"] as string | undefined;- if (subOrganizationSelector) {- await slugSchema().parseAsync(subOrganizationSelector);- }-switch (authMode) {case AuthMode.JWT: {const { user, tokenVersionId, orgId, orgName, rootOrgId, parentOrgId } =- await server.services.authToken.fnValidateJwtIdentity(token, subOrganizationSelector);+ await server.services.authToken.fnValidateJwtIdentity(token);requestContext.set("orgId", orgId);requestContext.set("orgName", orgName);requestContext.set("userAuthInfo", { userId: user.id, email: user.email || "" });@@ -180,11 +174,7 @@ export const injectIdentity = fp(break;}case AuthMode.IDENTITY_ACCESS_TOKEN: {- const identity = await server.services.identityAccessToken.fnValidateIdentityAccessToken(- token,- req.realIp,- subOrganizationSelector- );+ const identity = await server.services.identityAccessToken.fnValidateIdentityAccessToken(token, req.realIp);const serverCfg = await getServerCfg();requestContext.set("orgId", identity.orgId);requestContext.set("orgName", identity.orgName);@@ -223,9 +213,6 @@ export const injectIdentity = fp(const serviceToken = await server.services.serviceToken.fnValidateServiceToken(token);requestContext.set("orgId", serviceToken.orgId);- if (subOrganizationSelector)- throw new BadRequestError({ message: `Service token doesn't support sub organization selector` });-req.auth = {orgId: serviceToken.orgId,rootOrgId: serviceToken.rootOrgId,@@ -248,9 +235,6 @@ export const injectIdentity = fp(const { orgId, scimTokenId } = await server.services.scim.fnValidateScimToken(token);requestContext.set("orgId", orgId);- if (subOrganizationSelector)- throw new BadRequestError({ message: `SCIM token doesn't support sub organization selector` });-req.auth = {authMode: AuthMode.SCIM_TOKEN,actor,diff --git a/backend/src/services/identity-access-token/identity-access-token-dal.ts b/backend/src/services/identity-access-token/identity-access-token-dal.tsindex 74b624a7e1..7c98ac9f95 100644--- a/backend/src/services/identity-access-token/identity-access-token-dal.ts+++ b/backend/src/services/identity-access-token/identity-access-token-dal.ts@@ -18,7 +18,8 @@ export const identityAccessTokenDALFactory = (db: TDbClient) => {.where(filter).join(TableName.Identity, `${TableName.Identity}.id`, `${TableName.IdentityAccessToken}.identityId`).select(selectAllTableCols(TableName.IdentityAccessToken))- .select(db.ref("orgId").withSchema(TableName.Identity).as("identityScopeOrgId"))+ .select(db.ref("orgId").withSchema(TableName.Identity).as("identityOrgId"))+ .select(db.ref("scopeOrgId").withSchema(TableName.IdentityAccessToken).as("scopeOrgId")).first();return doc;diff --git a/backend/src/services/identity-access-token/identity-access-token-service.ts b/backend/src/services/identity-access-token/identity-access-token-service.tsindex 244e98908f..cf39b38c9c 100644--- a/backend/src/services/identity-access-token/identity-access-token-service.ts+++ b/backend/src/services/identity-access-token/identity-access-token-service.ts@@ -184,11 +184,7 @@ export const identityAccessTokenServiceFactory = ({return { revokedToken };};- const fnValidateIdentityAccessToken = async (- token: TIdentityAccessTokenJwtPayload,- ipAddress?: string,- subOrganizationSelector?: string- ) => {+ const fnValidateIdentityAccessToken = async (token: TIdentityAccessTokenJwtPayload, ipAddress?: string) => {const identityAccessToken = await identityAccessTokenDAL.findOne({[`${TableName.IdentityAccessToken}.id` as "id"]: token.identityAccessTokenId,isAccessTokenRevoked: false@@ -209,46 +205,32 @@ export const identityAccessTokenServiceFactory = ({trustedIps: trustedIps as TIp[]});}- let orgId = "";- let orgName = "";- let parentOrgId = "";- const identityOrgDetails = await orgDAL.findOne({ id: identityAccessToken.identityScopeOrgId });- const rootOrgId = identityOrgDetails.rootOrgId || identityOrgDetails.id;-- if (subOrganizationSelector) {- const subOrganization = await orgDAL.findOne({ rootOrgId, slug: subOrganizationSelector });- if (!subOrganization)- throw new BadRequestError({ message: `Sub organization ${subOrganizationSelector} not found` });-- const identityOrgMembership = await membershipIdentityDAL.findOne({- scope: AccessScope.Organization,- actorIdentityId: identityAccessToken.identityId,- scopeOrgId: subOrganization.id- });- if (!identityOrgMembership) {- throw new BadRequestError({ message: "Identity does not belong to this organization" });- }- orgId = subOrganization.id;- orgName = subOrganization.name;+ const scopeOrgId = identityAccessToken.scopeOrgId || identityAccessToken.identityOrgId;- parentOrgId = subOrganization.parentOrgId as string;- } else {- const identityOrgMembership = await membershipIdentityDAL.findOne({- scope: AccessScope.Organization,- actorIdentityId: identityAccessToken.identityId,- scopeOrgId: identityOrgDetails.id- });+ const identityOrgDetails = await orgDAL.findOne({ id: scopeOrgId });- if (!identityOrgMembership) {- throw new BadRequestError({ message: "Identity does not belong to this organization" });- }+ const isSubOrg = !!(identityOrgDetails.rootOrgId || identityOrgDetails.parentOrgId);- orgId = identityOrgDetails.id;- orgName = identityOrgDetails.name;- parentOrgId = rootOrgId;+ const rootOrgId = isSubOrg+ ? identityOrgDetails.rootOrgId || identityOrgDetails.parentOrgId || identityOrgDetails.id+ : identityOrgDetails.id;++ // Verify identity membership in the organization+ const identityOrgMembership = await membershipIdentityDAL.findOne({+ scope: AccessScope.Organization,+ actorIdentityId: identityAccessToken.identityId,+ scopeOrgId: identityOrgDetails.id+ });++ if (!identityOrgMembership) {+ throw new BadRequestError({ message: "Identity does not belong to this organization" });}+ const orgId = identityOrgDetails.id;+ const orgName = identityOrgDetails.name;+ const parentOrgId = identityOrgDetails.parentOrgId || rootOrgId;+let { accessTokenNumUses } = identityAccessToken;const tokenStatusInCache = await accessTokenQueue.getIdentityTokenDetailsInCache(identityAccessToken.id);if (tokenStatusInCache) {diff --git a/backend/src/services/identity-access-token/identity-access-token-types.ts b/backend/src/services/identity-access-token/identity-access-token-types.tsindex e7b73a8c1a..b7505810d2 100644--- a/backend/src/services/identity-access-token/identity-access-token-types.ts+++ b/backend/src/services/identity-access-token/identity-access-token-types.ts@@ -7,6 +7,7 @@ export type TIdentityAccessTokenJwtPayload = {clientSecretId: string;identityAccessTokenId: string;authTokenType: string;+ subOrganizationId?: string;identityAuth: {oidc?: {claims: Record<string, string>;diff --git a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityTable.tsx b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityTable.tsxindex b94846f072..a3c8abaa63 100644--- a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityTable.tsx+++ b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityTable.tsx@@ -304,7 +304,7 @@ export const IdentityTable = ({ handlePopUpOpen }: Props) => {to: "/organizations/$orgId/identities/$identityId",params: {identityId: id,- orgId+ orgId: currentOrg.id}})}