Repository navigation
Key rate limits on client IP only; configurable trusted proxy count #42
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
3 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
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,78 @@ | ||
| """Rate limits must key on the client IP, not on headers the client controls.""" | ||
|
|
||
| import pytest | ||
|
|
||
| HEADERS = {"User-Agent": "pytest-client/1.0 (+https://example.test)", "Content-Type": "application/json"} | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def app_module(): | ||
| import app as app_module | ||
|
|
||
| return app_module | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(app_module, monkeypatch): | ||
| monkeypatch.setitem(app_module.app.config, "TESTING", True) | ||
| with app_module.app.test_client() as c: | ||
| yield c | ||
|
|
||
|
|
||
| def test_identifier_ignores_user_agent_and_language(app_module): | ||
| seen = set() | ||
| for i in range(5): | ||
| with app_module.app.test_request_context( | ||
| "/", headers={"User-Agent": f"agent-number-{i}", "Accept-Language": f"xx-{i}"} | ||
| ): | ||
| seen.add(app_module.get_client_identifier()[2]) | ||
| assert len(seen) == 1 | ||
|
|
||
|
|
||
| def test_rotating_user_agent_still_gets_blocked(client, app_module, monkeypatch): | ||
| monkeypatch.setattr(app_module, "test_mode", True) | ||
| statuses = [] | ||
| for i in range(app_module.MAX_ATTEMPTS + 1): | ||
| client.delete_cookie("session") # also drop the cookie so only the IP can throttle | ||
| h = {**HEADERS, "User-Agent": f"rotating-agent-{i}-padding", "Accept-Language": f"l{i}"} | ||
| statuses.append(client.post("/open-door", json={"pin": "0000"}, headers=h).status_code) | ||
| assert statuses[-1] == 429 | ||
|
|
||
|
|
||
| def test_env_override_wins_even_if_ini_value_is_invalid(tmp_path): | ||
| """DOOROPENER_TRUSTED_PROXIES must be selected before the INI value is parsed. | ||
|
|
||
| conftest mocks ConfigParser, so run the real thing in a subprocess against a throwaway copy. | ||
| """ | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
|
|
||
| root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) | ||
| work = tmp_path / "app" | ||
| work.mkdir() | ||
| for name in os.listdir(root): | ||
| if name.endswith(".py"): | ||
| shutil.copy(os.path.join(root, name), work / name) | ||
| for d in ("templates", "static"): | ||
| shutil.copytree(os.path.join(root, d), work / d) | ||
| (work / "config.ini").write_text( | ||
| "[HomeAssistant]\nurl = http://x\ntoken = t\nswitch_entity = switch.d\n" | ||
| "[admin]\nadmin_password = a-real-password\n[server]\ntrusted_proxies = not-a-number\n" | ||
| ) | ||
| # Drop pytest-cov's env vars: otherwise its .pth hook measures this throwaway copy of the app | ||
| # and its uncovered lines drag the project's coverage below the CI gate. | ||
| env = {k: v for k, v in os.environ.items() if not k.startswith(("COV_CORE", "COVERAGE"))} | ||
| env.update(DOOROPENER_LOG_DIR=str(tmp_path / "logs"), USERS_STORE_PATH=str(tmp_path / "u.json")) | ||
| code = f"import sys; sys.path.insert(0, {str(work)!r}); import app; print(app.TRUSTED_PROXIES)" | ||
|
|
||
| ok = subprocess.run( | ||
| [sys.executable, "-I", "-c", code], | ||
| cwd=work, | ||
| env={**env, "DOOROPENER_TRUSTED_PROXIES": "0"}, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| assert ok.returncode == 0, ok.stderr[-1500:] | ||
| assert ok.stdout.strip().endswith("0") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Assert the exact trusted proxy count. If 🤖 Prompt for AI Agents |
||
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.