feat: Trivy Pre-Commit Hook with Security Scanning #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a comprehensive pre-commit hook that integrates Trivy security scanning to automatically detect vulnerabilities before commits are finalized. The implementation includes a Python-based hook with extensive configuration options, comprehensive test coverage, and CI/CD automation.
Key Features
🔒 Core Functionality
--dependency-treeflag to show vulnerability chains🛠️ Configuration Options
All options are customizable via command-line arguments:
--severity: Severity filtering (default: HIGH,CRITICAL)--format: Output format (default: table)--scanners: Scanner types (default: vuln)--dependency-tree: Show dependency tree with vulnerabilities--skip-db-update: Skip database updates for faster scans--timeout: Scan timeout configuration--ignore-unfixed: Ignore unfixed vulnerabilities--trivyignore: Custom ignore file support--config: Custom Trivy configuration file✅ Quality Assurance
📦 Implementation Details
Files Added:
pre_commit_hooks/trivy_scan.py- Main hook implementation (212 lines)tests/test_trivy_scan.py- Comprehensive test suite (283 lines)tests/conftest.py- Pytest fixtures and configuration.pre-commit-hooks.yaml- Pre-commit framework integration.github/workflows/ci.yml- CI/CD automationpyproject.toml- Project metadata and tool configurationuv.lock- Locked dependencies for reproducibility.pylintrc- Linting configuration.trivyignore.example- Example vulnerability ignore filetrivy.yaml.example- Example Trivy configurationREADME.md- Comprehensive documentationRecent Fixes:
committopre-commit(fixes compatibility warning).gitignoreentries for cache and IDE filesUsage
As a Pre-commit Hook
Add to
.pre-commit-config.yaml:Standalone
Testing
All validation commands pass successfully:
CI/CD
GitHub Actions workflow runs on every push and PR:
Breaking Changes
None - this is the initial release.
Dependencies
Documentation
Complete documentation includes:
Commits Included
Related Issues
Closes #1 - Implement Trivy pre-commit hook
Closes #3 - Fix deprecated pre-commit stage name
Ready for Review: All tests passing, documentation complete, CI/CD configured.