Skip to content

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

Merge remote-tracking branch 'origin/main' into fix/secret-reference-missing-secret

Claude98e00b703657
diff --cc backend/src/services/secret-v2-bridge/secret-reference-fns.ts
index 35c5633317,5a3d358422..0068377415
--- a/backend/src/services/secret-v2-bridge/secret-reference-fns.ts
+++ b/backend/src/services/secret-v2-bridge/secret-reference-fns.ts
@@@ -85,16 -118,48 +118,52 @@@ export const expandSecretReferencesFact
secretDAL,
folderDAL,
canExpandValue,
- userId
+ userId,
+ actorOrgId,
+ orgDAL,
+ projectFolderGrantDAL,
+ projectDAL,
+ kmsService,
+ crossProjectSecretDAL
}: 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}`;
+ let crossProjectAllowedCache: boolean | undefined;
+ const hasCrossProjectConfig = Boolean(actorOrgId && orgDAL && projectFolderGrantDAL && projectDAL && kmsService);
+ const checkCrossProjectAllowed = async () => {
+ if (!hasCrossProjectConfig || !actorOrgId || !orgDAL) return false;
+ if (crossProjectAllowedCache !== undefined) return crossProjectAllowedCache;
+ crossProjectAllowedCache = await isCrossProjectEnabled(actorOrgId, orgDAL);
+ return crossProjectAllowedCache;
+ };
+ const slugToProjectId = new Map<string, string | null>();
+ const resolveProjectSlugs = async (slugs: string[]) => {
+ if (!projectDAL || !actorOrgId) return;
+ const uncached = slugs.filter((s) => !slugToProjectId.has(s));
+ if (!uncached.length) return;
+
+ const projects = await projectDAL.find({ orgId: actorOrgId, $in: { slug: uncached } });
+ const found = new Map(projects.map((p) => [p.slug, p.id]));
+ for (const s of uncached) {
+ slugToProjectId.set(s, found.get(s) ?? null);
+ }
+ };
+ const getProjectDecryptor = async (sourceProjectId: string) => {
+ if (!kmsService) return undefined;
+ const { decryptor: sourceDecryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId: sourceProjectId
+ });
+ return (value: Buffer | null | undefined) => (value ? sourceDecryptor({ cipherTextBlob: value }).toString() : "");
+ };
+ // Cache key includes project to avoid collisions between same-named envs across projects
+ const getCacheUniqueKey = (environment: string, secretPath: string, srcProjectId?: string) =>
+ srcProjectId ? `${srcProjectId}:${environment}-${secretPath}` : `${environment}-${secretPath}`;
- const fetchSecret = async (environment: string, secretPath: string, secretKey: string) => {
+ const fetchSecret = async (
+ environment: string,
+ secretPath: string,
+ secretKey: string
+ ): Promise<{ value: string; tags: string[]; exists: boolean }> => {
const cacheKey = getCacheUniqueKey(environment, secretPath);
if (secretCache?.[cacheKey]) {
@@@ -186,7 -268,8 +280,9 @@@
let referencedSecretKey = "";
let referencedSecretEnvironmentSlug = "";
let referencedSecretValue = "";
+ let referencedSecretExists = false;
+ let referencedProjectSlug: string | undefined;
+ let isCrossProjectRef = false;
if (entities.length === 1) {
const [secretKey] = entities;
@@@ -207,6 -288,100 +302,105 @@@
referencedSecretKey = secretKey;
referencedSecretPath = secretPath;
referencedSecretEnvironmentSlug = environment;
+ } else if (entities[0].startsWith("@")) {
+ // Cross-project reference: ${@project-slug.env.path.KEY}
+ // eslint-disable-next-line no-await-in-loop
+ if (!hasCrossProjectConfig || !projectFolderGrantDAL || !crossProjectSecretSharingEnabled) {
+ // eslint-disable-next-line no-continue
+ continue;
+ }
+
+ isCrossProjectRef = true;
+ const crossProjSlug = entities[0].slice(1);
+ const crossProjEnv = entities[1];
+ const crossProjPath = path.join("/", ...entities.slice(2, entities.length - 1));
+ const crossProjKey = entities[entities.length - 1];
+
+ const sourceProjectId = slugToProjectId.get(crossProjSlug) ?? null;
+ if (!sourceProjectId) {
+ // eslint-disable-next-line no-continue
+ continue;
+ }
+
+ const crossProjCacheKey = getCacheUniqueKey(crossProjEnv, crossProjPath, sourceProjectId);
+
- let crossProjSecretData: { value: string; tags: string[] };
++ let crossProjSecretData: { value: string; tags: string[]; exists: boolean };
+ if (secretCache?.[crossProjCacheKey]) {
- crossProjSecretData = secretCache[crossProjCacheKey][crossProjKey] || { value: "", tags: [] };
++ crossProjSecretData = secretCache[crossProjCacheKey][crossProjKey] || {
++ value: "",
++ tags: [],
++ exists: false
++ };
+ } else {
+ try {
+ // eslint-disable-next-line no-await-in-loop
+ const sourceFolder = await folderDAL.findBySecretPath(sourceProjectId, crossProjEnv, crossProjPath);
+ if (!sourceFolder) {
+ secretCache[crossProjCacheKey] = {};
- crossProjSecretData = { value: "", tags: [] };
++ crossProjSecretData = { value: "", tags: [], exists: false };
+ } else {
+ // Verify that a folder grant exists: source folder → current target project
+ // eslint-disable-next-line no-await-in-loop
+ const [grant] = await projectFolderGrantDAL.find({
+ sourceProjectId,
+ sourceFolderId: sourceFolder.id,
+ targetProjectId: projectId
+ });
+
+ if (!grant) {
+ secretCache[crossProjCacheKey] = {};
- crossProjSecretData = { value: "", tags: [] };
++ crossProjSecretData = { value: "", tags: [], exists: false };
+ } else {
+ // eslint-disable-next-line no-await-in-loop
+ const sourceDecrypt = await getProjectDecryptor(sourceProjectId);
+ if (!sourceDecrypt) {
+ secretCache[crossProjCacheKey] = {};
- crossProjSecretData = { value: "", tags: [] };
++ crossProjSecretData = { value: "", tags: [], exists: false };
+ // eslint-disable-next-line no-continue
+ continue;
+ }
+ // Use the explicit raw DAL when same-project resolution wraps secretDAL
+ // with import-aware behavior.
+ // eslint-disable-next-line no-await-in-loop
+ const sourceSecrets = await (crossProjectSecretDAL || secretDAL).findByFolderId({
+ folderId: sourceFolder.id
+ });
+
- const crossProjDecrypted = sourceSecrets.reduce<Record<string, { value: string; tags: string[] }>>(
- (prev, secret) => {
- // Only shared secrets (no personal overrides) from source project
- if (!secret.userId) {
- // eslint-disable-next-line no-param-reassign
- prev[secret.key] = {
- value: sourceDecrypt(secret.encryptedValue) || "",
- tags: secret.tags?.map((el) => el.slug) || []
- };
- }
- return prev;
- },
- {}
- );
++ const crossProjDecrypted = sourceSecrets.reduce<
++ Record<string, { value: string; tags: string[]; exists: boolean }>
++ >((prev, secret) => {
++ // Only shared secrets (no personal overrides) from source project
++ if (!secret.userId) {
++ // eslint-disable-next-line no-param-reassign
++ prev[secret.key] = {
++ value: sourceDecrypt(secret.encryptedValue) || "",
++ tags: secret.tags?.map((el) => el.slug) || [],
++ exists: true
++ };
++ }
++ return prev;
++ }, {});
+
+ secretCache[crossProjCacheKey] = crossProjDecrypted;
- crossProjSecretData = crossProjDecrypted[crossProjKey] || { value: "", tags: [] };
++ crossProjSecretData = crossProjDecrypted[crossProjKey] || { value: "", tags: [], exists: false };
+ }
+ }
+ } catch (error) {
+ logger.error(
+ { err: error, crossProjSlug, crossProjEnv, crossProjPath, crossProjKey },
+ `Failed to expand cross-project reference [slug=${crossProjSlug}] [env=${crossProjEnv}] [path=${crossProjPath}] [key=${crossProjKey}]`
+ );
+ secretCache[crossProjCacheKey] = {};
- crossProjSecretData = { value: "", tags: [] };
++ crossProjSecretData = { value: "", tags: [], exists: false };
+ }
+ }
+
+ referencedSecretValue = crossProjSecretData.value;
++ referencedSecretExists = crossProjSecretData.exists;
+ referencedSecretKey = crossProjKey;
+ referencedSecretPath = crossProjPath;
+ referencedSecretEnvironmentSlug = crossProjEnv;
+ referencedProjectSlug = crossProjSlug;
} else {
const secretReferenceEnvironment = entities[0];
const secretReferencePath = path.join("/", ...entities.slice(1, entities.length - 1));