LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。
TypeScript
Harden embedding custom headers
nash_subb37ed43bb68
diff --git a/src-tauri/src/commands/search.rs b/src-tauri/src/commands/search.rsindex ad18074b..e34f8631 100644--- a/src-tauri/src/commands/search.rs+++ b/src-tauri/src/commands/search.rs@@ -64,7 +64,8 @@ pub struct SearchEmbeddingConfig {/// Extra HTTP headers to send with every embedding request, e.g./// `X-Model-Provider-Id: siliconflow` for the mify gateway./// Reserved names (Authorization, Content-Type, Host,- /// Content-Length) are skipped — they're managed by the client.+ /// Content-Length, x-goog-api-key) are skipped — they're managed+ /// by the client.#[serde(default)]pub extra_headers: Option<BTreeMap<String, String>>,}@@ -690,17 +691,14 @@ async fn fetch_embedding(text: &str, cfg: &SearchEmbeddingConfig) -> Result<Vec<if let Some(extra) = cfg.extra_headers.as_ref() {for (name, value) in extra {let trimmed = name.trim();- if trimmed.is_empty() {+ let value = value.trim();+ if trimmed.is_empty() || value.is_empty() || !is_safe_extra_header_name(trimmed) {continue;}- let lower = trimmed.to_ascii_lowercase();- if matches!(- lower.as_str(),- "authorization" | "content-type" | "host" | "content-length"- ) {+ if is_reserved_extra_header_name(trimmed) {continue;}- req = req.header(trimmed, value.as_str());+ req = req.header(trimmed, value);}}let body = if is_google {@@ -752,6 +750,39 @@ async fn fetch_embedding(text: &str, cfg: &SearchEmbeddingConfig) -> Result<Vec<Ok(out)}+fn is_safe_extra_header_name(name: &str) -> bool {+ !name.is_empty()+ && name.bytes().all(|b| {+ matches!(+ b,+ b'!' | b'#'+ | b'$'+ | b'%'+ | b'&'+ | b'\''+ | b'*'+ | b'+'+ | b'-'+ | b'.'+ | b'^'+ | b'_'+ | b'`'+ | b'|'+ | b'~'+ | b'0'..=b'9'+ | b'A'..=b'Z'+ | b'a'..=b'z'+ )+ })+}++fn is_reserved_extra_header_name(name: &str) -> bool {+ matches!(+ name.trim().to_ascii_lowercase().as_str(),+ "authorization" | "content-type" | "host" | "content-length" | "x-goog-api-key"+ )+}+fn is_google_embedding_config(cfg: &SearchEmbeddingConfig) -> bool {let endpoint = cfg.endpoint.to_lowercase();endpoint.contains("generativelanguage.googleapis.com") || endpoint.contains(":embedcontent")@@ -976,6 +1007,20 @@ mod tests {assert_eq!(body["output_dimensionality"], 768);}+ #[test]+ fn extra_embedding_header_names_are_validated_and_reserved_names_are_skipped() {+ assert!(is_safe_extra_header_name("X-Model-Provider-Id"));+ assert!(is_safe_extra_header_name("x_trace.id"));+ assert!(!is_safe_extra_header_name(""));+ assert!(!is_safe_extra_header_name("Bad Header"));+ assert!(!is_safe_extra_header_name("中文"));++ assert!(is_reserved_extra_header_name("Authorization"));+ assert!(is_reserved_extra_header_name("content-type"));+ assert!(is_reserved_extra_header_name("X-Goog-Api-Key"));+ assert!(!is_reserved_extra_header_name("X-Model-Provider-Id"));+ }+#[test]fn rrf_combines_token_and_vector_ranks_and_keeps_vector_score() {let mut results = vec![diff --git a/src/components/settings/sections/embedding-section.tsx b/src/components/settings/sections/embedding-section.tsxindex 495ea96d..39d4a4a5 100644--- a/src/components/settings/sections/embedding-section.tsx+++ b/src/components/settings/sections/embedding-section.tsx@@ -38,7 +38,14 @@ function parsePositiveInteger(value: string): number | undefined {return Math.floor(n)}-const RESERVED_HEADER_NAMES = new Set(["authorization", "content-type", "host", "content-length"])+const RESERVED_HEADER_NAMES = new Set([+ "authorization",+ "content-type",+ "host",+ "content-length",+ "x-goog-api-key",+])+const HTTP_HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/function headersToText(headers: Record<string, string>): string {return Object.entries(headers)@@ -55,7 +62,7 @@ function parseHeadersText(text: string): Record<string, string> {if (idx <= 0) continueconst name = line.slice(0, idx).trim()const value = line.slice(idx + 1).trim()- if (!name || RESERVED_HEADER_NAMES.has(name.toLowerCase())) continue+ if (!name || !value || !HTTP_HEADER_NAME_RE.test(name) || RESERVED_HEADER_NAMES.has(name.toLowerCase())) continueout[name] = value}return outdiff --git a/src/i18n/en.json b/src/i18n/en.jsonindex 67461ca4..02ac8689 100644--- a/src/i18n/en.json+++ b/src/i18n/en.json@@ -279,7 +279,7 @@"outputDimensionality": "Output dimensionality (Gemini only)","outputDimensionalityHint": "Optional. Sent only for Gemini native embedContent endpoints as `output_dimensionality`; OpenAI-compatible endpoints ignore this field. Leave empty for the model default. Rebuild embeddings after changing model or dimension.","extraHeaders": "Custom request headers",- "extraHeadersHint": "Optional. One per line, formatted `Header-Name: value`. Used for gateways that route by request header — e.g. mify needs `X-Model-Provider-Id: siliconflow`. Authorization, Content-Type, Host, and Content-Length are managed by the client and will be ignored.",+ "extraHeadersHint": "Optional. One per line, formatted `Header-Name: value`. Used for gateways that route by request header — e.g. mify needs `X-Model-Provider-Id: siliconflow`. Authorization, Content-Type, Host, Content-Length, and x-goog-api-key are managed by the client and will be ignored.","chunking": "Chunking","maxChunkChars": "Max chunk size (characters)","maxChunkCharsHint": "Target size of each chunk. Lower this (e.g. 500) when your endpoint's context is small (llama.cpp n_ctx=512, mxbai-embed-large, etc.).",diff --git a/src/i18n/zh.json b/src/i18n/zh.jsonindex 3ca36cf4..7c9f8dd7 100644--- a/src/i18n/zh.json+++ b/src/i18n/zh.json@@ -279,7 +279,7 @@"outputDimensionality": "输出维度(仅 Gemini)","outputDimensionalityHint": "可选。仅对 Gemini 原生 embedContent endpoint 作为 `output_dimensionality` 发送;OpenAI 兼容 endpoint 会忽略此项。留空则使用模型默认值。修改模型或维度后需要重建嵌入。","extraHeaders": "自定义请求头",- "extraHeadersHint": "可选。每行一条,格式 `Header-Name: value`。用于需要按请求头路由的网关,例如 mify 需要 `X-Model-Provider-Id: siliconflow`。Authorization、Content-Type、Host、Content-Length 由系统管理,会被忽略。",+ "extraHeadersHint": "可选。每行一条,格式 `Header-Name: value`。用于需要按请求头路由的网关,例如 mify 需要 `X-Model-Provider-Id: siliconflow`。Authorization、Content-Type、Host、Content-Length、x-goog-api-key 由系统管理,会被忽略。","chunking": "切分参数","maxChunkChars": "每块最大字符数","maxChunkCharsHint": "单个 chunk 的目标长度。endpoint 上下文较小时(llama.cpp n_ctx=512、mxbai-embed-large 等)调低(如 500)。",diff --git a/src/lib/embedding.test.ts b/src/lib/embedding.test.tsindex be8dda87..cd23ab67 100644--- a/src/lib/embedding.test.ts+++ b/src/lib/embedding.test.ts@@ -271,6 +271,65 @@ describe("fetchEmbedding — provider wire formats", () => {})})+ it("sends safe custom embedding headers on OpenAI-compatible endpoints", async () => {+ mockHttpFetch.mockResolvedValueOnce(okResponse([0.5, 0.6]))++ const out = await fetchEmbedding("hi", {+ enabled: true,+ endpoint: "https://gateway.example.com/v1/embeddings",+ apiKey: "sk-test",+ model: "text-embedding-3-small",+ extraHeaders: {+ "X-Model-Provider-Id": " siliconflow ",+ "X-Empty": "",+ "Bad Header": "nope",+ Authorization: "Bearer attacker",+ "Content-Type": "text/plain",+ Host: "evil.example.com",+ "Content-Length": "999",+ "x-goog-api-key": "wrong-google-key",+ },+ })++ expect(out).toEqual([0.5, 0.6])+ const [, opts] = mockHttpFetch.mock.calls[0]+ const headers = opts?.headers as Record<string, string>+ expect(headers["X-Model-Provider-Id"]).toBe("siliconflow")+ expect(headers.Authorization).toBe("Bearer sk-test")+ expect(headers["Content-Type"]).toBe("application/json")+ expect(headers.Host).toBeUndefined()+ expect(headers["Content-Length"]).toBeUndefined()+ expect(headers["x-goog-api-key"]).toBeUndefined()+ expect(headers["Bad Header"]).toBeUndefined()+ expect(headers["X-Empty"]).toBeUndefined()+ })++ it("does not let custom headers override the Gemini API key header", async () => {+ mockHttpFetch.mockResolvedValueOnce(+ new Response(JSON.stringify({ embedding: { values: [0.7, 0.8] } }), {+ status: 200,+ headers: { "Content-Type": "application/json" },+ }),+ )++ const out = await fetchEmbedding("hello", {+ enabled: true,+ endpoint: "https://generativelanguage.googleapis.com/v1beta",+ apiKey: "real-google-key",+ model: "gemini-embedding-001",+ extraHeaders: {+ "x-goog-api-key": "wrong-google-key",+ "X-Trace-Id": "trace-1",+ },+ })++ expect(out).toEqual([0.7, 0.8])+ const [, opts] = mockHttpFetch.mock.calls[0]+ const headers = opts?.headers as Record<string, string>+ expect(headers["x-goog-api-key"]).toBe("real-google-key")+ expect(headers["X-Trace-Id"]).toBe("trace-1")+ })+it("supports Gemini native embedContent endpoint and response shape", async () => {mockHttpFetch.mockResolvedValueOnce(new Response(JSON.stringify({ embedding: { values: [0.1, 0.2, 0.3] } }), {diff --git a/src/lib/embedding.ts b/src/lib/embedding.tsindex d79fb12c..1b44c832 100644--- a/src/lib/embedding.ts+++ b/src/lib/embedding.ts@@ -28,6 +28,26 @@ import { normalizePath } from "@/lib/path-utils"import { getHttpFetch, isFetchNetworkError } from "@/lib/tauri-fetch"import { chunkMarkdown, type Chunk } from "@/lib/text-chunker"+const RESERVED_EMBEDDING_HEADER_NAMES = new Set([+ "authorization",+ "content-type",+ "host",+ "content-length",+ "x-goog-api-key",+])+const HTTP_HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/++function isSafeExtraHeader(name: string, value: string): boolean {+ const trimmedName = name.trim()+ const trimmedValue = value.trim()+ return (+ trimmedName.length > 0 &&+ trimmedValue.length > 0 &&+ HTTP_HEADER_NAME_RE.test(trimmedName) &&+ !RESERVED_EMBEDDING_HEADER_NAMES.has(trimmedName.toLowerCase())+ )+}+// ── Error surfacing ──────────────────────────────────────────────────────/**@@ -95,11 +115,11 @@ export async function fetchEmbedding(}}if (cfg.extraHeaders) {- const reserved = new Set(["authorization", "content-type", "host", "content-length"])for (const [k, v] of Object.entries(cfg.extraHeaders)) {const name = k.trim()- if (!name || reserved.has(name.toLowerCase())) continue- headers[name] = v+ const value = v.trim()+ if (!isSafeExtraHeader(name, value)) continue+ headers[name] = value}}diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.tsindex e047967d..80b3d233 100644--- a/src/stores/wiki-store.ts+++ b/src/stores/wiki-store.ts@@ -111,8 +111,8 @@ interface EmbeddingConfig {* Extra HTTP headers to send with every embedding request, e.g.* { "X-Model-Provider-Id": "siliconflow" }* for gateways like mify that route by header. Reserved names- * (Authorization, Content-Type, Host, Content-Length) are ignored- * — they're managed by the embedding client itself.+ * (Authorization, Content-Type, Host, Content-Length, x-goog-api-key)+ * are ignored — they're managed by the embedding client itself.*/extraHeaders?: Record<string, string>}