Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(sql-editor): malformed persisted folder paths send sheet tree into infinite recursion (#20578)

Danny Xu6934d479b6a0
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index 10bab1e06d..209e020249 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -32,6 +32,7 @@ import {
storageKeySqlEditorWorksheetTree,
workspaceCacheScope,
} from "@/utils";
+import { isSubFolder } from "./folder";
import { type SheetViewMode, SheetViewModeList } from "./types";
// Worksheet caches, folder sets, and the sheet-tree contain Map / Set
@@ -422,21 +423,6 @@ const rootTreeNodeFor = (view: SheetViewMode): WorksheetFolderNode => ({
editable: false,
});
-const isSubFolder = ({
- parent,
- path,
- dig,
-}: {
- parent: string;
- path: string;
- dig: boolean;
-}) => {
- const parentPrefix = `${parent}/`;
- return path !== parentPrefix && path.startsWith(parentPrefix) && dig
- ? true
- : !path.replace(parentPrefix, "").includes("/");
-};
-
const ensureFolderPath = (view: SheetViewMode, path: string): string => {
const root = rootPathFor(view);
let p = path
diff --git a/frontend/src/views/sql-editor/Sheet/folder.test.ts b/frontend/src/views/sql-editor/Sheet/folder.test.ts
new file mode 100644
index 0000000000..49e3eab590
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/folder.test.ts
@@ -0,0 +1,60 @@
+import { describe, expect, test } from "vitest";
+import { isSubFolder } from "./folder";
+
+describe("isSubFolder", () => {
+ test("direct child matches with dig=false", () => {
+ expect(isSubFolder({ parent: "/my", path: "/my/a", dig: false })).toBe(
+ true
+ );
+ });
+
+ test("deeper descendant does not match with dig=false", () => {
+ expect(isSubFolder({ parent: "/my", path: "/my/a/b", dig: false })).toBe(
+ false
+ );
+ });
+
+ test("any descendant matches with dig=true", () => {
+ expect(isSubFolder({ parent: "/my", path: "/my/a", dig: true })).toBe(true);
+ expect(isSubFolder({ parent: "/my", path: "/my/a/b", dig: true })).toBe(
+ true
+ );
+ });
+
+ test("a path outside the parent never matches", () => {
+ expect(isSubFolder({ parent: "/my", path: "/shared/a", dig: false })).toBe(
+ false
+ );
+ expect(isSubFolder({ parent: "/my", path: "/shared/a", dig: true })).toBe(
+ false
+ );
+ });
+
+ test("the parent itself (with or without trailing slash) is not its own subfolder", () => {
+ expect(isSubFolder({ parent: "/my", path: "/my", dig: false })).toBe(false);
+ expect(isSubFolder({ parent: "/my", path: "/my/", dig: false })).toBe(
+ false
+ );
+ });
+
+ // Malformed entries can reach us from persisted localStorage (the read
+ // path only validates "array of strings"). They must be inert — the old
+ // implementation treated them as subfolders of EVERY parent, including
+ // themselves, sending the sheet-tree builder into infinite recursion.
+ test("malformed persisted entries are never subfolders of anything", () => {
+ for (const dig of [false, true]) {
+ expect(isSubFolder({ parent: "/my", path: "", dig })).toBe(false);
+ expect(isSubFolder({ parent: "/my", path: "foo", dig })).toBe(false);
+ expect(isSubFolder({ parent: "", path: "", dig })).toBe(false);
+ expect(isSubFolder({ parent: "foo", path: "foo", dig })).toBe(false);
+ }
+ });
+
+ test("a parent that appears as an inner segment elsewhere does not match", () => {
+ // The old `.replace(parentPrefix, "")` stripped the FIRST occurrence
+ // anywhere in the string, not just the prefix.
+ expect(
+ isSubFolder({ parent: "/my", path: "/shared/my/a", dig: false })
+ ).toBe(false);
+ });
+});
diff --git a/frontend/src/views/sql-editor/Sheet/folder.ts b/frontend/src/views/sql-editor/Sheet/folder.ts
new file mode 100644
index 0000000000..e9873cc44c
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/folder.ts
@@ -0,0 +1,29 @@
+// Pure worksheet-folder path helpers, extracted from context.ts so the
+// hierarchy logic is directly unit-testable.
+
+/**
+ * Whether `path` is a subfolder of `parent`. With `dig` set, any depth of
+ * descendant matches; otherwise only direct children.
+ *
+ * Folder paths come back from localStorage with only an "array of strings"
+ * shape check, so malformed entries (empty strings, paths missing the
+ * `/{view}` root prefix) must be treated as inert — never as subfolders.
+ */
+export const isSubFolder = ({
+ parent,
+ path,
+ dig,
+}: {
+ parent: string;
+ path: string;
+ dig: boolean;
+}): boolean => {
+ const parentPrefix = `${parent}/`;
+ if (path === parentPrefix || !path.startsWith(parentPrefix)) {
+ return false;
+ }
+ if (dig) {
+ return true;
+ }
+ return !path.slice(parentPrefix.length).includes("/");
+};