Skip to content

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

Fix different PKI permission checks

Carlos Monastyrskid4fc9003f17b
diff --git a/backend/src/ee/services/permission/permission-service.ts b/backend/src/ee/services/permission/permission-service.ts
index 7634cacbca..0b1d8a7bd7 100644
--- a/backend/src/ee/services/permission/permission-service.ts
+++ b/backend/src/ee/services/permission/permission-service.ts
@@ -401,6 +401,7 @@ export const permissionServiceFactory = ({
});
if (!permissionData?.length)
throw new ForbiddenRequestError({
+ name: "ProjectMembershipNotFound",
message: `You are not a member of this project with ID ${projectId}. Please assign this ${actor} to the project with the appropriate permissions, then try again.`
});
@@ -629,6 +630,10 @@ export const permissionServiceFactory = ({
});
}
+ if (!actorOrgId) {
+ throw new BadRequestError({ message: "Organization context is required for resource permission checks" });
+ }
+
const memoKey = requestMemoKeys.resourcePermission({
projectId,
resourceType,
@@ -640,15 +645,8 @@ export const permissionServiceFactory = ({
});
return requestMemoize(memoKey, async () => {
- const resourceMemberships = await permissionDAL.getResourceMembership({
- projectId,
- resourceType,
- resourceId,
- actorId,
- actorType: actor
- });
-
let isProjectAdmin = false;
+ let isProjectMember = false;
try {
const projectPerm = await getProjectPermission({
actor,
@@ -659,10 +657,32 @@ export const permissionServiceFactory = ({
actionProjectType: ActionProjectType.CertificateManager
});
isProjectAdmin = projectPerm.hasRole(ProjectMembershipRole.Admin);
- } catch {
- isProjectAdmin = false;
+ isProjectMember = true;
+ } catch (err) {
+ if (!(err instanceof ForbiddenRequestError) || err.name !== "ProjectMembershipNotFound") {
+ throw err;
+ }
+ }
+
+ if (!isProjectMember) {
+ await getOrgPermission({
+ actor,
+ actorId,
+ orgId: actorOrgId,
+ actorOrgId,
+ scope: OrganizationActionScope.Any,
+ actorAuthMethod
+ });
}
+ const resourceMemberships = await permissionDAL.getResourceMembership({
+ projectId,
+ resourceType,
+ resourceId,
+ actorId,
+ actorType: actor
+ });
+
if (resourceMemberships?.length) {
const permissionFromRoles = flattenActiveRolesFromMemberships(
resourceMemberships,
diff --git a/backend/src/services/certificate-authority/ca-signing-config/ca-signing-config-service.ts b/backend/src/services/certificate-authority/ca-signing-config/ca-signing-config-service.ts
index 7e8abdd747..29e8d03771 100644
--- a/backend/src/services/certificate-authority/ca-signing-config/ca-signing-config-service.ts
+++ b/backend/src/services/certificate-authority/ca-signing-config/ca-signing-config-service.ts
@@ -464,7 +464,7 @@ export const caSigningConfigServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(
- ProjectPermissionCertificateAuthorityActions.Edit,
+ ProjectPermissionCertificateAuthorityActions.IssueCACertificate,
subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
);
@@ -519,7 +519,7 @@ export const caSigningConfigServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(
- ProjectPermissionCertificateAuthorityActions.Edit,
+ ProjectPermissionCertificateAuthorityActions.IssueCACertificate,
subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
);
diff --git a/backend/src/services/certificate-profile/certificate-profile-service.ts b/backend/src/services/certificate-profile/certificate-profile-service.ts
index 2754604116..228ce720f0 100644
--- a/backend/src/services/certificate-profile/certificate-profile-service.ts
+++ b/backend/src/services/certificate-profile/certificate-profile-service.ts
@@ -121,6 +121,23 @@ const validateAcmEnrollmentType = async (
}
};
+const validateCaProjectMatch = async (
+ caId: string | null | undefined,
+ projectId: string,
+ certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findById">
+) => {
+ if (!caId) return;
+ const ca = await certificateAuthorityDAL.findById(caId);
+ if (!ca) {
+ throw new NotFoundError({ message: "Certificate Authority not found" });
+ }
+ if (ca.projectId !== projectId) {
+ throw new ForbiddenRequestError({
+ message: "Invalid Certificate Authority"
+ });
+ }
+};
+
const validateExternalConfigs = async (
externalConfigs: Record<string, unknown> | null | undefined,
caId: string | null,
@@ -365,6 +382,8 @@ export const certificateProfileServiceFactory = ({
}
}
+ await validateCaProjectMatch(data.caId, projectId, certificateAuthorityDAL);
+
// Check for slug uniqueness within project
const existingSlugProfile = await certificateProfileDAL.findBySlugAndProjectId(data.slug, projectId);
if (existingSlugProfile) {
@@ -625,6 +644,10 @@ export const certificateProfileServiceFactory = ({
const finalEnrollmentType = data.enrollmentType || existingProfile.enrollmentType;
const finalCaId = data.caId !== undefined ? data.caId : existingProfile.caId;
+ if (data.caId !== undefined) {
+ await validateCaProjectMatch(data.caId, existingProfile.projectId, certificateAuthorityDAL);
+ }
+
validateIssuerTypeConstraints(finalIssuerType, finalEnrollmentType, finalCaId ?? null, existingProfile.caId);
await validateAcmEnrollmentType(finalCaId, finalEnrollmentType, externalCertificateAuthorityDAL);
diff --git a/backend/src/services/certificate-v3/certificate-v3-service.ts b/backend/src/services/certificate-v3/certificate-v3-service.ts
index f4ca58e7a2..6829693686 100644
--- a/backend/src/services/certificate-v3/certificate-v3-service.ts
+++ b/backend/src/services/certificate-v3/certificate-v3-service.ts
@@ -1237,6 +1237,12 @@ export const certificateV3ServiceFactory = ({
throw new NotFoundError({ message: "Certificate Authority not found" });
}
+ if (ca.projectId !== profile.projectId) {
+ throw new ForbiddenRequestError({
+ message: "Invalid Certificate Authority"
+ });
+ }
+
validateCaSupport(ca, "direct certificate issuance");
validateAlgorithmCompatibility(ca, policy);
@@ -1487,6 +1493,12 @@ export const certificateV3ServiceFactory = ({
throw new NotFoundError({ message: "Certificate Authority not found" });
}
+ if (ca.projectId !== profile.projectId) {
+ throw new ForbiddenRequestError({
+ message: "Invalid Certificate Authority"
+ });
+ }
+
validateCaSupport(ca, "CSR signing");
const policy = await certificatePolicyService.getPolicyById({
diff --git a/backend/src/services/pki-application/pki-application-service.ts b/backend/src/services/pki-application/pki-application-service.ts
index 4ee2ac58a5..6e86d51908 100644
--- a/backend/src/services/pki-application/pki-application-service.ts
+++ b/backend/src/services/pki-application/pki-application-service.ts
@@ -1,4 +1,4 @@
-import { ForbiddenError } from "@casl/ability";
+import { ForbiddenError, subject } from "@casl/ability";
import { packRules } from "@casl/ability/extra";
import {
@@ -11,6 +11,7 @@ import {
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
ProjectPermissionApplicationActions,
+ ProjectPermissionCertificateProfileActions,
ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import {
@@ -140,6 +141,13 @@ export const pkiApplicationServiceFactory = ({
message: "One or more profileIds do not belong to this project."
});
}
+
+ for (const profile of profilesInProject) {
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionCertificateProfileActions.ManageApplicationAttachments,
+ subject(ProjectPermissionSub.CertificateProfiles, { slug: profile.slug })
+ );
+ }
}
try {
@@ -448,6 +456,19 @@ export const pkiApplicationServiceFactory = ({
throw new BadRequestError({ message: "One or more profileIds do not belong to this project." });
}
+ const { permission: projectPermission } = await $loadProjectPermission(projectId, {
+ actor,
+ actorId,
+ actorAuthMethod,
+ actorOrgId
+ });
+ for (const profile of profilesInProject) {
+ ForbiddenError.from(projectPermission).throwUnlessCan(
+ ProjectPermissionCertificateProfileActions.ManageApplicationAttachments,
+ subject(ProjectPermissionSub.CertificateProfiles, { slug: profile.slug })
+ );
+ }
+
return pkiApplicationDAL.transaction(async (tx) => {
const existing = await pkiApplicationProfileDAL.findByApplicationId(applicationId, tx);
const existingProfileIds = new Set(existing.map((row) => row.profileId));
@@ -497,6 +518,22 @@ export const pkiApplicationServiceFactory = ({
});
}
+ const [profile] = await pkiApplicationProfileDAL.findProfilesInProject([profileId], projectId);
+ if (!profile) {
+ throw new NotFoundError({ message: `Profile '${profileId}' not found in project.` });
+ }
+
+ const { permission: projectPermission } = await $loadProjectPermission(projectId, {
+ actor,
+ actorId,
+ actorAuthMethod,
+ actorOrgId
+ });
+ ForbiddenError.from(projectPermission).throwUnlessCan(
+ ProjectPermissionCertificateProfileActions.ManageApplicationAttachments,
+ subject(ProjectPermissionSub.CertificateProfiles, { slug: profile.slug })
+ );
+
await pkiApplicationProfileDAL.delete({ applicationId, profileId });
return { applicationId, profileId };
};
diff --git a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts
index fa3a38c5df..325e2f508f 100644
--- a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts
@@ -298,6 +298,25 @@ const removeNetScalerCertificate = async (
await deleteCertKey(session, certKeyName, true);
};
+const escapeRegex = (s: string) => s.replace(new RE2("[\\\\^$.*+?()\\[\\]{}|/]", "g"), "\\$&");
+
+const buildManagedCertNamePattern = (certificateNameSchema: string | undefined): RE2 => {
+ const effectiveSchema = certificateNameSchema || "Infisical-{{certificateId}}";
+
+ const CERT_ID_TOKEN = "__INFISICAL_CERT_ID_PLACEHOLDER__";
+ const ENV_TOKEN = "__INFISICAL_ENV_PLACEHOLDER__";
+
+ const tokenized = effectiveSchema
+ .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), CERT_ID_TOKEN)
+ .replace(new RE2("\\{\\{environment\\}\\}", "g"), ENV_TOKEN);
+
+ const pattern = escapeRegex(tokenized)
+ .replace(new RE2(CERT_ID_TOKEN, "g"), "[0-9a-f]{32}")
+ .replace(new RE2(ENV_TOKEN, "g"), "global");
+
+ return new RE2(`^${pattern}$`);
+};
+
export const netScalerPkiSyncFactory = ({
certificateSyncDAL,
certificateDAL,
@@ -567,13 +586,11 @@ export const netScalerPkiSyncFactory = ({
}
}
- const namingPrefix = certificateNameSchema ? certificateNameSchema.split("{{")[0] : "Infisical-";
+ const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
- if (namingPrefix) {
- for (const certKeyName of existingCertKeyNames) {
- if (certKeyName.startsWith(namingPrefix) && !activeExternalIdentifiers.has(certKeyName)) {
- certKeysToRemove.add(certKeyName);
- }
+ for (const certKeyName of existingCertKeyNames) {
+ if (managedCertNamePattern.test(certKeyName) && !activeExternalIdentifiers.has(certKeyName)) {
+ certKeysToRemove.add(certKeyName);
}
}
diff --git a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-schemas.ts b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-schemas.ts
index 7d1ef35234..b9c520d774 100644
--- a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-schemas.ts
+++ b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-schemas.ts
@@ -23,9 +23,13 @@ export const NetScalerPkiSyncOptionsSchema = z.object({
(schema) => {
if (!schema) return true;
+ if (!schema.includes("{{certificateId}}")) {
+ return false;
+ }
+
const testName = schema
- .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), "")
- .replace(new RE2("\\{\\{environment\\}\\}", "g"), "");
+ .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), "test-cert-id")
+ .replace(new RE2("\\{\\{environment\\}\\}", "g"), "test-env");
const hasForbiddenChars = NETSCALER_NAMING.FORBIDDEN_CHARACTERS.split("").some((char) =>
testName.includes(char)
@@ -35,7 +39,7 @@ export const NetScalerPkiSyncOptionsSchema = z.object({
},
{
message:
- "Certificate name schema must result in names that contain only alphanumeric characters, hyphens (-), underscores (_), and periods (.) and be 1-255 characters long for NetScaler"
+ "Certificate name schema must include the {{certificateId}} placeholder and result in names that contain only alphanumeric characters, hyphens (-), underscores (_), and periods (.) and be 1-255 characters long for NetScaler"
}
)
});