sync update from LAN repo (source master @ c21a2a4a)
This commit is contained in:
1 parent
ea9dcff9c0
commit
66a443e915
140 files changed
+13777
-1040
No files matched your search
@@ -681,6 +681,9 @@ def _embed_allowed_weknora_ids(ctx: WeKnoraEmbedContext) -> tuple[str, ...]:
|
||||
|
||||
|
||||
def _embed_context_payload(ctx: WeKnoraEmbedContext) -> dict[str, Any]:
|
||||
# Keep this payload bounded: it is signed directly into the browser cookie.
|
||||
# The visible knowledge-base list is hydrated only for the embedded page or
|
||||
# its list API call, after the cookie has been verified.
|
||||
return {
|
||||
"mapping_id": ctx.mapping_id,
|
||||
"weknora_id": ctx.weknora_id,
|
||||
@@ -688,11 +691,79 @@ def _embed_context_payload(ctx: WeKnoraEmbedContext) -> dict[str, Any]:
|
||||
"is_admin": ctx.is_admin,
|
||||
"can_write": ctx.can_write,
|
||||
"is_conversation_deposit": ctx.is_conversation_deposit,
|
||||
"allowed_weknora_ids": list(_embed_allowed_weknora_ids(ctx)),
|
||||
"exp": ctx.exp,
|
||||
}
|
||||
|
||||
|
||||
async def _with_visible_embed_knowledge_bases(
|
||||
request: Request,
|
||||
ctx: WeKnoraEmbedContext,
|
||||
) -> WeKnoraEmbedContext:
|
||||
"""Hydrate the iframe-only picker allowlist without putting it in a cookie."""
|
||||
|
||||
visible_rows = await _store(request).list_visible(
|
||||
ctx.user_id,
|
||||
scope="all",
|
||||
# Mirror the regular picker. An administrator must not receive another
|
||||
# user's private bases merely because the iframe uses an admin token.
|
||||
is_admin=False,
|
||||
)
|
||||
return replace(
|
||||
ctx,
|
||||
allowed_weknora_ids=tuple(
|
||||
dict.fromkeys(
|
||||
value
|
||||
for value in (
|
||||
ctx.weknora_id,
|
||||
*(str(item.get("weknora_id") or "") for item in visible_rows),
|
||||
)
|
||||
if value
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _embed_directory_time(value: Any) -> str | None:
|
||||
if isinstance(value, datetime):
|
||||
return value.isoformat()
|
||||
if isinstance(value, str) and value.strip():
|
||||
return value
|
||||
return None
|
||||
|
||||
|
||||
async def _local_embed_knowledge_base_directory(
|
||||
request: Request,
|
||||
ctx: WeKnoraEmbedContext,
|
||||
) -> Response:
|
||||
"""Serve the iframe picker from DeerFlow mappings, never WeKnora's full directory."""
|
||||
|
||||
rows = await _store(request).list_visible(
|
||||
ctx.user_id,
|
||||
scope="all",
|
||||
# Match the normal picker: the WeKnora service-admin token must not
|
||||
# make an administrator's private libraries discoverable to others.
|
||||
is_admin=False,
|
||||
)
|
||||
items = [
|
||||
{
|
||||
"id": str(row.get("weknora_id") or ""),
|
||||
"name": str(row.get("name") or ""),
|
||||
"description": str(row.get("description") or ""),
|
||||
"type": str(row.get("kb_type") or "document"),
|
||||
"status": "active",
|
||||
"created_at": _embed_directory_time(row.get("created_at")),
|
||||
"updated_at": _embed_directory_time(row.get("updated_at")),
|
||||
}
|
||||
for row in rows
|
||||
if str(row.get("weknora_id") or "")
|
||||
]
|
||||
response = _json_response(
|
||||
{"success": True, "data": items, "total": len(items)},
|
||||
)
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return response
|
||||
|
||||
|
||||
def _weknora_id_from_platform_path(path: str) -> str | None:
|
||||
prefix = "/platform/knowledge-bases/"
|
||||
if not path.startswith(prefix):
|
||||
@@ -708,15 +779,13 @@ async def _switch_embed_context(
|
||||
ctx: WeKnoraEmbedContext,
|
||||
weknora_id: str,
|
||||
) -> WeKnoraEmbedContext:
|
||||
"""Resolve a permitted native-UI switch to the exact cmzs mapping.
|
||||
"""Resolve a native-UI switch to the exact cmzs mapping.
|
||||
|
||||
The embedded WeKnora UI uses a service-admin token, so every target remains
|
||||
constrained to an ID signed into this short-lived session and is checked
|
||||
again against the cmzs visibility rules before its page can be proxied.
|
||||
The embedded WeKnora UI uses a service-admin token, so the target must be
|
||||
checked against the current cmzs visibility rules on every switch. This is
|
||||
deliberately dynamic rather than a signed ID list in the browser cookie.
|
||||
"""
|
||||
|
||||
if weknora_id not in _embed_allowed_weknora_ids(ctx):
|
||||
raise HTTPException(status_code=403, detail="Knowledge base is outside the embedded session scope")
|
||||
store = _store(request)
|
||||
mapped = await store.get_by_weknora_id(weknora_id)
|
||||
if mapped is None:
|
||||
@@ -2807,13 +2876,6 @@ async def _prepare_weknora_embed_session(
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
_runtime_for_iframe_proxy(request)
|
||||
row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=False)
|
||||
visible_rows = await _store(request).list_visible(
|
||||
user_id,
|
||||
scope="all",
|
||||
# The iframe must mirror the knowledge-base picker, not grant an
|
||||
# administrator an unlisted user's private knowledge bases.
|
||||
is_admin=False,
|
||||
)
|
||||
writable = None
|
||||
if not _is_conversation_deposit_mapping(row):
|
||||
writable = await _store(request).get_authorized(mapping_id, user_id, write=True, is_admin=is_admin)
|
||||
@@ -2825,7 +2887,6 @@ async def _prepare_weknora_embed_session(
|
||||
is_admin=is_admin,
|
||||
can_write=writable is not None,
|
||||
is_conversation_deposit=_is_conversation_deposit_mapping(row),
|
||||
allowed_weknora_ids=tuple(dict.fromkeys(value for value in (weknora_id, *(str(item.get("weknora_id") or "") for item in visible_rows)) if value)),
|
||||
exp=int(time.time()) + _WEKNORA_EMBED_TTL_SECONDS,
|
||||
)
|
||||
return weknora_id, _embed_context_payload(ctx)
|
||||
@@ -2885,6 +2946,7 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
|
||||
requested_weknora_id = _weknora_id_from_platform_path(requested)
|
||||
if requested_weknora_id is not None:
|
||||
selected_ctx = await _switch_embed_context(request, ctx, requested_weknora_id)
|
||||
selected_ctx = await _with_visible_embed_knowledge_bases(request, selected_ctx)
|
||||
response = await _proxy_weknora_request(
|
||||
request,
|
||||
target_base_url=_runtime_for_iframe_proxy(request).web_base_url,
|
||||
@@ -2909,6 +2971,7 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
|
||||
public_target = _with_public_prefix(_request_public_prefix(request), allowed_path)
|
||||
return RedirectResponse(f"{public_target}?{target_query}", status_code=302)
|
||||
runtime = _runtime_for_iframe_proxy(request)
|
||||
ctx = await _with_visible_embed_knowledge_bases(request, ctx)
|
||||
return await _proxy_weknora_request(
|
||||
request,
|
||||
target_base_url=runtime.web_base_url,
|
||||
@@ -2958,6 +3021,8 @@ async def proxy_weknora_api(request: Request, proxied_path: str) -> Response:
|
||||
if request.method.upper() == "OPTIONS":
|
||||
return Response(status_code=204)
|
||||
ctx = await _read_embed_context(request)
|
||||
if request.method.upper() in {"GET", "HEAD"} and proxied_path.rstrip("/") == "knowledge-bases":
|
||||
return await _local_embed_knowledge_base_directory(request, ctx)
|
||||
runtime = _runtime_for_iframe_proxy(request)
|
||||
return await _proxy_weknora_request(
|
||||
request,
|
||||
@@ -2982,6 +3047,8 @@ async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Respon
|
||||
if request.method.upper() == "OPTIONS":
|
||||
return Response(status_code=204)
|
||||
ctx = await _read_embed_context(request)
|
||||
if request.method.upper() in {"GET", "HEAD"} and proxied_path.rstrip("/") == "knowledge-bases":
|
||||
return await _local_embed_knowledge_base_directory(request, ctx)
|
||||
runtime = _runtime_for_iframe_proxy(request)
|
||||
return await _proxy_weknora_request(
|
||||
request,
|
||||
|
||||
Reference in new issue
Block a user