(null)
@@ -135,7 +146,13 @@ export function KnowledgeBaseBinding() {
const busyNow = busy[env]
const notice = notices[env]
- const secure = typeof window !== 'undefined' && secretEntryAllowed(window.location)
+ const console = consoleLocation ?? (typeof window !== 'undefined' ? window.location : { protocol: '', hostname: '' })
+ // The server decides whether it will accept a key on this connection; the
+ // browser URL cannot tell a trusted TLS proxy from an untrusted one.
+ const secretEntryAllowed = snapshot?.secret_entry_allowed
+ const secure = secretEntryAllowed === true
+ const plaintext = secure && plaintextRemoteConsole(console)
+ const untrustedProxy = secretEntryAllowed === false && console.protocol === 'https:'
const storageReady = Boolean(snapshot?.storage_ready)
const keyAction = saved && draft ? keyActionFor(saved, draft.baseURL, draft.replaceKey) : 'replace'
const keyReady = keyAction === 'keep' ? Boolean(saved?.key_configured) : draft?.apiKey.trim() !== ''
@@ -518,9 +535,20 @@ export function KnowledgeBaseBinding() {
)}
- {!secure ? (
+ {untrustedProxy ? (
+
+ 服务器未信任本控制台的 TLS 反代:HTTPS 在反代终止时需 QA_AI_TRUST_TLS_PROXY=true,并传递
+ X-Forwarded-Proto。
+
+ ) : !secure ? (
当前不是安全密钥录入连接:请用有效 HTTPS 或本机 localhost 录入新密钥。
) : null}
+ {plaintext ? (
+
+ 当前通过明文 HTTP 录入:密钥会以明文经过这段内网。该部署已放开私网
+ HTTP(WEKNORA_ALLOW_PRIVATE_HTTP=true)。
+
+ ) : null}
{saved.base_url && draft.baseURL.trim() !== saved.base_url && saved.key_configured ? (
已更换服务地址:必须为新地址重新录入 API Key,旧密钥不会发往新地址。
) : null}
diff --git a/web/src/knowledgeBaseBindings.test.ts b/web/src/knowledgeBaseBindings.test.ts
index 97237b4..342fc94 100644
--- a/web/src/knowledgeBaseBindings.test.ts
+++ b/web/src/knowledgeBaseBindings.test.ts
@@ -39,11 +39,13 @@ describe('knowledgeBaseBindings', () => {
data: {
environments: [binding('test'), binding('production')],
storage_ready: true,
+ secret_entry_allowed: true,
},
})
expect(parsed.test.base_url).toBe('https://test.example')
expect(parsed.production.internal_kb_name).toBe('内部资料')
expect(parsed.storage_ready).toBe(true)
+ expect(parsed.secret_entry_allowed).toBe(true)
const asRecord = parsed.test as unknown as Record
expect(asRecord.api_key).toBeUndefined()
expect(asRecord.key_ciphertext).toBeUndefined()
@@ -53,6 +55,24 @@ describe('knowledgeBaseBindings', () => {
expect(() => parseKBBindingSnapshot({ data: { environments: [binding('test')], storage_ready: true } })).toThrow()
})
+ it('leaves the capability absent when an older API does not report it', () => {
+ const parsed = parseKBBindingSnapshot({
+ data: { environments: [binding('test'), binding('production')], storage_ready: true },
+ })
+ expect(parsed.secret_entry_allowed).toBeUndefined()
+ })
+
+ it('keeps an explicit refusal distinguishable from a missing field', () => {
+ const parsed = parseKBBindingSnapshot({
+ data: {
+ environments: [binding('test'), binding('production')],
+ storage_ready: true,
+ secret_entry_allowed: false,
+ },
+ })
+ expect(parsed.secret_entry_allowed).toBe(false)
+ })
+
it('shows a short id next to the name so same-name bases are distinguishable', () => {
expect(shortKnowledgeBaseID('0b217e8a-1234-5678')).toBe('0b217e8a…')
expect(knowledgeBaseLabel('0b217e8a-1234-5678', '客户帮助中心')).toBe('客户帮助中心(0b217e8a…)')
diff --git a/web/src/knowledgeBaseBindings.ts b/web/src/knowledgeBaseBindings.ts
index aedd7f8..3373e52 100644
--- a/web/src/knowledgeBaseBindings.ts
+++ b/web/src/knowledgeBaseBindings.ts
@@ -34,6 +34,9 @@ export interface KBBindingSnapshot {
test: KBEnvironmentBinding
production: KBEnvironmentBinding
storage_ready: boolean
+ // Absent stays absent: an older API must not be reported as an untrusted
+ // proxy, and it must still fail closed for key entry.
+ secret_entry_allowed?: boolean
}
export interface KBCatalogOption {
@@ -130,7 +133,13 @@ export function parseKBBindingSnapshot(value: unknown): KBBindingSnapshot {
const test = byEnvironment.get('test')
const production = byEnvironment.get('production')
if (!test || !production) throw new Error('知识库绑定响应缺少环境')
- return { test, production, storage_ready: bool(value.data.storage_ready) }
+ return {
+ test,
+ production,
+ storage_ready: bool(value.data.storage_ready),
+ secret_entry_allowed:
+ typeof value.data.secret_entry_allowed === 'boolean' ? value.data.secret_entry_allowed : undefined,
+ }
}
// 名称相同是常见情况,短 ID 是唯一可辨的部分;两处都显示,不靠名称猜。
diff --git a/web/src/secureEntry.test.ts b/web/src/secureEntry.test.ts
new file mode 100644
index 0000000..fe9e603
--- /dev/null
+++ b/web/src/secureEntry.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from 'vitest'
+import { plaintextRemoteConsole, secretEntryAllowed } from './secureEntry'
+
+describe('secureEntry', () => {
+ it('accepts HTTPS and loopback whatever the deployment toggle says', () => {
+ for (const location of [
+ { protocol: 'https:', hostname: 'kb.example' },
+ { protocol: 'http:', hostname: 'localhost' },
+ { protocol: 'http:', hostname: '127.0.0.1' },
+ { protocol: 'http:', hostname: '[::1]' },
+ { protocol: 'http:', hostname: '::1' },
+ ]) {
+ expect(secretEntryAllowed(location), JSON.stringify(location)).toBe(true)
+ expect(plaintextRemoteConsole(location)).toBe(false)
+ }
+ })
+
+ it('treats plain HTTP from any other host as a plaintext network path', () => {
+ for (const hostname of ['172.29.151.11', '10.0.0.5', 'kb.internal.example', '203.0.113.10']) {
+ const location = { protocol: 'http:', hostname }
+ expect(secretEntryAllowed(location), hostname).toBe(false)
+ expect(plaintextRemoteConsole(location), hostname).toBe(true)
+ }
+ })
+})
diff --git a/web/src/secureEntry.ts b/web/src/secureEntry.ts
index 7d500d2..2b47b59 100644
--- a/web/src/secureEntry.ts
+++ b/web/src/secureEntry.ts
@@ -3,3 +3,10 @@
export function secretEntryAllowed(location: { protocol: string; hostname: string }): boolean {
return location.protocol === 'https:' || ['localhost', '127.0.0.1', '[::1]', '::1'].includes(location.hostname)
}
+
+// True when a key typed here would cross a network in the clear, even though
+// the server presently allows it. Drives the warning only, never the gate:
+// the gate is the server-reported capability.
+export function plaintextRemoteConsole(location: { protocol: string; hostname: string }): boolean {
+ return !secretEntryAllowed(location)
+}