test: add security tests for worker authentication edge cases#426
Conversation
|
All CI checks are now passing! Happy to address any feedback or requested changes. |
There was a problem hiding this comment.
Hey @iamkrishpathak! Thanks for working on security test coverage - this is a valuable area to strengthen! 🔐
I've done a thorough review and have some feedback to help get this ready for merge.
Main Issues to Address
1. Dead Code: tests/_disable_db.py
This file is created but never imported or used anywhere in the PR. The sys.modules mocking is duplicated directly in security_tests/test_worker_auth_security.py instead. Either delete this file or actually use it via import.
2. Unsafe sys.modules Manipulation
The module-level sys.modules mocking (lines 1-8) can cause test pollution:
sys.modules["api.database"] = MagicMock()This modifies sys.modules at import time and persists across all tests in the pytest session. If other tests run alongside these, they might get the mocked module instead of the real one.
Recommendation: Use pytest fixtures with monkeypatch like the rest of the project does in tests/conftest.py:
@pytest.fixture
def mock_database(monkeypatch):
mock_db = MagicMock()
mock_db.fetch_one = AsyncMock()
monkeypatch.setattr("api.worker_auth.database", mock_db)
yield mock_db3. Separate security_tests/ Directory
Creating a separate directory breaks project conventions:
pytest.inihastestpaths = ["tests"], sosecurity_tests/won't be discovered by default- The project convention is to put all tests in
tests/
Recommendation: Move to tests/test_worker_auth_unit.py or similar.
4. Unrelated .env.example Changes
The audit log rotation settings (VLOG_AUDIT_LOG_BACKUP_COUNT, VLOG_AUDIT_LOG_MAX_BYTES) aren't related to security tests. These should be in a separate PR - especially since the functionality to read these settings isn't implemented in config.py or api/audit.py yet.
5. Missing Test: Worker Not Found
The verify_worker_key function has a code path for when the worker record doesn't exist (lines 176-187 in worker_auth.py), but there's no test for this scenario.
Minor Suggestions
-
Import at line 100 -
from datetime import datetime, timedelta, timezoneappears mid-file after test functions. Move to top. -
Strengthen
test_hash_api_key_not_reversible- Currently just checkskey not in hashed. Could also verify hash is 64 hex characters. -
Add docstrings - Brief descriptions of what security scenario each test covers would be helpful.
A Note on Existing Coverage
Just a heads up: tests/test_worker_api.py already has integration tests covering many of these scenarios (missing key, expired key, revoked key, disabled worker, hash consistency) using actual database fixtures. Your unit tests with mocks are still valuable for fast feedback, but you might want to check that file to avoid duplication and ensure you're adding new coverage. If you're going "but Damen, the issue called those out!" that is my bad - I need to go back through and update some issues :D
Looking forward to the updates! Once these are addressed, this will be a nice addition to our test suite. 🙌
|
Hey @iamkrishpathak, I owe you a sincere apology. 😔 After doing a more thorough audit of our codebase, I discovered that the tests you wrote already exist in Here's what's already covered in
The existing tests use proper database fixtures rather than However, your If you'd like to continue with this PR, I'd suggest:
Alternatively, if you'd prefer to close this PR given the confusion, I completely understand - and again, I'm really sorry for the wasted effort. This is entirely our fault for not maintaining the issue properly. Thank you for your patience and for wanting to contribute to the project. 🙏 |
|
Just a quick follow-up - I've updated issue #398 to reflect the current state. It now clearly shows what's already covered and focuses specifically on the If you'd like to continue with this PR, the updated issue should give you a clear scope for what to keep. No pressure either way! 🙂 |
- Remove duplicate tests that already exist in tests/test_worker_api.py (verify_worker_key, hash_api_key tests) - Keep only the valuable _get_request_context() tests for X-Forwarded-For handling, trusted proxy logic, and IPv6 support - Move tests to tests/test_worker_auth.py following project conventions - Use proper pytest monkeypatch fixtures instead of sys.modules manipulation - Remove unused tests/_disable_db.py file - Remove security_tests/ directory (tests belong in tests/) Based on code review feedback from @filthyrake 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
d94270e to
54a1504
Compare
|
Hi @iamkrishpathak! 👋 I've applied the code review feedback to get this PR ready for merge: Changes made:
All 9 tests pass locally and CI security scans are green. Ready for merge! 🎉 Thanks for contributing these tests - the |
Description
Adds comprehensive security-focused tests for worker authentication logic, covering API key validation, request context extraction, and edge cases such as expired, revoked, and disabled workers.
Target Branch
devmainRelated Issues
Addresses missing security test coverage for worker authentication.
Type of Change
Changes Made
verify_worker_key_get_request_contexthash_api_keyTesting
pytest security_tests/Checklist
Additional Notes
These tests intentionally isolate security logic and avoid PostgreSQL/Redis dependencies.