Skip to content

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

28.6 KB
Raw Blame
/* eslint-disable no-nested-ternary */
import { createMongoAbility, ForbiddenError, MongoAbility, PureAbility, RawRuleOf, subject } from "@casl/ability";
import handlebars from "handlebars";
import picomatch from "picomatch";
import { z } from "zod";

import { SecretFolderRole, TOrganizations } from "@app/db/schemas";
import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
import { validatePermissionBoundary } from "@app/lib/casl/boundary";
import {
  BadRequestError,
  ForbiddenRequestError,
  NotFoundError,
  PermissionBoundaryError,
  UnauthorizedError
} from "@app/lib/errors";
import { TAdditionalPrivilegeDALFactory } from "@app/services/additional-privilege/additional-privilege-dal";
import { ActorAuthMethod, ActorType, AuthMethod } from "@app/services/auth/auth-type";
import { TSecretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";

import { FOLDER_SCOPED_DENY_RULES, SECRET_FOLDER_ROLE_PERMISSIONS } from "./folder-roles";
import { OrgPermissionSet } from "./org-permission";
import { TPermissionDALFactory } from "./permission-dal";
import { TCachedFolderScopedPrivileges, TProjectFolderScopedPrivilege } from "./permission-service-types";
import {
  ActionAllowedConditions,
  ProjectPermissionGroupActions,
  ProjectPermissionIdentityActions,
  ProjectPermissionMemberActions,
  ProjectPermissionSecretActions,
  ProjectPermissionSet,
  ProjectPermissionSub,
  ProjectPermissionV2Schema,
  SecretSubjectFields
} from "./project-permission";

export function throwIfMissingSecretReadValueOrDescribePermission(
  permission: MongoAbility<ProjectPermissionSet> | PureAbility,
  action: Extract<
    ProjectPermissionSecretActions,
    ProjectPermissionSecretActions.ReadValue | ProjectPermissionSecretActions.DescribeSecret
  >,
  subjectFields?: SecretSubjectFields
) {
  const secretSubject = subjectFields
    ? subject(ProjectPermissionSub.Secrets, subjectFields)
    : ProjectPermissionSub.Secrets;
  // ForbiddenError captures a stack trace, so only build one when access is actually denied.
  if (
    permission.can(ProjectPermissionSecretActions.DescribeAndReadValue, secretSubject) ||
    permission.can(action, secretSubject)
  ) {
    return;
  }
  ForbiddenError.from(permission).throwUnlessCan(action, secretSubject);
}

export function hasSecretReadValueOrDescribePermission(
  permission: MongoAbility<ProjectPermissionSet>,
  action: Extract<
    ProjectPermissionSecretActions,
    ProjectPermissionSecretActions.DescribeSecret | ProjectPermissionSecretActions.ReadValue
  >,
  subjectFields?: SecretSubjectFields
) {
  let canNewPermission = false;
  let canOldPermission = false;

  if (subjectFields) {
    canNewPermission = permission.can(action, subject(ProjectPermissionSub.Secrets, subjectFields));
    canOldPermission = permission.can(
      ProjectPermissionSecretActions.DescribeAndReadValue,
      subject(ProjectPermissionSub.Secrets, subjectFields)
    );
  } else {
    canNewPermission = permission.can(action, ProjectPermissionSub.Secrets);
    canOldPermission = permission.can(
      ProjectPermissionSecretActions.DescribeAndReadValue,
      ProjectPermissionSub.Secrets
    );
  }

  return canNewPermission || canOldPermission;
}

// authorizes moving secrets from one (environment, path) to another at the path level: a move is a
// delete-at-source + create/edit-at-destination, and the source read also requires read-value/describe.
// this is path-scoped (no per-secret name/tag conditions), so a single call covers a whole batch of
// secrets sharing the same source and destination path. throws ForbiddenError on the first missing grant.
export function validateSecretMovePermissions(
  permission: MongoAbility<ProjectPermissionSet> | PureAbility,
  {
    sourceEnvironment,
    sourceSecretPath,
    destinationEnvironment,
    destinationSecretPath
  }: {
    sourceEnvironment: string;
    sourceSecretPath: string;
    destinationEnvironment: string;
    destinationSecretPath: string;
  }
) {
  const sourceActions = [
    ProjectPermissionSecretActions.Delete,
    ProjectPermissionSecretActions.DescribeSecret,
    ProjectPermissionSecretActions.ReadValue
  ] as const;
  const destinationActions = [ProjectPermissionSecretActions.Create, ProjectPermissionSecretActions.Edit] as const;

  for (const destinationAction of destinationActions) {
    ForbiddenError.from(permission).throwUnlessCan(
      destinationAction,
      subject(ProjectPermissionSub.Secrets, {
        environment: destinationEnvironment,
        secretPath: destinationSecretPath
      })
    );
  }

  for (const sourceAction of sourceActions) {
    if (
      sourceAction === ProjectPermissionSecretActions.ReadValue ||
      sourceAction === ProjectPermissionSecretActions.DescribeSecret
    ) {
      throwIfMissingSecretReadValueOrDescribePermission(permission, sourceAction, {
        environment: sourceEnvironment,
        secretPath: sourceSecretPath
      });
    } else {
      ForbiddenError.from(permission).throwUnlessCan(
        sourceAction,
        subject(ProjectPermissionSub.Secrets, {
          environment: sourceEnvironment,
          secretPath: sourceSecretPath
        })
      );
    }
  }
}

export const OptionalArrayPermissionSchema = ProjectPermissionV2Schema.array().optional();
export function checkForInvalidPermissionCombination(permissions: z.infer<typeof OptionalArrayPermissionSchema>) {
  if (!permissions) return;

  for (const permission of permissions) {
    if (permission.subject === ProjectPermissionSub.Secrets) {
      if (permission.action.includes(ProjectPermissionSecretActions.DescribeAndReadValue)) {
        const hasReadValue = permission.action.includes(ProjectPermissionSecretActions.ReadValue);
        const hasDescribeSecret = permission.action.includes(ProjectPermissionSecretActions.DescribeSecret);

        if (hasReadValue || hasDescribeSecret) {
          const hasBothDescribeAndReadValue = hasReadValue && hasDescribeSecret;

          throw new BadRequestError({
            message: `You have selected Read, and ${
              hasBothDescribeAndReadValue
                ? "both Read Value and Describe Secret"
                : hasReadValue
                  ? "Read Value"
                  : hasDescribeSecret
                    ? "Describe Secret"
                    : ""
            }. You cannot select Read Value or Describe Secret if you have selected Read. The Read permission is a legacy action which has been replaced by Describe Secret and Read Value.`
          });
        }
      }
    }

    if (permission.subject === ProjectPermissionSub.Member) {
      if (permission.action.includes(ProjectPermissionMemberActions.GrantPrivileges)) {
        const hasAssignRole = permission.action.includes(ProjectPermissionMemberActions.AssignRole);
        const hasAssignAdditionalPrivileges = permission.action.includes(
          ProjectPermissionMemberActions.AssignAdditionalPrivileges
        );

        if (hasAssignRole || hasAssignAdditionalPrivileges) {
          const hasBothNewActions = hasAssignRole && hasAssignAdditionalPrivileges;

          throw new BadRequestError({
            message: `You have selected Grant Privileges, and ${
              hasBothNewActions
                ? "both Assign Role and Assign Additional Privileges"
                : hasAssignRole
                  ? "Assign Role"
                  : hasAssignAdditionalPrivileges
                    ? "Assign Additional Privileges"
                    : ""
            }. You cannot select Assign Role or Assign Additional Privileges if you have selected Grant Privileges. The Grant Privileges permission is a legacy action which has been replaced by Assign Role and Assign Additional Privileges.`
          });
        }
      }
    }

    if (permission.subject === ProjectPermissionSub.Identity) {
      if (permission.action.includes(ProjectPermissionIdentityActions.GrantPrivileges)) {
        const hasAssignRole = permission.action.includes(ProjectPermissionIdentityActions.AssignRole);
        const hasAssignAdditionalPrivileges = permission.action.includes(
          ProjectPermissionIdentityActions.AssignAdditionalPrivileges
        );

        if (hasAssignRole || hasAssignAdditionalPrivileges) {
          const hasBothNewActions = hasAssignRole && hasAssignAdditionalPrivileges;

          throw new BadRequestError({
            message: `You have selected Grant Privileges, and ${
              hasBothNewActions
                ? "both Assign Role and Assign Additional Privileges"
                : hasAssignRole
                  ? "Assign Role"
                  : hasAssignAdditionalPrivileges
                    ? "Assign Additional Privileges"
                    : ""
            }. You cannot select Assign Role or Assign Additional Privileges if you have selected Grant Privileges. The Grant Privileges permission is a legacy action which has been replaced by Assign Role and Assign Additional Privileges.`
          });
        }
      }
    }

    if (permission.subject === ProjectPermissionSub.Groups) {
      if (permission.action.includes(ProjectPermissionGroupActions.GrantPrivileges)) {
        const hasAssignRole = permission.action.includes(ProjectPermissionGroupActions.AssignRole);

        if (hasAssignRole) {
          throw new BadRequestError({
            message:
              "You have selected Grant Privileges and Assign Role. You cannot select Assign Role if you have selected Grant Privileges. The Grant Privileges permission is a legacy action which has been replaced by Assign Role."
          });
        }
      }
    }

    const subjectConditions = ActionAllowedConditions[permission.subject as ProjectPermissionSub];
    const permissionConditions = "conditions" in permission ? permission.conditions : undefined;
    if (permissionConditions && subjectConditions) {
      const conditionKeys = Object.keys(permissionConditions);
      for (const action of permission.action) {
        const allowedConditions = subjectConditions[action];
        if (allowedConditions) {
          for (const condKey of conditionKeys) {
            if (!allowedConditions.includes(condKey)) {
              throw new BadRequestError({
                message: `Condition "${condKey}" is not allowed for action "${action}" on subject "${permission.subject}"`
              });
            }
          }
        }
      }
    }
  }

  return true;
}

function isAuthMethodSaml(actorAuthMethod: ActorAuthMethod) {
  if (!actorAuthMethod) return false;

  return [
    AuthMethod.AZURE_SAML,
    AuthMethod.OKTA_SAML,
    AuthMethod.JUMPCLOUD_SAML,
    AuthMethod.GOOGLE_SAML,
    AuthMethod.KEYCLOAK_SAML,
    AuthMethod.AUTH0_SAML
  ].includes(actorAuthMethod);
}

export const FOREIGN_SSO_SESSION_ERROR_NAME = "ForeignSsoSession";

function validateOrgSSO({
  actorAuthMethod,
  isOrgSsoEnforced,
  isOrgGoogleSsoEnforced,
  isOrgSsoBypassEnabled,
  isAdmin,
  issuingRootOrgId,
  rootOrgId
}: {
  actorAuthMethod: ActorAuthMethod;
  isOrgSsoEnforced: TOrganizations["authEnforced"];
  isOrgGoogleSsoEnforced: TOrganizations["googleSsoAuthEnforced"];
  isOrgSsoBypassEnabled: TOrganizations["bypassOrgAuthEnabled"];
  isAdmin: boolean;
  // Root org whose IdP issued the session, when the session is the request's own; tokens issued
  // before the claim existed carry none and are taken at their auth method.
  issuingRootOrgId?: string;
  rootOrgId: string;
}) {
  if (actorAuthMethod === undefined) {
    throw new UnauthorizedError({ name: "No auth method defined" });
  }

  if ((isOrgSsoEnforced || isOrgGoogleSsoEnforced) && isOrgSsoBypassEnabled && isAdmin) {
    return;
  }

  // case: google sso is enforced, but the actor is not using google sso
  if (isOrgGoogleSsoEnforced && actorAuthMethod !== null && actorAuthMethod !== AuthMethod.GOOGLE) {
    throw new ForbiddenRequestError({
      message:
        "Organization authentication is enforced. Cannot access org-scoped resource. Login with Google SSO to access this resource."
    });
  }

  // case: SAML SSO is enforced, but the actor is not using SAML SSO
  if (
    isOrgSsoEnforced &&
    actorAuthMethod !== null &&
    !isAuthMethodSaml(actorAuthMethod) &&
    actorAuthMethod !== AuthMethod.OIDC
  ) {
    throw new ForbiddenRequestError({
      message:
        "Organization authentication is enforced. Cannot access org-scoped resource. Login with SAML SSO to access this resource."
    });
  }

  // A session another org's IdP minted would otherwise satisfy this org's enforcement without its
  // IdP, MFA or deprovisioning ever seeing the login.
  if (isOrgSsoEnforced && actorAuthMethod !== null && issuingRootOrgId && issuingRootOrgId !== rootOrgId) {
    throw new ForbiddenRequestError({
      name: FOREIGN_SSO_SESSION_ERROR_NAME,
      message:
        "This organization requires its own SSO. Your session was issued by another organization's identity provider; log in again through this organization's SSO."
    });
  }
}

const escapeHandlebarsMissingDict = (obj: Record<string, string>, key: string) => {
  const handler = {
    get(target: Record<string, string>, prop: string) {
      if (!Object.hasOwn(target, prop)) {
        // eslint-disable-next-line no-param-reassign
        target[prop] = `{{${key}.${prop}}}`; // Add missing key as an "own" property
      }
      return target[prop];
    }
  };

  return new Proxy(obj, handler);
};

// This function serves as a transition layer between the old and new privilege management system
// the old privilege management system is based on the actor having more privileges than the managed permission
// the new privilege management system is based on the actor having the appropriate permission to perform the privilege change,
// regardless of the actor's privilege level.
const validatePrivilegeChangeOperation = (
  shouldUseNewPrivilegeSystem: boolean,
  opActions: (OrgPermissionSet[0] | ProjectPermissionSet[0]) | (OrgPermissionSet[0] | ProjectPermissionSet[0])[],
  opSubject: OrgPermissionSet[1] | ProjectPermissionSet[1],
  actorPermission: MongoAbility,
  managedPermission: MongoAbility,
  subjectFields?: Record<string, string | undefined>
) => {
  const actions = Array.isArray(opActions) ? opActions : [opActions];

  if (shouldUseNewPrivilegeSystem) {
    const subjectToCheck = subjectFields ? subject(opSubject as string, subjectFields) : opSubject;

    for (const opAction of actions) {
      if (actorPermission.can(opAction, subjectToCheck)) {
        return {
          isValid: true,
          missingPermissions: []
        };
      }
    }

    // Report the first (primary) action in missingPermissions.
    // For example, when evaluating legacy actions fallback, it returns the error related to the new one not the legacy one.
    return {
      isValid: false,
      missingPermissions: [
        {
          action: actions[0],
          subject: opSubject
        }
      ]
    };
  }

  // if not, we check if the actor is indeed more privileged than the managed permission - this is the old system
  return validatePermissionBoundary(actorPermission, managedPermission);
};

const constructPermissionErrorMessage = (
  baseMessage: string,
  shouldUseNewPrivilegeSystem: boolean,
  opAction: OrgPermissionSet[0] | ProjectPermissionSet[0],
  opSubject: OrgPermissionSet[1] | ProjectPermissionSet[1]
) => {
  return `${baseMessage}${
    shouldUseNewPrivilegeSystem
      ? `. Permission denied: ${opAction as string} on ${opSubject as string}. Check that the actor's role grants this permission and that all permission conditions are met.`
      : ". Actor privilege level is not high enough to perform this action"
  }`;
};

type TAssertRoleSetBoundaryArg = {
  shouldUseNewPrivilegeSystem: boolean;
  opActions: (OrgPermissionSet[0] | ProjectPermissionSet[0]) | (OrgPermissionSet[0] | ProjectPermissionSet[0])[];
  opSubject: OrgPermissionSet[1] | ProjectPermissionSet[1];
  actorPermission: MongoAbility;
  targetPermissions: { permission: MongoAbility; role?: { slug: string } }[];
  baseMessage: string;
  subjectFields?: Record<string, string | undefined>;
};

// Bounds a privilege change against every role the target holds, not just the first one.
const assertRoleSetBoundary = ({
  shouldUseNewPrivilegeSystem,
  opActions,
  opSubject,
  actorPermission,
  targetPermissions,
  baseMessage,
  subjectFields
}: TAssertRoleSetBoundaryArg) => {
  const primaryAction = Array.isArray(opActions) ? opActions[0] : opActions;

  const targets = targetPermissions.length ? targetPermissions : [{ permission: createMongoAbility([]) }];

  for (const target of targets) {
    const targetSubjectFields = target.role ? { ...subjectFields, assignableRole: target.role.slug } : subjectFields;

    const boundary = validatePrivilegeChangeOperation(
      shouldUseNewPrivilegeSystem,
      opActions,
      opSubject,
      actorPermission,
      target.permission,
      targetSubjectFields
    );

    if (!boundary.isValid)
      throw new PermissionBoundaryError({
        message: constructPermissionErrorMessage(baseMessage, shouldUseNewPrivilegeSystem, primaryAction, opSubject),
        details: { missingPermissions: boundary.missingPermissions }
      });
  }
};

// Subjects whose forbid rules on new fine-grained actions must also forbid the
// legacy umbrella action they replaced. Without this expansion, an admin allow
// on the legacy action survives a custom-role forbid on the new actions and
// acts as a backdoor through the helpers/call sites that OR-fallback to it
// (e.g. hasSecretReadValueOrDescribePermission, validatePrivilegeChangeOperation).
const LEGACY_FORBID_ACTION_EXPANSIONS: Partial<
  Record<ProjectPermissionSub, { legacyAction: string; newActions: string[] }>
> = {
  [ProjectPermissionSub.Secrets]: {
    legacyAction: ProjectPermissionSecretActions.DescribeAndReadValue,
    newActions: [ProjectPermissionSecretActions.ReadValue, ProjectPermissionSecretActions.DescribeSecret]
  },
  [ProjectPermissionSub.Member]: {
    legacyAction: ProjectPermissionMemberActions.GrantPrivileges,
    newActions: [ProjectPermissionMemberActions.AssignRole, ProjectPermissionMemberActions.AssignAdditionalPrivileges]
  },
  [ProjectPermissionSub.Identity]: {
    legacyAction: ProjectPermissionIdentityActions.GrantPrivileges,
    newActions: [
      ProjectPermissionIdentityActions.AssignRole,
      ProjectPermissionIdentityActions.AssignAdditionalPrivileges
    ]
  },
  [ProjectPermissionSub.Groups]: {
    legacyAction: ProjectPermissionGroupActions.GrantPrivileges,
    newActions: [ProjectPermissionGroupActions.AssignRole]
  }
};

const expandLegacyForbidActions = <T extends RawRuleOf<MongoAbility<ProjectPermissionSet>>>(rules: T[]): T[] => {
  return rules.map((rule) => {
    if (!rule.inverted) return rule;

    const subjects = Array.isArray(rule.subject) ? rule.subject : [rule.subject];
    if (subjects.length !== 1) return rule;

    const expansion = LEGACY_FORBID_ACTION_EXPANSIONS[subjects[0] as ProjectPermissionSub];
    if (!expansion) return rule;

    const actions = Array.isArray(rule.action) ? rule.action : [rule.action];
    const shouldExpand = actions.some((a) => expansion.newActions.includes(a as string));
    if (!shouldExpand || actions.includes(expansion.legacyAction as (typeof actions)[number])) return rule;

    return { ...rule, action: [...actions, expansion.legacyAction] as typeof rule.action };
  });
};

const HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH = 256;
const HBS_TRIM_SUFFIX_MAX_GLOB_WILDCARDS = 5;

const hbsStripPrefix = (text: string, prefix: string) => {
  const textStr = String(text || "");
  if (!textStr) return textStr;

  return textStr.startsWith(prefix) ? textStr.substring(prefix.length) : textStr;
};

const hbsTrimSuffix = (text: string, suffix: string) => {
  const textStr = String(text || "");
  if (!textStr) return textStr;

  if (typeof suffix !== "string" || !suffix) return textStr;

  if (suffix.length > HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH) return textStr;

  if (!picomatch.scan(suffix).isGlob) {
    return textStr.endsWith(suffix) ? textStr.slice(0, -suffix.length) : textStr;
  }

  // the matcher is run once per suffix position below, so every variable-length wildcard multiplies
  // the backtracking across that whole scan.
  const wildcardCount = [...suffix].filter((char) => char === "*" || char === "?").length;
  if (wildcardCount > HBS_TRIM_SUFFIX_MAX_GLOB_WILDCARDS) return textStr;

  if (textStr.length > HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH) return textStr;

  let isSuffixMatch: (input: string) => boolean;
  try {
    isSuffixMatch = picomatch(suffix, { dot: true });
  } catch {
    return textStr;
  }

  for (let i = textStr.length; i >= 0; i -= 1) {
    if (isSuffixMatch(textStr.slice(i))) return textStr.slice(0, i);
  }

  return textStr;
};

const handlebarsClient = (() => {
  const hbs = handlebars.create();

  hbs.registerHelper("stripPrefix", hbsStripPrefix);
  hbs.registerHelper("trimSuffix", hbsTrimSuffix);

  return hbs;
})();

export const isActiveRole = <U extends { isTemporary?: boolean; temporaryAccessEndTime?: Date | null }>(
  role: U
): boolean =>
  !role.isTemporary ||
  Boolean(role.isTemporary && role.temporaryAccessEndTime && new Date() < role.temporaryAccessEndTime);

export const getFolderPermissionVersionFingerprint = async (
  projectId: string,
  keyStore: Pick<TKeyStoreFactory, "pgGetIntItem">
) => String((await keyStore.pgGetIntItem(KeyStorePrefixes.ProjectFolderPermissionVersion(projectId))) ?? 0);

export const getProjectPermissionFingerprint = async (
  {
    projectId,
    orgId,
    actorId,
    actorType
  }: {
    projectId: string;
    orgId: string;
    actorId: string;
    actorType: ActorType.USER | ActorType.IDENTITY;
  },
  {
    permissionDAL,
    keyStore
  }: {
    permissionDAL: Pick<TPermissionDALFactory, "getPermissionFingerprint">;
    keyStore: Pick<TKeyStoreFactory, "pgGetIntItem">;
  }
): Promise<string> => {
  const [membershipFingerprint, folderVersion] = await Promise.all([
    permissionDAL.getPermissionFingerprint({ projectId, orgId, actorId, actorType }),
    getFolderPermissionVersionFingerprint(projectId, keyStore)
  ]);

  return `${membershipFingerprint}:${folderVersion}`;
};

type TFolderScopedPrivilegeDeps = {
  additionalPrivilegeDAL: Pick<TAdditionalPrivilegeDALFactory, "findFolderScopedPrivileges">;
  secretFolderDAL: Pick<TSecretFolderDALFactory, "findSecretPathByFolderIds">;
};

// A grant whose folder no longer exists, or that has no role, grants nothing and is left out.
const $resolveFolderScopedPrivileges = async (
  projectId: string,
  rows: Awaited<ReturnType<TAdditionalPrivilegeDALFactory["findFolderScopedPrivileges"]>>,
  secretFolderDAL: TFolderScopedPrivilegeDeps["secretFolderDAL"]
) => {
  if (!rows.length) return [];

  const foldersWithPath = await secretFolderDAL.findSecretPathByFolderIds(
    projectId,
    rows.map((row) => row.folderId)
  );

  return rows.flatMap((row, idx) => {
    const folder = foldersWithPath[idx];
    if (!folder || !row.role) return [];
    return [
      {
        actorId: row.actorUserId ?? row.actorIdentityId,
        privilege: {
          id: row.id,
          folderId: row.folderId,
          role: row.role,
          environmentSlug: folder.environmentSlug,
          secretPath: folder.path,
          isTemporary: row.isTemporary,
          temporaryAccessEndTime: row.temporaryAccessEndTime
        }
      }
    ];
  });
};

export const fetchFolderScopedPrivileges = async (
  projectId: string,
  actor: ActorType.USER | ActorType.IDENTITY,
  actorId: string,
  { additionalPrivilegeDAL, secretFolderDAL }: TFolderScopedPrivilegeDeps
): Promise<TCachedFolderScopedPrivileges> => {
  const rows = await additionalPrivilegeDAL.findFolderScopedPrivileges({ projectId, actorId, actorType: actor });
  const resolved = await $resolveFolderScopedPrivileges(projectId, rows, secretFolderDAL);
  return { privileges: resolved.map(({ privilege }) => privilege) };
};

// Every user's and identity's folder grants in the project, keyed by the actor's id.
export const fetchProjectFolderScopedPrivileges = async (
  projectId: string,
  { additionalPrivilegeDAL, secretFolderDAL }: TFolderScopedPrivilegeDeps
) => {
  const rows = await additionalPrivilegeDAL.findFolderScopedPrivileges({ projectId });
  const privilegesByActorId = new Map<string, TCachedFolderScopedPrivileges["privileges"]>();
  (await $resolveFolderScopedPrivileges(projectId, rows, secretFolderDAL)).forEach(({ actorId, privilege }) => {
    if (!actorId) return;
    privilegesByActorId.set(actorId, [...(privilegesByActorId.get(actorId) ?? []), privilege]);
  });
  return privilegesByActorId;
};

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>>[] => {
  const scopedGrants = privileges.map((privilege) => {
    // make sure the role is valid
    if (!Object.values(SecretFolderRole).includes(privilege.role as SecretFolderRole)) {
      throw new NotFoundError({
        name: "FolderRoleInvalid",
        message: `Folder access role '${privilege.role}' on grant with ID '${privilege.id}' not found`
      });
    }
    return {
      role: privilege.role as SecretFolderRole,
      conditions: { environment: privilege.environmentSlug, secretPath: privilege.secretPath }
    };
  });

  const withConditions = (rules: RawRuleOf<MongoAbility<ProjectPermissionSet>>[], conditions: object) =>
    rules.map((rule) => ({ ...rule, conditions }) as RawRuleOf<MongoAbility<ProjectPermissionSet>>);

  return [
    ...scopedGrants.flatMap(({ role, conditions }) =>
      withConditions(FOLDER_SCOPED_DENY_RULES_BY_ROLE[role], conditions)
    ),
    ...scopedGrants.flatMap(({ role, conditions }) => withConditions(SECRET_FOLDER_ROLE_PERMISSIONS[role], conditions))
  ];
};

// 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 and
// returning the rules untouched skips the compile, the render and the reparse.
export const interpolatePermissionRules = <T>(rules: T[], identityContext: Record<string, unknown>): T[] => {
  const serializedRules = JSON.stringify(rules);

  if (!serializedRules.includes("{{")) return rules;

  const templatedRules = handlebarsClient.compile(serializedRules, { data: false });

  return JSON.parse(templatedRules(identityContext, { data: false })) as T[];
};

// `identity.auth.*` is read off the credential an identity authenticates with, so it is knowable only
// for the actor of the live request. Rendered against a context that lacks it, the mustache collapses to
// an empty string, and the condition it guarded turns into one nothing satisfies, which reads to a
// privilege boundary as a grant anybody outranks. Rules that reference it keep their mustaches instead,
// so they stay unsatisfiable on both sides of the comparison.
const AUTH_TEMPLATE_PATH = "identity.auth";

export const interpolateStoredIdentityRules = <T>(rules: T[], identityContext: Record<string, unknown>): T[] => {
  const serializedRules = JSON.stringify(rules);

  if (!serializedRules.includes("{{")) return rules;
  if (!serializedRules.includes(AUTH_TEMPLATE_PATH)) return interpolatePermissionRules(rules, identityContext);

  return rules.map((rule) =>
    JSON.stringify(rule).includes(AUTH_TEMPLATE_PATH) ? rule : interpolatePermissionRules([rule], identityContext)[0]
  );
};

export {
  assertRoleSetBoundary,
  constructPermissionErrorMessage,
  escapeHandlebarsMissingDict,
  expandLegacyForbidActions,
  handlebarsClient,
  isAuthMethodSaml,
  validateOrgSSO,
  validatePrivilegeChangeOperation
};