Skip to content

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

feat: endpoint-normalizer catches malformed URLs before fetch time

nash_su88809a49fa6c
diff --git a/src/lib/endpoint-normalizer.test.ts b/src/lib/endpoint-normalizer.test.ts
index 5b1094c4..0e36fa94 100644
--- a/src/lib/endpoint-normalizer.test.ts
+++ b/src/lib/endpoint-normalizer.test.ts
@@ -126,3 +126,62 @@ describe("normalizeEndpoint — anthropic_messages mode", () => {
expect(r.normalized).toBe("https://api.anthropic.com/v1")
})
})
+
+describe("normalizeEndpoint — URL well-formedness catches", () => {
+ it("flags a 5-octet IP-shaped host as malformed (real user paste)", () => {
+ // Real user paste: "http://192.168.1.1.50:8000/v1".
+ // WHATWG URL's IPv4 parser actually rejects this at `new URL()`
+ // time with a TypeError, so it surfaces via the generic
+ // "URL not well-formed" branch — but it DOES surface, which is the
+ // point. Before this guard, the browser fetch failure was the
+ // user's only signal, with nothing to say it was a typo.
+ const r = normalizeEndpoint("http://192.168.1.1.50:8000/v1", "chat_completions")
+ expect(r.warning).toBeDefined()
+ expect(r.warning!.toLowerCase()).toMatch(/well-formed|typo|ipv4|octets/)
+ })
+
+ // 3-octet hosts like "10.0.0" are silently normalised to "10.0.0.0"
+ // by WHATWG URL parsing (IPv4 shorthand expansion), so they don't
+ // actually reach our regex as 3-octet. Not an issue in practice.
+
+ it("rejects an octet > 255 at the URL-parsing layer", () => {
+ // WHATWG URL treats "999" as invalid IPv4 too, so `new URL()`
+ // throws outright — our malformed-URL branch catches it first
+ // with the generic "not well-formed" message. Either way the user
+ // is informed the URL is broken before the HTTP client sees it.
+ const r = normalizeEndpoint("http://192.168.1.999:8080/v1", "chat_completions")
+ expect(r.warning).toBeDefined()
+ expect(r.warning!.toLowerCase()).toMatch(/well-formed|typo|ipv4|255/)
+ })
+
+ it("accepts a well-formed IPv4 host with port and path", () => {
+ const r = normalizeEndpoint("http://192.168.1.50:8080/v1", "chat_completions")
+ expect(r.warning).toBeUndefined()
+ expect(r.normalized).toBe("http://192.168.1.50:8080/v1")
+ })
+
+ it("does NOT flag DNS-name-shaped hosts that contain dots", () => {
+ // `api.openai.com` has dots but isn't IP-shaped (non-numeric
+ // labels) — shouldn't trigger the IPv4 check.
+ const r = normalizeEndpoint("https://api.openai.com/v1", "chat_completions")
+ expect(r.warning).toBeUndefined()
+ })
+
+ it("rejects an unparseable URL (triple-t protocol typo)", () => {
+ const r = normalizeEndpoint("htttp://api.openai.com/v1", "chat_completions")
+ // missingProtocol fires first since /^https?:\/\//i doesn't match.
+ expect(r.warning).toMatch(/https?:\/\//i)
+ })
+
+ it("rejects URL with illegal characters", () => {
+ const r = normalizeEndpoint("https://api.openai.com:port/v1", "chat_completions")
+ // Port "port" isn't numeric → new URL throws.
+ expect(r.warning).toBeDefined()
+ expect(r.warning!.toLowerCase()).toMatch(/well-formed|typo/)
+ })
+
+ it("accepts localhost host (non-IP) without flagging", () => {
+ const r = normalizeEndpoint("http://localhost:11434/v1", "chat_completions")
+ expect(r.warning).toBeUndefined()
+ })
+})
diff --git a/src/lib/endpoint-normalizer.ts b/src/lib/endpoint-normalizer.ts
index 22d3c2ac..38a3c8ef 100644
--- a/src/lib/endpoint-normalizer.ts
+++ b/src/lib/endpoint-normalizer.ts
@@ -55,6 +55,43 @@ export function normalizeEndpoint(raw: string, mode: EndpointMode): NormalizedEn
let url = trimmed
const notes: string[] = []
+ // Sanity-check the URL can be parsed at all. `new URL(...)` catches
+ // typos like five-octet IPs ("192.168.1.1.50"), triple-t protocols
+ // ("htttp://"), stray backslashes, and similar paste mistakes that
+ // would otherwise only be diagnosed at request time by the HTTP
+ // client — a much worse user experience. Emit the warning up front
+ // and still return whatever we've got so the input field behaves.
+ let parsed: URL | null = null
+ try {
+ parsed = new URL(trimmed)
+ } catch {
+ return {
+ normalized: trimmed.replace(/\/+$/, ""),
+ changed: trimmed !== trimmed.replace(/\/+$/, ""),
+ warning: "URL is not well-formed — check for typos in the host / port / path.",
+ }
+ }
+
+ // Also catch IPv4-shaped hostnames with too many / too few octets
+ // — these parse fine as generic DNS names but will fail at lookup.
+ // If the hostname looks IP-shaped but isn't a valid IPv4, flag it.
+ const host = parsed.hostname
+ const looksNumericDotted = /^\d+(?:\.\d+)+$/.test(host)
+ if (looksNumericDotted) {
+ const octets = host.split(".")
+ const validIpv4 =
+ octets.length === 4 &&
+ octets.every((o) => {
+ const n = Number(o)
+ return Number.isInteger(n) && n >= 0 && n <= 255
+ })
+ if (!validIpv4) {
+ notes.push(
+ `Host "${host}" looks like an IPv4 address but has ${octets.length} octets (valid IPv4 has exactly 4, each 0-255).`,
+ )
+ }
+ }
+
// Strip trailing slashes (cheap, always safe)
url = url.replace(/\/+$/, "")