Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions orchestrator/routes/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,10 @@ def mutate_contract(identifier: str) -> tuple[Response, int]:
return validation_error

body = request.get_json()
if not body:
if body is None:
return _error("Missing request body")
if not isinstance(body, dict):
return _error("Request body must be a JSON object")

field_path = body.get("field_path")
new_value = body.get("new_value", ...) # sentinel: allow explicit None
Expand Down Expand Up @@ -410,8 +412,10 @@ def validate_contract_mutation() -> tuple[Response, int]:
against field_path/new_value via the shared validator.
"""
body = request.get_json()
if not body:
if body is None:
return _error("Missing request body")
if not isinstance(body, dict):
return _error("Request body must be a JSON object")

field_path = body.get("field_path")
new_value = body.get("new_value", ...)
Expand Down
8 changes: 6 additions & 2 deletions orchestrator/routes/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,8 +1415,10 @@ def create_pipeline() -> tuple[Response, int]:
}
"""
data = request.get_json()
if not data:
if data is None:
return make_error_response("Missing request body")
if not isinstance(data, dict):
return make_error_response("Request body must be a JSON object")

network_mode = data.get("network_mode")
if network_mode is not None and network_mode not in ("public", "private"):
Expand Down Expand Up @@ -2262,8 +2264,10 @@ def update_pipeline(pipeline_id: str) -> tuple[Response, int]:
}
"""
data = request.get_json()
if not data:
if data is None:
return make_error_response("Missing request body")
if not isinstance(data, dict):
return make_error_response("Request body must be a JSON object")

repo_path = get_repo_path()

Expand Down
4 changes: 3 additions & 1 deletion orchestrator/routes/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def emit_progress(pipeline_id: str) -> tuple[Response, int]:
}
"""
body = request.get_json()
if not body:
if body is None:
return _make_error("Missing request body")
if not isinstance(body, dict):
return _make_error("Request body must be a JSON object")

agent_role = body.get("agent_role")
if not agent_role:
Expand Down
10 changes: 8 additions & 2 deletions orchestrator/routes/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ def handle_signal(pipeline_id: str) -> tuple[Response, int]:
}
"""
data = request.get_json()
if not data:
if data is None:
return make_error_response("Missing request body")
if not isinstance(data, dict):
return make_error_response("Request body must be a JSON object")

signal_type = data.get("signal_type")
if not signal_type:
Expand Down Expand Up @@ -2445,7 +2447,11 @@ def handle_batch_signals(pipeline_id: str) -> tuple[Response, int]:
}
"""
data = request.get_json()
if not data or "signals" not in data:
if data is None:
return make_error_response("Missing request body")
if not isinstance(data, dict):
return make_error_response("Request body must be a JSON object")
if "signals" not in data:
return make_error_response("Missing signals array")

signals = data["signals"]
Expand Down
63 changes: 63 additions & 0 deletions orchestrator/tests/test_contracts_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,69 @@ def test_missing_field_path(self, client):
assert response.status_code == 400


class TestNonObjectJsonBodyReturns400:
"""Fix for #2673: non-object JSON bodies must 400, not 500.

Mirrors the #2656 fix on the decisions route. Without the guard,
``body.get("field_path")`` raises ``AttributeError`` for a
list/scalar body and the generic handler returns 500.
"""

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_mutate_non_object_body_returns_400(self, client, fake_worktree, raw_body):
pipeline_id, worktree = fake_worktree
_seed_contract(worktree, pipeline_id)
response = client.post(
f"/api/v1/contracts/{pipeline_id}/mutate",
data=raw_body,
content_type="application/json",
headers={"X-Egg-Role": "implementer"},
)
assert response.status_code == 400, response.data
body = json.loads(response.data)
assert body["success"] is False
assert "json object" in body["message"].lower(), body

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_validate_non_object_body_returns_400(self, client, raw_body):
response = client.post(
"/api/v1/contract-mutations/validate",
data=raw_body,
content_type="application/json",
headers={"X-Egg-Role": "implementer"},
)
assert response.status_code == 400, response.data
body = json.loads(response.data)
assert body["success"] is False
assert "json object" in body["message"].lower(), body


