Skip to content

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

fix(secret-reference): preserve exists:false for cached negative lookups

Claude538d27957edb
diff --git a/backend/e2e-test/routes/v3/secret-reference.spec.ts b/backend/e2e-test/routes/v3/secret-reference.spec.ts
index 9e42eb6ffa..7c22b9c843 100644
--- a/backend/e2e-test/routes/v3/secret-reference.spec.ts
+++ b/backend/e2e-test/routes/v3/secret-reference.spec.ts
@@ -188,6 +188,53 @@ describe("Secret expansion", () => {
await Promise.all(secrets.map((el) => deleteSecretV2(el)));
});
+ test("Local secret reference with repeated non-existent secret keeps literal references", async () => {
+ const secrets = [
+ {
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: projectId,
+ secretPath: "/",
+ authToken: jwtAuthToken,
+ key: "TEST",
+ // eslint-disable-next-line
+ value: "${MISSING} ${MISSING}"
+ }
+ ];
+
+ for (const secret of secrets) {
+ // eslint-disable-next-line no-await-in-loop
+ await createSecretV2(secret);
+ }
+
+ const expandedSecret = await getSecretByNameV2({
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: projectId,
+ secretPath: "/",
+ authToken: jwtAuthToken,
+ key: "TEST"
+ });
+ // eslint-disable-next-line
+ expect(expandedSecret.secretValue).toBe("${MISSING} ${MISSING}");
+
+ const listSecrets = await getSecretsV2({
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: projectId,
+ secretPath: "/",
+ authToken: jwtAuthToken
+ });
+ expect(listSecrets.secrets).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ secretKey: "TEST",
+ // eslint-disable-next-line
+ secretValue: "${MISSING} ${MISSING}"
+ })
+ ])
+ );
+
+ await Promise.all(secrets.map((el) => deleteSecretV2(el)));
+ });
+
test("Cross environment secret reference", async () => {
const secrets = [
{
diff --git a/backend/src/services/secret-v2-bridge/secret-reference-fns.ts b/backend/src/services/secret-v2-bridge/secret-reference-fns.ts
index 2394e0880f..35c5633317 100644
--- a/backend/src/services/secret-v2-bridge/secret-reference-fns.ts
+++ b/backend/src/services/secret-v2-bridge/secret-reference-fns.ts
@@ -87,7 +87,7 @@ export const expandSecretReferencesFactory = ({
canExpandValue,
userId
}: TInterpolateSecretArg) => {
- const secretCache: Record<string, Record<string, { value: string; tags: string[] }>> = {};
+ const secretCache: Record<string, Record<string, { value: string; tags: string[]; exists: boolean }>> = {};
const getCacheUniqueKey = (environment: string, secretPath: string) => `${environment}-${secretPath}`;
const fetchSecret = async (
@@ -99,7 +99,7 @@ export const expandSecretReferencesFactory = ({
if (secretCache?.[cacheKey]) {
const cachedSecret = secretCache[cacheKey][secretKey];
- if (cachedSecret) return { ...cachedSecret, exists: true };
+ if (cachedSecret) return { ...cachedSecret };
return { value: "", tags: [], exists: false };
}
@@ -110,26 +110,30 @@ export const expandSecretReferencesFactory = ({
// Personal overrides will take precedence over shared secrets in the reduce below.
const secrets = await secretDAL.findByFolderId({ folderId: folder.id, userId });
- const decryptedSecret = secrets.reduce<Record<string, { value: string; tags: string[] }>>((prev, secret) => {
- // When userId is set, personal overrides (userId !== null) should take precedence
- // over shared secrets for the same key. We skip overwriting if a personal override
- // is already stored and the current secret is a shared one.
- if (userId && prev[secret.key] && !secret.userId) {
- return prev;
- }
+ const decryptedSecret = secrets.reduce<Record<string, { value: string; tags: string[]; exists: boolean }>>(
+ (prev, secret) => {
+ // When userId is set, personal overrides (userId !== null) should take precedence
+ // over shared secrets for the same key. We skip overwriting if a personal override
+ // is already stored and the current secret is a shared one.
+ if (userId && prev[secret.key] && !secret.userId) {
+ return prev;
+ }
- // eslint-disable-next-line no-param-reassign
- prev[secret.key] = {
- value: decryptSecret(secret.encryptedValue) || "",
- tags: secret.tags?.map((el) => el.slug)
- };
- return prev;
- }, {});
+ // eslint-disable-next-line no-param-reassign
+ prev[secret.key] = {
+ value: decryptSecret(secret.encryptedValue) || "",
+ tags: secret.tags?.map((el) => el.slug),
+ exists: true
+ };
+ return prev;
+ },
+ {}
+ );
secretCache[cacheKey] = decryptedSecret;
const fetchedSecret = secretCache[cacheKey][secretKey];
- if (fetchedSecret) return { ...fetchedSecret, exists: true };
+ if (fetchedSecret) return { ...fetchedSecret };
return { value: "", tags: [], exists: false };
} catch (error) {
secretCache[cacheKey] = {};