-
Notifications
You must be signed in to change notification settings - Fork 91
Disable rate limiter memory fallback in production #387
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
Closed
silentgeckoaudit3801
wants to merge
5
commits into
Quantarq:main
from
silentgeckoaudit3801:security/prod-rate-limit-redis-194
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c719f3
security: disable rate limiter fallback in production
silentgeckoaudit3801 6786b77
security: fail prod startup without limiter redis
silentgeckoaudit3801 2a5dce2
test: cover prod rate limiter fallback policy
silentgeckoaudit3801 e55db7f
style: separate rate limiter helpers
silentgeckoaudit3801 5381f69
Use unittest assertions for rate limiter fallback tests
silentgeckoaudit3801 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import importlib | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
|
|
||
| def test_production_disables_in_memory_rate_limit_fallback(): | ||
| with patch.dict("os.environ", {"ENV_VERSION": "PROD", "REDIS_URL": "redis://redis:6379"}): | ||
| import web_app.api.rate_limiter as rate_limiter | ||
|
|
||
| importlib.reload(rate_limiter) | ||
|
|
||
| assert rate_limiter.allow_in_memory_fallback() is False | ||
| assert rate_limiter.limiter._in_memory_fallback_enabled is False | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
|
|
||
| def test_development_keeps_in_memory_rate_limit_fallback(): | ||
| with patch.dict("os.environ", {"ENV_VERSION": "DEV", "REDIS_URL": "redis://127.0.0.1:1"}): | ||
| import web_app.api.rate_limiter as rate_limiter | ||
|
|
||
| importlib.reload(rate_limiter) | ||
|
|
||
| assert rate_limiter.allow_in_memory_fallback() is True | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| assert rate_limiter.limiter._in_memory_fallback_enabled is True | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
|
|
||
| def test_production_startup_pings_redis_backend(): | ||
| with patch.dict("os.environ", {"ENV_VERSION": "PROD", "REDIS_URL": "redis://redis:6379"}): | ||
| import web_app.api.rate_limiter as rate_limiter | ||
|
|
||
| importlib.reload(rate_limiter) | ||
| fake_client = MagicMock() | ||
| with patch.object(rate_limiter.redis.Redis, "from_url", return_value=fake_client) as from_url: | ||
| rate_limiter.assert_rate_limiter_backend_available() | ||
|
|
||
| from_url.assert_called_once() | ||
| fake_client.ping.assert_called_once() | ||
| fake_client.close.assert_called_once() | ||
|
|
||
|
|
||
| def test_development_startup_skips_redis_ping(): | ||
| with patch.dict("os.environ", {"ENV_VERSION": "DEV", "REDIS_URL": "redis://127.0.0.1:1"}): | ||
| import web_app.api.rate_limiter as rate_limiter | ||
|
|
||
| importlib.reload(rate_limiter) | ||
| with patch.object(rate_limiter.redis.Redis, "from_url") as from_url: | ||
| rate_limiter.assert_rate_limiter_backend_available() | ||
|
|
||
| from_url.assert_not_called() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.