Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): trim the base URL without a backtracking regex

Elian Doran338ecc40e779
diff --git a/apps/server/src/services/llm/providers/base_provider.spec.ts b/apps/server/src/services/llm/providers/base_provider.spec.ts
index 22c6749b17..34d9bef0fd 100644
--- a/apps/server/src/services/llm/providers/base_provider.spec.ts
+++ b/apps/server/src/services/llm/providers/base_provider.spec.ts
@@ -104,6 +104,10 @@ class TestProvider extends BaseProvider {
public callBaseGetProviderPrices() {
return super.getProviderPrices();
}
+ /** Expose the normalized endpoint override for assertions. */
+ public get normalizedBaseUrl() {
+ return this.baseURL;
+ }
}
/** Build a stub attachment with the given content/availability. */
@@ -143,6 +147,27 @@ describe("getAvailableModels / getModelPricing (from the price table)", () => {
});
});
+describe("base URL normalization", () => {
+ it("strips trailing slashes and treats a blank override as none", () => {
+ expect(new TestProvider("k", "http://localhost:11434/v1").normalizedBaseUrl).toBe("http://localhost:11434/v1");
+ expect(new TestProvider("k", "http://localhost:11434/v1/").normalizedBaseUrl).toBe("http://localhost:11434/v1");
+ expect(new TestProvider("k", "http://localhost:11434/v1///").normalizedBaseUrl).toBe("http://localhost:11434/v1");
+ expect(new TestProvider("k").normalizedBaseUrl).toBeUndefined();
+ expect(new TestProvider("k", "").normalizedBaseUrl).toBeUndefined();
+ expect(new TestProvider("k", "///").normalizedBaseUrl).toBeUndefined();
+ });
+
+ it("trims a pathological run of trailing slashes without backtracking", () => {
+ // The base URL comes straight from a request body, and the previous
+ // `/\/+$/` pattern backtracked polynomially on this input, hanging the
+ // server (CodeQL js/polynomial-redos).
+ const hostile = `http://x${"/".repeat(100_000)}`;
+ const startedAt = Date.now();
+ expect(new TestProvider("k", hostile).normalizedBaseUrl).toBe("http://x");
+ expect(Date.now() - startedAt).toBeLessThan(1000);
+ });
+});
+
describe("mergeModelLists", () => {
const CURATED: ModelInfo[] = [
{ id: "gpt-4.1", name: "GPT-4.1", pricing: { input: 2, output: 8 }, contextWindow: 1047576, isDefault: true },
diff --git a/apps/server/src/services/llm/providers/base_provider.ts b/apps/server/src/services/llm/providers/base_provider.ts
index 9cfc163f8c..2ef45edeb4 100644
--- a/apps/server/src/services/llm/providers/base_provider.ts
+++ b/apps/server/src/services/llm/providers/base_provider.ts
@@ -174,6 +174,25 @@ export function mergeModelLists(curated: ModelInfo[], remote: RemoteModel[]): Mo
return merged;
}
+/**
+ * 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;
+}
+
export abstract class BaseProvider implements LlmProvider {
abstract name: string;
@@ -189,7 +208,7 @@ export abstract class BaseProvider implements LlmProvider {
constructor(apiKey = "", baseURL?: string) {
this.apiKey = apiKey;
- this.baseURL = baseURL?.replace(/\/+$/, "") || undefined;
+ this.baseURL = normalizeBaseUrl(baseURL);
}
/** Create a language model instance for the given model ID. */