Skip to content

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

feat: refact permission validation on move secrets

adilsonfd525b212364
diff --git a/backend/src/ee/services/permission/permission-fns.ts b/backend/src/ee/services/permission/permission-fns.ts
index 767bc056b7..d3cbbc3c53 100644
--- a/backend/src/ee/services/permission/permission-fns.ts
+++ b/backend/src/ee/services/permission/permission-fns.ts
@@ -77,6 +77,62 @@ export function hasSecretReadValueOrDescribePermission(
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
+ })
+ );
+ }
+ }
+}
+
const OptionalArrayPermissionSchema = ProjectPermissionV2Schema.array().optional();
export function checkForInvalidPermissionCombination(permissions: z.infer<typeof OptionalArrayPermissionSchema>) {
if (!permissions) return;
diff --git a/backend/src/services/secret-folder/secret-folder-service.ts b/backend/src/services/secret-folder/secret-folder-service.ts
index d2fbb17517..ec41d56b5b 100644
--- a/backend/src/services/secret-folder/secret-folder-service.ts
+++ b/backend/src/services/secret-folder/secret-folder-service.ts
@@ -7,6 +7,7 @@ import { v4 as uuidv4, validate as uuidValidate } from "uuid";
import { ActionProjectType, TProjectEnvironments, TSecretFolders, TSecretFoldersInsert } from "@app/db/schemas";
import { TDynamicSecretDALFactory } from "@app/ee/services/dynamic-secret/dynamic-secret-dal";
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 { TSecretApprovalPolicyServiceFactory } from "@app/ee/services/secret-approval-policy/secret-approval-policy-service";
@@ -1477,9 +1478,9 @@ export const secretFolderServiceFactory = ({
const pathByFolderId = new Map(subtree.map((f) => [f.id, f.path]));
const resolvePath = (folderId: string) => pathByFolderId.get(folderId) ?? fallbackPath;
- // sequential early-exit checks, cheapest single-table queries first.
- const importHit = await secretImportDAL.findImportByFolderIds(folderIds, tx);
- if (importHit) return { blockingType: "secret_import", blockingPath: resolvePath(importHit.folderId) };
+ // 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 [dynamicSecret] = await dynamicSecretDAL.find({ $in: { folderId: folderIds } }, { limit: 1, tx });
if (dynamicSecret) return { blockingType: "dynamic_secret", blockingPath: resolvePath(dynamicSecret.folderId) };
@@ -1648,6 +1649,23 @@ export const secretFolderServiceFactory = ({
return { plan: planEntries };
});
+ // pre-authorize the secret moves for the entire subtree before any folder is created or secret is
+ // moved, so an unauthorized move fails atomically instead of partway through. each folder that holds
+ // secrets is checked once at its own source/destination path (moveSecrets is then told to skip its
+ // per-batch check below). folders without secrets are never moved, so they need no secret-level check.
+ for (const entry of plan) {
+ if (!entry.secretIds.length) {
+ // eslint-disable-next-line no-continue
+ continue;
+ }
+ validateSecretMovePermissions(permission, {
+ sourceEnvironment,
+ sourceSecretPath: entry.sourceAbsPath,
+ destinationEnvironment,
+ destinationSecretPath: entry.destinationAbsPath
+ });
+ }
+
// 6. recreate the folder tree at the destination (idempotent; manages its own transaction, commits and snapshot).
await createManyFolders({
projectId,
@@ -1681,7 +1699,9 @@ export const secretFolderServiceFactory = ({
actor,
actorId,
actorAuthMethod,
- actorOrgId
+ actorOrgId,
+ // the whole subtree was authorized up front, so skip the redundant per-batch check
+ skipPermissionCheck: true
});
// a missing direct update on either side means moveSecrets routed the change through an approval request
if (!result.isDestinationUpdated || !result.isSourceUpdated) {
diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
index 9547140b84..c926f60f9b 100644
--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
@@ -2980,7 +2980,8 @@ export const secretV2BridgeServiceFactory = ({
actor,
actorId,
actorAuthMethod,
- actorOrgId
+ actorOrgId,
+ skipPermissionCheck
}: TMoveSecretsDTO) => {
const { permission } = await permissionService.getProjectPermission({
actor,
@@ -3038,6 +3039,9 @@ export const secretV2BridgeServiceFactory = ({
throw new BadRequestError({ message: `Cannot move honey token secret: ${secret.key}` });
}
+ // the caller (e.g. folder move) has already pre-authorized the move at the path level
+ if (skipPermissionCheck) return;
+
for (const sourceAction of sourceActions) {
if (
sourceAction === ProjectPermissionSecretActions.DescribeSecret ||
@@ -3158,19 +3162,22 @@ export const secretV2BridgeServiceFactory = ({
}
// permission check whether can create or edit the ones in the destination folder
- locallyCreatedSecrets.forEach((secret) => {
- for (const destinationAction of destinationActions) {
- ForbiddenError.from(permission).throwUnlessCan(
- destinationAction,
- subject(ProjectPermissionSub.Secrets, {
- environment: destinationEnvironment,
- secretPath: destinationFolder.path,
- secretName: secret.key,
- secretTags: secret.tags.map((el) => el.slug)
- })
- );
- }
- });
+ // (skipped when the caller has already pre-authorized the move at the path level)
+ if (!skipPermissionCheck) {
+ locallyCreatedSecrets.forEach((secret) => {
+ for (const destinationAction of destinationActions) {
+ ForbiddenError.from(permission).throwUnlessCan(
+ destinationAction,
+ subject(ProjectPermissionSub.Secrets, {
+ environment: destinationEnvironment,
+ secretPath: destinationFolder.path,
+ secretName: secret.key,
+ secretTags: secret.tags.map((el) => el.slug)
+ })
+ );
+ }
+ });
+ }
const destinationFolderPolicy = await secretApprovalPolicyService.getSecretApprovalPolicy(
projectId,
diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-types.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-types.ts
index 6940bff4de..9e0efd0461 100644
--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-types.ts
+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-types.ts
@@ -352,6 +352,7 @@ export type TMoveSecretsDTO = {
destinationSecretPath: string;
secretIds: string[];
shouldOverwrite: boolean;
+ skipPermissionCheck?: boolean;
} & Omit<TProjectPermission, "projectId">;
export type TDuplicateSecretAttributes = {
diff --git a/backend/src/services/secret/secret-service.ts b/backend/src/services/secret/secret-service.ts
index e85a1e4d13..200157eb1e 100644
--- a/backend/src/services/secret/secret-service.ts
+++ b/backend/src/services/secret/secret-service.ts
@@ -18,7 +18,8 @@ import { TUserGroupMembershipDALFactory } from "@app/ee/services/group/user-grou
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import {
hasSecretReadValueOrDescribePermission,
- throwIfMissingSecretReadValueOrDescribePermission
+ throwIfMissingSecretReadValueOrDescribePermission,
+ validateSecretMovePermissions
} from "@app/ee/services/permission/permission-fns";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { ProjectPermissionSecretActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
@@ -2982,7 +2983,8 @@ export const secretServiceFactory = ({
actorId,
actorAuthMethod,
actorOrgId,
- projectId: inputProjectId
+ projectId: inputProjectId,
+ skipPermissionCheck
}: TMoveSecretsDTO) => {
let project;
if (projectSlug) {
@@ -3012,7 +3014,8 @@ export const secretServiceFactory = ({
actor,
actorId,
actorAuthMethod,
- actorOrgId
+ actorOrgId,
+ skipPermissionCheck
});
}
@@ -3064,64 +3067,34 @@ export const secretServiceFactory = ({
});
}
- const sourceActions = [
- ProjectPermissionSecretActions.Delete,
- ProjectPermissionSecretActions.DescribeSecret,
- ProjectPermissionSecretActions.ReadValue
- ] as const;
- const destinationActions = [ProjectPermissionSecretActions.Create, ProjectPermissionSecretActions.Edit] as const;
+ // a move is delete-at-source + create/edit-at-destination. this is path-scoped, so it is checked
+ // once for the whole batch rather than per secret (skipped when the caller has pre-authorized).
+ if (!skipPermissionCheck) {
+ validateSecretMovePermissions(permission, {
+ sourceEnvironment,
+ sourceSecretPath,
+ destinationEnvironment,
+ destinationSecretPath
+ });
+ }
- const decryptedSourceSecrets = sourceSecrets.map((secret) => {
- const secretKey = crypto.encryption().symmetric().decrypt({
+ const decryptedSourceSecrets = sourceSecrets.map((secret) => ({
+ ...secret,
+ secretKey: crypto.encryption().symmetric().decrypt({
ciphertext: secret.secretKeyCiphertext,
iv: secret.secretKeyIV,
tag: secret.secretKeyTag,
key: botKey,
keySize: SymmetricKeySize.Bits128
- });
-
- 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
- })
- );
- }
- }
-
- return {
- ...secret,
- secretKey,
- secretValue: crypto.encryption().symmetric().decrypt({
- ciphertext: secret.secretValueCiphertext,
- iv: secret.secretValueIV,
- tag: secret.secretValueTag,
- key: botKey,
- keySize: SymmetricKeySize.Bits128
- })
- };
- });
+ }),
+ secretValue: crypto.encryption().symmetric().decrypt({
+ ciphertext: secret.secretValueCiphertext,
+ iv: secret.secretValueIV,
+ tag: secret.secretValueTag,
+ key: botKey,
+ keySize: SymmetricKeySize.Bits128
+ })
+ }));
let isSourceUpdated = false;
let isDestinationUpdated = false;
diff --git a/backend/src/services/secret/secret-types.ts b/backend/src/services/secret/secret-types.ts
index 10b157cc26..b6b829656d 100644
--- a/backend/src/services/secret/secret-types.ts
+++ b/backend/src/services/secret/secret-types.ts
@@ -562,6 +562,7 @@ export type TMoveSecretsDTO = {
destinationSecretPath: string;
secretIds: string[];
shouldOverwrite: boolean;
+ skipPermissionCheck?: boolean;
} & Omit<TProjectPermission, "projectId">;
export enum SecretProtectionType {