From c3b2552ed5e47eef8dc2a691d5f76f30b61ceafd Mon Sep 17 00:00:00 2001 From: Andreas Opferkuch Date: Mon, 14 Apr 2025 18:05:55 +0200 Subject: [PATCH] fix(HttpContextProvider): support all possible local servers via a library --- core/context/providers/HttpContextProvider.ts | 10 +++------- core/package-lock.json | 10 ++++++++++ core/package.json | 1 + extensions/vscode/package-lock.json | 1 + gui/package-lock.json | 1 + 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/context/providers/HttpContextProvider.ts b/core/context/providers/HttpContextProvider.ts index 68b94c918..835fc319a 100644 --- a/core/context/providers/HttpContextProvider.ts +++ b/core/context/providers/HttpContextProvider.ts @@ -1,3 +1,4 @@ +import isLocalhost from "is-localhost-ip"; import { ContextItem, ContextProviderDescription, @@ -18,13 +19,8 @@ class HttpContextProvider extends BaseContextProvider { private async getWorkspacePath(ide: IDE, url: URL) { try { const currentFile = await ide.getCurrentFile(); - const hostname = url.hostname.toLowerCase(); - const isLocalServer= hostname === "localhost" || - hostname === "127.0.0.1" || - hostname.startsWith("192.168.") || - hostname === "[::1]"; - - return isLocalServer ? + // `isLocalhost` actually also returns true for other local addresses, not just localhost + return await isLocalhost(url.hostname) ? (await ide.getWorkspaceDirs()).find(workspaceDirectory => { return currentFile?.path.startsWith(workspaceDirectory) }) : undefined diff --git a/core/package-lock.json b/core/package-lock.json index 24aeb4f18..5e4899d95 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -39,6 +39,7 @@ "http-proxy-agent": "^7.0.1", "https-proxy-agent": "^7.0.3", "ignore": "^5.3.1", + "is-localhost-ip": "^2.0.0", "jinja-js": "0.1.8", "js-tiktoken": "^1.0.8", "jsdom": "^24.0.0", @@ -9872,6 +9873,15 @@ "integrity": "sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ==", "optional": true }, + "node_modules/is-localhost-ip": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-localhost-ip/-/is-localhost-ip-2.0.0.tgz", + "integrity": "sha512-vlgs2cSgMOfnKU8c1ewgKPyum9rVrjjLLW2HBdL5i0iAJjOs8NY55ZBd/hqUTaYR0EO9CKZd3hVSC2HlIbygTQ==", + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/is-map": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/is-map/-/is-map-2.0.3.tgz", diff --git a/core/package.json b/core/package.json index a405bad55..05f3ad9e7 100644 --- a/core/package.json +++ b/core/package.json @@ -73,6 +73,7 @@ "http-proxy-agent": "^7.0.1", "https-proxy-agent": "^7.0.3", "ignore": "^5.3.1", + "is-localhost-ip": "^2.0.0", "jinja-js": "0.1.8", "js-tiktoken": "^1.0.8", "jsdom": "^24.0.0", diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 108520e49..d3b78b262 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -132,6 +132,7 @@ "http-proxy-agent": "^7.0.1", "https-proxy-agent": "^7.0.3", "ignore": "^5.3.1", + "is-localhost-ip": "^2.0.0", "jinja-js": "0.1.8", "js-tiktoken": "^1.0.8", "jsdom": "^24.0.0", diff --git a/gui/package-lock.json b/gui/package-lock.json index 69c571f84..f15a844ba 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -135,6 +135,7 @@ "http-proxy-agent": "^7.0.1", "https-proxy-agent": "^7.0.3", "ignore": "^5.3.1", + "is-localhost-ip": "^2.0.0", "jinja-js": "0.1.8", "js-tiktoken": "^1.0.8", "jsdom": "^24.0.0",