class TestBranchReadFallback:
"""Covers the #1977 branch-read fallback path for finished pipelines.

Expand Down
59 changes: 59 additions & 0 deletions orchestrator/tests/test_pipelines_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,65 @@ def test_clear_runtime_state_evicts_context_pr_dedupe(self):
assert pipeline_id not in _context_pr_events_emitted


class TestNonObjectJsonBodyReturns400:
"""Fix for #2673: non-object JSON bodies must 400, not 500.

Mirrors the #2656 fix on the decisions route. Without the guard,
``data.get(...)`` raises ``AttributeError`` for a list/scalar body
and the generic handler returns 500.
"""

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_create_pipeline_non_object_body_returns_400(self, client, raw_body):
response = client.post(
"/api/v1/pipelines",
data=raw_body,
content_type="application/json",
)
assert response.status_code == 400, response.data
body = response.get_json()
assert body["success"] is False
assert "json object" in body["message"].lower(), body

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_update_pipeline_non_object_body_returns_400(self, client, raw_body):
response = client.patch(
"/api/v1/pipelines/issue-42",
data=raw_body,
content_type="application/json",
)
assert response.status_code == 400, response.data
body = response.get_json()
assert body["success"] is False
assert "json object" in body["message"].lower(), body


class TestEpicModeNonEpicRejection:
"""Regression tests for issue #1557 review feedback (N5).

Expand Down
31 changes: 31 additions & 0 deletions orchestrator/tests/test_progress_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ def test_emit_progress_minimal_fields(self, client):
assert event["detail"] == ""
assert event["blocker"] == ""

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_emit_progress_non_object_body_returns_400(self, client, raw_body):
"""Fix for #2673: non-object JSON bodies must 400, not 500.

Mirrors the #2656 fix on the decisions route. Without the guard,
``body.get("agent_role")`` raises ``AttributeError`` for a
list/scalar body and the generic handler returns 500.
"""
resp = client.post(
"/api/v1/pipelines/issue-100/progress",
data=raw_body,
content_type="application/json",
)
assert resp.status_code == 400, resp.data
body = resp.get_json()
assert body["success"] is False
assert "json object" in body["message"].lower(), body


# ---------------------------------------------------------------------------
# GET /api/v1/pipelines/<pipeline_id>/progress
Expand Down
66 changes: 66 additions & 0 deletions orchestrator/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def app():
yield app


@pytest.fixture
def client(app):
"""Test client for the signals blueprint."""
return app.test_client()


@pytest.fixture
def mock_pipeline():
"""Create a mock pipeline."""
Expand Down Expand Up @@ -1717,6 +1723,66 @@ def test_ack_version_not_overwritten_if_already_in_payload(self, mock_subprocess
assert payload_passed.get("ack_version") == 3


class TestNonObjectJsonBodyReturns400:
"""Fix for #2673: non-object JSON bodies must 400, not 500.

Mirrors the #2656 fix on the decisions route. Previously
``data = request.get_json()`` left a list / scalar in ``data``
and ``data.get(...)`` raised ``AttributeError`` → the generic
exception handler returned 500.
"""

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_handle_signal_non_object_body_returns_400(self, client, raw_body):
response = client.post(
"/api/v1/pipelines/issue-42/signal",
data=raw_body,
content_type="application/json",
)
assert response.status_code == 400, response.data
body = json.loads(response.data)
assert body["success"] is False
assert "json object" in body["message"].lower(), body

@pytest.mark.parametrize(
"raw_body",
["[1, 2, 3]", '"a string body"', "42", "true", "[]", "0", "false", '""'],
ids=[
"array",
"string",
"number",
"bool",
"empty-array",
"zero",
"false",
"empty-string",
],
)
def test_batch_signal_non_object_body_returns_400(self, client, raw_body):
response = client.post(
"/api/v1/pipelines/issue-42/signal/batch",
data=raw_body,
content_type="application/json",
)
assert response.status_code == 400, response.data
body = json.loads(response.data)
assert body["success"] is False
assert "json object" in body["message"].lower(), body


# ---------------------------------------------------------------------------
# ACK version presence enforcement at the route boundary (#2674)
# ---------------------------------------------------------------------------
Expand Down
Loading
Loading