fix(r30/CR-02): enforce 64 KiB MAX_CONTENT_LENGTH with 413 JSON handler

- Default MAX_CONTENT_LENGTH to 64 KiB before test_config override in create_app

- Add 413 error handler returning JSON error payload

- Add test_request_body_exceeding_max_content_length_returns_413
This commit is contained in:
kiskaadee committed 2026-11-21 17:49:14 -05:00
1 parent 202fcc14ee
commit 34852a9c76
2 files changed
+17

No files matched your search

+5
View File
@@ -40,6 +40,7 @@ def create_app(test_config: dict | None = None) -> Flask:
app = Flask(__name__, template_folder="templates", static_folder="../static") app = Flask(__name__, template_folder="templates", static_folder="../static")
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1) app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
app.config["MAX_CONTENT_LENGTH"] = 64 * 1024
if test_config: if test_config:
app.config.update(test_config) app.config.update(test_config)
@@ -56,6 +57,10 @@ def create_app(test_config: dict | None = None) -> Flask:
) )
app.limiter = limiter app.limiter = limiter
@app.errorhandler(413)
def handle_payload_too_large(_exc):
return jsonify({"error": "El cuerpo de la solicitud excede el límite permitido."}), 413
@app.errorhandler(429) @app.errorhandler(429)
def handle_rate_limit(_exc): def handle_rate_limit(_exc):
return ( return (
+12
View File
@@ -703,6 +703,18 @@ def test_parse_body_rejects_non_dict_json(client):
assert resp.status_code == 400 assert resp.status_code == 400
def test_request_body_exceeding_max_content_length_returns_413(client):
test_client, _ = client
oversized = '{"padding": "' + ("a" * (65 * 1024)) + '"}'
resp = test_client.post(
"/api/appointments",
data=oversized,
content_type="application/json",
)
assert resp.status_code == 413
assert "error" in resp.get_json()
def test_zoom_api_error_status_passthrough_returns_401(client, monkeypatch): def test_zoom_api_error_status_passthrough_returns_401(client, monkeypatch):
test_client, _ = client test_client, _ = client
monkeypatch.setattr( monkeypatch.setattr(