fix(streaming): refuse a proxied container whose host and subfolder disagree

A same-origin entry sends the browser to `host`, but activate answers with
the room URL the broker built from its own SUBFOLDER, and that is an
absolute path, so urljoin lets it replace the path `host` carries. The two
values are therefore not independent: both have to be the container's own
SUBFOLDER.

Get them wrong and nothing complains. A container mounted at /streaming-2
while declaring /streaming resolves, claims its own key, and hands the
player a URL the proxy routes to whoever owns /streaming: a different
container, mid-session, with no trace of it in either log. The likeliest
way in is a second pool member proxied at its own path with `subfolder`
left to its default.

Resolve the entry, log it, and clear broker_host so it cannot be claimed,
matching how the other unusable configurations are handled. It stays in
the fleet view, where the operator can see what is wrong with it. A full
URL `host` is untouched; it carries an origin of its own for that absolute
path to land on, so there the two are free to differ.
This commit is contained in:
Zach Clendenen
2026-09-15 13:30:34 -05:00
parent 32264a11d8
commit df6ded2786
2 changed files with 120 additions and 1 deletions

View File

@@ -23,7 +23,11 @@ from handler.streaming.capabilities import (
slot_capabilities,
state_transfer_limits,
)
from handler.streaming.protocol import BrokerProtocol, protocol_for
from handler.streaming.protocol import (
BrokerProtocol,
WebstationProtocol,
protocol_for,
)
from logger.logger import log
# Keys a `platforms:` block may override for the one platform it names.
@@ -238,6 +242,19 @@ def _derive_broker_host(entry: dict[str, Any], protocol: BrokerProtocol) -> str
return urlunparse(parsed._replace(netloc=f"{parsed.hostname}:8000")).rstrip("/")
def _proxy_path_matches_subfolder(host: str, protocol: WebstationProtocol) -> bool:
"""Whether a same-origin entry's mount path agrees with its subfolder.
Only a container configured as a path has to agree. A full URL carries an
origin of its own for the broker's absolute room path to land on, so there
`host` and `subfolder` are free to differ.
"""
host = host.strip()
if not host.startswith("/"):
return True
return host.rstrip("/") == protocol.subfolder
def _emulator_namespace(entry: dict[str, Any]) -> str:
"""Namespace for stored states, e.g. 'pcsx2'. Keeps streaming states apart
from the EmulatorJS states of the same ROM."""
@@ -274,6 +291,27 @@ def _resolve_one(
platform,
_loggable(entry),
)
elif isinstance(
protocol, WebstationProtocol
) and not _proxy_path_matches_subfolder(str(entry.get("host", "")), protocol):
# The browser is sent to `host`, but activate answers with an
# absolute room path the broker builds from its own SUBFOLDER, and
# urljoin lets that path replace the one `host` carries. Mounted at
# /streaming-2 while serving /streaming, a claim would hand the
# player a URL the proxy routes to whoever owns /streaming: another
# container, mid-session, with nothing logged anywhere. Both values
# have to be the container's own SUBFOLDER, so refuse the entry
# rather than resolve one that can only mislead.
log.warning(
"container for platform '%s' is proxied at '%s' but declares "
"subfolder '%s'; both must be the container's own SUBFOLDER, "
"it cannot be claimed: %s",
platform,
str(entry.get("host", "")).strip(),
protocol.subfolder,
_loggable(entry),
)
broker_host = None
emulator = _emulator_namespace(entry)
card_sync = bool(entry.get("memory_card_sync", False))

View File

@@ -1339,6 +1339,87 @@ def test_webstation_pool_members_at_different_subfolders_are_still_a_pool(caplog
assert "not a pool" not in caplog.text
def test_a_proxied_host_disagreeing_with_its_subfolder_cannot_be_claimed(caplog):
"""The broker's room URL is an absolute path built from its own SUBFOLDER,
and it replaces the path `host` carries. Mounted at one path while serving
another, a claim would route the player to whoever owns that other path, so
the entry resolves unclaimable rather than silently misdirecting."""
entry = {
"platform": "ps2",
"host": "/streaming-2",
"broker_host": "http://192.168.1.11:8000",
"protocol": "webstation",
"subfolder": "/streaming",
"emulator": "pcsx2",
}
romm_logger = logging.getLogger("romm")
romm_logger.addHandler(caplog.handler)
try:
with _streaming(entry):
with caplog.at_level(logging.WARNING, logger="romm"):
candidates = streaming.containers_for_platform("ps2")
listed = streaming.resolve_containers()
finally:
romm_logger.removeHandler(caplog.handler)
assert candidates == []
# Still resolved, so the fleet view shows the operator what is wrong.
assert [c.key for c in listed] == [""]
assert "must be the container's own SUBFOLDER" in caplog.text
def test_a_subfolder_left_to_its_default_is_caught_against_the_mount_path(caplog):
"""The likeliest form of the mistake: a second member proxied at its own
path with `subfolder` forgotten, which defaults to /streaming."""
entry = {
"platform": "ps2",
"host": "/streaming-2",
"broker_host": "http://192.168.1.11:8000",
"protocol": "webstation",
"emulator": "pcsx2",
}
romm_logger = logging.getLogger("romm")
romm_logger.addHandler(caplog.handler)
try:
with _streaming(entry):
with caplog.at_level(logging.WARNING, logger="romm"):
candidates = streaming.containers_for_platform("ps2")
finally:
romm_logger.removeHandler(caplog.handler)
assert candidates == []
def test_a_container_mounted_at_the_root_agrees_with_an_empty_subfolder():
"""SUBFOLDER=/ makes the broker's prefix empty and RomM's subfolder "", and
a host of "/" is the same mount. Normalization has to see those as equal."""
entry = {
"platform": "ps2",
"host": "/",
"broker_host": "http://192.168.1.11:8000",
"protocol": "webstation",
"subfolder": "/",
"emulator": "pcsx2",
}
with _streaming(entry):
candidates = streaming.containers_for_platform("ps2")
assert [c.broker_host for c in candidates] == ["http://192.168.1.11:8000"]
def test_a_cross_origin_host_may_differ_from_its_subfolder():
"""A full URL carries its own origin, so the broker's absolute room path
lands on it correctly and the two are free to disagree."""
entry = {
"platform": "ps2",
"host": "https://webstation.example.com",
"broker_host": "http://192.168.1.11:8000",
"protocol": "webstation",
"subfolder": "/streaming",
"emulator": "pcsx2",
}
with _streaming(entry):
candidates = streaming.containers_for_platform("ps2")
assert [c.broker_host for c in candidates] == ["http://192.168.1.11:8000"]
def test_webstation_pool_claim_rolls_over_across_different_subfolders(
client, access_token, viewer_access_token, rom: Rom
):