---
name: "path-traversal-canonical-sanitizer"
description: "Prevent unauthorized file system access by strictly validating and canonicalizing paths."
metadata:
  author: "OWASP"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "prevent path traversal"
  - "sanitize file download path"
  - "validate path stays in base directory"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# Path Traversal Defense & Canonical Path Sanitizer

Defends file serving and download endpoints against directory traversal attacks (../): resolving canonical realpaths, validating paths stay within root base directories, and stripping null bytes.

## When to Invoke
Invoke this skill whenever you need to:
- prevent path traversal
- sanitize file download path
- validate path stays in base directory

## Execution SOP & Rules
1. Resolve target path to absolute canonical path using path.resolve() or os.path.realpath().
2. Verify canonical path starts with base directory: resolvedPath.startsWith(baseDirectory).
3. Reject or strip directory separator characters (/ and \) from filenames intended for storage.
4. Use random UUIDs or database IDs on the filesystem rather than user-provided filenames.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
