Python Safety Toolkit
Purpose
Comprehensive framework for building secure and safety-critical Python applications. Covers vulnerability detection, secure coding patterns, input sanitization, and defense-in-depth strategies for production systems.
When to use
This skill activates when:
- Analyzing security vulnerabilities in Python code
- Implementing input validation and sanitization
- Building safety-critical or security-sensitive systems
- Conducting security code reviews
- Designing secure APIs and data handling
- Evaluating third-party dependency risks
Core principles
Defense-in-depth architecture
CRITICAL: Multiple layers of security controls
- ALWAYS implement validation at every trust boundary
- Apply principle of least privilege throughout
- Fail securely with safe defaults
- Defense through depth, not obscurity
Secure-by-default design
MANDATORY: Security must be the default state
from typing import Protocol, TypeGuard
from dataclasses import dataclass
import re
@dataclass(frozen=True)
class SanitizedInput:
"""Validated and sanitized user input."""
value: str
@classmethod
def from_untrusted(cls, raw: str) -> "SanitizedInput":
"""Validate and sanitize untrusted input."""
if not raw or len(raw) > 1000:
raise ValueError("Input length invalid")
# Remove dangerous patterns
safe = re.sub(r'[^\w\s\-_.]', '', raw)
return cls(value=safe)
Security practices
Input validation framework
from pathlib import Path
import shlex
from typing import Final
class InputValidator:
"""Centralized input validation with security guarantees."""
DANGEROUS_CHARS: Final = frozenset([';', '|', '&', '`', '$', '\n', '\r'])
MAX_INPUT_LENGTH: Final = 2048
@staticmethod
def validate_command_arg(user_input: str) -> str:
"""Validate command-line argument for safety."""
if any(char in user_input for char in InputValidator.DANGEROUS_CHARS):
raise ValueError("Potentially dangerous characters detected")
if len(user_input) > InputValidator.MAX_INPUT_LENGTH:
raise ValueError("Input exceeds maximum length")
return shlex.quote(user_input)
@staticmethod
def validate_file_path(user_path: str, allowed_base: Path) -> Path:
"""Prevent path traversal attacks."""
resolved = (allowed_base / user_path).resolve()
if not resolved.is_relative_to(allowed_base):
raise ValueError("Path traversal attempt detected")
return resolved
Cryptographic operations
import secrets
from hashlib import sha256, pbkdf2_hmac
from typing import Final
class SecureTokenGenerator:
"""Cryptographically secure token generation."""
TOKEN_BYTES: Final = 32
PBKDF2_ITERATIONS: Final = 600_000
@staticmethod
def generate_token() -> str:
"""Generate cryptographically secure random token."""
return secrets.token_urlsafe(SecureTokenGenerator.TOKEN_BYTES)
@staticmethod
def hash_password(password: str, salt: bytes | None = None) -> tuple[bytes, bytes]:
"""Hash password with PBKDF2-HMAC-SHA256."""
if salt is None:
salt = secrets.token_bytes(32)
hashed = pbkdf2_hmac(
'sha256',
password.encode('utf-8'),
salt,
SecureTokenGenerator.PBKDF2_ITERATIONS
)
return hashed, salt
SQL injection prevention
from typing import Any
import sqlite3
class SafeDatabaseQuery:
"""Parameterized queries to prevent SQL injection."""
def __init__(self, db_path: str) -> None:
self.conn = sqlite3.connect(db_path)
self.conn.row_factory = sqlite3.Row
def fetch_user(self, user_id: int) -> dict[str, Any] | None:
"""Fetch user with parameterized query."""
cursor = self.conn.cursor()
# ALWAYS use parameterized queries
cursor.execute(
"SELECT * FROM users WHERE id = ?",
(user_id,)
)
row = cursor.fetchone()
return dict(row) if row else None
def search_records(self, term: str) -> list[dict[str, Any]]:
"""Search with safe parameter binding."""
cursor = self.conn.cursor()
# Never concatenate user input into SQL
cursor.execute(
"SELECT * FROM records WHERE name LIKE ? LIMIT 100",
(f"%{term}%",)
)
return [dict(row) for row in cursor.fetchall()]
Dependency security
Vulnerability scanning
# Scan dependencies for known vulnerabilities
uv run pip-audit
# Check for outdated packages with security issues
uv run safety check --json
# Generate SBOM for supply chain analysis
uv export --format requirements > requirements.txt
Minimal attack surface
# pyproject.toml - Minimize dependencies
[project]
requires-python = ">=3.14"
# Only essential, well-audited dependencies
dependencies = [
"cryptography>=42.0.0", # Pin minimum secure versions
]
[tool.pip-audit]
vulnerability-service = "osv"
ignore-vulns = [] # Never ignore vulnerabilities
Safe deserialization
import json
from typing import Any, TypeGuard
def safe_json_load(data: str) -> dict[str, Any]:
"""Safely deserialize JSON with validation."""
try:
obj = json.loads(data)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON: {e}") from e
if not isinstance(obj, dict):
raise ValueError("Expected JSON object, got array or primitive")
return obj
# NEVER use pickle on untrusted data
# NEVER use eval() or exec() on user input
# NEVER deserialize YAML without SafeLoader
Error handling without information leakage
import logging
from typing import NoReturn
class SecurityError(Exception):
"""Base exception for security violations."""
pass
class AuthenticationError(SecurityError):
"""Authentication failure - generic message."""
pass
def authenticate_user(username: str, password: str) -> bool:
"""Authenticate without leaking user existence."""
try:
# Perform constant-time comparison
# Always hash even if user doesn't exist (timing attack prevention)
stored_hash = fetch_user_hash(username) # Returns dummy if not exists
computed_hash, salt = hash_password(password, stored_hash.salt)
if not secrets.compare_digest(computed_hash, stored_hash.value):
# Generic error - don't reveal if user exists
raise AuthenticationError("Invalid credentials")
return True
except Exception:
# Don't leak stack traces to users
logging.exception("Authentication error")
raise AuthenticationError("Invalid credentials") from None
Testing security properties
import pytest
from hypothesis import given, strategies as st
class TestInputValidation:
"""Property-based security tests."""
@given(st.text())
def test_validator_never_allows_shell_injection(self, user_input: str):
"""Validator must reject all shell metacharacters."""
if any(char in user_input for char in InputValidator.DANGEROUS_CHARS):
with pytest.raises(ValueError):
InputValidator.validate_command_arg(user_input)
@given(st.text(min_size=2049))
def test_validator_rejects_oversized_input(self, long_input: str):
"""Validator must reject inputs exceeding max length."""
with pytest.raises(ValueError):
InputValidator.validate_command_arg(long_input)
def test_path_traversal_blocked(self):
"""Validator must prevent directory traversal."""
base = Path("/safe/directory")
with pytest.raises(ValueError):
InputValidator.validate_file_path("../../etc/passwd", base)
Quick reference
Security checklist
- All user inputs validated and sanitized
- Parameterized queries for all database operations
- Cryptographically secure random number generation
- No sensitive data in logs or error messages
- Constant-time comparison for secrets
- Path traversal protection on file operations
- Dependencies scanned for vulnerabilities
- No use of pickle, eval, or exec on untrusted data
- Proper exception handling without info leakage
- Property-based tests for security invariants
Remember: Security is not a feature, it's a fundamental requirement. Every line of code is a potential attack vector.