logo

Database

Lack of data validation - Path Traversal In banks

Description

Banks: Path traversal in DirectoryPromptRegistry.set() allows arbitrary file write outside the registry root

Summary

DirectoryPromptRegistry.set() interpolates the attacker-controllable Prompt.name into a Path expression with no canonicalization. An application that derives the prompt name from request data lets a caller write attacker-controlled bytes outside the configured registry directory.

Details

src/banks/registries/directory.py:44

prompt_file = path / f"{prompt.name}.{prompt.version}.jinja"
prompt_file.write_text(prompt.raw)

Two failure modes:

    Relative traversal. name="../victim/foo" resolves to <registry>/../victim/foo.0.jinja — outside the configured root.

    Absolute-path bypass. pathlib documents that Path("/a") / Path("/b") returns Path("/b"). So name="/abs/path" discards the registry root entirely; the registry is never consulted.

The poisoned name is then persisted to index.json, so the out-of-root path keeps reconstructing on later _load() calls (directory.py:135-141). With overwrite=True, existing files at the target path are replaced.

Proof of Concept

import tempfile
from pathlib import Path
from banks import Prompt
from banks.registries import DirectoryPromptRegistry

work = Path(tempfile.mkdtemp())
registry = work / "registry"; registry.mkdir()
victim   = work / "victim";   victim.mkdir()...

Output (verified on banks==2.4.2):

pwn
abs pwn
CLOBBERED

test_sandbox_baseline.py

Screenshot 2026-05-10 at 3 19 49 PM

registry_path_traversal.py

Screenshot 2026-05-10 at 3 20 02 PM

registry_path_traversal_v2.py

Screenshot 2026-05-10 at 3 20 18 PM

Negative control: with a benign name="okay-name", the file lands inside <registry>/ and the victim directory remains untouched.

Impact

Arbitrary file write at an attacker-chosen path with attacker-controlled bytes, scoped to whatever the application process can write to. The .0.jinja suffix limits some chains, but does not prevent overwriting templates consumed by the same or another application, planting files that other tooling ingests, or clobbering predictable-path config artifacts.

Realistic threat model: any "prompt management" service that exposes prompt creation through an authenticated API and forwards user-supplied name (and version) to Prompt(...) plus DirectoryPromptRegistry.set().

Suggested Fix

Reject obviously dangerous names early and verify the resulting path stays under the registry root after canonicalization:

# src/banks/registries/directory.py
import re

_NAME_RE = re.compile(r"[A-Za-z0-9._-]+")

@classmethod
def from_prompt_path(cls, prompt, path):
    if not _NAME_RE.fullmatch(prompt.name or ""):...

The same enforcement should run inside _load() and _get_prompt_file() so a poisoned index.json from a vulnerable run cannot keep escaping after upgrade.

Mitigation

Update Impact

Minimal update. May introduce new vulnerabilities or breaking changes.

Ecosystem
Component
Affected version
Patched versions