Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(server): a missing file path gets a 404, not the SPA shell
trohitg05a912a54336
diff --git a/docs-internal/desktop_host_contract.md b/docs-internal/desktop_host_contract.mdindex 8fd267c4..bf1a8be8 100644--- a/docs-internal/desktop_host_contract.md+++ b/docs-internal/desktop_host_contract.md@@ -53,9 +53,16 @@ the rule: no other module under `server/` may climb above `server/` withthat maps an attacker-controlled path onto the tree: the SPA fallback in`main.py`. It joins, `os.path.normpath`s, and returns `None` unless theresult sits under the real base directory — so `..` traversal, an absolute-path, and a prefix sibling such as `client/dist2` all fall through to the-SPA shell instead of serving a file. Keeping it beside `client_dist()` is-what lets the route itself build no paths at all.+path, and a prefix sibling such as `client/dist2` all serve no file. Keeping+it beside `client_dist()` is what lets the route itself build no paths at+all. `is_spa_route(relative)` beside it decides what a miss gets: a path+whose last segment has a dot names a file and gets a 404 (the "dot rule" of+connect-history-api-fallback), anything else gets the SPA shell, sent with+`Cache-Control: no-cache`. Answering a missing script with the shell is how+the Home orb went flat on 2026-10-12: an old self-host container on the dev+port answered the orb engine's URL with HTML, which the browser refused as a+module, and with no `Cache-Control` a browser may keep reusing such a+response after the real file is back.## 2. Env layering without the CLIdiff --git a/server/core/approot.py b/server/core/approot.pyindex 29a1ccc2..18d1acad 100644--- a/server/core/approot.py+++ b/server/core/approot.py@@ -99,6 +99,20 @@ def resolve_static_asset(base_dir: Path, relative: str) -> str | None:return candidate+def is_spa_route(relative: str) -> bool:+ """True when a request path that matched no file should get the SPA shell.++ The "dot rule" of connect-history-api-fallback (webpack-dev-server's SPA+ fallback): a last segment with a dot names a file (a script, a+ stylesheet, a stale chunk), and a missing file is a 404. Answering it with+ the HTML shell gives a browser HTML under a script URL: the module is+ refused ("Expected a JavaScript module script but the server responded+ with a MIME type of text/html"), and a cached copy keeps failing after the+ real file is back. The client has no URL routes with dots.+ """+ return "." not in relative.rsplit("/", 1)[-1]++def env_template_path() -> Path:"""The canonical ``.env.template`` (baseline for every env var)."""return _env_path("OPENCOMPANY_ENV_TEMPLATE") or (app_root() / ".env.template")diff --git a/server/main.py b/server/main.pyindex e2f86738..7ee8f195 100644--- a/server/main.py+++ b/server/main.py@@ -58,6 +58,7 @@ apply_file_defaults_to_environ()from core.approot import (app_version as _app_version,client_dist as _client_dist,+ is_spa_route as _is_spa_route,resolve_static_asset as _resolve_static_asset,)from core.config import Settings, cookie_posture_warnings, dev_secret_offenders@@ -799,8 +800,10 @@ async def health_ready():# on) so a dist-less dev checkout is unaffected.## Registered LAST so every real API / WS / mounted route above wins; this-# only catches otherwise-unmatched GET paths and returns the SPA shell for-# client-side routing. Non-SPA prefixes return 404 so API misses stay JSON.+# only catches otherwise-unmatched GET paths. A built file is served as is;+# a missing file (a path whose last segment has a dot) is a 404, never the+# shell; anything else gets the SPA shell, which is always revalidated.+# Non-SPA prefixes return 404 so API misses stay JSON.import os as _spa_osfrom pathlib import Path as _SpaPathfrom fastapi.staticfiles import StaticFiles@@ -831,7 +834,9 @@ if _SERVE_STATIC and (_CLIENT_DIST / "index.html").is_file():asset = _resolve_static_asset(_CLIENT_DIST, full_path)if asset is not None:return FileResponse(asset)- return FileResponse(str(_CLIENT_DIST / "index.html"))+ if not _is_spa_route(full_path):+ return JSONResponse(status_code=404, content={"detail": "Not Found"})+ return FileResponse(str(_CLIENT_DIST / "index.html"), headers={"Cache-Control": "no-cache"})logger.info("Serving built client from %s", _CLIENT_DIST)diff --git a/server/tests/core_config/test_approot.py b/server/tests/core_config/test_approot.pyindex fc83a2c8..2fab17f9 100644--- a/server/tests/core_config/test_approot.py+++ b/server/tests/core_config/test_approot.py@@ -84,6 +84,29 @@ class TestOverrides:assert approot.app_root() == (Path.home() / "some-bundle").resolve()+class TestSpaRoute:+ """What a path that matched no built file gets: the SPA shell, or a 404."""++ @pytest.mark.parametrize("path", ["", "employees/w1", "login"])+ def test_app_paths_get_the_shell(self, approot, path):+ assert approot.is_spa_route(path)++ @pytest.mark.parametrize(+ "path",+ [+ # A dev-server module path: answered with the shell, the orb+ # engine was refused as a module ("MIME type of text/html").+ "src/features/home/orb/orbEngine.ts",+ "assets/orbEngine-DnFDXWr7.js",+ "assets/index.css",+ "favicon.ico",+ "../.env",+ ],+ )+ def test_missing_files_get_a_404(self, approot, path):+ assert not approot.is_spa_route(path)++class TestConsumers:def test_paths_project_root_follows_approot(self, approot, monkeypatch, tmp_path):monkeypatch.setenv("OPENCOMPANY_APP_ROOT", str(tmp_path))