Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /TrueSelph_jvspatial /tests /storage /test_security.py
"""Comprehensive test suite for storage security components.
Tests PathSanitizer and FileValidator for security vulnerabilities,
edge cases, and proper validation behavior.
"""
import os
import tempfile
from pathlib import Path
import pytest
from jvspatial.storage.exceptions import (
FileSizeLimitError,
InvalidMimeTypeError,
InvalidPathError,
PathTraversalError,
ValidationError,
)
from jvspatial.storage.security import FileValidator, PathSanitizer
# ============================================================================
# PathSanitizer Tests
# ============================================================================
class TestPathTraversalPrevention:
"""Tests for path traversal attack prevention."""
async def test_parent_directory_single(self):
"""Test that single parent directory reference is blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../etc/passwd")
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_parent_directory_multiple(self):
"""Test that multiple parent directory references are blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../../etc/passwd")
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_parent_directory_deep(self):
"""Test that deep parent directory traversal is blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../../../../../../../../etc/passwd")
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_parent_directory_mixed(self):
"""Test parent directory mixed with valid path."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("uploads/../../../etc/passwd")
async def test_parent_directory_windows(self):
"""Test Windows-style parent directory traversal."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("..\\..\\windows\\system32")
async def test_absolute_path_unix(self):
"""Test that absolute Unix paths are blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("/etc/passwd")
assert "absolute" in str(exc_info.value).lower()
async def test_absolute_path_windows(self):
"""Test that absolute Windows paths are blocked."""
with pytest.raises((PathTraversalError, InvalidPathError)):
PathSanitizer.sanitize_path("C:\\Windows\\System32\\config")
async def test_absolute_path_with_drive_letter(self):
"""Test Windows drive letters are blocked."""
with pytest.raises((PathTraversalError, InvalidPathError)):
PathSanitizer.sanitize_path("D:/data/secret.txt")
class TestDangerousPatterns:
"""Tests for dangerous pattern blocking."""
async def test_home_directory_tilde(self):
"""Test that tilde (~) for home directory is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("~/documents/secret.txt")
async def test_shell_variable_dollar(self):
"""Test that shell variables ($) are blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("$HOME/documents")
async def test_command_substitution_backtick(self):
"""Test that command substitution (`) is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("uploads/`whoami`.txt")
async def test_pipe_operator(self):
"""Test that pipe operator (|) is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("file.txt|cat")
async def test_command_separator_semicolon(self):
"""Test that semicolon (;) command separator is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("file.txt;rm -rf /")
async def test_background_execution_ampersand(self):
"""Test that ampersand (&) is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("file.txt&")
async def test_redirect_greater_than(self):
"""Test that redirect (>) is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("output>file.txt")
async def test_redirect_less_than(self):
"""Test that redirect (<) is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("input<file.txt")
async def test_null_byte_injection(self):
"""Test that null byte (\0) is removed."""
# Null bytes should be stripped
result = PathSanitizer.sanitize_path("file\x00.txt")
assert "\x00" not in result
class TestPathLimits:
"""Tests for path depth and length limits."""
async def test_path_depth_at_limit(self):
"""Test path at maximum depth (10 levels)."""
# Create a path with exactly 10 components
path = "/".join([f"dir{i}" for i in range(10)])
result = PathSanitizer.sanitize_path(path)
assert result is not None
async def test_path_depth_exceeds_limit(self):
"""Test that paths exceeding depth limit are rejected."""
# Create a path with 11 components (exceeds limit of 10)
path = "/".join([f"dir{i}" for i in range(11)])
with pytest.raises(InvalidPathError) as exc_info:
PathSanitizer.sanitize_path(path)
assert "depth" in str(exc_info.value).lower()
async def test_filename_length_at_limit(self):
"""Test filename at maximum length (255 chars)."""
filename = "a" * 255 + ".txt" # Total 259, but component is 255
# This should fail because component exceeds 255
with pytest.raises(InvalidPathError):
PathSanitizer.sanitize_path(filename)
async def test_filename_length_within_limit(self):
"""Test filename within limit."""
filename = "a" * 250 + ".txt"
result = PathSanitizer.sanitize_path(filename)
assert result is not None
async def test_filename_length_exceeds_limit(self):
"""Test that long filenames are rejected."""
filename = "a" * 300
with pytest.raises(InvalidPathError) as exc_info:
PathSanitizer.sanitize_path(filename)
assert "too long" in str(exc_info.value).lower()
async def test_path_length_at_limit(self):
"""Test path at maximum total length (4096 chars)."""
# Create a long but valid path (stay under depth limit of 10)
path = "a/b/c/d/e/f/g/h/i/j.txt" # Exactly 10 components
result = PathSanitizer.sanitize_path(path)
assert result is not None
async def test_path_length_exceeds_limit(self):
"""Test that paths exceeding total length limit are rejected."""
# Create a path longer than 4096 chars
path = "a" * 5000
with pytest.raises(InvalidPathError) as exc_info:
PathSanitizer.sanitize_path(path)
assert "exceeds maximum" in str(exc_info.value).lower()
class TestValidPathSanitization:
"""Tests for valid path sanitization."""
async def test_simple_filename(self):
"""Test simple filename sanitization."""
result = PathSanitizer.sanitize_path("document.pdf")
assert result == "document.pdf"
async def test_relative_path(self):
"""Test relative path sanitization."""
result = PathSanitizer.sanitize_path("uploads/documents/file.pdf")
assert result == "uploads/documents/file.pdf"
async def test_path_normalization(self):
"""Test that redundant separators are normalized."""
result = PathSanitizer.sanitize_path("uploads//documents///file.pdf")
assert "//" not in result
async def test_windows_separators_converted(self):
"""Test Windows backslashes converted to forward slashes."""
result = PathSanitizer.sanitize_path("uploads\\documents\\file.pdf")
assert "\\" not in result
assert "/" in result
async def test_alphanumeric_filename(self):
"""Test alphanumeric filenames are allowed."""
result = PathSanitizer.sanitize_path("file123.txt")
assert result == "file123.txt"
async def test_dash_and_underscore(self):
"""Test that dashes and underscores are allowed."""
result = PathSanitizer.sanitize_path("my-file_name.txt")
assert result == "my-file_name.txt"
async def test_multiple_extensions(self):
"""Test files with multiple extensions."""
result = PathSanitizer.sanitize_path("archive.tar.gz")
assert result == "archive.tar.gz"
class TestHiddenFiles:
"""Tests for hidden file handling."""
async def test_hidden_file_blocked_by_default(self):
"""Test that hidden files are blocked by default."""
with pytest.raises(InvalidPathError) as exc_info:
PathSanitizer.sanitize_path(".hidden")
assert "hidden" in str(exc_info.value).lower()
async def test_hidden_file_in_path_blocked(self):
"""Test that hidden directories in path are blocked."""
with pytest.raises(InvalidPathError):
PathSanitizer.sanitize_path("uploads/.hidden/file.txt")
async def test_hidden_file_allowed_with_flag(self):
"""Test that hidden files are allowed when flag is set."""
result = PathSanitizer.sanitize_path(".gitignore", allow_hidden=True)
assert result == ".gitignore"
async def test_dotfile_extension_allowed(self):
"""Test that files with extensions starting with dot work."""
# file.txt has a dot but isn't a hidden file
result = PathSanitizer.sanitize_path("document.txt")
assert result == "document.txt"
class TestBaseDirectoryConfinement:
"""Tests for base directory confinement."""
async def test_confinement_within_base(self):
"""Test that valid paths within base directory are allowed."""
with tempfile.TemporaryDirectory() as tmpdir:
result = PathSanitizer.sanitize_path("uploads/file.txt", base_dir=tmpdir)
assert result == "uploads/file.txt"
async def test_confinement_escape_attempt(self):
"""Test that escape attempts are blocked."""
with tempfile.TemporaryDirectory() as tmpdir:
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../outside/file.txt", base_dir=tmpdir)
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_confinement_absolute_path_in_base(self):
"""Test absolute paths are rejected even with base_dir."""
with tempfile.TemporaryDirectory() as tmpdir:
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("/etc/passwd", base_dir=tmpdir)
class TestEdgeCases:
"""Tests for edge cases and special scenarios."""
async def test_empty_path(self):
"""Test that empty path raises error."""
with pytest.raises(InvalidPathError) as exc_info:
PathSanitizer.sanitize_path("")
assert "empty" in str(exc_info.value).lower()
async def test_whitespace_only_path(self):
"""Test that whitespace-only path is invalid."""
with pytest.raises(InvalidPathError):
PathSanitizer.sanitize_path(" ")
async def test_special_characters_in_filename(self):
"""Test that special characters are rejected."""
with pytest.raises((InvalidPathError, PathTraversalError)):
PathSanitizer.sanitize_path("file@#$.txt")
async def test_unicode_filename(self):
"""Test unicode characters are rejected."""
with pytest.raises(InvalidPathError):
PathSanitizer.sanitize_path("文件.txt")
async def test_spaces_in_filename(self):
"""Test that spaces are rejected."""
with pytest.raises(InvalidPathError):
PathSanitizer.sanitize_path("my file.txt")
async def test_control_characters_stripped(self):
"""Test that control characters are stripped."""
result = PathSanitizer.sanitize_path("file\x01\x02.txt")
assert "\x01" not in result
assert "\x02" not in result
class TestSanitizeFilename:
"""Tests for sanitize_filename method."""
async def test_simple_filename(self):
"""Test simple filename sanitization."""
result = PathSanitizer.sanitize_filename("document.pdf")
assert result == "document.pdf"
async def test_path_separators_removed(self):
"""Test that path separators are converted to underscores."""
result = PathSanitizer.sanitize_filename("path/to/file.txt")
assert "/" not in result
assert "_" in result
async def test_windows_separators_removed(self):
"""Test that Windows separators are converted."""
result = PathSanitizer.sanitize_filename("path\\to\\file.txt")
assert "\\" not in result
async def test_invalid_chars_sanitized(self):
"""Test that invalid characters are replaced."""
result = PathSanitizer.sanitize_filename("file@#$.txt")
assert "@" not in result
assert "#" not in result
assert "$" not in result
async def test_long_filename_truncated(self):
"""Test that long filenames are truncated."""
long_name = "a" * 300 + ".txt"
result = PathSanitizer.sanitize_filename(long_name)
assert len(result) <= 255
async def test_extension_preserved_when_truncated(self):
"""Test that extension is preserved when truncating."""
long_name = "a" * 300 + ".pdf"
result = PathSanitizer.sanitize_filename(long_name)
assert result.endswith(".pdf")
async def test_empty_filename_error(self):
"""Test that empty filename raises error."""
with pytest.raises(InvalidPathError):
PathSanitizer.sanitize_filename("")
class TestValidationMethods:
"""Tests for validation utility methods."""
async def test_validate_path_returns_true_for_valid(self):
"""Test that validate_path returns True for valid paths."""
assert PathSanitizer.validate_path("uploads/file.txt") is True
async def test_validate_path_returns_false_for_invalid(self):
"""Test that validate_path returns False for invalid paths."""
assert PathSanitizer.validate_path("../etc/passwd") is False
async def test_is_safe_path_alias(self):
"""Test that is_safe_path is an alias for validate_path."""
path = "uploads/file.txt"
assert PathSanitizer.is_safe_path(path) == PathSanitizer.validate_path(path)
async def test_is_safe_path_with_base_dir(self):
"""Test is_safe_path with base directory."""
with tempfile.TemporaryDirectory() as tmpdir:
assert PathSanitizer.is_safe_path("file.txt", base_dir=tmpdir) is True
assert PathSanitizer.is_safe_path("../file.txt", base_dir=tmpdir) is False
# ============================================================================
# FileValidator Tests
# ============================================================================
class TestMimeTypeDetection:
"""Tests for MIME type detection."""
async def test_pdf_detection(self):
"""Test PDF MIME type detection."""
# PDF header
pdf_content = b"%PDF-1.4\n%"
validator = FileValidator()
mime = validator.detect_mime_type(pdf_content, "document.pdf")
assert "pdf" in mime.lower()
async def test_jpeg_detection(self):
"""Test JPEG MIME type detection."""
# JPEG header (FF D8 FF)
jpeg_content = b"\xff\xd8\xff\xe0\x00\x10JFIF"
validator = FileValidator()
mime = validator.detect_mime_type(jpeg_content, "image.jpg")
assert "image" in mime.lower()
async def test_png_detection(self):
"""Test PNG MIME type detection."""
# PNG header
png_content = b"\x89PNG\r\n\x1a\n"
validator = FileValidator()
mime = validator.detect_mime_type(png_content, "image.png")
assert "image" in mime.lower()
async def test_fallback_to_extension(self):
"""Test fallback to extension-based detection."""
# Random content that won't match any signature
content = b"random content here"
validator = FileValidator()
mime = validator.detect_mime_type(content, "document.txt")
assert mime is not None
async def test_empty_content_with_filename(self):
"""Test detection with empty content but filename."""
validator = FileValidator()
mime = validator.detect_mime_type(b"", "document.pdf")
# Should fall back to extension
assert mime is not None
async def test_no_filename_no_content(self):
"""Test detection with no filename and no content."""
validator = FileValidator()
mime = validator.detect_mime_type(b"", None)
assert mime == "application/octet-stream"
class TestFileSizeValidation:
"""Tests for file size validation."""
async def test_file_under_limit(self):
"""Test that files under limit are accepted."""
content = b"x" * 1024 # 1 KB
validator = FileValidator(max_size_mb=1)
result = validator.validate_file(content, "small.txt")
assert result["valid"] is True
assert result["size_bytes"] == 1024
async def test_file_at_limit(self):
"""Test file at exactly the size limit."""
max_mb = 1
content = b"x" * (max_mb * 1024 * 1024)
validator = FileValidator(max_size_mb=max_mb)
result = validator.validate_file(content, "exact.txt")
assert result["valid"] is True
async def test_file_exceeds_limit(self):
"""Test that files exceeding limit are rejected."""
max_mb = 1
content = b"x" * (max_mb * 1024 * 1024 + 1) # 1 byte over
validator = FileValidator(max_size_mb=max_mb)
with pytest.raises(FileSizeLimitError) as exc_info:
validator.validate_file(content, "large.txt")
assert exc_info.value.details["file_size"] == len(content)
assert exc_info.value.details["max_size"] == max_mb * 1024 * 1024
async def test_empty_file(self):
"""Test that empty files are accepted."""
validator = FileValidator()
result = validator.validate_file(b"", "empty.txt")
assert result["valid"] is True
assert result["size_bytes"] == 0
async def test_huge_file_rejected(self):
"""Test that huge files are rejected."""
# Create reference to huge file without actually allocating
validator = FileValidator(max_size_mb=0.001) # 1 KB limit
content = b"x" * 2048 # 2 KB
with pytest.raises(FileSizeLimitError):
validator.validate_file(content, "huge.bin")
async def test_check_size_method(self):
"""Test the check_size utility method."""
validator = FileValidator(max_size_mb=1)
assert validator.check_size(b"x" * 1024) is True
assert validator.check_size(b"x" * (2 * 1024 * 1024)) is False
class TestDangerousFileBlocking:
"""Tests for dangerous file type blocking."""
async def test_exe_extension_blocked(self):
"""Test that .exe files are blocked."""
validator = FileValidator()
content = b"MZ\x90\x00" # DOS/Windows executable header
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, "virus.exe")
async def test_sh_extension_blocked(self):
"""Test that .sh shell scripts are blocked."""
validator = FileValidator()
content = b"#!/bin/bash\n"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, "script.sh")
async def test_py_extension_blocked(self):
"""Test that .py Python files are blocked."""
validator = FileValidator()
content = b"import os\n"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, "malicious.py")
async def test_bat_extension_blocked(self):
"""Test that .bat batch files are blocked."""
validator = FileValidator()
content = b"@echo off\n"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, "script.bat")
async def test_dll_extension_blocked(self):
"""Test that .dll files are blocked."""
validator = FileValidator()
content = b"MZ\x90\x00"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, "library.dll")
async def test_php_extension_blocked(self):
"""Test that .php files are blocked."""
validator = FileValidator()
content = b"<?php\n"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, "webshell.php")
class TestAllowedMimeTypes:
"""Tests for allowed MIME type validation."""
async def test_allowed_image_jpeg(self):
"""Test that JPEG images are allowed."""
validator = FileValidator()
content = b"\xff\xd8\xff\xe0\x00\x10JFIF"
result = validator.validate_file(content, "photo.jpg")
assert result["valid"] is True
async def test_allowed_image_png(self):
"""Test that PNG images are allowed."""
validator = FileValidator()
content = b"\x89PNG\r\n\x1a\n"
result = validator.validate_file(content, "graphic.png")
assert result["valid"] is True
async def test_allowed_pdf(self):
"""Test that PDF documents are allowed."""
validator = FileValidator()
content = b"%PDF-1.4\n"
result = validator.validate_file(content, "document.pdf")
assert result["valid"] is True
async def test_allowed_text_plain(self):
"""Test that plain text files are allowed."""
validator = FileValidator()
content = b"Hello, world!"
result = validator.validate_file(content, "note.txt")
assert result["valid"] is True
async def test_custom_allowed_types(self):
"""Test custom allowed MIME types."""
allowed = {"text/plain", "application/json"}
validator = FileValidator(allowed_mime_types=allowed)
# Text should work
result = validator.validate_file(b"text", "file.txt")
assert result["valid"] is True
# PDF should be rejected (not in custom allowed list)
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(b"%PDF-1.4", "doc.pdf")
class TestBlockedMimeTypes:
"""Tests for blocked MIME type validation."""
async def test_blocked_executable(self):
"""Test that executable MIME types are blocked."""
validator = FileValidator()
# Even with a safe extension, dangerous MIME should be blocked
content = b"MZ\x90\x00" # DOS executable
# Note: Depending on magic library availability, this may or may not detect executable
# Just verify the validation runs
try:
result = validator.validate_file(content, "notavirus.txt")
# If it passes, ensure it's because magic isn't detecting it
assert result["valid"] is True
except InvalidMimeTypeError:
# If it fails, that's the expected secure behavior
pass
async def test_custom_blocked_types(self):
"""Test custom blocked MIME types."""
blocked = {"text/plain"}
validator = FileValidator(blocked_mime_types=blocked)
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(b"text content", "file.txt")
async def test_check_mime_type_method(self):
"""Test the check_mime_type utility method."""
validator = FileValidator()
assert validator.check_mime_type("image/jpeg") is True
assert validator.check_mime_type("application/x-executable") is False
class TestFileContentValidation:
"""Tests for comprehensive file content validation."""
async def test_complete_validation_success(self):
"""Test complete validation with valid file."""
validator = FileValidator(max_size_mb=1)
content = b"%PDF-1.4\nvalid pdf content"
result = validator.validate_file(content, "report.pdf")
assert result["valid"] is True
assert "mime_type" in result
assert "size_bytes" in result
assert "extension" in result
assert "filename" in result
assert result["filename"] == "report.pdf"
async def test_strict_mime_check_enabled(self):
"""Test strict MIME type checking."""
validator = FileValidator(strict_mime_check=True)
# PDF content with wrong expected type
content = b"%PDF-1.4\n"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(
content, "document.pdf", expected_mime_type="text/plain"
)
async def test_strict_mime_check_disabled(self):
"""Test that strict checking can be disabled."""
validator = FileValidator(strict_mime_check=False)
content = b"text content"
result = validator.validate_file(content, "file.txt")
assert result["valid"] is True
async def test_get_file_info_method(self):
"""Test the get_file_info utility method."""
validator = FileValidator()
content = b"test content"
info = validator.get_file_info(content, "test.txt")
assert "filename" in info
assert "size_bytes" in info
assert "extension" in info
assert "mime_type" in info
assert "within_size_limit" in info
assert info["size_bytes"] == len(content)
class TestValidatorEdgeCases:
"""Tests for edge cases in file validation."""
async def test_none_content_raises_error(self):
"""Test that None content is handled."""
validator = FileValidator()
# None should fail size check or raise attribute error
with pytest.raises((AttributeError, TypeError)):
validator.validate_file(None, "file.txt")
async def test_binary_data_validated(self):
"""Test that binary data is properly validated."""
# Use a different extension since .bin is blocked
validator = FileValidator()
binary_data = bytes(range(256))
# Use .dat which isn't explicitly blocked
result = validator.validate_file(binary_data, "data.txt")
assert result["size_bytes"] == 256
async def test_validator_with_no_restrictions(self):
"""Test validator with minimal restrictions."""
# Allow everything by providing large set of allowed types
import mimetypes
all_types = set(mimetypes.types_map.values())
all_types.add("application/octet-stream")
all_types.add("chemical/x-xyz") # Add xyz type
validator = FileValidator(
max_size_mb=1000,
allowed_mime_types=all_types,
blocked_mime_types=set(),
blocked_extensions=set(),
)
content = b"anything goes"
result = validator.validate_file(content, "file.xyz")
assert result["valid"] is True
async def test_extension_extraction(self):
"""Test that file extensions are correctly extracted."""
validator = FileValidator()
content = b"test"
result = validator.validate_file(content, "file.TXT")
assert result["extension"] == ".txt" # Should be lowercase
result = validator.validate_file(content, "archive.tar.gz")
assert result["extension"] == ".gz"
async def test_no_extension_file(self):
"""Test files without extensions."""
# Add octet-stream to allowed types for files without extension
allowed = FileValidator.DEFAULT_ALLOWED_MIME_TYPES.copy()
allowed.add("application/octet-stream")
validator = FileValidator(allowed_mime_types=allowed)
content = b"text content"
result = validator.validate_file(content, "README")
assert result["extension"] == ""
class TestValidatorInitialization:
"""Tests for FileValidator initialization and configuration."""
async def test_default_initialization(self):
"""Test validator with default settings."""
validator = FileValidator()
assert validator.max_size_bytes == FileValidator.DEFAULT_MAX_SIZE_BYTES
assert validator.allowed_mime_types == FileValidator.DEFAULT_ALLOWED_MIME_TYPES
assert validator.blocked_mime_types == FileValidator.BLOCKED_MIME_TYPES
async def test_custom_size_limit(self):
"""Test custom size limit initialization."""
validator = FileValidator(max_size_mb=5)
assert validator.max_size_bytes == 5 * 1024 * 1024
async def test_custom_allowed_types(self):
"""Test custom allowed types initialization."""
custom_types = {"image/jpeg", "image/png"}
validator = FileValidator(allowed_mime_types=custom_types)
assert validator.allowed_mime_types == custom_types
async def test_custom_blocked_types(self):
"""Test custom blocked types initialization."""
custom_blocked = {"text/plain"}
validator = FileValidator(blocked_mime_types=custom_blocked)
assert validator.blocked_mime_types == custom_blocked
async def test_custom_blocked_extensions(self):
"""Test custom blocked extensions initialization."""
custom_ext = {".tmp", ".bak"}
validator = FileValidator(blocked_extensions=custom_ext)
assert validator.blocked_extensions == custom_ext
# ============================================================================
# Integration Tests
# ============================================================================
class TestIntegration:
"""Integration tests combining PathSanitizer and FileValidator."""
async def test_safe_file_upload_workflow(self):
"""Test complete safe file upload workflow."""
# Sanitize path
safe_path = PathSanitizer.sanitize_path("uploads/documents/report.pdf")
assert safe_path == "uploads/documents/report.pdf"
# Validate file content
validator = FileValidator(max_size_mb=10)
content = b"%PDF-1.4\nSample PDF content"
result = validator.validate_file(content, "report.pdf")
assert result["valid"] is True
assert safe_path is not None
async def test_malicious_file_rejected(self):
"""Test that malicious files are rejected at multiple stages."""
# Path traversal should be caught by sanitizer
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("../../../etc/passwd")
# Dangerous file type should be caught by validator
validator = FileValidator()
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(b"#!/bin/bash", "evil.sh")
async def test_combined_security_checks(self):
"""Test multiple security checks work together."""
with tempfile.TemporaryDirectory() as tmpdir:
# Valid path within base directory
safe_path = PathSanitizer.sanitize_path(
"uploads/image.jpg", base_dir=tmpdir
)
# Valid JPEG content
validator = FileValidator(max_size_mb=5)
jpeg_content = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01"
result = validator.validate_file(jpeg_content, "image.jpg")
assert safe_path == "uploads/image.jpg"
assert result["valid"] is True
async def test_realistic_attack_scenarios(self):
"""Test realistic attack scenarios are blocked."""
validator = FileValidator()
# Scenario 1: Shell script disguised as text file
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(b"#!/bin/sh\nrm -rf /", "innocent.sh")
# Scenario 2: Path traversal to system files
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("../../../../etc/shadow")
# Scenario 3: Executable with misleading extension
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(b"MZ\x90\x00", "virus.exe")
# Scenario 4: Oversized file upload
with pytest.raises(FileSizeLimitError):
validator.validate_file(b"x" * 200 * 1024 * 1024, "huge.bin")
# ============================================================================
# Cleanup and Fixtures
# ============================================================================
@pytest.fixture
def temp_directory():
"""Provide a temporary directory for tests."""
with tempfile.TemporaryDirectory() as tmpdir:
yield tmpdir
@pytest.fixture
def sample_validator():
"""Provide a configured FileValidator instance."""
return FileValidator(max_size_mb=10)
@pytest.fixture
def sample_files():
"""Provide sample file contents for testing."""
return {
"pdf": b"%PDF-1.4\nTest PDF content",
"jpeg": b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01",
"png": b"\x89PNG\r\n\x1a\n",
"text": b"Plain text content",
"executable": b"MZ\x90\x00", # DOS executable header
"shell_script": b'#!/bin/bash\necho "test"',
}
# ============================================================================
# Performance and Stress Tests
# ============================================================================
class TestPerformance:
"""Performance and stress tests."""
async def test_large_path_validation_performance(self):
"""Test that path validation performs well with deep paths."""
# Create a path near the depth limit
path = "/".join([f"dir{i}" for i in range(9)])
# Should complete quickly
import time
start = time.time()
result = PathSanitizer.sanitize_path(path)
duration = time.time() - start
assert result is not None
assert duration < 0.1 # Should be very fast
async def test_many_validations(self):
"""Test validator can handle many validations."""
validator = FileValidator()
content = b"test content"
# Validate many times
for i in range(100):
result = validator.validate_file(content, f"file{i}.txt")
assert result["valid"] is True
async def test_large_file_size_check(self):
"""Test that size checking is efficient."""
validator = FileValidator(max_size_mb=1)
# Create increasingly large content references (use .txt instead of .bin)
for size_kb in [1, 10, 100, 500]:
content = b"x" * (size_kb * 1024)
if size_kb * 1024 <= validator.max_size_bytes:
result = validator.validate_file(content, "file.txt")
assert result["valid"] is True
else:
with pytest.raises(FileSizeLimitError):
validator.validate_file(content, "file.txt")
# ============================================================================
# Documentation Examples
# ============================================================================
class TestDocumentationExamples:
"""Tests that verify documentation examples work correctly."""
async def test_path_sanitizer_basic_usage(self):
"""Test basic PathSanitizer usage from docs."""
# Example from docstring
safe_path = PathSanitizer.sanitize_path("docs/report.pdf")
assert safe_path == "docs/report.pdf"
# Example of rejection
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("../../etc/passwd")
async def test_file_validator_basic_usage(self):
"""Test basic FileValidator usage from docs."""
validator = FileValidator(max_size_mb=10)
# Valid file
content = b"%PDF-1.4\ntest"
result = validator.validate_file(content, "document.pdf")
assert result["valid"] is True
assert "mime_type" in result
assert "size_bytes" in result
async def test_combined_usage_example(self):
"""Test combined usage example."""
# Sanitize path first
filename = "report.pdf"
upload_path = f"uploads/documents/{filename}"
safe_path = PathSanitizer.sanitize_path(upload_path)
# Then validate content
validator = FileValidator(max_size_mb=10)
content = b"%PDF-1.4\nReport content"
result = validator.validate_file(content, filename)
assert safe_path == upload_path
assert result["valid"] is True
class TestHintMime:
"""Tests for the hint_mime parameter in detect_mime_type and validate_file."""
def test_detect_mime_type_hint_used_when_extension_missing(self):
"""hint_mime is used when neither magic nor extension can detect the type."""
validator = FileValidator()
content = b"\x00\x01\x02\x03unknown_binary"
mime = validator.detect_mime_type(
content, filename="0_upload", hint_mime="image/jpeg"
)
assert mime == "image/jpeg"
def test_detect_mime_type_hint_ignored_when_extension_works(self):
"""hint_mime is not used when extension-based detection succeeds."""
validator = FileValidator()
content = b"plain text"
mime = validator.detect_mime_type(
content, filename="photo.jpg", hint_mime="image/png"
)
assert mime == "image/jpeg"
def test_detect_mime_type_octet_stream_hint_not_used(self):
"""hint_mime of application/octet-stream is ignored (not useful)."""
validator = FileValidator()
content = b"\x00\x01binary"
mime = validator.detect_mime_type(
content, filename="upload", hint_mime="application/octet-stream"
)
assert mime == "application/octet-stream"
def test_detect_mime_type_none_hint(self):
"""hint_mime=None is same as not providing it."""
validator = FileValidator()
content = b"\x00\x01binary"
mime = validator.detect_mime_type(content, filename="upload", hint_mime=None)
assert mime == "application/octet-stream"
def test_validate_file_with_hint_mime(self):
"""validate_file accepts hint_mime and uses it when detection fails."""
validator = FileValidator()
content = b"\x00\x01\x02\x03jpeg_binary"
result = validator.validate_file(
content, filename="0_upload", hint_mime="image/jpeg"
)
assert result["valid"] is True
assert result["mime_type"] == "image/jpeg"
def test_validate_file_without_hint_mime_extensionless_fails(self):
"""validate_file rejects extensionless files when no hint is provided."""
validator = FileValidator()
content = b"\x00\x01\x02\x03unknown"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(content, filename="0_upload")
def test_validate_file_hint_mime_blocked_type_still_rejected(self):
"""hint_mime doesn't bypass blocked MIME types."""
validator = FileValidator()
content = b"#!/bin/bash\necho test"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(
content, filename="upload", hint_mime="application/x-executable"
)
def test_validate_file_hint_mime_not_in_allowlist_rejected(self):
"""hint_mime pointing to a type not in the allowlist is still rejected."""
validator = FileValidator()
content = b"\x00\x01binary"
with pytest.raises(InvalidMimeTypeError):
validator.validate_file(
content, filename="upload", hint_mime="application/x-custom-unknown"
)
"""Comprehensive test suite for storage security components.
Tests PathSanitizer and FileValidator for security vulnerabilities,
edge cases, and proper validation behavior.
"""
import os
import tempfile
from pathlib import Path
import pytest
from jvspatial.storage.exceptions import (
FileSizeLimitError,
InvalidMimeTypeError,
InvalidPathError,
PathTraversalError,
ValidationError,
)
from jvspatial.storage.security import FileValidator, PathSanitizer
# ============================================================================
# PathSanitizer Tests
# ============================================================================
class TestPathTraversalPrevention:
"""Tests for path traversal attack prevention."""
async def test_parent_directory_single(self):
"""Test that single parent directory reference is blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../etc/passwd")
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_parent_directory_multiple(self):
"""Test that multiple parent directory references are blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../../etc/passwd")
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_parent_directory_deep(self):
"""Test that deep parent directory traversal is blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("../../../../../../../../etc/passwd")
assert (
"dangerous pattern" in str(exc_info.value).lower()
or "traversal" in str(exc_info.value).lower()
)
async def test_parent_directory_mixed(self):
"""Test parent directory mixed with valid path."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("uploads/../../../etc/passwd")
async def test_parent_directory_windows(self):
"""Test Windows-style parent directory traversal."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("..\\..\\windows\\system32")
async def test_absolute_path_unix(self):
"""Test that absolute Unix paths are blocked."""
with pytest.raises(PathTraversalError) as exc_info:
PathSanitizer.sanitize_path("/etc/passwd")
assert "absolute" in str(exc_info.value).lower()
async def test_absolute_path_windows(self):
"""Test that absolute Windows paths are blocked."""
with pytest.raises((PathTraversalError, InvalidPathError)):
PathSanitizer.sanitize_path("C:\\Windows\\System32\\config")
async def test_absolute_path_with_drive_letter(self):
"""Test Windows drive letters are blocked."""
with pytest.raises((PathTraversalError, InvalidPathError)):
PathSanitizer.sanitize_path("D:/data/secret.txt")
class TestDangerousPatterns:
"""Tests for dangerous pattern blocking."""
async def test_home_directory_tilde(self):
"""Test that tilde (~) for home directory is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("~/documents/secret.txt")
async def test_shell_variable_dollar(self):
"""Test that shell variables ($) are blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("$HOME/documents")
async def test_command_substitution_backtick(self):
"""Test that command substitution (`) is blocked."""
with pytest.raises(PathTraversalError):
PathSanitizer.sanitize_path("uploads/`whoami`.txt")
Xet Storage Details
- Size:
- 40.5 kB
- Xet hash:
- 408b29dcb2cdbc1418e6507175d71c7e7a4b991a3b73e83d1f9430726dd10678
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.