python-filename-sanitization

Sanitize user-provided filenames with whitelist regex and pathlib.

5|Updated Feb 7, 2021
One-click install
npx skills add https://github.com/ViktorBarzin/infra --skill python-filename-sanitization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-filename-sanitization
Source: https://github.com/ViktorBarzin/infra/tree/main/.claude/skills/python-filename-sanitization
Command: npx skills add https://github.com/ViktorBarzin/infra --skill python-filename-sanitization

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

User-provided filenames can contain malicious characters that enable path traversal, shell injection, or filesystem corruption. Directly using user input in file operations is a security risk.

Core Features & Use Cases

  • Whitelist-based sanitization: Allows only safe characters and preserves file extensions.
  • Safe integration with Python file operations: Uses pathlib to avoid shell commands and prevent directory traversal.
  • Real-world use case: When handling file uploads or renaming files in a web app, sanitize the input before saving to disk.

Quick Start

Use sanitize_filename to clean a user-provided filename before saving it.

Frequently Asked Questions about python-filename-sanitization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I sanitize user filenames in Python to prevent path traversal?

Sanitizing user filenames in Python involves applying a whitelist-based regex to allow only safe characters while using pathlib to prevent directory traversal. This approach preserves file extensions and limits filename length for secure filesystem operations.

Why does directly using user input in Python file operations cause security issues?

Directly using user input in Python file operations causes security issues because user filenames can contain malicious characters enabling path traversal, shell injection, or filesystem corruption during file uploads, renames, and downloads.

What is the best way to handle file uploads safely in Python web applications?

The best way to handle file uploads safely in Python web applications is to sanitize user-provided filenames before saving to disk. This uses a whitelist-based regex and pathlib to prevent unsafe filesystem operations and directory traversal.

Does Python pathlib prevent path traversal attacks from user-provided filenames?

Python pathlib prevents path traversal by avoiding shell commands during filesystem operations, but it must be paired with whitelist-based filename sanitization to filter malicious characters and safely handle user input across platforms and frameworks.

Can I preserve file extensions when sanitizing filenames in Python?

Yes, you can preserve file extensions when sanitizing filenames in Python. The whitelist-based sanitization process cleans the user-provided filename while explicitly retaining the original file extension for safe integration.