fix: guard negative remote_id in federation_connect endpoint
Negative Python list indices bypass the intended 404 guard — e.g. remote_id=-1 on a non-empty list silently proxied to the last remote instead of returning 404. Add an explicit 'idx < 0' check before the list lookup, consistent with the sibling federation_terminal_ws_proxy endpoint (line 728). Also adds test_federation_connect_returns_404_for_negative_remote_id to document the expected behavior and prevent regression.
This commit is contained in:
@@ -1008,6 +1008,8 @@ async def federation_connect(
|
|||||||
remotes = settings.get("remote_instances", [])
|
remotes = settings.get("remote_instances", [])
|
||||||
try:
|
try:
|
||||||
idx = int(remote_id)
|
idx = int(remote_id)
|
||||||
|
if idx < 0 or idx >= len(remotes):
|
||||||
|
raise IndexError
|
||||||
remote = remotes[idx]
|
remote = remotes[idx]
|
||||||
except (ValueError, IndexError):
|
except (ValueError, IndexError):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|||||||
@@ -1915,6 +1915,42 @@ def test_federation_connect_returns_502_when_remote_returns_error_status(
|
|||||||
assert response.status_code == 502
|
assert response.status_code == 502
|
||||||
|
|
||||||
|
|
||||||
|
def test_federation_connect_returns_404_for_negative_remote_id(
|
||||||
|
client, monkeypatch, tmp_path
|
||||||
|
):
|
||||||
|
"""POST /api/federation/{remote_id}/connect/{session_name} returns 404 for negative remote_id.
|
||||||
|
|
||||||
|
Negative indices are valid Python list indices ("from the end"), so without an
|
||||||
|
explicit guard a value like -1 would silently proxy to the last configured remote
|
||||||
|
instead of returning 404. The endpoint must treat negatives as out-of-range.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
|
||||||
|
import muxplex.settings as settings_mod
|
||||||
|
|
||||||
|
settings_path = tmp_path / "settings.json"
|
||||||
|
monkeypatch.setattr(settings_mod, "SETTINGS_PATH", settings_path)
|
||||||
|
|
||||||
|
# One remote configured — without the guard, remote_id=-1 would return it
|
||||||
|
settings_path.write_text(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"remote_instances": [
|
||||||
|
{
|
||||||
|
"url": "http://remote-host:8088",
|
||||||
|
"key": "secret-key-123",
|
||||||
|
"name": "remote-host",
|
||||||
|
"id": "remote-0",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.post("/api/federation/-1/connect/my-session")
|
||||||
|
assert response.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# POST /api/federation/generate-key (task-13)
|
# POST /api/federation/generate-key (task-13)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user