Skip to content

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

Address greptile comments

Carlos Monastyrskia63e5e5b8bad
diff --git a/backend/src/db/migrations/20260325201236_add-scep-enrollment.ts b/backend/src/db/migrations/20260325201236_add-scep-enrollment.ts
index 7fae4266b8..2a8f324756 100644
--- a/backend/src/db/migrations/20260325201236_add-scep-enrollment.ts
+++ b/backend/src/db/migrations/20260325201236_add-scep-enrollment.ts
@@ -38,6 +38,8 @@ export async function up(knex: Knex): Promise<void> {
t.uuid("certificateRequestId").nullable();
t.index("certificateRequestId");
+ t.string("clientCipherOid", 64).nullable();
+
t.timestamp("expiresAt").notNullable();
t.index("expiresAt");
diff --git a/backend/src/db/schemas/pki-certificate-profiles.ts b/backend/src/db/schemas/pki-certificate-profiles.ts
index 6595f396a4..fbcc479836 100644
--- a/backend/src/db/schemas/pki-certificate-profiles.ts
+++ b/backend/src/db/schemas/pki-certificate-profiles.ts
@@ -20,10 +20,10 @@ export const PkiCertificateProfilesSchema = z.object({
createdAt: z.date(),
updatedAt: z.date(),
acmeConfigId: z.string().uuid().nullable().optional(),
- scepConfigId: z.string().uuid().nullable().optional(),
issuerType: z.string().default("ca"),
externalConfigs: z.string().nullable().optional(),
- defaults: z.unknown().nullable().optional()
+ defaults: z.unknown().nullable().optional(),
+ scepConfigId: z.string().uuid().nullable().optional()
});
export type TPkiCertificateProfiles = z.infer<typeof PkiCertificateProfilesSchema>;
diff --git a/backend/src/db/schemas/pki-scep-transactions.ts b/backend/src/db/schemas/pki-scep-transactions.ts
index 623cd57844..841e7623f3 100644
--- a/backend/src/db/schemas/pki-scep-transactions.ts
+++ b/backend/src/db/schemas/pki-scep-transactions.ts
@@ -16,6 +16,7 @@ export const PkiScepTransactionsSchema = z.object({
senderNonce: zodBuffer,
signerCertDer: zodBuffer,
certificateRequestId: z.string().uuid().nullable().optional(),
+ clientCipherOid: z.string().nullable().optional(),
expiresAt: z.date(),
createdAt: z.date(),
updatedAt: z.date()
diff --git a/backend/src/ee/services/pki-scep/pki-scep-service.ts b/backend/src/ee/services/pki-scep/pki-scep-service.ts
index fcd071f385..47751e2777 100644
--- a/backend/src/ee/services/pki-scep/pki-scep-service.ts
+++ b/backend/src/ee/services/pki-scep/pki-scep-service.ts
@@ -1,4 +1,5 @@
import * as x509 from "@peculiar/x509";
+import RE2 from "re2";
import { extractX509CertFromChain } from "@app/lib/certificates/extract-certificate";
import { crypto } from "@app/lib/crypto/cryptography";
@@ -166,7 +167,8 @@ export const pkiScepServiceFactory = ({
type TScepContext = Awaited<ReturnType<typeof loadScepContext>>;
const derToPem = (der: Buffer, label: string): string => {
- const base64 = der.toString("base64").replace(/(.{64})/g, "$1\n");
+ const re = new RE2("(.{64})", "g");
+ const base64 = re.replace(der.toString("base64"), "$1\n");
return `-----BEGIN ${label}-----\n${base64}\n-----END ${label}-----`;
};
@@ -382,17 +384,23 @@ export const pkiScepServiceFactory = ({
});
if (result.status === CertificateRequestStatus.PENDING_APPROVAL) {
- const expiresAt = new Date();
- expiresAt.setHours(expiresAt.getHours() + SCEP_TRANSACTION_EXPIRY_HOURS);
-
- await scepTransactionDAL.create({
- profileId: profile.id,
- transactionId: parsed.transactionId,
- senderNonce: parsed.senderNonce,
- signerCertDer: parsed.signerCertDer,
- certificateRequestId: result.certificateRequestId,
- expiresAt
- });
+ // If the client retries with the same transactionId (e.g. network hiccup),
+ // return PENDING instead of crashing on unique-constraint violation.
+ const existingTx = await scepTransactionDAL.findByProfileAndTransactionId(profile.id, parsed.transactionId);
+ if (!existingTx) {
+ const expiresAt = new Date();
+ expiresAt.setHours(expiresAt.getHours() + SCEP_TRANSACTION_EXPIRY_HOURS);
+
+ await scepTransactionDAL.create({
+ profileId: profile.id,
+ transactionId: parsed.transactionId,
+ senderNonce: parsed.senderNonce,
+ signerCertDer: parsed.signerCertDer,
+ certificateRequestId: result.certificateRequestId,
+ clientCipherOid: parsed.clientCipherOid || null,
+ expiresAt
+ });
+ }
void auditLogService.createAuditLog({
projectId: profile.projectId,
@@ -542,17 +550,21 @@ export const pkiScepServiceFactory = ({
});
if (result.status === CertificateRequestStatus.PENDING_APPROVAL) {
- const expiresAt = new Date();
- expiresAt.setHours(expiresAt.getHours() + SCEP_TRANSACTION_EXPIRY_HOURS);
+ const existingTx = await scepTransactionDAL.findByProfileAndTransactionId(profile.id, parsed.transactionId);
+ if (!existingTx) {
+ const expiresAt = new Date();
+ expiresAt.setHours(expiresAt.getHours() + SCEP_TRANSACTION_EXPIRY_HOURS);
- await scepTransactionDAL.create({
- profileId: profile.id,
- transactionId: parsed.transactionId,
- senderNonce: parsed.senderNonce,
- signerCertDer: parsed.signerCertDer,
- certificateRequestId: result.certificateRequestId,
- expiresAt
- });
+ await scepTransactionDAL.create({
+ profileId: profile.id,
+ transactionId: parsed.transactionId,
+ senderNonce: parsed.senderNonce,
+ signerCertDer: parsed.signerCertDer,
+ certificateRequestId: result.certificateRequestId,
+ clientCipherOid: parsed.clientCipherOid || null,
+ expiresAt
+ });
+ }
return buildCertRepPending({
raCertDer,
@@ -702,7 +714,8 @@ export const pkiScepServiceFactory = ({
raCertDer,
raPrivateKeyDer,
transactionId: parsed.transactionId,
- recipientNonce: parsed.senderNonce
+ recipientNonce: parsed.senderNonce,
+ clientCipherOid: transaction.clientCipherOid || undefined
});
}
diff --git a/backend/src/services/certificate-profile/certificate-profile-service.test.ts b/backend/src/services/certificate-profile/certificate-profile-service.test.ts
index 3311627c0a..d7739441a1 100644
--- a/backend/src/services/certificate-profile/certificate-profile-service.test.ts
+++ b/backend/src/services/certificate-profile/certificate-profile-service.test.ts
@@ -335,6 +335,7 @@ describe("CertificateProfileService", () => {
apiConfigId: "api-config-123",
estConfigId: null,
acmeConfigId: null,
+ scepConfigId: null,
projectId: "project-123"
},
undefined