Skip to content

Dataset folder validation mutates shared BFS path list, causing false-positive and false-negative duplicate folder name checks #46639

Backlog

Description

@vishalmore90

Bug description

When DATASET_FOLDERS is enabled, validate_folders() in superset/commands/dataset/update.py (lines 941–994) performs a breadth-first traversal over the folder tree to verify UUID uniqueness, detect cycles, and enforce that folder names are unique among siblings while allowing identical folder names in different parent folders.

However, during BFS child traversal (lines 992–994), path is mutated in-place and the same list reference is shared across every sibling under a parent:

# superset/commands/dataset/update.py (lines 955-994)
    queue: list[tuple[FolderSchema, list[UUID]]] = [(folder, []) for folder in folders]
    seen_uuids = set()
    seen_fqns = set()  # fully qualified folder names
    while queue:
        obj, path = queue.pop(0)
        uuid, name = obj["uuid"], obj.get("name")

        if uuid in path:
            raise ValidationError(f"Cycle detected: {uuid} appears in its ancestry")
        ...
        # folders can have duplicate name as long as they're not siblings
        if name:
            fqn = tuple(path + [name])
            if name and fqn in seen_fqns:
                raise ValidationError(f"Duplicate folder name: {name}")
            seen_fqns.add(fqn)
        ...
        # traverse children
        if children := obj.get("children"):
            path.append(uuid)
            queue.extend((folder, path) for folder in children)

Whenever a parent folder has multiple children and an earlier child itself has children (such as columns, metrics, or subfolders), path.append(uuid) mutates the shared path list seen by all remaining siblings (and their descendants) in queue. This breaks both sibling duplicate detection and non-sibling allowance:

  1. False negative (allows duplicate sibling folder names):

    • Put two sibling folders both named "Sales" inside a parent folder "Root", where the first "Sales" folder has any child (for example, a subfolder, metric, or column).
    • Expected: ValidationError("Duplicate folder name: Sales") (rejected as 422 on PUT /api/v1/dataset/<pk>).
    • Actual: Validation passes. After visiting the first "Sales" folder, path.append(sales_1_uuid) mutates the shared list from [root_uuid] to [root_uuid, sales_1_uuid]. When the second "Sales" sibling is popped, its fqn is computed as (root_uuid, sales_1_uuid, "Sales"), which does not match (root_uuid, "Sales") in seen_fqns.
  2. False positive (rejects valid non-sibling folders with the same name):

    • Put two non-sibling folders named "Overview" inside "Root": one nested under "Root" -> "GroupA" -> "Overview", and one directly under "Root" -> "Overview" (after "GroupA" in "Root"'s children list).
    • Expected: Validation passes because "Overview" under "GroupA" and "Overview" under "Root" are not siblings.
    • Actual: Validation raises ValidationError("Duplicate folder name: Overview"). "GroupA" appends group_a_uuid to the shared path list ([root_uuid, group_a_uuid]). Both "Root" -> "Overview" (dequeued next in BFS) and "Root" -> "GroupA" -> "Overview" read that same mutated path, producing the identical fqn (root_uuid, group_a_uuid, "Overview").
  3. Corrupted ancestry tracking (if uuid in path):

    • Because path accumulates UUIDs from earlier siblings that had children rather than holding only true ancestors, path no longer represents a node's ancestry chain.

Steps to reproduce

Enable DATASET_FOLDERS = True and send a PUT /api/v1/dataset/<pk> payload (or call validate_folders(FolderSchema(many=True).load(folders), valid_uuids) directly) with either folder structure:

Case 1 — Duplicate sibling folders erroneously accepted:

[
  {
    "uuid": "11111111-1111-1111-1111-111111111111",
    "type": "folder",
    "name": "Root",
    "children": [
      {
        "uuid": "22222222-2222-2222-2222-222222222222",
        "type": "folder",
        "name": "Sales",
        "children": [
          {
            "uuid": "33333333-3333-3333-3333-333333333333",
            "type": "folder",
            "name": "Sub",
            "children": []
          }
        ]
      },
      {
        "uuid": "44444444-4444-4444-4444-444444444444",
        "type": "folder",
        "name": "Sales",
        "children": []
      }
    ]
  }
]

Case 2 — Valid non-sibling folders erroneously rejected (Duplicate folder name: Overview):

[
  {
    "uuid": "11111111-1111-1111-1111-111111111111",
    "type": "folder",
    "name": "Root",
    "children": [
      {
        "uuid": "22222222-2222-2222-2222-222222222222",
        "type": "folder",
        "name": "GroupA",
        "children": [
          {
            "uuid": "33333333-3333-3333-3333-333333333333",
            "type": "folder",
            "name": "Overview",
            "children": []
          }
        ]
      },
      {
        "uuid": "44444444-4444-4444-4444-444444444444",
        "type": "folder",
        "name": "Overview",
        "children": []
      }
    ]
  }
]

Screenshots/recordings

No response

Superset version

master / latest-dev

Python version

3.11

Node version

16

Browser

Chrome

Additional context

Additional context

  • Code pointers:
    • superset/commands/dataset/update.py lines 451–473 (UpdateDatasetCommand._validate_semantics) and lines 941–994 (validate_folders).
    • superset/datasets/schemas.py lines 231–258 (FolderSchema).
    • superset/config.py line 793 ("DATASET_FOLDERS": False, enabled in dev at docker/pythonpath_dev/superset_config.py line 118).
  • Why existing tests missed this:
    • test_validate_folders_duplicate_name_not_siblings (tests/unit_tests/commands/dataset/update_test.py lines 1280–1324) and test_validate_folders_duplicate_name_siblings (lines 1327–1373) both place the duplicate folder names in separate top-level root items (folders = [root1, root2]), which receive independent [] lists on line 955 rather than sharing a parent's path list.
  • Proposed fix:
    • Avoid mutating path in-place (for example, use immutable tuple[UUID, ...] paths with collections.deque, or create a new path new_path = [*path, uuid] when enqueuing children):
      queue: deque[tuple[FolderSchema, tuple[UUID, ...]]] = deque(
          (folder, ()) for folder in folders
      )
      ...
      while queue:
          obj, path = queue.popleft()
          ...
          if name:
              fqn = (*path, name)
              if fqn in seen_fqns:
                  raise ValidationError(f"Duplicate folder name: {name}")
              seen_fqns.add(fqn)
          ...
          if children := obj.get("children"):
              child_path = (*path, uuid)
              queue.extend((folder, child_path) for folder in children)
    • Add regression unit tests in tests/unit_tests/commands/dataset/update_test.py covering both nested sibling duplicates (with a populated first sibling) and nested non-sibling folders at different depths sharing a name under the same ancestor.

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    #bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions