Skip to content

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

Address greptile comments

Carlos Monastyrski12c93d82f275
diff --git a/backend/src/services/secret-folder/secret-folder-dal.ts b/backend/src/services/secret-folder/secret-folder-dal.ts
index 3eb8b22856..11f537cb64 100644
--- a/backend/src/services/secret-folder/secret-folder-dal.ts
+++ b/backend/src/services/secret-folder/secret-folder-dal.ts
@@ -408,13 +408,11 @@ export const secretFolderDALFactory = (db: TDbClient) => {
const childrenMap = buildChildrenMap(allFoldersIncludingParents);
const results: (TSecretFolders & { path: string; depth: number; environment: string })[] = [];
- const parentIdSet = new Set(parentIds);
const traverse = (folder: TSecretFolders, currentPath: string, depth: number, environment: string) => {
results.push({ ...folder, path: currentPath, depth, environment });
- const children = childrenMap[folder.id] || [];
+ const children = (childrenMap[folder.id] || []).filter((child) => !child.isReserved);
for (const child of children) {
- if (child.isReserved) continue;
const childPath = currentPath === "/" ? `/${child.name}` : `${currentPath}/${child.name}`;
traverse(child, childPath, depth + 1, environment);
}
@@ -463,9 +461,8 @@ export const secretFolderDALFactory = (db: TDbClient) => {
if (folderIdSet.has(folder.id)) {
results.push({ ...folder, path: currentPath, depth, environment });
}
- const children = childrenMap[folder.id] || [];
+ const children = (childrenMap[folder.id] || []).filter((child) => !child.isReserved);
for (const child of children) {
- if (child.isReserved) continue;
const childPath = currentPath === "/" ? `/${child.name}` : `${currentPath}/${child.name}`;
traverse(child, childPath, depth + 1);
}
diff --git a/backend/src/services/secret-folder/secret-folder-fns.test.ts b/backend/src/services/secret-folder/secret-folder-fns.test.ts
index daacd89c4d..25f4cd3469 100644
--- a/backend/src/services/secret-folder/secret-folder-fns.test.ts
+++ b/backend/src/services/secret-folder/secret-folder-fns.test.ts
@@ -109,6 +109,15 @@ describe("buildFolderPath", () => {
expect(buildFolderPath(folderD, idMap)).toBe("/folder-a/folder-d");
});
+ test("handles orphaned folder with missing parent gracefully", () => {
+ // Folder whose parentId points to a non-existent folder
+ const orphan = makeFolder("orphan-id", "orphan", "missing-parent-id");
+ const orphanMap = buildFolderIdMap([orphan]);
+
+ // Should not throw — returns path treating orphan as top-level
+ expect(buildFolderPath(orphan, orphanMap)).toBe("/orphan");
+ });
+
test("throws when depth exceeds 20", () => {
// Create a chain of 22 folders (root + 21 children)
const deepFolders: TSecretFolders[] = [makeFolder("deep-0", "root", null)];
diff --git a/backend/src/services/secret-folder/secret-folder-fns.ts b/backend/src/services/secret-folder/secret-folder-fns.ts
index a92510c301..c1ac7e2397 100644
--- a/backend/src/services/secret-folder/secret-folder-fns.ts
+++ b/backend/src/services/secret-folder/secret-folder-fns.ts
@@ -13,7 +13,13 @@ export const buildFolderPath = (
return depth === 0 ? "/" : "";
}
- return `${buildFolderPath(foldersMap[folder.parentId], foldersMap, depth + 1)}/${folder.name}`;
+ const parent = foldersMap[folder.parentId];
+ if (!parent) {
+ // Orphaned folder
+ return `/${folder.name}`;
+ }
+
+ return `${buildFolderPath(parent, foldersMap, depth + 1)}/${folder.name}`;
};
export const buildFolderIdMap = (folders: TSecretFolders[]): Record<string, TSecretFolders> => {