mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 16:02:12 +00:00
fix(auth,chat): 加固认证逻辑与修复前端交互缺陷
- 移除 verify_api_key 中 public_enabled 无密钥时的免认证后门 - 对 public_key 进行 strip 归一化,避免配置空白导致哈希不匹配 - 前端空密钥时直接缓存空值,移除多余的 verify 网络请求 - 阻止删除正在流式传输中的消息,防止 session 状态不同步 - 编辑消息取消时使用 DOM 克隆恢复,避免 innerHTML 注入风险 - 合并三个全局 click 监听器为一个,减少事件绑定开销
This commit is contained in:
1 parent
e3b3aab4dd
commit
623570ccb4
3 files changed
+37
-39
No files matched your search
+7
-10
@@ -63,10 +63,13 @@ def _match_public_key(credentials: str, public_key: str) -> bool:
|
||||
"""检查凭证是否匹配 public_key(支持原始值和 public-<sha256> 哈希格式)。"""
|
||||
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-<sha256> 哈希格式)。
|
||||
若 public_enabled 且未配置 public_key,允许无认证访问。
|
||||
同时支持通过 public_key 认证(原始值或 public-<sha256> 哈希格式)。
|
||||
"""
|
||||
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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user