Skip to content

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

fix(encryption): improve error handling in secret blind index generation

Victor Hugo dos Santos1673477d5e0c
diff --git a/backend/src/lib/crypto/encryption.ts b/backend/src/lib/crypto/encryption.ts
index f2a881a99b..3c0c5060c0 100644
--- a/backend/src/lib/crypto/encryption.ts
+++ b/backend/src/lib/crypto/encryption.ts
@@ -1,6 +1,7 @@
import argon2 from "argon2";
import { SecretKeyEncoding } from "@app/db/schemas";
+import { logger } from "@app/lib/logger";
import { crypto, SymmetricKeySize } from "./cryptography";
import { getLegacyDecryptionCandidates } from "./legacy-key";
@@ -38,6 +39,7 @@ export const buildSecretBlindIndexFromName = async ({
}
let salt = "";
+ let lastError: unknown;
for (const candidate of candidates) {
try {
if (candidate.ROOT_ENCRYPTION_KEY && keyEncoding === SecretKeyEncoding.BASE64) {
@@ -51,12 +53,19 @@ export const buildSecretBlindIndexFromName = async ({
.symmetric()
.decrypt({ iv, ciphertext, key: candidate.ENCRYPTION_KEY, tag, keySize: SymmetricKeySize.Bits128 });
}
- } catch {
+ } catch (err) {
+ lastError = err;
salt = "";
}
if (salt) break;
}
- if (!salt) throw new Error("Missing secret blind index key");
+ if (!salt) {
+ if (lastError) {
+ logger.error({ err: lastError }, "No configured encryption key decrypts this secret blind index salt");
+ throw new Error("No configured encryption key decrypts this secret blind index");
+ }
+ throw new Error("Missing secret blind index key");
+ }
const secretBlindIndex = await argon2.hash(secretName, {
type: argon2.argon2id,
diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.ts
index ba4577f37c..e339dd24ed 100644
--- a/backend/src/services/kms/kms-service.ts
+++ b/backend/src/services/kms/kms-service.ts
@@ -1624,6 +1624,7 @@ export const kmsServiceFactory = ({
const rootKey = await $decryptRootKey(row).then(
(key) => key,
(err: unknown) => {
+ if (row.encryptionStrategy === RootKeyEncryptionStrategy.HSM) throw err;
errors.push(err);
return null;
}
@@ -1758,8 +1759,15 @@ export const kmsServiceFactory = ({
if (current) return;
const sentinel = await kmsRootConfigDAL.findById(KMS_ROOT_CONFIG_UUID);
- await kmsKekHistoryDAL
- .create({ kekFingerprint: fingerprint, activatedAt: sentinel?.activatedAt ?? sentinel?.createdAt ?? new Date() })
+ await kmsRootConfigDAL
+ .transaction(async (tx) => {
+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsRootKeyInit]);
+ if (await kmsKekHistoryDAL.findCurrent(tx)) return;
+ await kmsKekHistoryDAL.create(
+ { kekFingerprint: fingerprint, activatedAt: sentinel?.activatedAt ?? sentinel?.createdAt ?? new Date() },
+ tx
+ );
+ })
.catch((err: unknown) => {
// Never block boot on a bookkeeping row.
logger.warn({ err }, "KMS: Failed to record initial encryption key history entry");
@@ -1869,7 +1877,8 @@ export const kmsServiceFactory = ({
KMS_ROOT_CONFIG_UUID,
{
encryptedRootKey,
- encryptionStrategy: strategy
+ encryptionStrategy: strategy,
+ kekFingerprint: strategy === RootKeyEncryptionStrategy.Software ? $currentKekFingerprint() : null
},
tx
);