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
fix folder grant
Thiago Araujo da Silva15c97cf4d445
diff --git a/backend/src/ee/services/permission/folder-scoped-privilege-rules.test.ts b/backend/src/ee/services/permission/folder-scoped-privilege-rules.test.tsindex 79b256ab99..6c95caf3b3 100644--- a/backend/src/ee/services/permission/folder-scoped-privilege-rules.test.ts+++ b/backend/src/ee/services/permission/folder-scoped-privilege-rules.test.ts@@ -3,6 +3,7 @@ import { z } from "zod";import { ProjectMembershipRole, ProjectType, SecretFolderRole } from "@app/db/schemas";import { conditionsMatcher } from "@app/lib/casl";+import { validatePermissionBoundary } from "@app/lib/casl/boundary";import { NotFoundError } from "@app/lib/errors";import { FOLDER_SCOPED_DENY_RULES } from "./folder-roles";@@ -378,3 +379,61 @@ describe("filterOverriddenFolderScopedDenyRules", () => {);});});++// The boundary reads an ability literally, with no notion of CASL's last-match-wins, so the deny half+// of a folder grant used to make its own holder look barred from the path the grant opens.+describe("folder-scoped privilege boundary", () => {+ // How `$folderScopedGrantAbilities` presents a target's grant: the denies are dropped because the+ // boundary walks the subset's rules as if each were a grant.+ const grantAbility = (privileges: TProjectFolderScopedPrivilege[]) =>+ createMongoAbility<ProjectPermissionSet>(+ buildFolderScopedPrivilegeRules(privileges).filter((rule) => !rule.inverted),+ { conditionsMatcher }+ );++ test("a grant does not bound out an identical grant", () => {+ const actor = abilityFor(memberRoles, [privilege()]);++ expect(validatePermissionBoundary(actor, grantAbility([privilege()]))).toEqual({ isValid: true });+ });++ test("equal access holds on the list tier, whose denies are only partly re-allowed", () => {+ const actor = abilityFor(memberRoles, [privilege({ role: SecretFolderRole.List })]);++ expect(validatePermissionBoundary(actor, grantAbility([privilege({ role: SecretFolderRole.List })]))).toEqual({+ isValid: true+ });+ });++ test("a lower tier cannot bound a higher one at the same path", () => {+ const actor = abilityFor(memberRoles, [privilege({ role: SecretFolderRole.Read })]);++ expect(validatePermissionBoundary(actor, grantAbility([privilege({ role: SecretFolderRole.Edit })])).isValid).toBe(+ false+ );+ });++ test("a grant on another path does not bound one at the granted path", () => {+ const actor = abilityFor(viewerRoles, [privilege({ secretPath: "/x" })]);++ expect(validatePermissionBoundary(actor, grantAbility([privilege({ secretPath: "/a/b" })])).isValid).toBe(false);+ });++ // The deny half still has to strip the base role at the granted path: a member reads every path+ // except the one their grant narrows to list-only.+ test("a grant still strips what the base role would have allowed at the granted path", () => {+ const actor = abilityFor(memberRoles, [privilege({ role: SecretFolderRole.List })]);+ const target = createMongoAbility<ProjectPermissionSet>(+ [+ {+ action: ProjectPermissionSecretActions.ReadValue,+ subject: ProjectPermissionSub.Secrets,+ conditions: { environment: "dev", secretPath: "/a/b" }+ }+ ] as RawRuleOf<MongoAbility<ProjectPermissionSet>>[],+ { conditionsMatcher }+ );++ expect(validatePermissionBoundary(actor, target).isValid).toBe(false);+ });+});diff --git a/backend/src/ee/services/permission/permission-fns.ts b/backend/src/ee/services/permission/permission-fns.tsindex 269667a7da..f8f26ad88f 100644--- a/backend/src/ee/services/permission/permission-fns.ts+++ b/backend/src/ee/services/permission/permission-fns.ts@@ -600,6 +600,40 @@ export const fetchFolderScopedPrivileges = async (};};+export const filterOverriddenFolderScopedDenyRules = (+ rules: RawRuleOf<MongoAbility<ProjectPermissionSet>>[]+): RawRuleOf<MongoAbility<ProjectPermissionSet>>[] => {+ const toArray = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);++ const allowedPairs = new Set<string>();+ rules+ .filter((rule) => !rule.inverted)+ .forEach((rule) => {+ toArray(rule.subject as string | string[]).forEach((sub) => {+ toArray(rule.action).forEach((action) => allowedPairs.add(`${sub}:${action}`));+ });+ });++ return rules.flatMap((rule) => {+ if (!rule.inverted) return [rule];+ return toArray(rule.subject as string | string[]).flatMap((sub) => {+ const deniedActions = toArray(rule.action).filter((action) => !allowedPairs.has(`${sub}:${action}`));+ if (!deniedActions.length) return [];+ return [{ ...rule, subject: sub, action: deniedActions } as RawRuleOf<MongoAbility<ProjectPermissionSet>>];+ });+ });+};++const FOLDER_SCOPED_DENY_RULES_BY_ROLE = Object.fromEntries(+ Object.values(SecretFolderRole).map((role) => [+ role,+ filterOverriddenFolderScopedDenyRules([+ ...FOLDER_SCOPED_DENY_RULES,+ ...SECRET_FOLDER_ROLE_PERMISSIONS[role]+ ]).filter((rule) => rule.inverted)+ ])+) as Record<SecretFolderRole, RawRuleOf<MongoAbility<ProjectPermissionSet>>[]>;+export const buildFolderScopedPrivilegeRules = (privileges: TProjectFolderScopedPrivilege[]): RawRuleOf<MongoAbility<ProjectPermissionSet>>[] => {@@ -620,38 +654,14 @@ export const buildFolderScopedPrivilegeRules = (const withConditions = (rules: RawRuleOf<MongoAbility<ProjectPermissionSet>>[], conditions: object) =>rules.map((rule) => ({ ...rule, conditions }) as RawRuleOf<MongoAbility<ProjectPermissionSet>>);- // first we deny all tthe defined paths, and later we just allow the ones that the role has access.- // on CASL, the last match rule wins, so this works as expected.return [- ...scopedGrants.flatMap(({ conditions }) => withConditions(FOLDER_SCOPED_DENY_RULES, conditions)),+ ...scopedGrants.flatMap(({ role, conditions }) =>+ withConditions(FOLDER_SCOPED_DENY_RULES_BY_ROLE[role], conditions)+ ),...scopedGrants.flatMap(({ role, conditions }) => withConditions(SECRET_FOLDER_ROLE_PERMISSIONS[role], conditions))];};-export const filterOverriddenFolderScopedDenyRules = (- rules: RawRuleOf<MongoAbility<ProjectPermissionSet>>[]-): RawRuleOf<MongoAbility<ProjectPermissionSet>>[] => {- const toArray = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);-- const allowedPairs = new Set<string>();- rules- .filter((rule) => !rule.inverted)- .forEach((rule) => {- toArray(rule.subject as string | string[]).forEach((sub) => {- toArray(rule.action).forEach((action) => allowedPairs.add(`${sub}:${action}`));- });- });-- return rules.flatMap((rule) => {- if (!rule.inverted) return [rule];- return toArray(rule.subject as string | string[]).flatMap((sub) => {- const deniedActions = toArray(rule.action).filter((action) => !allowedPairs.has(`${sub}:${action}`));- if (!deniedActions.length) return [];- return [{ ...rule, subject: sub, action: deniedActions } as RawRuleOf<MongoAbility<ProjectPermissionSet>>];- });- });-};-// Compiling a template is the most expensive step of building an ability, and almost no rule set needs// it: built-in roles carry no `{{ }}` at all, only custom roles with identity conditions do. A template// with no mustaches renders byte-identical to its input, so serializing once to look for one anddiff --git a/backend/src/ee/services/permission/permission-service.ts b/backend/src/ee/services/permission/permission-service.tsindex 9668a08601..427258e53e 100644--- a/backend/src/ee/services/permission/permission-service.ts+++ b/backend/src/ee/services/permission/permission-service.ts@@ -77,7 +77,6 @@ import {escapeHandlebarsMissingDict,expandLegacyForbidActions,fetchFolderScopedPrivileges,- filterOverriddenFolderScopedDenyRules,getProjectPermissionFingerprint,interpolatePermissionRules,interpolateStoredIdentityRules,@@ -1192,7 +1191,7 @@ export const permissionServiceFactory = ({isTemporary: Boolean(priv.isTemporary),temporaryAccessStartTime: privilegeById[priv.id]?.temporaryAccessStartTime?.toISOString(),temporaryAccessEndTime: priv.temporaryAccessEndTime?.toISOString(),- permissions: packRules(filterOverriddenFolderScopedDenyRules(buildFolderScopedPrivilegeRules([priv])))+ permissions: packRules(buildFolderScopedPrivilegeRules([priv]))});});