fix(webui): allow SSH quick pairing behind HTTPS proxies

This commit is contained in:
Xubin Ren committed 2026-11-18 20:15:13 +08:00
1 parent d1e1d7bc5e
commit 307b3bce82
4 files changed
+92 -15

No files matched your search

+47 -5
View File
@@ -7,7 +7,7 @@ import sys
import threading
import time
from types import SimpleNamespace
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, Mock
import pytest
from cryptography.hazmat.primitives import serialization
@@ -456,11 +456,53 @@ async def test_paired_profile_follows_moved_data_root(tmp_path, monkeypatch, wit
assert len(RemoteInstances(moved).snapshot()["profiles"]) == 1
def test_model_keys_never_enter_pairing_metadata(tmp_path):
@pytest.mark.parametrize("field", ["publicWsUrl", "public_ws_url"])
def test_authorize_with_public_websocket_keeps_only_the_private_gateway_target(pair, tmp_path, monkeypatch, field):
store, _, request, original, _ = pair
path = tmp_path / "config.json"
path.write_text(json.dumps({"providers": {"deepseek": {"apiKey": "not-exported"}},
"channels": {"websocket": {"enabled": True, "tokenIssueSecret": "webui-only"}}}))
assert "not-exported" not in json.dumps(remote_pair_server.metadata(path))
before = json.dumps({
"providers": {"deepseek": {"apiKey": "not-exported"}},
"channels": {"websocket": {"enabled": True, "port": 8865, "tokenIssueSecret": "webui-only",
field: "wss://example.test/chat", "path": "/chat"}},
})
path.write_text(before)
install = Mock()
monkeypatch.setattr(remote_pair_server, "as_account", install)
code = remote_pair_server.authorize(request, host=original.host, user=original.user, ssh_port=22,
config=path, host_key=original.host_key, until=original.authorized_until)
saved = store.finish(request.id, code)
assert saved.port == 8865 and saved.secret == "webui-only"
assert saved.config_path == str(path.resolve())
assert "wss://" not in saved.model_dump_json() and "not-exported" not in saved.model_dump_json()
install.assert_called_once_with(
original.user, "install", {"request": request.model_dump(), "port": 8865, "until": original.authorized_until},
)
assert path.read_text() == before
@pytest.mark.parametrize(("settings", "error"), [
({"trustedProxyAuth": {"assertionHeader": "X-User", "trustedPeerCidrs": ["127.0.0.1/32"]}},
"incompatible_gateway"),
({"trusted_proxy_auth": {"assertion_header": "X-User", "trusted_peer_cidrs": ["127.0.0.1/32"]}},
"incompatible_gateway"),
({"sslCertfile": "/etc/nanobot/cert.pem", "sslKeyfile": "/etc/nanobot/key.pem"}, "incompatible_gateway"),
({"tokenIssueSecret": ""}, "webui_auth_required"),
])
def test_public_websocket_does_not_override_ssh_pairing_requirements(pair, tmp_path, monkeypatch, settings, error):
_, _, request, original, _ = pair
path = tmp_path / "config.json"
path.write_text(json.dumps({"channels": {"websocket": {
"enabled": True, "tokenIssueSecret": "webui-only", "publicWsUrl": "wss://example.test/chat",
"path": "/chat", **settings,
}}}))
install = Mock()
monkeypatch.setattr(remote_pair_server, "as_account", install)
with pytest.raises(RemoteError, match=f"^{error}$"):
remote_pair_server.authorize(request, host=original.host, user=original.user, ssh_port=22,
config=path, host_key=original.host_key, until=original.authorized_until)
install.assert_not_called()
def test_pairing_modules_can_import_without_posix_server_dependencies():
+17
View File
@@ -242,6 +242,23 @@ async def test_remote_credentials_never_leave_the_backend(remote):
assert request.headers["Authorization"] != "Bearer " + issued["api_token"]
async def test_public_websocket_url_does_not_redirect_the_ssh_proxy(remote):
public_url = "wss://public.test/custom/ws"
remote.services.http.config.public_ws_url = public_url
host = f"http://127.0.0.1:{remote.port}"
direct = await remote.client.get(host + "/webui/bootstrap", headers={"X-Nanobot-Auth": ROOT})
assert direct.status_code == 200 and direct.json()["ws_url"] == public_url
for attempt in range(2):
issued = await bootstrap(remote)
assert issued["ws_url"].startswith(f"ws://127.0.0.1:{remote.proxy.port}/remote-session/")
assert public_url not in json.dumps(issued)
async with connect(issued["ws_url"] + "?token=" + issued["token"], origin=remote.proxy.origin) as ws:
assert json.loads(await ws.recv())["ready"] is True
await ws.send(f'{{"synthetic_turn":{attempt}}}')
assert json.loads(await ws.recv())["synthetic_turn"] == attempt
@pytest.mark.parametrize("query", ["?token={token}&token=evil", "?%74oken={token}&token=evil"])
async def test_ambiguous_auth_aliases_are_rejected(remote, query):
issued = await bootstrap(remote)