Comfy-Org/ComfyUI · error · ValueError

Invalid folder name {folder_name!r}: resolves outside of {ba

Error message

Invalid folder name {folder_name!r}: resolves outside of {base_dir}

What it means

secure_subfolder_path joins folder_name onto base_dir, resolves it to an absolute path, and uses folder_paths.is_within_directory to guarantee the result stays inside base_dir. Any folder_name that escapes — '..' segments, absolute paths, Windows drive letters, or symlinked escapes — raises this ValueError. It is the containment guard that stops dataset node inputs from touching arbitrary filesystem paths.

Source

Thrown at comfy_extras/nodes_dataset.py:54

        if img.mode == "I":
            img = img.point(lambda i: i * (1 / 255))
        img = img.convert("RGB")
        img_array = np.array(img).astype(np.float32) / 255.0
        img_tensor = torch.from_numpy(img_array)[None,]
        output_images.append(img_tensor)

    return output_images


def secure_subfolder_path(base_dir, folder_name):
    """Resolve folder_name inside base_dir, rejecting anything that escapes it.

    Blocks '..', absolute paths, drive letters and symlink escapes using the
    same realpath containment check as the core file endpoints.
    """
    target = os.path.abspath(os.path.join(base_dir, folder_name))
    if not folder_paths.is_within_directory(base_dir, target):
        raise ValueError(f"Invalid folder name {folder_name!r}: resolves outside of {base_dir}")
    return target


def list_dataset_folders():
    """Relative paths of dataset folders found under all dataset roots.

    Any subfolder containing a metadata.json or *.safetensors shard counts as
    a dataset; the walk doesn't descend into matched folders.

    Symlinked directories are followed, but symlink loops are avoided.
    """
    found = set()

    for root in folder_paths.get_folder_paths("datasets"):
        if not os.path.isdir(root):
            continue

        root = os.path.abspath(root)

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Use a plain relative subfolder name such as 'my_dataset' or 'sets/train_a'.
  2. Remove any leading '/', drive letters, or '..' components from the input.
  3. If a symlinked dataset legitimately lives outside the root, add its parent as an extra datasets folder in extra_model_paths.yaml instead of symlinking past the guard.

Example fix

# before
folder_name = "/data/my_dataset"
# after
folder_name = "my_dataset"
Defensive patterns

Strategy: validation

Validate before calling

import re
SAFE_NAME = re.compile(r"^[^/\\]+(?:[/\\][^/\\]+)*$")
def safe_folder_name(name: str) -> bool:
    return bool(name) and name not in (".", "..") and ".." not in name.split("/" + "/") and SAFE_NAME.match(name) is not None

Type guard

def is_relative_subfolder(name) -> bool:
    return (isinstance(name, str) and name.strip() != "" and not os.path.isabs(name)
            and ".." not in name.replace("\\", "/").split("/"))

Prevention

When it happens

Trigger: Passing folder_name like '../..', '/etc', 'C:\\other', or a path whose resolved symlink target lies outside the datasets root. Also crafted names like 'safe/../../outside' that normalize outside the root.

Common situations: Users pasting absolute paths into a field that expects a relative subfolder; prompt-injection via untrusted combo strings; symlinked dataset folders whose target moved outside the root.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/61a6653bb9924b6d. Report an issue: GitHub.