Audit and improve security related tests: eliminate code duplication and implement missing tests#12
Conversation
…rovements Co-authored-by: GrantBirki <23362539+GrantBirki@users.noreply.github.com>
…ent auth TODOs Co-authored-by: GrantBirki <23362539+GrantBirki@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR centralizes the list of dangerous class names into a shared constant, refactors validation logic to use that constant, and fills in previously missing security tests for the auth module.
- Introduced
Hooks::Security::DANGEROUS_CLASSESand removed duplicated arrays - Refactored
config_validatorandhelpersto reference the shared constant - Completed all TODOs in auth security specs and added logging stub
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| spec/unit/lib/hooks/core/config_validator_security_spec.rb | Replaced hardcoded dangerous_configs with iteration over the shared constant |
| spec/unit/lib/hooks/app/helpers_security_spec.rb | Updated tests to reference DANGEROUS_CLASSES and added inclusion checks |
| spec/unit/lib/hooks/app/auth/auth_security_spec.rb | Implemented 8 previously TODO tests for validate_auth! and stubbed logging |
| lib/hooks/security.rb | Added DANGEROUS_CLASSES constant with comprehensive class list |
| lib/hooks/core/config_validator.rb | Refactored to use Hooks::Security::DANGEROUS_CLASSES |
| lib/hooks/app/helpers.rb | Refactored to use Hooks::Security::DANGEROUS_CLASSES |
| lib/hooks/app/auth/auth.rb | Enhanced auth type validation to reject empty or whitespace-only strings |
Comments suppressed due to low confidence (2)
spec/unit/lib/hooks/app/helpers_security_spec.rb:39
- The test iterates over all dangerous classes, not just system-related ones. Consider renaming the description to "rejects dangerous class names" for accuracy.
it "rejects system class names" do
spec/unit/lib/hooks/app/auth/auth_security_spec.rb:70
- Add a test case for a missing or nil auth type (e.g.,
{ auth: {} }or{ auth: { type: nil } }) to ensure the validation path for non-string or absent types is covered.
it "rejects request with empty string type" do
| end | ||
|
|
||
| auth_plugin_type = auth_config[:type].downcase | ||
| auth_plugin_type = auth_type.downcase |
There was a problem hiding this comment.
To prevent leading or trailing whitespace from breaking plugin lookup, use auth_type.strip.downcase when deriving auth_plugin_type.
| auth_plugin_type = auth_type.downcase | |
| auth_plugin_type = auth_type.strip.downcase |
| File Dir Kernel Object Class Module Proc Method | ||
| IO Socket TCPSocket UDPSocket BasicSocket | ||
| Process Thread Fiber Mutex ConditionVariable | ||
| Marshal YAML JSON Pathname |
There was a problem hiding this comment.
[nitpick] Consider sorting the entries in DANGEROUS_CLASSES alphabetically to improve readability and make future additions easier to manage.
| File Dir Kernel Object Class Module Proc Method | |
| IO Socket TCPSocket UDPSocket BasicSocket | |
| Process Thread Fiber Mutex ConditionVariable | |
| Marshal YAML JSON Pathname | |
| BasicSocket Class ConditionVariable Dir Fiber File IO JSON Kernel | |
| Marshal Method Module Mutex Object Pathname Proc Process Socket | |
| TCPSocket Thread UDPSocket YAML |
This PR addresses the audit and improvement of security-related tests as requested in the issue. The main focus was on cleaning up TODO comments, eliminating code duplication, and ensuring tests are maintainable while remaining robust.
Changes Made
🔒 Eliminated Code Duplication
Hooks::Security::DANGEROUS_CLASSESinlib/hooks/security.rbto centralize the list of dangerous class nameslib/hooks/app/helpers.rbandlib/hooks/core/config_validator.rbto use the shared constant instead of duplicated arrays✅ Implemented Missing Security Tests
Completed all 8 TODO test cases in
spec/unit/lib/hooks/app/auth/auth_security_spec.rb:secret_env_keyvalues🛡️ Enhanced Security Validation
lib/hooks/app/auth/auth.rbto properly validate empty string auth types🧪 Test Quality Improvements
Example of Eliminated Duplication
Before:
After:
Security Considerations
Fixes #11.