Skip to content

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

add read check on all folders on the subtree

adilsonb455fffd96a5
diff --git a/backend/src/services/secret-folder/secret-folder-service.ts b/backend/src/services/secret-folder/secret-folder-service.ts
index 475f67f5cc..bd363c36e4 100644
--- a/backend/src/services/secret-folder/secret-folder-service.ts
+++ b/backend/src/services/secret-folder/secret-folder-service.ts
@@ -1,5 +1,5 @@
/* eslint-disable no-await-in-loop */
-import { ForbiddenError, subject } from "@casl/ability";
+import { ForbiddenError, MongoAbility, subject } from "@casl/ability";
import { Knex } from "knex";
import path from "path";
import { v4 as uuidv4, validate as uuidValidate } from "uuid";
@@ -9,7 +9,14 @@ import { TDynamicSecretDALFactory } from "@app/ee/services/dynamic-secret/dynami
import { THoneyTokenDALFactory } from "@app/ee/services/honey-token/honey-token-dal";
import { validateSecretMovePermissions } from "@app/ee/services/permission/permission-fns";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
-import { ProjectPermissionActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
+import {
+ ProjectPermissionActions,
+ ProjectPermissionDynamicSecretActions,
+ ProjectPermissionHoneyTokenActions,
+ ProjectPermissionSecretRotationActions,
+ ProjectPermissionSet,
+ ProjectPermissionSub
+} from "@app/ee/services/permission/project-permission";
import { TSecretApprovalPolicyServiceFactory } from "@app/ee/services/secret-approval-policy/secret-approval-policy-service";
import { TSecretApprovalRequestDALFactory } from "@app/ee/services/secret-approval-request/secret-approval-request-dal";
import { TSecretApprovalRequestSecretDALFactory } from "@app/ee/services/secret-approval-request/secret-approval-request-secret-dal";
@@ -107,6 +114,30 @@ type TSecretFolderServiceFactoryDep = {
export type TSecretFolderServiceFactory = ReturnType<typeof secretFolderServiceFactory>;
+// read permission used to decide whether an actor is allowed to learn that a given blocking resource
+// exists at a folder path. used to avoid disclosing subtree contents (e.g. honey tokens) to actors
+// without access while still scanning the full subtree to enforce move correctness.
+const FOLDER_MOVE_BLOCK_PERMISSION: Record<TFolderMoveBlockingType, { action: string; subject: ProjectPermissionSub }> =
+ {
+ secret_import: { action: ProjectPermissionActions.Read, subject: ProjectPermissionSub.SecretImports },
+ dynamic_secret: {
+ action: ProjectPermissionDynamicSecretActions.ReadRootCredential,
+ subject: ProjectPermissionSub.DynamicSecrets
+ },
+ honey_token: { action: ProjectPermissionHoneyTokenActions.Read, subject: ProjectPermissionSub.HoneyTokens },
+ secret_rotation: {
+ action: ProjectPermissionSecretRotationActions.Read,
+ subject: ProjectPermissionSub.SecretRotation
+ },
+ secret_approval_policy: { action: ProjectPermissionActions.Read, subject: ProjectPermissionSub.SecretFolders }
+ };
+
+type TFolderMoveAccessScope = {
+ permission: MongoAbility<ProjectPermissionSet>;
+ environment: string;
+ rootFolderPath: string;
+};
+
export const secretFolderServiceFactory = ({
folderDAL,
snapshotService,
@@ -1508,12 +1539,21 @@ export const secretFolderServiceFactory = ({
return folder;
};
- // shared subtree scan used by both the eligibility check and the move itself:
- // a folder can only be moved if its entire recursive subtree contains nothing but plain static secrets.
+ const $canActorReadBlock = (
+ permission: MongoAbility<ProjectPermissionSet>,
+ environment: string,
+ blockingType: TFolderMoveBlockingType,
+ secretPath: string
+ ) => {
+ const { action, subject: sub } = FOLDER_MOVE_BLOCK_PERMISSION[blockingType];
+ return (permission as MongoAbility).can(action, subject(sub, { environment, secretPath }));
+ };
+
const $getFolderMoveBlock = async (
subtree: { id: string; path: string }[],
fallbackPath: string,
- tx: Knex
+ tx: Knex,
+ accessScope?: TFolderMoveAccessScope
): Promise<{ blockingType: TFolderMoveBlockingType; blockingPath: string } | null> => {
const folderIds = subtree.map((f) => f.id);
if (!folderIds.length) return null;
@@ -1521,18 +1561,53 @@ export const secretFolderServiceFactory = ({
const pathByFolderId = new Map(subtree.map((f) => [f.id, f.path]));
const resolvePath = (folderId: string) => pathByFolderId.get(folderId) ?? fallbackPath;
+ // compute each folder's absolute path once up front, then reuse it across the per-type readability filters.
+ // when the root being scanned is "/", its prefix is dropped so child paths stay "/child" rather than "//child".
+ const absPathByFolderId = accessScope
+ ? new Map(
+ subtree.map((f) => {
+ const prefix = accessScope.rootFolderPath === "/" ? "" : accessScope.rootFolderPath;
+ return [f.id, f.path === "/" ? accessScope.rootFolderPath : `${prefix}${f.path}`];
+ })
+ )
+ : null;
+
+ const folderIdsForType = (blockingType: TFolderMoveBlockingType) => {
+ if (!accessScope || !absPathByFolderId) return folderIds;
+ return folderIds.filter((folderId) =>
+ $canActorReadBlock(
+ accessScope.permission,
+ accessScope.environment,
+ blockingType,
+ absPathByFolderId.get(folderId) ?? fallbackPath
+ )
+ );
+ };
+
// check the secret types to make sure the folder only has personal and shared secrets
- const importExists = await secretImportDAL.findImportByFolderIds(folderIds, tx);
- if (importExists) return { blockingType: "secret_import", blockingPath: resolvePath(importExists.folderId) };
+ const importFolderIds = folderIdsForType("secret_import");
+ if (importFolderIds.length) {
+ const importExists = await secretImportDAL.findImportByFolderIds(importFolderIds, tx);
+ if (importExists) return { blockingType: "secret_import", blockingPath: resolvePath(importExists.folderId) };
+ }
- const [dynamicSecret] = await dynamicSecretDAL.find({ $in: { folderId: folderIds } }, { limit: 1, tx });
- if (dynamicSecret) return { blockingType: "dynamic_secret", blockingPath: resolvePath(dynamicSecret.folderId) };
+ const dynamicFolderIds = folderIdsForType("dynamic_secret");
+ if (dynamicFolderIds.length) {
+ const [dynamicSecret] = await dynamicSecretDAL.find({ $in: { folderId: dynamicFolderIds } }, { limit: 1, tx });
+ if (dynamicSecret) return { blockingType: "dynamic_secret", blockingPath: resolvePath(dynamicSecret.folderId) };
+ }
- const [honeyToken] = await honeyTokenDAL.find({ $in: { folderId: folderIds } }, { limit: 1, tx });
- if (honeyToken) return { blockingType: "honey_token", blockingPath: resolvePath(honeyToken.folderId) };
+ const honeyTokenFolderIds = folderIdsForType("honey_token");
+ if (honeyTokenFolderIds.length) {
+ const [honeyToken] = await honeyTokenDAL.find({ $in: { folderId: honeyTokenFolderIds } }, { limit: 1, tx });
+ if (honeyToken) return { blockingType: "honey_token", blockingPath: resolvePath(honeyToken.folderId) };
+ }
- const rotation = await secretRotationV2DAL.existsByFolderIds(folderIds, tx);
- if (rotation) return { blockingType: "secret_rotation", blockingPath: resolvePath(rotation.folderId) };
+ const rotationFolderIds = folderIdsForType("secret_rotation");
+ if (rotationFolderIds.length) {
+ const rotation = await secretRotationV2DAL.existsByFolderIds(rotationFolderIds, tx);
+ if (rotation) return { blockingType: "secret_rotation", blockingPath: resolvePath(rotation.folderId) };
+ }
return null;
};
@@ -1541,15 +1616,24 @@ export const secretFolderServiceFactory = ({
projectId,
environment,
rootFolderPath,
- subtree
+ subtree,
+ accessScope
}: {
projectId: string;
environment: string;
rootFolderPath: string;
subtree: { path: string }[];
+ accessScope?: TFolderMoveAccessScope;
}) => {
const toSourceAbsPath = (f: { path: string }) => (f.path === "/" ? rootFolderPath : `${rootFolderPath}${f.path}`);
- const subtreeAbsPaths = subtree.map(toSourceAbsPath);
+ let subtreeAbsPaths = subtree.map(toSourceAbsPath);
+
+ // only consider paths the actor is allowed to read, so policy presence at inaccessible paths isn't disclosed.
+ if (accessScope) {
+ subtreeAbsPaths = subtreeAbsPaths.filter((secretPath) =>
+ $canActorReadBlock(accessScope.permission, accessScope.environment, "secret_approval_policy", secretPath)
+ );
+ }
const policyByPath = await secretApprovalPolicyService.getSecretApprovalPolicyByPaths(
projectId,
@@ -1575,19 +1659,36 @@ export const secretFolderServiceFactory = ({
}: TGetFolderByIdDTO): Promise<TFolderMoveEligibility> => {
const folder = await getFolderById({ actor, actorId, actorOrgId, actorAuthMethod, id });
+ // resolve the actor's ability so subtree disclosure is gated by per-path read permission. this is
+ // request-memoized, so it reuses the lookup getFolderById already performed (no extra DB read).
+ const { permission } = await permissionService.getProjectPermission({
+ actor,
+ actorId,
+ projectId: folder.projectId,
+ actorAuthMethod,
+ actorOrgId,
+ actionProjectType: ActionProjectType.SecretManager
+ });
+ const accessScope: TFolderMoveAccessScope = {
+ permission,
+ environment: folder.environment.envSlug,
+ rootFolderPath: folder.path
+ };
+
// run every read inside a transaction so it hits the primary database rather than a read replica
const block = await folderDAL.transaction(async (tx) => {
// parent folder + full recursive subtree (with paths); reserved folders are already excluded.
const subtree = await folderDAL.findByEnvsDeep({ parentIds: [folder.id] }, tx);
- const secretBlock = await $getFolderMoveBlock(subtree, folder.path, tx);
+ const secretBlock = await $getFolderMoveBlock(subtree, folder.path, tx, accessScope);
if (secretBlock) return secretBlock;
const policyBlock = await $getFolderMovePolicyBlock({
projectId: folder.projectId,
environment: folder.environment.envSlug,
rootFolderPath: folder.path,
- subtree
+ subtree,
+ accessScope
});
if (policyBlock) {
return { blockingType: "secret_approval_policy" as const, blockingPath: policyBlock.blockedPath };
@@ -1712,21 +1813,29 @@ export const secretFolderServiceFactory = ({
tx
);
- // check if the folder contain only static secrets
+ // absolute source path of a subtree folder (the moved folder itself is "/"). reused below when building the plan.
+ const toSourceAbsPath = (f: { path: string }) =>
+ f.path === "/" ? sourceFolderPath : `${sourceFolderPath}${f.path}`;
+
+ // check if the folder contain only static secrets. the scan covers the full subtree (no access scope) so a
+ // block always prevents the move, but the error message only reveals the offending path/type when the actor
+ // is allowed to read it. otherwise it stays generic so inaccessible subtree contents aren't disclosed.
const block = await $getFolderMoveBlock(subtree, sourceFolderPath, tx);
if (block) {
+ const blockAbsPath = toSourceAbsPath({ path: block.blockingPath });
+ if ($canActorReadBlock(permission, sourceEnvironment, block.blockingType, blockAbsPath)) {
+ throw new BadRequestError({
+ message: `Cannot move folder '${folderName}'. It contains a ${block.blockingType.replace(
+ "_",
+ " "
+ )} at path '${block.blockingPath}'. Only folders that contain exclusively static secrets can be moved.`
+ });
+ }
throw new BadRequestError({
- message: `Cannot move folder '${folderName}'. It contains a ${block.blockingType.replace(
- "_",
- " "
- )} at path '${block.blockingPath}'. Only folders that contain exclusively static secrets can be moved.`
+ message: `Cannot move folder '${folderName}'. It contains resources that cannot be moved. Only folders that contain exclusively static secrets can be moved.`
});
}
- // absolute source path of a subtree folder (the moved folder itself is "/"). reused below when building the plan.
- const toSourceAbsPath = (f: { path: string }) =>
- f.path === "/" ? sourceFolderPath : `${sourceFolderPath}${f.path}`;
-
const policyBlock = await $getFolderMovePolicyBlock({
projectId,
environment: sourceEnvironment,
@@ -1734,9 +1843,14 @@ export const secretFolderServiceFactory = ({
subtree
});
if (policyBlock) {
+ if ($canActorReadBlock(permission, sourceEnvironment, "secret_approval_policy", policyBlock.blockedPath)) {
+ throw new BadRequestError({
+ message: `Cannot move folder '${folderName}'. The folder at path '${policyBlock.blockedPath}' is protected by the secret approval policy '${policyBlock.policyName}'. Folders governed by a secret approval policy cannot be moved. If you need to move this folder, please disable the secret approval policy for the path '${policyBlock.blockedPath}'`,
+ name: "MoveFolderProtectedByPolicy"
+ });
+ }
throw new BadRequestError({
- message: `Cannot move folder '${folderName}'. The folder at path '${policyBlock.blockedPath}' is protected by the secret approval policy '${policyBlock.policyName}'. Folders governed by a secret approval policy cannot be moved. If you need to move this folder, please disable the secret approval policy for the path '${policyBlock.blockedPath}'`,
- name: "MoveFolderProtectedByPolicy"
+ message: `Cannot move folder '${folderName}'. It contains resources that cannot be moved. Only folders that contain exclusively static secrets can be moved.`
});
}