fix(security): require JSON Content-Type on mutating routes#357
Open
kevinnft wants to merge 1 commit into
Open
fix(security): require JSON Content-Type on mutating routes#357kevinnft wants to merge 1 commit into
kevinnft wants to merge 1 commit into
Conversation
Add RequireJsonContentTypeMiddleware that rejects non-JSON bodies on /api/add-extra-deposit/* and /api/save-bug-report with 415 + Accept hint. GET/health and non-listed paths are unchanged. Closes Quantarq#205 Signed-off-by: NossXBT <kevinnft@users.noreply.github.com>
4 tasks
| if state is not None and not isinstance(state, dict): | ||
| try: | ||
| state.is_mutating = is_mutating | ||
| except Exception: |
| content=b"not-json", | ||
| headers={"Content-Type": "text/plain"}, | ||
| ) | ||
| assert response.status_code == 415 |
| ) | ||
| assert response.status_code == 415 | ||
| body = response.json() | ||
| assert "application/json" in body.get("accept", "") |
| assert response.status_code == 415 | ||
| body = response.json() | ||
| assert "application/json" in body.get("accept", "") | ||
| assert response.headers.get("accept") == "application/json" |
| "/api/save-bug-report", | ||
| json={"title": "x", "body": "y"}, | ||
| ) | ||
| assert response.status_code == 200 |
| def test_get_health_never_triggers_check(client): | ||
| response = client.get("/health") | ||
| assert response.status_code == 200 | ||
| assert response.json() == {"ok": True} |
| content=b"anything", | ||
| headers={"Content-Type": "text/plain"}, | ||
| ) | ||
| assert response.status_code == 200 |
| headers={"Content-Type": "text/plain"}, | ||
| ) | ||
| assert response.status_code == 200 | ||
| assert response.json() == {"other": True} |
|
|
||
|
|
||
| def test_default_prefixes_cover_issue_targets(): | ||
| assert any(p.startswith("/api/add-extra-deposit") for p in DEFAULT_MUTATING_PATH_PREFIXES) |
|
|
||
| def test_default_prefixes_cover_issue_targets(): | ||
| assert any(p.startswith("/api/add-extra-deposit") for p in DEFAULT_MUTATING_PATH_PREFIXES) | ||
| assert "/api/save-bug-report" in DEFAULT_MUTATING_PATH_PREFIXES |
| if state is not None and not isinstance(state, dict): | ||
| try: | ||
| state.is_mutating = is_mutating | ||
| except Exception: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #205.
Adds
RequireJsonContentTypeMiddlewareso mutating routes reject non-JSON bodies with HTTP 415 and anAccept: application/jsonhint. Default prefixes cover/api/add-extra-deposit/*and/api/save-bug-report. GET/health and non-listed paths are unchanged.Changes
quantara/web_app/api/middleware.py: new middleware + prefix configquantara/web_app/api/main.py: register middlewarequantara/web_app/tests/test_middleware_content_type.py: 415 vs JSON vs GET coverageTest plan
pytest tests/test_middleware_content_type.py→ 7 passed