Sanitization Service¶
sanitization_service ¶
Sanitization Service.
Domain service for input sanitization and security.
This module provides comprehensive input sanitization to prevent common security vulnerabilities such as XSS, path traversal, and injection attacks.
Classes:
| Name | Description |
|---|---|
SanitizationService | Static methods for sanitizing various types of user input |
Notes
All methods are static and can be called directly from the class. Methods use logging for security event tracking.
Classes¶
SanitizationService ¶
Domain service for input sanitization.
Provides methods to sanitize user inputs and prevent common security vulnerabilities (XSS, path traversal, injection).
Notes
This is a Domain Service following Clean Architecture principles. All methods are static and stateless.
Functions¶
sanitize_filename staticmethod ¶
Sanitize filename to prevent path traversal attacks.
Removes path components and restricts characters to alphanumeric, underscore, dash, and dot only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | str | Original filename from upload | required |
Returns:
| Type | Description |
|---|---|
str | Sanitized filename (basename only, safe characters) |
Notes
- Removes path components (../, /)
- Allows only alphanumeric, underscore, dash, dot
- Falls back to 'upload.txt' if empty after sanitization
Source code in src/domain/services/sanitization_service.py
sanitize_sample_name staticmethod ¶
Sanitize and validate sample name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Original sample name from input | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str, str] | Tuple (is_valid, sanitized_name, error_message) |
Notes
- HTML-escapes the name for safe display (prevents XSS)
- Validates against allowed pattern
Source code in src/domain/services/sanitization_service.py
escape_html staticmethod ¶
Escape HTML special characters to prevent XSS attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Text to escape | required |
Returns:
| Type | Description |
|---|---|
str | HTML-escaped text |
Notes
Uses Python's built-in html.escape() function. Escapes: <, >, &, ", '
Source code in src/domain/services/sanitization_service.py
validate_path_safety staticmethod ¶
Check if path is safe (no traversal attempts).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | Path to validate | required |
Returns:
| Type | Description |
|---|---|
bool | True if path is safe, False if dangerous patterns detected |
Notes
Dangerous patterns detected: - Path traversal: .., /../, ..\ - Absolute Unix/Windows paths - Logs security events for detected attacks