From 623570ccb4d2ffbc0c8fdf84e4180fa676a17320 Mon Sep 17 00:00:00 2001 From: piexian <64474352+piexian@users.noreply.github.com> Date: Sun, 1 Mar 2026 03:36:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth,chat):=20=E5=8A=A0=E5=9B=BA=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E9=80=BB=E8=BE=91=E4=B8=8E=E4=BF=AE=E5=A4=8D=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E4=BA=A4=E4=BA=92=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 verify_api_key 中 public_enabled 无密钥时的免认证后门 - 对 public_key 进行 strip 归一化,避免配置空白导致哈希不匹配 - 前端空密钥时直接缓存空值,移除多余的 verify 网络请求 - 阻止删除正在流式传输中的消息,防止 session 状态不同步 - 编辑消息取消时使用 DOM 克隆恢复,避免 innerHTML 注入风险 - 合并三个全局 click 监听器为一个,减少事件绑定开销 --- app/core/auth.py | 17 +++++------ app/static/common/js/admin-auth.js | 12 ++------ app/static/public/js/chat.js | 47 ++++++++++++++++++------------ 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/app/core/auth.py b/app/core/auth.py index d01f54da..634e2e93 100644 --- a/app/core/auth.py +++ b/app/core/auth.py @@ -63,10 +63,13 @@ def _match_public_key(credentials: str, public_key: str) -> bool: """检查凭证是否匹配 public_key(支持原始值和 public- 哈希格式)。""" if not public_key: return False - if credentials == public_key: + normalized = public_key.strip() + if not normalized: + return False + if credentials == normalized: return True if credentials.startswith("public-"): - expected_hash = _hash_public_key(public_key) + expected_hash = _hash_public_key(normalized) if credentials == f"public-{expected_hash}": return True return False @@ -79,20 +82,13 @@ async def verify_api_key( 验证 Bearer Token 如果 config.toml 中未配置 api_key,则不启用认证。 - 同时支持面板通过 public_key 认证(原始值或 public- 哈希格式)。 - 若 public_enabled 且未配置 public_key,允许无认证访问。 + 同时支持通过 public_key 认证(原始值或 public- 哈希格式)。 """ api_key = get_admin_api_key() - public_key = get_public_api_key() - public_enabled = is_public_enabled() - if not api_key: return None - # 无认证头:仅在 public_enabled 且无 public_key 时放行 if not auth: - if public_enabled and not public_key: - return None raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing authentication token", @@ -104,6 +100,7 @@ async def verify_api_key( return auth.credentials # public_key 验证(原始值或哈希) + public_key = get_public_api_key() if _match_public_key(auth.credentials, public_key): return auth.credentials diff --git a/app/static/common/js/admin-auth.js b/app/static/common/js/admin-auth.js index 4022c06c..195223d0 100644 --- a/app/static/common/js/admin-auth.js +++ b/app/static/common/js/admin-auth.js @@ -190,16 +190,8 @@ async function ensurePublicKey() { const key = await getStoredPublicKey(); if (!key) { - try { - const ok = await verifyKey('/v1/public/verify', ''); - if (ok) { - cachedPublicKey = ''; - return cachedPublicKey; - } - } catch (e) { - // ignore - } - return null; + cachedPublicKey = ''; + return cachedPublicKey; } try { diff --git a/app/static/public/js/chat.js b/app/static/public/js/chat.js index 58539680..17c8880d 100644 --- a/app/static/public/js/chat.js +++ b/app/static/public/js/chat.js @@ -336,6 +336,12 @@ if (emptyState) emptyState.classList.remove('hidden'); } + function setRenderedHTML(el, html) { + const doc = new DOMParser().parseFromString(html, 'text/html'); + el.textContent = ''; + Array.from(doc.body.childNodes).forEach(n => el.appendChild(document.adoptNode(n))); + } + function escapeHtml(value) { return value .replace(/&/g, '&') @@ -664,6 +670,7 @@ function deleteMessageByRow(row) { if (!row || !chatLog) return; + if (activeStreamInfo && activeStreamInfo.entry.row === row) return; const rows = chatLog.querySelectorAll('.message-row'); const idx = Array.from(rows).indexOf(row); if (idx === -1 || idx >= messageHistory.length) return; @@ -703,7 +710,7 @@ btnWrap.appendChild(saveBtn); btnWrap.appendChild(cancelBtn); - const originalHTML = contentNode.innerHTML; + const savedChildren = Array.from(contentNode.childNodes).map(n => n.cloneNode(true)); const originalClass = contentNode.className; contentNode.className = 'message-content'; contentNode.innerHTML = ''; @@ -728,9 +735,10 @@ } msg.content = newText; contentNode.className = originalClass; + contentNode.textContent = ''; if (msg.role === 'assistant') { contentNode.classList.add('rendered'); - contentNode.innerHTML = renderMarkdown(newText); + setRenderedHTML(contentNode, renderMarkdown(newText)); } else { contentNode.textContent = newText; } @@ -745,7 +753,8 @@ function cancel() { contentNode.className = originalClass; - contentNode.innerHTML = originalHTML; + contentNode.textContent = ''; + savedChildren.forEach(n => contentNode.appendChild(n)); finish(); } } @@ -984,10 +993,10 @@ } if (finalize) { entry.contentNode.classList.add('rendered'); - entry.contentNode.innerHTML = renderMarkdown(entry.raw); + setRenderedHTML(entry.contentNode, renderMarkdown(entry.raw)); } else { if (entry.role === 'assistant') { - entry.contentNode.innerHTML = renderMarkdown(entry.raw); + setRenderedHTML(entry.contentNode, renderMarkdown(entry.raw)); } else { entry.contentNode.textContent = entry.raw; } @@ -1633,21 +1642,21 @@ }); } document.addEventListener('click', (event) => { - if (!settingsPanel || settingsPanel.classList.contains('hidden')) return; - if (settingsPanel.contains(event.target) || (settingsToggle && settingsToggle.contains(event.target))) { - return; + if (settingsPanel && !settingsPanel.classList.contains('hidden')) { + if (!settingsPanel.contains(event.target) && !(settingsToggle && settingsToggle.contains(event.target))) { + toggleSettings(false); + } + } + if (modelDropdown && !modelDropdown.classList.contains('hidden')) { + if (!(modelChip && modelChip.contains(event.target))) { + toggleModelDropdown(false); + } + } + if (reasoningDropdown && !reasoningDropdown.classList.contains('hidden')) { + if (!(reasoningChip && reasoningChip.contains(event.target))) { + toggleReasoningDropdown(false); + } } - toggleSettings(false); - }); - document.addEventListener('click', (event) => { - if (!modelDropdown || modelDropdown.classList.contains('hidden')) return; - if (modelChip && modelChip.contains(event.target)) return; - toggleModelDropdown(false); - }); - document.addEventListener('click', (event) => { - if (!reasoningDropdown || reasoningDropdown.classList.contains('hidden')) return; - if (reasoningChip && reasoningChip.contains(event.target)) return; - toggleReasoningDropdown(false); }); if (promptInput) { let composing = false;