Skip to content

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

feat(auth): enhance token validation with organization scope checks

Victor Hugo dos Santos3b56bdb7e8a6
diff --git a/backend/src/server/routes/v1/auth-router.ts b/backend/src/server/routes/v1/auth-router.ts
index 3477a55d59..8a22cbce76 100644
--- a/backend/src/server/routes/v1/auth-router.ts
+++ b/backend/src/server/routes/v1/auth-router.ts
@@ -107,6 +107,11 @@ export const registerAuthRoutes = async (server: FastifyZodProvider) => {
({ decodedToken, tokenVersion, isGraceHit } = await server.services.authToken.validateRefreshToken(
req.cookies.jid
));
+ await server.services.authToken.validateTokenOrgScope({
+ userId: decodedToken.userId,
+ organizationId: decodedToken.organizationId,
+ subOrganizationId: decodedToken.subOrganizationId
+ });
} catch (err) {
// Clear the expired/invalid jid cookie so it doesn't cause login loops
// when the browser keeps sending the stale cookie on subsequent requests.
diff --git a/backend/src/services/auth-token/auth-token-service.ts b/backend/src/services/auth-token/auth-token-service.ts
index 129b28f52b..95bc04c6a1 100644
--- a/backend/src/services/auth-token/auth-token-service.ts
+++ b/backend/src/services/auth-token/auth-token-service.ts
@@ -386,73 +386,79 @@ export const tokenServiceFactory = ({ tokenDAL, userDAL, orgDAL, keyStore }: TAu
return { user };
};
- // to parse jwt identity in inject identity plugin
- const fnValidateJwtIdentity = async (token: AuthModeJwtTokenPayload) => {
- const { user } = await validateUserSessionFreshness({
- userId: token.userId,
- tokenVersionId: token.tokenVersionId,
- accessVersion: token.accessVersion
+ const assertActiveOrgMembership = async (userId: string, orgId: string) => {
+ const orgMembership = await orgDAL.findEffectiveOrgMembership({
+ actorType: ActorType.USER,
+ actorId: userId,
+ orgId,
+ status: OrgMembershipStatus.Accepted
});
- let orgId = "";
- let orgName = "";
- let rootOrgId = "";
- let parentOrgId = "";
- if (token.organizationId) {
- // Check if token has sub-organization scope
- if (token.subOrganizationId) {
- const subOrganization = await orgDAL.findOne({
- id: token.subOrganizationId
- });
- if (!subOrganization)
- throw new BadRequestError({ message: `Sub organization ${token.subOrganizationId} not found` });
- // Verify the sub-org belongs to the token's root organization
- if (subOrganization.rootOrgId !== token.organizationId && subOrganization.id !== token.organizationId) {
- throw new ForbiddenRequestError({ message: "Sub-organization does not belong to the token's organization" });
- }
-
- const orgMembership = await orgDAL.findEffectiveOrgMembership({
- actorType: ActorType.USER,
- actorId: user.id,
- orgId: subOrganization.id,
- status: OrgMembershipStatus.Accepted
- });
+ if (!orgMembership) {
+ throw new ForbiddenRequestError({ message: "User not member of organization" });
+ }
- if (!orgMembership) {
- throw new ForbiddenRequestError({ message: "User not member of organization" });
- }
+ if (!orgMembership.isActive) {
+ throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
+ }
+ };
- if (!orgMembership.isActive) {
- throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
- }
- orgId = subOrganization.id;
- orgName = subOrganization.name;
- rootOrgId = token.organizationId;
- parentOrgId = subOrganization.parentOrgId as string;
- } else {
- const organization = await orgDAL.findOne({ id: token.organizationId });
- const orgMembership = await orgDAL.findEffectiveOrgMembership({
- actorType: ActorType.USER,
- actorId: user.id,
- orgId: token.organizationId,
- status: OrgMembershipStatus.Accepted
- });
+ const validateTokenOrgScope = async ({
+ userId,
+ organizationId,
+ subOrganizationId
+ }: {
+ userId: string;
+ organizationId?: string;
+ subOrganizationId?: string;
+ }) => {
+ if (!organizationId) {
+ if (subOrganizationId) throw new UnauthorizedError({ message: "Invalid token" });
+ return { orgId: "", orgName: "", rootOrgId: "", parentOrgId: "" };
+ }
- if (!orgMembership) {
- throw new ForbiddenRequestError({ message: "User not member of organization" });
- }
+ if (!subOrganizationId) {
+ const organization = await orgDAL.findOne({ id: organizationId });
+ await assertActiveOrgMembership(userId, organizationId);
- if (!orgMembership.isActive) {
- throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
- }
+ return {
+ orgId: organizationId,
+ orgName: organization.name,
+ rootOrgId: organizationId,
+ parentOrgId: organizationId
+ };
+ }
- orgId = token.organizationId;
- orgName = organization.name;
- rootOrgId = token.organizationId;
- parentOrgId = token.organizationId;
- }
+ const subOrganization = await orgDAL.findOne({ id: subOrganizationId });
+ if (!subOrganization) throw new BadRequestError({ message: `Sub organization ${subOrganizationId} not found` });
+ if (subOrganization.rootOrgId !== organizationId || subOrganization.id === organizationId) {
+ throw new ForbiddenRequestError({ message: "Sub-organization does not belong to the token's organization" });
}
+ await assertActiveOrgMembership(userId, organizationId);
+ await assertActiveOrgMembership(userId, subOrganization.id);
+
+ return {
+ orgId: subOrganization.id,
+ orgName: subOrganization.name,
+ rootOrgId: organizationId,
+ parentOrgId: subOrganization.parentOrgId as string
+ };
+ };
+
+ const fnValidateJwtIdentity = async (token: AuthModeJwtTokenPayload) => {
+ const { user } = await validateUserSessionFreshness({
+ userId: token.userId,
+ tokenVersionId: token.tokenVersionId,
+ accessVersion: token.accessVersion
+ });
+
+ const { orgId, orgName, rootOrgId, parentOrgId } = await validateTokenOrgScope({
+ userId: user.id,
+ organizationId: token.organizationId,
+ subOrganizationId: token.subOrganizationId
+ });
+
return { user, tokenVersionId: token.tokenVersionId, orgId, orgName, rootOrgId, parentOrgId };
};
@@ -547,6 +553,7 @@ export const tokenServiceFactory = ({ tokenDAL, userDAL, orgDAL, keyStore }: TAu
validateRefreshToken,
rotateRefreshToken,
validateUserSessionFreshness,
+ validateTokenOrgScope,
fnValidateJwtIdentity,
getUserTokenSessionById,
createEmailSignupToken,