-
Notifications
You must be signed in to change notification settings - Fork 27
Path Validation Security Hardening #1500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Extract helper methods to reduce cyclomatic complexity from 14 to under 10: - _is_macos_temp_path: Check if path is a macOS temp directory - _is_windows_system_directory: Handle Windows path detection logic - _normalized_path_matches_system_dir: Normalize and match Windows paths - _is_unix_system_directory: Handle Unix/Linux path detection This addresses flake8 C901 complexity warning.
- Move ctypes import to top of file with try/except (fixes E402) - Reorder imports: logging, os, platform (alphabetical) - Reorder typing imports: List, Optional (alphabetical) - Reorder exception imports alphabetically - Update check_user_permissions to use module-level ctypes - Remove unused imports from test files (F401)
- Apply black code formatter to all modified files - Remove trailing whitespace (W293) - Fix end-of-file newlines (W391) - Format list items and long lines per black style guide
- Add .editorconfig to enforce consistent code formatting - Trim trailing whitespace - Insert final newline - Set 120 character line length for Python - Update .pre-commit-config.yaml with additional hooks - Add trailing-whitespace hook - Add end-of-file-fixer hook - Add check-yaml and check-added-large-files hooks - Enhance flake8 args to catch W293 and W391 errors
a6efe57 to
017ef73
Compare
|
hi @raamana -- it looks like a few of our unit tests are failing with the new logic from your PR. |
This PR adds comprehensive path validation to prevent path traversal attacks and unauthorized access to system directories. All user-provided file paths in CLI commands are now validated before use.
Security Improvements
Path Traversal Prevention
..(parent directory traversal)System Directory Protection
/etc,/System,C:\Windows, etc.)/tmp,/private/var/folderson macOS)User Permission Warnings
Changes
New Files
cdisc_rules_engine/utilities/path_validator.py- Core path validation logiccdisc_rules_engine/exceptions/path_validation_exceptions.py- Custom exceptions for path validation errorstests/unit/test_utilities/test_path_validator.py- Comprehensive unit tests (18 tests)tests/unit/test_core_path_validation.py- CLI integration tests (8 tests)Modified Files
core.py- Integrated path validation for all CLI commands:validatecommand: validates output, cache, data, dataset, define-xml, and dictionary pathsupdate_cachecommand: validates cache, custom rules, and custom standard pathscdisc_rules_engine/exceptions/__init__.py- Exported new path validation exceptionsPath Validation Coverage
The following user-provided paths are now validated:
validatecommand:--output/-o- Output file path (write operation)--cache/-c- Cache directory path--data/-d- Data directory path (read)--dataset-path/-dp- Dataset file paths (read)--define-xml-path/-dxp- Define-XML file path (read)--report-template/-rt- Report template path (read)--whodrug,--meddra,--loinc,--medrt,--unii- Dictionary paths (read)update_cachecommand:--cache-path/-c- Cache directory path--custom-rules-directory/-crd- Custom rules directory (read)--custom-rule/-cr- Custom rule files (read)--update-custom-rule/-ucr- Custom rule update file (read)--custom-standard/-cs- Custom standard JSON file (read)Testing
Code Quality Improvements
core.pyto eliminate repetitive if/elif chainNOTE: AI was used in helping expedite the process of improving code and docs