Spaces:
Paused
Paused
Commit ·
9d1fdb1
1
Parent(s): 6ba0cb8
fix(proxy): keep nested static CSS inside the preview
Browse filesA session-root <base href> sent unscripted subpages to /s/css/style.css
and /s/{id}/overlays.css (both 404), so demos rendered as unstyled white
pages. Base the tag on the current document directory instead.
- backend/app/services/proxy.py +35 -10
- backend/tests/test_preview_base.py +33 -0
backend/app/services/proxy.py
CHANGED
|
@@ -29,6 +29,38 @@ _FRAME_BLOCK_HEADERS = {
|
|
| 29 |
_BASE_TAG_RE = re.compile(rb"<head([^>]*)>", re.IGNORECASE)
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
async def proxy_request(session_id: str, path: str, request: Request) -> Response:
|
| 33 |
sess = session_manager.get(session_id)
|
| 34 |
if not sess or sess.status != SessionStatus.running or not sess.port:
|
|
@@ -89,17 +121,10 @@ async def proxy_request(session_id: str, path: str, request: Request) -> Respons
|
|
| 89 |
content = upstream.content
|
| 90 |
ctype = (upstream.headers.get("content-type") or "").lower()
|
| 91 |
|
| 92 |
-
# Inject <base>
|
|
|
|
| 93 |
if method != "HEAD" and "text/html" in ctype and content:
|
| 94 |
-
|
| 95 |
-
base = b'<base href="' + prefix + b'">'
|
| 96 |
-
if b"<base " not in content.lower():
|
| 97 |
-
m = _BASE_TAG_RE.search(content)
|
| 98 |
-
if m:
|
| 99 |
-
insert_at = m.end()
|
| 100 |
-
content = content[:insert_at] + base + content[insert_at:]
|
| 101 |
-
else:
|
| 102 |
-
content = base + content
|
| 103 |
|
| 104 |
return Response(
|
| 105 |
content=content if method != "HEAD" else b"",
|
|
|
|
| 29 |
_BASE_TAG_RE = re.compile(rb"<head([^>]*)>", re.IGNORECASE)
|
| 30 |
|
| 31 |
|
| 32 |
+
def preview_base_href(session_id: str, path: str) -> str:
|
| 33 |
+
"""Directory URL for an injected <base href>.
|
| 34 |
+
|
| 35 |
+
Relative links must resolve against the *current document* directory, not
|
| 36 |
+
the session root. unscripted's overlays/popover.html uses
|
| 37 |
+
href="../css/style.css" and href="overlays.css". A root base of
|
| 38 |
+
/s/{id}/ turns those into /s/css/style.css (404) and /s/{id}/overlays.css
|
| 39 |
+
(404), so nested pages render as unstyled white documents. Root
|
| 40 |
+
index.html still gets /s/{id}/ so css/style.css keeps working.
|
| 41 |
+
"""
|
| 42 |
+
suffix = (path or "").lstrip("/")
|
| 43 |
+
if not suffix or suffix.endswith("/"):
|
| 44 |
+
directory = suffix
|
| 45 |
+
elif "/" in suffix:
|
| 46 |
+
directory = suffix.rsplit("/", 1)[0] + "/"
|
| 47 |
+
else:
|
| 48 |
+
directory = ""
|
| 49 |
+
return f"/s/{session_id}/{directory}"
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def inject_preview_base(content: bytes, session_id: str, path: str) -> bytes:
|
| 53 |
+
if b"<base " in content.lower():
|
| 54 |
+
return content
|
| 55 |
+
href = preview_base_href(session_id, path).encode()
|
| 56 |
+
base = b'<base href="' + href + b'">'
|
| 57 |
+
m = _BASE_TAG_RE.search(content)
|
| 58 |
+
if m:
|
| 59 |
+
insert_at = m.end()
|
| 60 |
+
return content[:insert_at] + base + content[insert_at:]
|
| 61 |
+
return base + content
|
| 62 |
+
|
| 63 |
+
|
| 64 |
async def proxy_request(session_id: str, path: str, request: Request) -> Response:
|
| 65 |
sess = session_manager.get(session_id)
|
| 66 |
if not sess or sess.status != SessionStatus.running or not sess.port:
|
|
|
|
| 121 |
content = upstream.content
|
| 122 |
ctype = (upstream.headers.get("content-type") or "").lower()
|
| 123 |
|
| 124 |
+
# Inject <base> for the current directory so nested relative assets
|
| 125 |
+
# (../css/style.css, ./overlays.css) stay inside /s/{id}/.
|
| 126 |
if method != "HEAD" and "text/html" in ctype and content:
|
| 127 |
+
content = inject_preview_base(content, session_id, suffix)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
return Response(
|
| 130 |
content=content if method != "HEAD" else b"",
|
backend/tests/test_preview_base.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""<base href> must follow the document directory, not the session root."""
|
| 2 |
+
from backend.app.services.proxy import inject_preview_base, preview_base_href
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_root_and_index_base_is_session_root():
|
| 6 |
+
assert preview_base_href("abc", "") == "/s/abc/"
|
| 7 |
+
assert preview_base_href("abc", "/") == "/s/abc/"
|
| 8 |
+
assert preview_base_href("abc", "index.html") == "/s/abc/"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_nested_html_base_is_its_directory():
|
| 12 |
+
assert preview_base_href("abc", "overlays/popover.html") == "/s/abc/overlays/"
|
| 13 |
+
assert preview_base_href("abc", "a/b/c.html") == "/s/abc/a/b/"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_inject_keeps_unscripted_relative_css():
|
| 17 |
+
html = (
|
| 18 |
+
b"<html><head>"
|
| 19 |
+
b'<link rel="stylesheet" href="../css/style.css">'
|
| 20 |
+
b'<link rel="stylesheet" href="overlays.css">'
|
| 21 |
+
b"</head><body>Popover</body></html>"
|
| 22 |
+
)
|
| 23 |
+
out = inject_preview_base(html, "abc", "overlays/popover.html")
|
| 24 |
+
assert b'<base href="/s/abc/overlays/">' in out
|
| 25 |
+
# What a browser resolves against that base:
|
| 26 |
+
assert b"../css/style.css" in out
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_inject_does_not_override_author_base():
|
| 30 |
+
html = b'<html><head><base href="./"></head></html>'
|
| 31 |
+
out = inject_preview_base(html, "abc", "index.html")
|
| 32 |
+
assert out.count(b"<base ") == 1
|
| 33 |
+
assert b'href="./"' in out
|