Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(runtime): spell the server-side backend address as the IPv4 loopback everywhere

Bingxi Zhao (Frank)9af5f890f4ec
diff --git a/deeptutor/services/codex_auth/contracts.py b/deeptutor/services/codex_auth/contracts.py
index b868dac7e..754b7180b 100644
--- a/deeptutor/services/codex_auth/contracts.py
+++ b/deeptutor/services/codex_auth/contracts.py
@@ -163,8 +163,10 @@ class CodexModel:
supports_reasoning_summary=bool(payload["supports_reasoning_summary"]),
supports_parallel_tool_calls=bool(payload["supports_parallel_tool_calls"]),
use_responses_lite=bool(payload["use_responses_lite"]),
- context_window=_optional_positive_int(payload.get("context_window")),
- max_context_window=_optional_positive_int(payload.get("max_context_window")),
+ context_window=_require_optional_positive_int(payload.get("context_window")),
+ max_context_window=_require_optional_positive_int(
+ payload.get("max_context_window")
+ ),
)
except (KeyError, TypeError, ValueError) as exc:
raise CodexAuthError(
@@ -174,7 +176,15 @@ class CodexModel:
) from exc
-def _optional_positive_int(value: object) -> int | None:
+def _require_optional_positive_int(value: object) -> int | None:
+ """Validate a cached context window, rejecting anything malformed.
+
+ Deliberately stricter than ``catalog._optional_positive_int``, which drops
+ junk from a *live* API response so one odd field can't fail the whole sync.
+ Here the payload is our own cache: a value we never could have written means
+ the file is corrupt, so the ``ValueError`` is caught by ``from_dict`` above
+ and reported as ``catalog_corrupt`` rather than silently read as "unknown".
+ """
if value is None:
return None
if isinstance(value, bool) or not isinstance(value, int) or value <= 0:
diff --git a/deeptutor/services/config/runtime_settings.py b/deeptutor/services/config/runtime_settings.py
index 7d28470e0..64c528bf3 100644
--- a/deeptutor/services/config/runtime_settings.py
+++ b/deeptutor/services/config/runtime_settings.py
@@ -526,10 +526,16 @@ class RuntimeSettingsService:
# so the two deployment paths stay in sync. DEEPTUTOR_API_BASE_URL is
# the address the frontend *server* uses to reach the backend; the
# browser itself only ever talks to the frontend origin.
+ #
+ # The fallback is the IPv4 loopback, not "localhost": on a dual-stack
+ # host that name resolves to ::1 first, while uvicorn binds 0.0.0.0
+ # (IPv4 only), so every rewritten /api/* request fails to connect.
+ # The launcher passes the same literal (see runtime/launcher.py), so
+ # both deployment paths agree.
"DEEPTUTOR_API_BASE_URL": (
system["next_public_api_base"]
or system["next_public_api_base_external"]
- or f"http://localhost:{system['backend_port']}"
+ or f"http://127.0.0.1:{system['backend_port']}"
),
"DEEPTUTOR_AUTH_ENABLED": _bool_env(auth["enabled"]),
"POCKETBASE_URL": integrations["pocketbase_url"],
diff --git a/tests/services/config/test_runtime_settings.py b/tests/services/config/test_runtime_settings.py
index c531d31e6..ef89c6cbf 100644
--- a/tests/services/config/test_runtime_settings.py
+++ b/tests/services/config/test_runtime_settings.py
@@ -108,15 +108,50 @@ def test_render_environment_uses_json_backed_runtime_names(monkeypatch, tmp_path
# Server-side proxy contract consumed by web/proxy.ts (the Next.js
# middleware). DEEPTUTOR_AUTH_ENABLED gates the login redirect;
# DEEPTUTOR_API_BASE_URL is where the frontend server reaches the backend
- # (falls back to localhost:<backend_port> when no in-network / external base
- # is configured).
+ # (falls back to the IPv4 loopback on <backend_port> when no in-network /
+ # external base is configured — see the dedicated test below for why).
assert env["DEEPTUTOR_AUTH_ENABLED"] == "true"
- assert env["DEEPTUTOR_API_BASE_URL"] == "http://localhost:8010"
+ assert env["DEEPTUTOR_API_BASE_URL"] == "http://127.0.0.1:8010"
assert env["AUTH_TOKEN_EXPIRE_HOURS"] == "12"
assert env["POCKETBASE_URL"] == "http://pocketbase:8090"
assert "AUTH_SECRET" not in env
+def test_api_base_url_falls_back_to_ipv4_loopback(monkeypatch, tmp_path: Path) -> None:
+ """The server-side backend address must never be spelled "localhost".
+
+ On a dual-stack host that name resolves to ::1 first, while uvicorn binds
+ 0.0.0.0 (IPv4 only) — so every /api/* rewrite issued by web/proxy.ts fails
+ to connect. The launcher was fixed in #784; this is the Docker entrypoint
+ path, which renders the same variable and must agree with it.
+ """
+ _clear_runtime_env(monkeypatch)
+ service = RuntimeSettingsService(tmp_path / "settings")
+ service.save_system({"backend_port": 8042})
+
+ env = service.render_environment()
+
+ assert env["DEEPTUTOR_API_BASE_URL"] == "http://127.0.0.1:8042"
+
+
+def test_api_base_url_prefers_a_configured_base_over_the_loopback(
+ monkeypatch, tmp_path: Path
+) -> None:
+ """The loopback is only a fallback: an explicit base still wins."""
+ _clear_runtime_env(monkeypatch)
+ service = RuntimeSettingsService(tmp_path / "settings")
+ service.save_system(
+ {
+ "backend_port": 8042,
+ "next_public_api_base": "http://backend.internal:9000",
+ }
+ )
+
+ env = service.render_environment()
+
+ assert env["DEEPTUTOR_API_BASE_URL"] == "http://backend.internal:9000"
+
+
def test_system_settings_accept_public_api_base_alias_and_normalize_origins(
monkeypatch, tmp_path: Path
) -> None:
diff --git a/web/proxy.ts b/web/proxy.ts
index bc4cb136f..fc33daefb 100644
--- a/web/proxy.ts
+++ b/web/proxy.ts
@@ -13,10 +13,14 @@ import {
// Backend base URL for `/api/*` and `/ws/*` rewrites. The container entrypoint
// exports `DEEPTUTOR_API_BASE_URL` from `data/user/settings/system.json`
// (preferring `next_public_api_base`, then `next_public_api_base_external`,
-// then `http://localhost:${BACKEND_PORT}`). In dev (`deeptutor start`) it
-// defaults to `http://localhost:8001`.
+// then `http://127.0.0.1:${BACKEND_PORT}`). This last-resort default applies
+// only when nothing exported the variable at all.
+//
+// The loopback is spelled as the IPv4 literal, not `localhost`: on a dual-stack
+// host that name resolves to ::1 first, while uvicorn binds 0.0.0.0 (IPv4
+// only), so every rewrite would fail to connect.
const API_BASE_URL =
- process.env.DEEPTUTOR_API_BASE_URL ?? "http://localhost:8001";
+ process.env.DEEPTUTOR_API_BASE_URL ?? "http://127.0.0.1:8001";
const AUTH_ENABLED = parseAuthEnabled(process.env.DEEPTUTOR_AUTH_ENABLED);