mirror of
https://github.com/debpalash/VoiceStudio.git
synced 2026-12-04 01:18:16 +00:00
fix(colab): pass the security-floor check with duplicate packages and load the UI through the new proxy
- security_floors.installed() keeps the first copy on sys.path, as import does, so a stale apt copy no longer fails the check (#4066) - the notebook run() helper prints the child's output so failures show their cause (#4066) - the Colab launch cell trusts any Host behind Google's authenticated proxy; the origin allowlist stays exact (#4067) - the browser guard logs a refused Host at debug level (#4067) Fixes #4066 Fixes #4067
This commit is contained in:
1 parent
5088afa443
commit
37ba6e18b9
7 files changed
+54
-13
No files matched your search
@@ -32,6 +32,7 @@ servers, LAN sharing by IP address and Tailscale MagicDNS names):
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import ipaddress
|
||||
import os
|
||||
import socket
|
||||
@@ -50,6 +51,8 @@ from core.csrf import (
|
||||
)
|
||||
from core.user_env import DESKTOP_ENV_FILE_HINT
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
ALLOWED_HOSTS_ENV = "OMNIVOICE_ALLOWED_HOSTS"
|
||||
_MCP_HOSTS_ENV = "OMNIVOICE_MCP_ALLOWED_HOSTS"
|
||||
# MagicDNS names only resolve to tailnet addresses (or Tailscale's Funnel
|
||||
@@ -292,6 +295,7 @@ class BrowserGuardMiddleware:
|
||||
and not host_allowed(_header(scope, b"host"))
|
||||
and _network_authorized_only(connection)
|
||||
):
|
||||
logger.debug("browser guard refused Host %r", _header(scope, b"host"))
|
||||
return HOST_DETAIL
|
||||
unsafe = scope["type"] != "http" or method not in SAFE_HTTP_METHODS
|
||||
return CROSS_SITE_DETAIL if unsafe and is_cross_site(connection) else None
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Colab: the security-floor check judges the Mako copy Python actually imports, and setup cells now print the failing command's output (#4066)
|
||||
@@ -0,0 +1 @@
|
||||
Colab: the web UI loads through the new `*.prod.r.colab.dev` proxy instead of hanging on 403s (#4067)
|
||||
@@ -33,12 +33,13 @@ def floors(path: Path = CONSTRAINTS) -> list[Requirement]:
|
||||
|
||||
|
||||
def installed() -> dict[str, str]:
|
||||
"""Installed distributions by normalized name."""
|
||||
return {
|
||||
canonicalize_name(dist.metadata["Name"]): dist.version
|
||||
for dist in md.distributions()
|
||||
if dist.metadata["Name"]
|
||||
}
|
||||
"""Installed distributions by normalized name; the first copy on sys.path wins, as in `import`."""
|
||||
versions: dict[str, str] = {}
|
||||
for dist in md.distributions():
|
||||
name = dist.metadata["Name"]
|
||||
if name:
|
||||
versions.setdefault(canonicalize_name(name), dist.version)
|
||||
return versions
|
||||
|
||||
|
||||
def below_floor(reqs: list[Requirement], versions: dict[str, str]) -> list[tuple[Requirement, str]]:
|
||||
|
||||
@@ -101,7 +101,12 @@
|
||||
" \"\"\"Stream a command's output; stop the cell with an actionable message on failure.\"\"\"\n",
|
||||
" shown = cmd if isinstance(cmd, str) else \" \".join(cmd)\n",
|
||||
" print(f\"\\n$ {shown}\")\n",
|
||||
" rc = subprocess.run(cmd, cwd=cwd, shell=isinstance(cmd, str)).returncode\n",
|
||||
" # Capture and print the child's output: Colab does not show a child's inherited\n",
|
||||
" # stdout/stderr, so a failure would otherwise carry no cause.\n",
|
||||
" proc = subprocess.run(cmd, cwd=cwd, shell=isinstance(cmd, str), text=True,\n",
|
||||
" stdout=subprocess.PIPE, stderr=subprocess.STDOUT)\n",
|
||||
" print(proc.stdout, end=\"\")\n",
|
||||
" rc = proc.returncode\n",
|
||||
" if rc != 0:\n",
|
||||
" raise SystemExit(\n",
|
||||
" f\"\\nFAILED: {what or shown} (exit code {rc}).\\n\"\n",
|
||||
@@ -302,7 +307,11 @@
|
||||
"\n",
|
||||
" return {\n",
|
||||
" \"OMNIVOICE_ALLOWED_ORIGINS\": add(\"OMNIVOICE_ALLOWED_ORIGINS\", origin),\n",
|
||||
" \"OMNIVOICE_ALLOWED_HOSTS\": add(\"OMNIVOICE_ALLOWED_HOSTS\", host),\n",
|
||||
" # The proxy forwards a Host that differs from the address-bar URL (*.prod.r.colab.dev),\n",
|
||||
" # so exact-host trust 403s. The backend listens on the VM's loopback, reachable only\n",
|
||||
" # through Google's authenticated proxy, so DNS rebinding cannot target it; the origin\n",
|
||||
" # allowlist above still guards unsafe methods.\n",
|
||||
" \"OMNIVOICE_ALLOWED_HOSTS\": add(\"OMNIVOICE_ALLOWED_HOSTS\", \"*\"),\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -45,19 +45,20 @@ def proxy_trust_env():
|
||||
|
||||
def test_trusts_exactly_the_session_proxy_origin(proxy_trust_env):
|
||||
env = proxy_trust_env(PROXY + "/?authuser=0", {})
|
||||
# Hosts are "*": the Colab proxy forwards a Host unlike the address-bar URL (#4067).
|
||||
assert env == {
|
||||
"OMNIVOICE_ALLOWED_ORIGINS": PROXY,
|
||||
"OMNIVOICE_ALLOWED_HOSTS": PROXY_HOST,
|
||||
"OMNIVOICE_ALLOWED_HOSTS": "*",
|
||||
}
|
||||
|
||||
|
||||
def test_existing_allow_lists_are_kept(proxy_trust_env):
|
||||
env = proxy_trust_env(PROXY, {
|
||||
"OMNIVOICE_ALLOWED_ORIGINS": "http://localhost:3901",
|
||||
"OMNIVOICE_ALLOWED_HOSTS": f"studio.example.com,{PROXY_HOST}",
|
||||
"OMNIVOICE_ALLOWED_HOSTS": "studio.example.com",
|
||||
})
|
||||
assert env["OMNIVOICE_ALLOWED_ORIGINS"] == f"http://localhost:3901,{PROXY}"
|
||||
assert env["OMNIVOICE_ALLOWED_HOSTS"] == f"studio.example.com,{PROXY_HOST}"
|
||||
assert env["OMNIVOICE_ALLOWED_HOSTS"] == "studio.example.com,*"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad", [
|
||||
@@ -107,7 +108,10 @@ def test_proxied_ui_is_refused_without_the_trust(clean_env, forwarded_host):
|
||||
assert r.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.parametrize("forwarded_host", [PROXY_HOST, "localhost:3900"])
|
||||
@pytest.mark.parametrize(
|
||||
"forwarded_host",
|
||||
[PROXY_HOST, "localhost:3900", "3900-gpu-t4-s-abc-c.us-east1.prod.r.colab.dev"], # #4067
|
||||
)
|
||||
def test_proxied_ui_reaches_the_api_with_the_trust(clean_env, proxy_trust_env, forwarded_host):
|
||||
for name, value in proxy_trust_env(PROXY, {}).items():
|
||||
clean_env.setenv(name, value)
|
||||
@@ -123,4 +127,3 @@ def test_another_colab_session_stays_refused(clean_env, proxy_trust_env):
|
||||
other = "https://zzz-3900-colab.googleusercontent.com"
|
||||
client = _client(PROXY_HOST)
|
||||
assert client.post("/generate", headers={"Origin": other}).status_code == 403
|
||||
assert _client(other.removeprefix("https://")).get("/profiles").status_code == 403
|
||||
@@ -89,3 +89,25 @@ def test_duplicate_lock_entries_cannot_hide_a_vulnerable_version():
|
||||
resolved = _resolved_versions(packages, floors)
|
||||
assert resolved == {"demo": [Version("2.0"), Version("0.9")]}
|
||||
assert _versions_below_floors(resolved, floors) == {"demo": ["0.9"]}
|
||||
|
||||
|
||||
def test_installed_keeps_the_first_copy_on_sys_path(monkeypatch):
|
||||
"""#4066: a stale apt copy later on sys.path must not shadow the imported one."""
|
||||
import importlib.metadata as md
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "deploy"))
|
||||
try:
|
||||
import security_floors
|
||||
finally:
|
||||
sys.path.pop(0)
|
||||
|
||||
class Dist:
|
||||
def __init__(self, name, version):
|
||||
self.metadata = {"Name": name}
|
||||
self.version = version
|
||||
|
||||
monkeypatch.setattr(
|
||||
md, "distributions", lambda: iter([Dist("Mako", "1.4.3"), Dist("mako", "1.3.2.dev0")])
|
||||
)
|
||||
assert security_floors.installed() == {"mako": "1.4.3"}
|
||||
Reference in new issue
Block a user