Skip to content

Commit 3c0d165

Browse files
hittytPangjiping
authored andcommitted
fix(server): probe Docker publish address for host ports
1 parent 8ef40bf commit 3c0d165

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

server/opensandbox_server/services/docker_port_allocator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
from opensandbox_server.services.constants import SandboxErrorCodes
2424

2525
DOCKER_PUBLISH_HOST = "0.0.0.0"
26-
PORT_PROBE_HOST = "127.0.0.1"
26+
# The probe is a short-lived availability check and must match Docker's
27+
# publish scope; probing only localhost can miss ports bound on other host
28+
# interfaces that Docker would later fail to publish.
29+
PORT_PROBE_HOST = DOCKER_PUBLISH_HOST
2730

2831

2932
def normalize_container_port_spec(port_spec: str) -> str:
@@ -59,6 +62,9 @@ def allocate_host_port(
5962
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
6063
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
6164
try:
65+
# This does not listen for or accept connections; it mirrors the
66+
# later Docker publish binding to catch host-wide port conflicts.
67+
# codeql[py/bind-socket-all-network-interfaces]
6268
sock.bind((PORT_PROBE_HOST, port))
6369
except OSError:
6470
continue

server/tests/test_docker_port_allocator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def bind(self, address):
3232
self._bound_addresses.append(address)
3333

3434

35-
def test_allocate_host_port_probes_loopback_only(monkeypatch) -> None:
35+
def test_allocate_host_port_probes_docker_publish_address(monkeypatch) -> None:
3636
bound_addresses: list[tuple[str, int]] = []
3737

3838
monkeypatch.setattr(docker_port_allocator.random, "randint", lambda min_port, max_port: 45678)

0 commit comments

Comments
 (0)