Repository navigation
Add Retry-After to rate-limited responses #771
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,60 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from .asgi import AikidoASGIMiddleware | ||
|
|
||
|
|
||
| async def call_middleware(result): | ||
| sent = [] | ||
|
|
||
| async def app(scope, receive, send): | ||
| raise AssertionError("app should not be called") | ||
|
|
||
| async def send(message): | ||
| sent.append(message) | ||
|
|
||
| with patch("aikido_zen.middleware.asgi.should_block_request", return_value=result): | ||
| await AikidoASGIMiddleware(app)({"type": "http"}, None, send) | ||
| return sent | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_rate_limited_response_has_retry_after(): | ||
| sent = await call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": 7, | ||
| } | ||
| ) | ||
| assert sent[0]["status"] == 429 | ||
| assert sent[0]["headers"] == [ | ||
| (b"content-type", b"text/plain"), | ||
| (b"retry-after", b"7"), | ||
| ] | ||
| assert sent[1]["body"] == b"You are rate limited by Zen." | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_rate_limited_response_without_retry_after_seconds(): | ||
| sent = await call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": None, | ||
| } | ||
| ) | ||
| assert sent[0]["status"] == 429 | ||
| assert sent[0]["headers"] == [(b"content-type", b"text/plain")] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_blocked_user_response_has_no_retry_after(): | ||
| sent = await call_middleware({"block": True, "type": "blocked", "trigger": "user"}) | ||
| assert sent[0]["status"] == 403 | ||
| assert sent[0]["headers"] == [(b"content-type", b"text/plain")] |
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,49 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from .django import AikidoDjangoMiddleware | ||
|
|
||
|
|
||
| class FakeHttpResponse(dict): | ||
| def __init__(self, content, content_type=None, status=200): | ||
| super().__init__() | ||
| self.content = content | ||
| self.content_type = content_type | ||
| self.status_code = status | ||
|
|
||
|
|
||
| def call_middleware(result): | ||
| middleware = AikidoDjangoMiddleware(get_response=None) | ||
| middleware.HttpResponse = FakeHttpResponse | ||
| with patch( | ||
| "aikido_zen.middleware.django.should_block_request", return_value=result | ||
| ): | ||
| return middleware(request=None) | ||
|
|
||
|
|
||
| def test_rate_limited_response_has_retry_after(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": 7, | ||
| } | ||
| ) | ||
| assert response.status_code == 429 | ||
| assert response["Retry-After"] == "7" | ||
| assert response.content == "You are rate limited by Zen." | ||
|
|
||
|
|
||
| def test_rate_limited_response_without_retry_after_seconds(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": None, | ||
| } | ||
| ) | ||
| assert response.status_code == 429 | ||
| assert "Retry-After" not in response |
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 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from werkzeug.test import create_environ | ||
|
|
||
| from .flask import AikidoFlaskMiddleware | ||
|
|
||
|
|
||
| def call_middleware(result): | ||
| captured = {} | ||
|
|
||
| def start_response(status, headers, exc_info=None): | ||
| captured["status"] = status | ||
| captured["headers"] = dict(headers) | ||
|
|
||
| with patch("aikido_zen.middleware.flask.should_block_request", return_value=result): | ||
| middleware = AikidoFlaskMiddleware(app=None) | ||
| captured["body"] = b"".join(middleware(create_environ("/"), start_response)) | ||
| return captured | ||
|
|
||
|
|
||
| def test_rate_limited_response_has_retry_after(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "ip", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": 7, | ||
| } | ||
| ) | ||
| assert response["status"] == "429 TOO MANY REQUESTS" | ||
| assert response["headers"]["Retry-After"] == "7" | ||
| assert response["body"] == b"You are rate limited by Zen. (Your IP: 1.1.1.1)" | ||
|
|
||
|
|
||
| def test_rate_limited_response_without_retry_after_seconds(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": None, | ||
| } | ||
| ) | ||
| assert response["status"] == "429 TOO MANY REQUESTS" | ||
| assert "Retry-After" not in response["headers"] |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shared
key/triggerselection consolidates the existing group, user, and IP branches; this restructuring is unrelated to returning retry timing.Details
✨ AI Reasoning
The behavioral change is to return retry timing for rate-limited requests. Selecting a key and trigger into shared variables replaces the existing group, user, and IP branches, but that restructuring is not needed to add the retry timing and can be omitted without changing the intended outcome.
🔧 How do I fix it?
Limit the change to what's needed to achieve its intent. Avoid bundling unrelated reformatting or refactoring, and don't rewrite code that didn't need to change.
Reply on this comment to give feedback or ignore the issue.
More info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was done on purpose, in answer to the earlier review comment on this function: adding
retry_after_secondsmade the group, user and IP branches repeat the same blocked result. Choosing the key and trigger first and checking once removes that duplication. The order (group, then user, then IP) and the results are unchanged, and the existing rate-limiting and QA rate-limit tests confirm it.