Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(llm): trim the SearXNG address without a backtracking regex
Elian Doranc7d2c0838af2
diff --git a/packages/trilium-core/src/services/llm/providers/base_provider.ts b/packages/trilium-core/src/services/llm/providers/base_provider.tsindex 88aff485dd..c39e321cc3 100644--- a/packages/trilium-core/src/services/llm/providers/base_provider.ts+++ b/packages/trilium-core/src/services/llm/providers/base_provider.ts@@ -7,6 +7,7 @@ import { LLM_ATTACHMENT_KINDS, type LlmAttachmentKind, type LlmMessage, type Llmimport { type FilePart, generateText, type ImagePart, type LanguageModel, type ModelMessage, stepCountIs, streamText, type SystemModelMessage, type TextPart, type ToolSet } from "ai";import { getLog } from "../../log.js";+import { trimTrailingSlashes } from "../../utils/index.js";import { attachmentPlaceholder, resolveAttachmentPart } from "../attachment_content.js";import { llmFetch } from "./fetch.js";import { buildNoteHint } from "../note_hint.js";@@ -213,20 +214,9 @@ export function mergeModelLists(curated: ModelInfo[], remote: RemoteModel[]): Mo/*** Normalize a custom endpoint override: strip trailing slashes, and treat an* empty result as "no override".- *- * Written as an index scan rather than `replace(/\/+$/, "")`: that pattern- * backtracks polynomially on a value ending in many slashes, and the base URL- * arrives straight from a request body (CodeQL js/polynomial-redos).*/function normalizeBaseUrl(baseURL: string | undefined): string | undefined {- if (!baseURL) {- return undefined;- }- let end = baseURL.length;- while (end > 0 && baseURL.charAt(end - 1) === "/") {- end--;- }- return baseURL.slice(0, end) || undefined;+ return baseURL ? trimTrailingSlashes(baseURL) || undefined : undefined;}export abstract class BaseProvider implements LlmProvider {diff --git a/packages/trilium-core/src/services/llm/web_search.ts b/packages/trilium-core/src/services/llm/web_search.tsindex cb53859514..be68926309 100644--- a/packages/trilium-core/src/services/llm/web_search.ts+++ b/packages/trilium-core/src/services/llm/web_search.ts@@ -11,6 +11,7 @@ import { z } from "zod";import markdownExport from "../export/markdown.js";import request, { validateFetchableUrl } from "../request.js";import { decodeUtf8 } from "../utils/binary.js";+import { trimTrailingSlashes } from "../utils/index.js";import { llmFetch } from "./providers/fetch.js";/** A configured search provider, as stored in the `llmProviders` option. */@@ -109,7 +110,7 @@ export async function searchWeb(setup: WebSearchSetup, query: string): Promise<Wthrow new Error("The SearXNG search provider has no address.");}const params = new URLSearchParams({ q: query, format: "json" });- const payload = await requestJson(`${setup.baseURL.replace(/\/+$/, "")}/search?${params}`, {+ const payload = await requestJson(`${trimTrailingSlashes(setup.baseURL)}/search?${params}`, {headers: { "Accept": "application/json", ...(setup.apiKey && { "Authorization": `Bearer ${setup.apiKey}` }) }}) as { results?: unknown[] };return toSources(payload.results, "content").slice(0, MAX_RESULTS);diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.tsindex c0d630a42b..1c218e4726 100644--- a/packages/trilium-core/src/services/utils/index.spec.ts+++ b/packages/trilium-core/src/services/utils/index.spec.ts@@ -767,6 +767,21 @@ describe("#normalizeUrl", () => {});});+describe("#trimTrailingSlashes", () => {+ it("removes every trailing slash and nothing else", () => {+ expect(utils.trimTrailingSlashes("http://localhost:8888///")).toBe("http://localhost:8888");+ expect(utils.trimTrailingSlashes("https://example.com/a//b")).toBe("https://example.com/a//b");+ expect(utils.trimTrailingSlashes("///")).toBe("");+ expect(utils.trimTrailingSlashes("")).toBe("");+ });++ it("stays linear on a long run of slashes", () => {+ const start = performance.now();+ utils.trimTrailingSlashes(`${"/".repeat(100_000)}x`);+ expect(performance.now() - start).toBeLessThan(100);+ });+});+describe("#normalizeCustomHandlerPattern", () => {const testCases: TestCase<typeof utils.normalizeCustomHandlerPattern>[] = [[ "should handle pattern without ending - add both versions", [ "foo" ], [ "foo", "foo/" ] ],diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex 7c4a6636ca..ccf12b01d3 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -119,6 +119,20 @@ export function normalizePreservingLength(str: string) {return result;}+/**+ * Removes every trailing `/` from `str`.+ *+ * Written as an index scan rather than `replace(/\/+$/, "")`: that pattern backtracks+ * polynomially on a value with many slashes (CodeQL js/polynomial-redos).+ */+export function trimTrailingSlashes(str: string) {+ let end = str.length;+ while (end > 0 && str.charAt(end - 1) === "/") {+ end--;+ }+ return str.slice(0, end);+}+/*** Normalizes URL by removing trailing slashes and fixing double slashes.* Preserves the protocol (http://, https://) but removes trailing slashes from the rest.