feat(安全培训): 新增员工安全培训相关功能

This commit is contained in:
runny committed 2026-08-12 11:56:35 +08:00
1 parent cbc3cb959f
commit 8e5c57790c
50 files changed
+3769 -87

No files matched your search

+26
View File
@@ -8,6 +8,7 @@ api/chat/preview_support.py
from __future__ import annotations
from .shared import * # noqa: F403
from urllib.parse import urljoin
def _normalize_allowed_share_path(path_value: str) -> tuple[str, str]:
@@ -65,6 +66,28 @@ def _is_allowed_share_path(target_url: str) -> bool:
]
return any(root and _is_path_within_allowed_root(normalized_target, root) for root in roots)
def _is_internal_relative_preview_url(target_url: str) -> bool:
"""
判断是否为当前站点内部的根路径预览地址。
用途:培训资料里的知识库文件会下发 `/api/kb/.../source-file` 这类相对地址,
预览代理必须把它视为“当前站点受保护资源”,而不是误判成非法外链。
"""
raw = str(target_url or '').strip()
return raw.startswith('/api/')
def _resolve_internal_preview_url(target_url: str) -> str:
"""
将站内相对预览地址补成完整绝对 URL,供 requests 访问。
AI 触发时机:当预览接口收到 `/api/...` 形式的受保护资源时使用,
必须补齐当前请求 Host,才能把登录态透传给后续内部文件下载接口。
"""
raw = str(target_url or '').strip()
if not raw or not _is_internal_relative_preview_url(raw) or not has_request_context():
return raw
return urljoin(request.host_url, raw)
def _is_allowed_preview_url(target_url: str) -> bool:
"""
预览代理 URL 白名单校验。
@@ -76,6 +99,8 @@ def _is_allowed_preview_url(target_url: str) -> bool:
try:
if _is_allowed_share_path(target_url):
return True
if _is_internal_relative_preview_url(target_url) and has_request_context():
return True
parsed = urlparse(target_url)
if parsed.scheme not in ('http', 'https'):
return False
@@ -117,6 +142,7 @@ def _fetch_preview_source_bytes(file_url: str, max_size: int | None=15 * 1024 *
import time
import sqlalchemy as sa
from urllib.parse import parse_qs
file_url = _resolve_internal_preview_url(file_url)
if _is_allowed_share_path(file_url):
_path_kind, normalized_path = _normalize_allowed_share_path(file_url)
try: