Skip to content

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

Address PR comments

Carlos Monastyrski9a1f35ab3f12
diff --git a/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts b/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts
index 31e339c104..5059c3eb57 100644
--- a/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts
+++ b/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts
@@ -144,6 +144,75 @@ export const internalCertificateAuthorityServiceFactory = ({
}
};
+ /**
+ * Validates CA path length constraints and creates the BasicConstraints extension.
+ * This handles both policy-level validation and issuing CA hierarchy validation.
+ */
+ const $createBasicConstraintsExtension = ({
+ caSettings,
+ pathLength,
+ caCertObj
+ }: {
+ caSettings?: { maxPathLength?: number } | null;
+ pathLength?: number | null;
+ caCertObj: x509.X509Certificate;
+ }): x509.BasicConstraintsExtension => {
+ const shouldIssueCaCertificate = caSettings !== undefined && caSettings !== null;
+
+ if (!shouldIssueCaCertificate) {
+ return new x509.BasicConstraintsExtension(false);
+ }
+
+ // Validate against policy's maxPathLength
+ const policyMaxPathLength = caSettings.maxPathLength;
+ if (policyMaxPathLength !== undefined && policyMaxPathLength !== null && policyMaxPathLength !== -1) {
+ if (pathLength === undefined || pathLength === null) {
+ throw new BadRequestError({
+ message: `Path length is required when issuing CA certificates. Policy allows maximum path length of ${policyMaxPathLength}.`
+ });
+ }
+ if (pathLength > policyMaxPathLength) {
+ throw new BadRequestError({
+ message: `Path length (${pathLength}) exceeds policy's maximum allowed path length (${policyMaxPathLength})`
+ });
+ }
+ }
+
+ // Validate against issuing CA's BasicConstraints
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
+ const issuingCaBasicConstraints = caCertObj.getExtension("2.5.29.19") as x509.BasicConstraintsExtension | null;
+
+ if (issuingCaBasicConstraints) {
+ const issuingCaPathLength = issuingCaBasicConstraints.pathLength;
+
+ if (issuingCaPathLength === 0) {
+ throw new BadRequestError({
+ message: "Issuing CA cannot issue subordinate CA certificates (path length is 0)"
+ });
+ }
+
+ if (issuingCaPathLength !== undefined && pathLength !== undefined && pathLength !== null) {
+ if (pathLength >= issuingCaPathLength) {
+ throw new BadRequestError({
+ message: `Path length (${pathLength}) must be less than issuing CA's path length (${issuingCaPathLength})`
+ });
+ }
+ }
+
+ const effectivePathLength =
+ pathLength !== undefined && pathLength !== null
+ ? pathLength
+ : issuingCaPathLength !== undefined
+ ? issuingCaPathLength - 1
+ : undefined;
+
+ return new x509.BasicConstraintsExtension(true, effectivePathLength, true);
+ }
+
+ const effectivePathLength = pathLength !== undefined && pathLength !== null ? pathLength : undefined;
+ return new x509.BasicConstraintsExtension(true, effectivePathLength, true);
+ };
+
const createCa = async ({
type,
friendlyName,
@@ -1372,60 +1441,11 @@ export const internalCertificateAuthorityServiceFactory = ({
const distributionPointUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/crl/${caCrl.id}/der`;
const caIssuerUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/ca/internal/${ca.id}/certificates/${caCert.id}/der`;
- const shouldIssueCaCertificate = caSettings !== undefined && caSettings !== null;
-
- let basicConstraintsExtension: x509.BasicConstraintsExtension;
- if (shouldIssueCaCertificate) {
- const templateMaxPathLength = caSettings.maxPathLength;
- if (templateMaxPathLength !== undefined && templateMaxPathLength !== null && templateMaxPathLength !== -1) {
- // When template has a maxPathLength set (not unlimited), pathLength is required
- if (pathLength === undefined || pathLength === null) {
- throw new BadRequestError({
- message: `Path length is required when issuing CA certificates. Template allows maximum path length of ${templateMaxPathLength}.`
- });
- }
- if (pathLength > templateMaxPathLength) {
- throw new BadRequestError({
- message: `Path length (${pathLength}) exceeds template's maximum allowed path length (${templateMaxPathLength})`
- });
- }
- }
-
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
- const issuingCaBasicConstraints = caCertObj.getExtension("2.5.29.19") as x509.BasicConstraintsExtension | null;
-
- if (issuingCaBasicConstraints) {
- const issuingCaPathLength = issuingCaBasicConstraints.pathLength;
-
- if (issuingCaPathLength === 0) {
- throw new BadRequestError({
- message: "Issuing CA cannot issue subordinate CA certificates (path length is 0)"
- });
- }
-
- if (issuingCaPathLength !== undefined && pathLength !== undefined && pathLength !== null) {
- if (pathLength >= issuingCaPathLength) {
- throw new BadRequestError({
- message: `Path length (${pathLength}) must be less than issuing CA's path length (${issuingCaPathLength})`
- });
- }
- }
-
- const effectivePathLength =
- pathLength !== undefined && pathLength !== null
- ? pathLength
- : issuingCaPathLength !== undefined
- ? issuingCaPathLength - 1
- : undefined;
-
- basicConstraintsExtension = new x509.BasicConstraintsExtension(true, effectivePathLength, true);
- } else {
- const effectivePathLength = pathLength !== undefined && pathLength !== null ? pathLength : undefined;
- basicConstraintsExtension = new x509.BasicConstraintsExtension(true, effectivePathLength, true);
- }
- } else {
- basicConstraintsExtension = new x509.BasicConstraintsExtension(false);
- }
+ const basicConstraintsExtension = $createBasicConstraintsExtension({
+ caSettings,
+ pathLength,
+ caCertObj
+ });
const extensions: x509.Extension[] = [
basicConstraintsExtension,
@@ -1795,60 +1815,11 @@ export const internalCertificateAuthorityServiceFactory = ({
const caIssuerUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/ca/internal/${ca.id}/certificates/${caCert.id}/der`;
- const shouldIssueCaCertificate = caSettings !== undefined && caSettings !== null;
-
- let basicConstraintsExtension: x509.BasicConstraintsExtension;
- if (shouldIssueCaCertificate) {
- const templateMaxPathLength = caSettings.maxPathLength;
- if (templateMaxPathLength !== undefined && templateMaxPathLength !== null && templateMaxPathLength !== -1) {
- // When template has a maxPathLength set (not unlimited), pathLength is required
- if (pathLength === undefined || pathLength === null) {
- throw new BadRequestError({
- message: `Path length is required when issuing CA certificates. Template allows maximum path length of ${templateMaxPathLength}.`
- });
- }
- if (pathLength > templateMaxPathLength) {
- throw new BadRequestError({
- message: `Path length (${pathLength}) exceeds template's maximum allowed path length (${templateMaxPathLength})`
- });
- }
- }
-
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
- const issuingCaBasicConstraints = caCertObj.getExtension("2.5.29.19") as x509.BasicConstraintsExtension | null;
-
- if (issuingCaBasicConstraints) {
- const issuingCaPathLength = issuingCaBasicConstraints.pathLength;
-
- if (issuingCaPathLength === 0) {
- throw new BadRequestError({
- message: "Issuing CA cannot issue subordinate CA certificates (path length is 0)"
- });
- }
-
- if (issuingCaPathLength !== undefined && pathLength !== undefined && pathLength !== null) {
- if (pathLength >= issuingCaPathLength) {
- throw new BadRequestError({
- message: `Path length (${pathLength}) must be less than issuing CA's path length (${issuingCaPathLength})`
- });
- }
- }
-
- const effectivePathLength =
- pathLength !== undefined && pathLength !== null
- ? pathLength
- : issuingCaPathLength !== undefined
- ? issuingCaPathLength - 1
- : undefined;
-
- basicConstraintsExtension = new x509.BasicConstraintsExtension(true, effectivePathLength, true);
- } else {
- const effectivePathLength = pathLength !== undefined && pathLength !== null ? pathLength : undefined;
- basicConstraintsExtension = new x509.BasicConstraintsExtension(true, effectivePathLength, true);
- }
- } else {
- basicConstraintsExtension = new x509.BasicConstraintsExtension(false);
- }
+ const basicConstraintsExtension = $createBasicConstraintsExtension({
+ caSettings,
+ pathLength,
+ caCertObj
+ });
const extensions: x509.Extension[] = [
basicConstraintsExtension,
diff --git a/backend/src/services/certificate-request/certificate-request-service.ts b/backend/src/services/certificate-request/certificate-request-service.ts
index 1b5fcb9346..02b4232878 100644
--- a/backend/src/services/certificate-request/certificate-request-service.ts
+++ b/backend/src/services/certificate-request/certificate-request-service.ts
@@ -77,6 +77,18 @@ const certificateRequestDataSchema = z
{
message: "notAfter must be after notBefore"
}
+ )
+ .refine(
+ (data) => {
+ // pathLength should only be set when isCA is true
+ if (data.caSettings?.pathLength !== undefined && !data.caSettings?.isCA) {
+ return false;
+ }
+ return true;
+ },
+ {
+ message: "pathLength can only be set when isCA is true"
+ }
);
const validateCertificateRequestData = (data: unknown) => {
diff --git a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateIssuanceModal.tsx b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateIssuanceModal.tsx
index e6e9546f53..75f365c7bb 100644
--- a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateIssuanceModal.tsx
+++ b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateIssuanceModal.tsx
@@ -650,7 +650,12 @@ export const CertificateIssuanceModal = ({ popUp, handlePopUpToggle, profileId }
isDisabled={!watchedIsCA}
onChange={(e) => {
const val = e.target.value;
- field.onChange(val === "" ? undefined : Number(val));
+ if (val === "") {
+ field.onChange(undefined);
+ } else {
+ const numVal = parseInt(val, 10);
+ field.onChange(Number.isNaN(numVal) ? undefined : numVal);
+ }
}}
/>
</FormControl>
diff --git a/frontend/src/pages/cert-manager/CertificatesPage/components/SubjectAttributesField.tsx b/frontend/src/pages/cert-manager/CertificatesPage/components/SubjectAttributesField.tsx
index ac7d01ff19..8479ed88ae 100644
--- a/frontend/src/pages/cert-manager/CertificatesPage/components/SubjectAttributesField.tsx
+++ b/frontend/src/pages/cert-manager/CertificatesPage/components/SubjectAttributesField.tsx
@@ -39,6 +39,7 @@ const getSubjectAttributePlaceholder = (type: CertSubjectAttributeType): string
};
type SubjectAttributesFieldProps = {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
control: Control<any>;
allowedAttributeTypes: CertSubjectAttributeType[];
error?: string;
@@ -103,7 +104,9 @@ export const SubjectAttributesField = ({
variant="plain"
size="sm"
onClick={() => {
- const newValue = currentValues.filter((_: any, i: number) => i !== index);
+ const newValue = currentValues.filter(
+ (_: SubjectAttribute, i: number) => i !== index
+ );
onChange(newValue);
}}
>
diff --git a/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx b/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx
index 493aaaeee0..2c901b1923 100644
--- a/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx
+++ b/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx
@@ -1160,7 +1160,12 @@ export const CreatePolicyModal = ({ isOpen, onClose, policy, mode = "create" }:
isDisabled={!watchedIsCA}
onChange={(e) => {
const val = e.target.value;
- field.onChange(val === "" ? undefined : Number(val));
+ if (val === "") {
+ field.onChange(undefined);
+ } else {
+ const numVal = parseInt(val, 10);
+ field.onChange(Number.isNaN(numVal) ? undefined : numVal);
+ }
}}
/>
</FormControl>