Skip to content

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

test(encryption): add tests for encryption key buffer resolution errors

Victor Hugo dos Santosb12d80ee203a
diff --git a/backend/src/services/encryption-key-rotation/encryption-key-rotation-fns.test.ts b/backend/src/services/encryption-key-rotation/encryption-key-rotation-fns.test.ts
index 6cc08fd389..7b9c6b2c60 100644
--- a/backend/src/services/encryption-key-rotation/encryption-key-rotation-fns.test.ts
+++ b/backend/src/services/encryption-key-rotation/encryption-key-rotation-fns.test.ts
@@ -63,4 +63,27 @@ describe("resolveKekBuffer", () => {
expect(() => resolveKekBuffer(generateRootEncryptionKey(false), true)).toThrow();
expect(() => resolveKekBuffer(generateRootEncryptionKey(true), false)).toThrow();
});
+
+ // The instance is down when this throws, so the command it names is the operator's way out. Naming
+ // the wrong one loops them: the hex recipe read as base64 is 24 bytes, which reproduces this error.
+ test("the wrong-length error names the command that works for the key it was reading", () => {
+ expect(() => resolveInstanceEncryptionKeyBuffer({ ENCRYPTION_KEY: "tooshort" })).toThrow(/openssl rand -hex 16/);
+ expect(() => resolveInstanceEncryptionKeyBuffer({ ROOT_ENCRYPTION_KEY: "dG9vc2hvcnQ=" })).toThrow(
+ /openssl rand -base64 32/
+ );
+ });
+
+ test("and the command it names actually produces a key that resolves", () => {
+ for (const isFips of [false, true]) {
+ const command = isFips ? "openssl rand -base64 32" : "openssl rand -hex 16";
+ let message = "";
+ try {
+ resolveKekBuffer("tooshort", isFips);
+ } catch (err) {
+ message = err instanceof Error ? err.message : String(err);
+ }
+ expect(message).toContain(command);
+ expect(resolveKekBuffer(generateRootEncryptionKey(isFips), isFips)).toHaveLength(32);
+ }
+ });
});
diff --git a/backend/src/services/kms/kms-fns.ts b/backend/src/services/kms/kms-fns.ts
index 3e3b0a5031..d1b264b626 100644
--- a/backend/src/services/kms/kms-fns.ts
+++ b/backend/src/services/kms/kms-fns.ts
@@ -39,10 +39,13 @@ export const resolveInstanceEncryptionKeyBuffer = (envCfg: {
});
}
- const buffer = Buffer.from(encryptionKey, envCfg.ENCRYPTION_KEY ? "utf8" : "base64");
+ const isUtf8Key = Boolean(envCfg.ENCRYPTION_KEY);
+ const buffer = Buffer.from(encryptionKey, isUtf8Key ? "utf8" : "base64");
if (buffer.length !== 32) {
throw new BadRequestError({
- message: `The configured encryption key resolves to ${buffer.length} bytes, but 32 are required. Generate one with 'openssl rand -hex 16'.`
+ message: `The configured encryption key resolves to ${buffer.length} bytes, but 32 are required. Generate one with '${
+ isUtf8Key ? "openssl rand -hex 16" : "openssl rand -base64 32"
+ }'.`
});
}
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index 76ab80a7c9..4fe0088b55 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -1148,7 +1148,7 @@ export const projectServiceFactory = ({
});
}
- recordLegacyRootKeyUsageMetric({ operation: "encrypt", surface: "project_ghost_user" });
+ recordLegacyRootKeyUsageMetric({ operation: "encrypt", surface: "user_private_key" });
const encryptedPrivateKey = crypto.encryption().symmetric().encryptWithRootEncryptionKey(userPrivateKey);
await projectQueue.upgradeProject({
diff --git a/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx b/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx
index bcd1bc659c..6d63a31b1c 100644
--- a/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx
+++ b/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx
@@ -49,7 +49,7 @@ const BLOCKER_COPY: Record<EncryptionRotationBlocker, string> = {
};
export const EncryptionKeyRotationSection = () => {
- const { data: status, isPending } = useGetEncryptionStatus();
+ const { data: status, isPending, isError } = useGetEncryptionStatus();
const { mutateAsync: createRotation, isPending: isCreating } = useCreateEncryptionKeyRotation();
const { mutateAsync: discardRotation } = useDiscardEncryptionKeyRotation();
const { mutateAsync: completeRotation, isPending: isCompleting } =
@@ -58,6 +58,26 @@ export const EncryptionKeyRotationSection = () => {
const [generatedKey, setGeneratedKey] = useState<TCreatedEncryptionKeyRotation | null>(null);
const [acknowledged, setAcknowledged] = useState(false);
+ if (isError || (!isPending && !status)) {
+ return (
+ <Card className="mt-6">
+ <CardHeader>
+ <CardTitle>Root encryption key</CardTitle>
+ </CardHeader>
+ <CardContent>
+ <Alert variant="warning">
+ <AlertTriangleIcon />
+ <AlertTitle>Encryption status could not be loaded</AlertTitle>
+ <AlertDescription>
+ Reload the page to try again. While this is unavailable you cannot generate, discard,
+ or remove an encryption key from here.
+ </AlertDescription>
+ </Alert>
+ </CardContent>
+ </Card>
+ );
+ }
+
if (isPending || !status) return null;
const rotateBlockers = status.blockers.filter(