Skip to main content

Module validation

Module validation 

Source
Expand description

Input validation and path safety utilities for Pattern Analysis commands.

Provides security-focused validation functions to mitigate:

  • T01 - Path Traversal: BLOCKED_PREFIXES for system directories
  • T02 - Project Root Enforcement: validate_file_path_in_project()
  • T03 - Integer Overflow: Checked arithmetic for depth calculations
  • T08 - Memory Exhaustion: Resource limit constants

All file paths are canonicalized and checked against project boundaries. Resource limits are enforced to prevent denial-of-service conditions.

Constants§

MAX_ANALYSIS_DEPTH
Maximum recursion depth for analysis algorithms. Used for CFG path enumeration, temporal mining, etc.
MAX_AST_DEPTH
Maximum AST traversal depth. Prevents stack overflow from deeply nested source code.
MAX_CLASSES_PER_FILE
Maximum classes per file.
MAX_CLASS_COMPLEXITY
Maximum class complexity (methods * fields) for analysis.
MAX_CONSTRAINTS_PER_FILE
Maximum constraints to report per file.
MAX_DIRECTORY_FILES
Maximum files to scan in directory analysis.
MAX_FIELDS_PER_CLASS
Maximum fields per class for cohesion analysis.
MAX_FILE_SIZE
Maximum file size for analysis (10 MB). Files larger than this will be rejected.
MAX_FUNCTION_NAME_LEN
Maximum function name length.
MAX_METHODS_PER_CLASS
Maximum methods per class for cohesion analysis.
MAX_PATHS
Maximum CFG paths to enumerate (TIGER-04). Prevents unbounded path enumeration in resources command.
MAX_TRIGRAMS
Maximum trigrams to collect (TIGER-05). Prevents memory exhaustion in temporal mining.
WARN_FILE_SIZE
Warning threshold for file size (1 MB). Files larger than this emit a warning but are still processed.

Functions§

approaching_limit
Check if a count is approaching a limit (>80%).
check_analysis_depth
Check if analysis depth limit has been exceeded.
check_ast_depth
Check if AST depth limit has been exceeded.
check_directory_file_count
Check if directory file count limit has been exceeded.
format_large_file_warning
Format a warning message for a large file.
is_path_traversal_attempt
Check if a path contains path traversal patterns.
read_file_safe
Safely read a file with size limits and UTF-8 validation.
saturating_count_add
Safely add to a counter with overflow protection.
saturating_depth_increment
Safely increment a depth counter with overflow protection.
should_warn_file_size
Check if a file size is large enough to warrant a warning.
validate_directory_path
Validate and canonicalize a directory path.
validate_file_path
Validate and canonicalize a file path.
validate_file_path_in_project
Validate a file path ensuring it stays within a project root.
validate_file_size
Validate file size against limits.
validate_function_name
Validate a function name for safety.
warn_if_approaching_limit
Log a warning if approaching a limit.
within_limit
Check if a value is within a limit using checked arithmetic.