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;