fix: harden WeKnora proxy and configurable search
This commit is contained in:
1 parent
b527bc94bb
commit
11e54550ed
10 files changed
+721
-72
No files matched your search
@@ -5,7 +5,7 @@ from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from starlette.datastructures import Headers, QueryParams
|
||||
from starlette.requests import Request
|
||||
|
||||
@@ -178,6 +178,18 @@ def test_weknora_embed_cookie_bypasses_only_proxy_paths() -> None:
|
||||
)
|
||||
assert auth_middleware._is_weknora_embed_proxy_request(_request("/assets/index.js", cookies=cookies))
|
||||
assert auth_middleware._is_weknora_embed_proxy_request(_request("/deerflow/assets/index.js", cookies=cookies))
|
||||
for static_path in (
|
||||
"/deerflow/css/app.css",
|
||||
"/deerflow/fonts/app.woff2",
|
||||
"/deerflow/img/logo.png",
|
||||
"/deerflow/images/empty.svg",
|
||||
"/deerflow/js/app.js",
|
||||
"/deerflow/media/intro.mp4",
|
||||
"/deerflow/static/chunk.js",
|
||||
):
|
||||
request = _request(static_path, cookies=cookies)
|
||||
assert auth_middleware._is_weknora_embed_proxy_request(request)
|
||||
assert csrf_middleware.is_weknora_embed_proxy_request(request)
|
||||
assert auth_middleware._is_weknora_embed_proxy_request(_request("/config.js", cookies=cookies))
|
||||
assert auth_middleware._is_weknora_embed_proxy_request(
|
||||
_request("/tdesign-icons/0.4.1/fonts/index.js", cookies=cookies)
|
||||
@@ -1054,7 +1066,175 @@ def test_weknora_iframe_proxy_is_fallback_after_deerflow_api_routes() -> None:
|
||||
== "/deerflow/platform/{proxied_path:path}"
|
||||
)
|
||||
assert _first_gateway_match("GET", "/deerflow/assets/index.js") == "/deerflow/assets/{proxied_path:path}"
|
||||
assert _first_gateway_match("GET", "/deerflow/js/index.js") == "/deerflow/{proxied_path:path}"
|
||||
assert _first_gateway_match("GET", "/deerflow/css/index.css") == "/deerflow/{proxied_path:path}"
|
||||
assert _first_gateway_match("GET", "/deerflow/fonts/index.woff2") == "/deerflow/{proxied_path:path}"
|
||||
assert _first_gateway_match("GET", "/deerflow/api/v1/knowledge-search") == "/deerflow/api/v1/{proxied_path:path}"
|
||||
assert _first_gateway_match("GET", "/js/index.js") == "/{proxied_path:path}"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("location", "target_base_url", "expected"),
|
||||
[
|
||||
("/platform/knowledge-bases/kb?tab=wiki", "http://weknora-web:8080", "/deerflow/platform/knowledge-bases/kb?tab=wiki"),
|
||||
("/deerflow/platform/knowledge-bases/kb", "http://weknora-web:8080", "/deerflow/platform/knowledge-bases/kb"),
|
||||
("/deerflow-api/api/v1/auth/me", "http://gateway/deerflow-api", "/deerflow/api/v1/auth/me"),
|
||||
(
|
||||
"http://gateway/deerflow-api/api/v1/auth/me?next=1#top",
|
||||
"http://gateway/deerflow-api",
|
||||
"/deerflow/api/v1/auth/me?next=1#top",
|
||||
),
|
||||
("https://external.test/login", "http://gateway/deerflow-api", "https://external.test/login"),
|
||||
],
|
||||
)
|
||||
def test_weknora_redirect_location_is_normalized_for_public_prefix(
|
||||
location: str,
|
||||
target_base_url: str,
|
||||
expected: str,
|
||||
) -> None:
|
||||
from app.gateway.routers import llmwiki as llmwiki_router
|
||||
|
||||
assert llmwiki_router._rewrite_weknora_location_header(location, "/deerflow", target_base_url) == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prefixed_weknora_proxy_serves_page_static_assets_and_api_without_auth_errors(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from app.gateway.auth_middleware import AuthMiddleware
|
||||
from app.gateway.csrf_middleware import CSRFMiddleware
|
||||
from app.gateway.routers import llmwiki as llmwiki_router
|
||||
from app.gateway.weknora_embed import WEKNORA_EMBED_COOKIE, sign_weknora_embed_payload
|
||||
|
||||
class Store:
|
||||
async def get_authorized(self, *_: object, **__: object) -> dict[str, str]:
|
||||
return {"id": "local-kb", "weknora_id": "remote-kb"}
|
||||
|
||||
runtime = SimpleNamespace(
|
||||
web_base_url="http://weknora-web.test",
|
||||
api_base_url="http://weknora-api.test",
|
||||
)
|
||||
upstream_requests: list[tuple[str, str, dict[str, str]]] = []
|
||||
|
||||
async def fake_session(_: object, *, force_refresh: bool = False) -> dict[str, object]:
|
||||
return {"token": "admin-token", "tenant": {"id": "tenant-1"}}
|
||||
|
||||
class FakeUpstreamClient:
|
||||
def __init__(self, **_: object) -> None:
|
||||
pass
|
||||
|
||||
async def __aenter__(self) -> FakeUpstreamClient:
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *_: object) -> None:
|
||||
return None
|
||||
|
||||
async def request(
|
||||
self,
|
||||
method: str,
|
||||
url: str,
|
||||
*,
|
||||
headers: dict[str, str],
|
||||
**__: object,
|
||||
) -> httpx.Response:
|
||||
upstream_requests.append((method, url, headers))
|
||||
path = httpx.URL(url).path
|
||||
if url.startswith(runtime.api_base_url):
|
||||
return httpx.Response(200, json={"success": True, "data": {"id": "admin"}})
|
||||
if path == "/platform/knowledge-bases/remote-kb":
|
||||
return httpx.Response(
|
||||
200,
|
||||
text=(
|
||||
'<html><head><script src="/assets/main.js"></script>'
|
||||
'<link rel="stylesheet" href="css/app.css">'
|
||||
'<script src="/config.js"></script></head><body></body></html>'
|
||||
),
|
||||
headers={"content-type": "text/html; charset=utf-8"},
|
||||
)
|
||||
if path == "/assets/main.js":
|
||||
return httpx.Response(
|
||||
200,
|
||||
text=(
|
||||
'const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=['
|
||||
'"assets/lazy.css","assets/lazy.js"])))=>i.map(i=>d[i]);'
|
||||
'const chunk="/static/chunk.js",matcher=/platform\\//;fetch("/api/v1/auth/me")'
|
||||
),
|
||||
headers={"content-type": "application/javascript"},
|
||||
)
|
||||
if path == "/css/app.css":
|
||||
return httpx.Response(
|
||||
200,
|
||||
text='@font-face{src:url("/fonts/app.woff2")}',
|
||||
headers={"content-type": "text/css"},
|
||||
)
|
||||
if path in {"/config.js", "/static/chunk.js", "/assets/deep.js"}:
|
||||
return httpx.Response(200, text=f'window.loaded="{path}"', headers={"content-type": "application/javascript"})
|
||||
if path == "/fonts/app.woff2":
|
||||
return httpx.Response(200, content=b"font", headers={"content-type": "font/woff2"})
|
||||
return httpx.Response(404, text=f"missing upstream path: {path}")
|
||||
|
||||
monkeypatch.setattr(llmwiki_router, "_runtime_for_iframe_proxy", lambda _: runtime)
|
||||
monkeypatch.setattr(llmwiki_router, "_store", lambda _: Store())
|
||||
monkeypatch.setattr(llmwiki_router, "_get_weknora_admin_session", fake_session)
|
||||
|
||||
real_async_client = httpx.AsyncClient
|
||||
monkeypatch.setattr(llmwiki_router.httpx, "AsyncClient", FakeUpstreamClient)
|
||||
|
||||
test_app = FastAPI()
|
||||
test_app.add_middleware(AuthMiddleware)
|
||||
test_app.add_middleware(CSRFMiddleware)
|
||||
test_app.include_router(llmwiki_router.router)
|
||||
test_app.include_router(llmwiki_router.proxy_router, prefix="/deerflow")
|
||||
test_app.include_router(llmwiki_router.proxy_router)
|
||||
|
||||
cookie = sign_weknora_embed_payload(
|
||||
{
|
||||
"mapping_id": "local-kb",
|
||||
"weknora_id": "remote-kb",
|
||||
"user_id": "user-a",
|
||||
"is_admin": False,
|
||||
"can_write": True,
|
||||
"exp": 4_102_444_800,
|
||||
}
|
||||
)
|
||||
transport = httpx.ASGITransport(app=test_app)
|
||||
async with real_async_client(transport=transport, base_url="http://deerflow.test") as client:
|
||||
client.cookies.set(WEKNORA_EMBED_COOKIE, cookie)
|
||||
|
||||
page = await client.get("/deerflow/platform/knowledge-bases/remote-kb?tab=wiki")
|
||||
assert page.status_code == 200
|
||||
assert 'src="/deerflow/assets/main.js"' in page.text
|
||||
assert 'href="/deerflow/css/app.css"' in page.text
|
||||
assert 'src="/deerflow/config.js"' in page.text
|
||||
|
||||
script = await client.get("/deerflow/assets/main.js")
|
||||
assert script.status_code == 200
|
||||
assert '"/deerflow/static/chunk.js"' in script.text
|
||||
assert '"deerflow/assets/lazy.css"' in script.text
|
||||
assert '"deerflow/assets/lazy.js"' in script.text
|
||||
assert '"/deerflow/assets/lazy.css"' not in script.text
|
||||
assert 'matcher=/platform\\//' in script.text
|
||||
assert 'fetch("/deerflow/api/v1/auth/me")' in script.text
|
||||
|
||||
stylesheet = await client.get("/deerflow/css/app.css")
|
||||
assert stylesheet.status_code == 200
|
||||
assert 'url("/deerflow/fonts/app.woff2")' in stylesheet.text
|
||||
|
||||
for path in (
|
||||
"/deerflow/config.js",
|
||||
"/deerflow/static/chunk.js",
|
||||
"/deerflow/fonts/app.woff2",
|
||||
"/deerflow/platform/knowledge-bases/remote-kb/assets/deep.js",
|
||||
):
|
||||
response = await client.get(path)
|
||||
assert response.status_code == 200, f"{path}: {response.status_code} {response.text}"
|
||||
|
||||
api = await client.get("/deerflow/api/llmwiki/weknora-embed/api/v1/auth/me")
|
||||
assert api.status_code == 200
|
||||
|
||||
api_request = next(request for request in upstream_requests if request[1].startswith(runtime.api_base_url))
|
||||
assert api_request[2]["Authorization"] == "Bearer admin-token"
|
||||
assert api_request[2]["X-Tenant-ID"] == "tenant-1"
|
||||
|
||||
|
||||
def test_weknora_iframe_hides_current_outer_sidebar_shell() -> None:
|
||||
@@ -1225,6 +1405,15 @@ async def test_weknora_iframe_proxy_blocks_other_knowledge_bases() -> None:
|
||||
"token",
|
||||
)
|
||||
|
||||
await llmwiki_router._authorize_weknora_api_proxy(
|
||||
FakeRequest(),
|
||||
ctx,
|
||||
object(),
|
||||
"/api/v1/me/invitations/pending-count",
|
||||
b"",
|
||||
"token",
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await llmwiki_router._authorize_weknora_api_proxy(
|
||||
FakeRequest(),
|
||||
|
||||
Reference in new issue
Block a user