fix: harden WeKnora proxy and configurable search

This commit is contained in:
Administrator committed 2026-08-13 23:44:47 +08:00
1 parent b527bc94bb
commit 11e54550ed
10 files changed
+721 -72

No files matched your search

@@ -1646,8 +1646,11 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# WeKnora's embedded frontend uses root-relative routes such as /platform,
# /assets, /locales and /api/v1/*. Register that proxy last so it stays a
# true fallback and cannot intercept DeerFlow APIs.
app.include_router(llmwiki.proxy_router)
# Register the public-prefix routes before the unprefixed static fallback.
# Otherwise proxy_router's /{proxied_path:path} route consumes every
# /deerflow/* request before the prefixed platform/assets routes can match.
app.include_router(llmwiki.proxy_router, prefix="/deerflow")
app.include_router(llmwiki.proxy_router)
return app
@@ -49,6 +49,83 @@ proxy_router = APIRouter(tags=["llmwiki-proxy"])
_MAX_UPLOAD_BYTES = 100 * 1024 * 1024
_WEKNORA_ADMIN_SESSION_CACHE: dict[str, dict[str, Any]] = {}
_WEKNORA_PUBLIC_PREFIX = "/deerflow"
_WEKNORA_PUBLIC_ROOT_PATHS = (
"/platform",
"/assets",
"/css",
"/fonts",
"/img",
"/images",
"/js",
"/locales",
"/media",
"/static",
"/tdesign-icons",
"/api/v1",
"/favicon.ico",
"/favicon.svg",
"/config.js",
"/logo.svg",
"/manifest.webmanifest",
)
_WEKNORA_PUBLIC_RELATIVE_ROOT_PATHS = (
"assets",
"css",
"fonts",
"img",
"images",
"js",
"locales",
"media",
"static",
"tdesign-icons",
)
_WEKNORA_PUBLIC_RELATIVE_FILE_PATHS = (
"config.js",
"favicon.ico",
"favicon.svg",
"logo.svg",
"manifest.webmanifest",
)
_WEKNORA_STATIC_FILE_SUFFIXES = (
".avif",
".css",
".gif",
".ico",
".jpeg",
".jpg",
".js",
".json",
".map",
".mjs",
".png",
".svg",
".ttf",
".txt",
".webmanifest",
".webp",
".woff",
".woff2",
)
_WEKNORA_STATIC_ROOT_PREFIXES = (
"/assets/",
"/css/",
"/fonts/",
"/img/",
"/images/",
"/js/",
"/locales/",
"/media/",
"/static/",
"/tdesign-icons/",
)
_WEKNORA_ROOT_ASSET_FILES = (
"/config.js",
"/favicon.ico",
"/favicon.svg",
"/logo.svg",
"/manifest.webmanifest",
)
_HOP_BY_HOP_RESPONSE_HEADERS = {
"connection",
"content-encoding",
@@ -399,6 +476,9 @@ async def _authorize_weknora_api_proxy(
return
raise HTTPException(status_code=403, detail="Only WeKnora session introspection is allowed")
if read_method and tail == ["me", "invitations", "pending-count"]:
return
if tail[:1] == ["knowledge-bases"]:
if len(tail) == 1:
if method == "GET":
@@ -535,30 +615,151 @@ def _without_public_prefix(prefix: str, path: str) -> str:
return path
def _rewrite_weknora_html_public_paths(html: str, public_prefix: str) -> str:
def _rewrite_weknora_html_public_paths(
html: str,
public_prefix: str,
*,
rewrite_relative: bool = True,
) -> str:
if not public_prefix:
return html
for path in (
"/platform",
"/assets",
"/locales",
"/tdesign-icons",
"/api/v1",
"/favicon.ico",
"/favicon.svg",
"/config.js",
"/logo.svg",
"/manifest.webmanifest",
):
for path in _WEKNORA_PUBLIC_ROOT_PATHS:
prefixed = f"{public_prefix}{path}"
html = re.sub(
rf'(?P<q>["\'(=]){re.escape(path)}(?P<next>[/?"\'\)])',
rf"\g<q>{prefixed}\g<next>",
rf"(?P<prefix>[\"'`(=:\s]){re.escape(path)}(?=[/?#\"'`),;\]\}}\s>]|$)",
rf"\g<prefix>{prefixed}",
html,
)
escaped_path = rf"\\/{re.escape(path.lstrip('/'))}"
escaped_prefixed = f"\\/{prefixed.lstrip('/')}"
html = re.sub(
rf"(?<!\\/{re.escape(public_prefix.lstrip('/'))}){escaped_path}(?=[\\/?#\"'`),;\]\}}\s>]|$)",
escaped_prefixed,
html,
)
if rewrite_relative:
for path in _WEKNORA_PUBLIC_RELATIVE_ROOT_PATHS:
prefixed = f"{public_prefix}/{path}"
html = re.sub(
rf"(?P<prefix>[\"'`(=:\s])(?:\./)?{re.escape(path)}(?=/)",
rf"\g<prefix>{prefixed}",
html,
)
for path in _WEKNORA_PUBLIC_RELATIVE_FILE_PATHS:
prefixed = f"{public_prefix}/{path}"
html = re.sub(
rf"(?P<prefix>[\"'`(=:\s])(?:\./)?{re.escape(path)}(?=[?#\"'`),;\]\}}\s>]|$)",
rf"\g<prefix>{prefixed}",
html,
)
return html
def _rewrite_weknora_script_public_paths(text: str, public_prefix: str) -> str:
if not public_prefix:
return text
for path in _WEKNORA_PUBLIC_ROOT_PATHS:
prefixed = f"{public_prefix}{path}"
text = re.sub(
rf"(?P<quote>[\"'`]){re.escape(path)}(?=[/?#\"'`),;\]\}}\s]|$)",
rf"\g<quote>{prefixed}",
text,
)
return _rewrite_weknora_vite_dependency_map(text, public_prefix)
def _should_rewrite_weknora_static_response(content_type: str, upstream_path: str) -> bool:
lower_type = content_type.lower()
if any(
marker in lower_type
for marker in (
"javascript",
"text/css",
"application/json",
"manifest",
"image/svg+xml",
)
):
return True
lower_path = upstream_path.lower()
return lower_path.endswith((".js", ".css", ".json", ".webmanifest", ".svg"))
def _rewrite_weknora_vite_dependency_map(text: str, public_prefix: str) -> str:
if not public_prefix or "__vite__mapDeps" not in text:
return text
marker = "m.f||(m.f=["
start = text.find(marker)
if start < 0:
return text
end = text.find("]))", start + len(marker))
if end < 0:
return text
prefix = public_prefix.strip("/")
dependency_map = text[start:end]
dependency_map = re.sub(
r'(?P<quote>["\'])assets/',
rf"\g<quote>{prefix}/assets/",
dependency_map,
)
return f"{text[:start]}{dependency_map}{text[end:]}"
def _weknora_static_fallback_upstream_path(request: Request) -> str | None:
path = _without_public_prefix(
_request_public_prefix(request),
str(request.scope.get("path") or request.url.path),
)
if not path.startswith("/"):
path = f"/{path}"
lower_path = path.lower()
if lower_path.startswith("/api/"):
return None
if lower_path.startswith(_WEKNORA_STATIC_ROOT_PREFIXES):
return path
for root_prefix in _WEKNORA_STATIC_ROOT_PREFIXES:
index = lower_path.find(root_prefix)
if index > 0:
return path[index:]
for asset_file in _WEKNORA_ROOT_ASSET_FILES:
if lower_path.endswith(asset_file):
return asset_file
if lower_path.endswith(_WEKNORA_STATIC_FILE_SUFFIXES):
return path
return None
def _rewrite_weknora_location_header(location: str, public_prefix: str, target_base_url: str) -> str:
if not location or not public_prefix:
return location
try:
current = urlsplit(location)
upstream = urlsplit(target_base_url)
except ValueError:
return location
def proxied_path(path: str) -> str:
upstream_base_path = upstream.path.rstrip("/")
if upstream_base_path and (path == upstream_base_path or path.startswith(f"{upstream_base_path}/")):
path = path[len(upstream_base_path) :] or "/"
return _without_public_prefix(public_prefix, path)
suffix = ""
if current.query:
suffix += f"?{current.query}"
if current.fragment:
suffix += f"#{current.fragment}"
if not current.scheme and location.startswith("/"):
path = proxied_path(current.path or "/")
return f"{public_prefix}{path}{suffix}"
if current.scheme and current.netloc and current.netloc == upstream.netloc:
path = proxied_path(current.path or "/")
return f"{public_prefix}{path}{suffix}"
return location
def _weknora_embed_injection(
*,
allowed_path: str,
@@ -1058,6 +1259,7 @@ async def _proxy_weknora_request(
content = response.content
content_type = response.headers.get("content-type", "")
public_prefix = _request_public_prefix(request)
if is_api and request.method.upper() == "GET" and upstream_path.rstrip("/") == "/api/v1/knowledge-bases":
content = _filter_knowledge_base_list_response(response, ctx) or content
if is_api and ctx.is_conversation_deposit and "application/json" in content_type.lower():
@@ -1072,7 +1274,6 @@ async def _proxy_weknora_request(
separators=(",", ":"),
).encode("utf-8")
if not is_api and "text/html" in content_type.lower():
public_prefix = _request_public_prefix(request)
allowed_path = _with_public_prefix(
public_prefix,
f"/platform/knowledge-bases/{quote(ctx.weknora_id, safe='')}",
@@ -1086,8 +1287,35 @@ async def _proxy_weknora_request(
public_prefix=public_prefix,
)
content_type = "text/html; charset=utf-8"
elif not is_api and _should_rewrite_weknora_static_response(content_type, upstream_path):
text = content.decode("utf-8", errors="replace")
lower_type = content_type.lower()
if "javascript" in lower_type or upstream_path.lower().endswith((".js", ".mjs")):
text = _rewrite_weknora_script_public_paths(text, public_prefix)
else:
text = _rewrite_weknora_html_public_paths(
text,
public_prefix,
rewrite_relative=False,
)
content = text.encode("utf-8")
headers_out = _safe_response_headers(response.headers)
if "location" in headers_out:
headers_out["location"] = _rewrite_weknora_location_header(
headers_out["location"],
public_prefix,
target_base_url,
)
if "Location" in headers_out:
headers_out["Location"] = _rewrite_weknora_location_header(
headers_out["Location"],
public_prefix,
target_base_url,
)
if not is_api:
headers_out.pop("etag", None)
headers_out.pop("ETag", None)
headers_out["Cache-Control"] = "no-store" if is_api or "text/html" in content_type.lower() else "private, max-age=3600"
return Response(
content=content,
@@ -1493,6 +1721,16 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
requested = f"/platform/{proxied_path}".rstrip("/")
allowed_path = f"/platform/knowledge-bases/{ctx.weknora_id}"
if requested != allowed_path:
static_upstream_path = _weknora_static_fallback_upstream_path(request)
if static_upstream_path is not None:
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
target_base_url=runtime.web_base_url,
upstream_path=static_upstream_path,
ctx=ctx,
is_api=False,
)
target_query = urlencode({"tab": request.query_params.get("tab") or "wiki"})
public_target = _with_public_prefix(_request_public_prefix(request), allowed_path)
return RedirectResponse(f"{public_target}?{target_query}", status_code=302)
@@ -1588,6 +1826,10 @@ async def proxy_weknora_api(request: Request, proxied_path: str) -> Response:
"/api/llmwiki/weknora-embed/api/v1/{proxied_path:path}",
methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
)
@router.api_route(
"/deerflow/api/llmwiki/weknora-embed/api/v1/{proxied_path:path}",
methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
)
async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Response:
"""Proxy iframe API calls under a collision-free cmzs namespace."""
@@ -1604,6 +1846,22 @@ async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Respon
)
@proxy_router.api_route("/{proxied_path:path}", methods=["GET", "HEAD"])
async def proxy_weknora_static_fallback(request: Request, proxied_path: str) -> Response:
upstream_path = _weknora_static_fallback_upstream_path(request)
if upstream_path is None:
raise HTTPException(status_code=404, detail="WeKnora proxy path not found")
ctx = await _read_embed_context(request)
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
target_base_url=runtime.web_base_url,
upstream_path=upstream_path,
ctx=ctx,
is_api=False,
)
@router.get("/api/llmwiki/runtime")
async def get_llmwiki_runtime() -> dict[str, Any]:
return _runtime_or_legacy()
@@ -28,6 +28,18 @@ WEKNORA_ROOT_ASSET_PATHS: frozenset[str] = frozenset(
"/manifest.webmanifest",
}
)
WEKNORA_STATIC_PATH_PREFIXES: tuple[str, ...] = (
"/assets/",
"/css/",
"/fonts/",
"/img/",
"/images/",
"/js/",
"/locales/",
"/media/",
"/static/",
"/tdesign-icons/",
)
def _load_weknora_proxy_secret() -> str:
@@ -71,9 +83,7 @@ def is_weknora_embed_proxy_path(path: str) -> bool:
return (
path == "/platform"
or path.startswith("/platform/")
or path.startswith("/assets/")
or path.startswith("/locales/")
or path.startswith("/tdesign-icons/")
or path.startswith(WEKNORA_STATIC_PATH_PREFIXES)
or path in WEKNORA_ROOT_ASSET_PATHS
or path.startswith(f"{WEKNORA_EMBED_API_PREFIX}/api/v1/")
or (path.startswith("/api/v1/") and not path.startswith("/api/v1/auth/"))