Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

Refactor local network URL detection into reusable utility (#137)

Gabriel Stein28ac9b599c98
diff --git a/src/components/OnboardingFlow.tsx b/src/components/OnboardingFlow.tsx
index 35e7db93a..d56a02365 100644
--- a/src/components/OnboardingFlow.tsx
+++ b/src/components/OnboardingFlow.tsx
@@ -37,6 +37,7 @@ import ModelCardList from "./ui/ModelCardList";
import { setAgentName as saveAgentName } from "../utils/agentName";
import { formatHotkeyLabel, getDefaultHotkey } from "../utils/hotkeys";
import { API_ENDPOINTS, buildApiUrl, normalizeBaseUrl } from "../config/constants";
+import { isSecureEndpoint } from "../utils/urlUtils";
import { HotkeyInput } from "./ui/HotkeyInput";
import { useHotkeyRegistration } from "../hooks/useHotkeyRegistration";
import { ActivationModeSelector } from "./ui/ActivationModeSelector";
@@ -212,12 +213,8 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
setCustomModelsLoading(true);
setCustomModelsError(null);
try {
- // Security: Only allow HTTPS endpoints (except localhost for development)
- const isLocalhost =
- normalizedReasoningBaseUrl.includes("://localhost") ||
- normalizedReasoningBaseUrl.includes("://127.0.0.1");
- if (!normalizedReasoningBaseUrl.startsWith("https://") && !isLocalhost) {
- throw new Error("Only HTTPS endpoints are allowed (except localhost for testing).");
+ if (!isSecureEndpoint(normalizedReasoningBaseUrl)) {
+ throw new Error("HTTPS required (HTTP allowed for local network only).");
}
const headers: Record<string, string> = {};
diff --git a/src/components/ReasoningModelSelector.tsx b/src/components/ReasoningModelSelector.tsx
index 04ae798ee..0561a0cfd 100644
--- a/src/components/ReasoningModelSelector.tsx
+++ b/src/components/ReasoningModelSelector.tsx
@@ -10,6 +10,7 @@ import { API_ENDPOINTS, buildApiUrl, normalizeBaseUrl } from "../config/constant
import { REASONING_PROVIDERS } from "../models/ModelRegistry";
import { modelRegistry } from "../models/ModelRegistry";
import { getProviderIcon } from "../utils/providerIcons";
+import { isSecureEndpoint } from "../utils/urlUtils";
type CloudModelOption = {
value: string;
@@ -161,13 +162,9 @@ export default function ReasoningModelSelector({
return;
}
- const isLocalhost =
- normalizedBase.includes("://localhost") || normalizedBase.includes("://127.0.0.1");
- if (!normalizedBase.startsWith("https://") && !isLocalhost) {
+ if (!isSecureEndpoint(normalizedBase)) {
if (isMountedRef.current && latestReasoningBaseRef.current === normalizedBase) {
- setCustomModelsError(
- "Only HTTPS endpoints are allowed (except localhost for testing)."
- );
+ setCustomModelsError("HTTPS required (HTTP allowed for local network only).");
setCustomModelsLoading(false);
}
return;
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index 358a04da6..98a4bbff6 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -2,6 +2,7 @@ import ReasoningService from "../services/ReasoningService";
import { API_ENDPOINTS, buildApiUrl, normalizeBaseUrl } from "../config/constants";
import logger from "../utils/logger";
import { isBuiltInMicrophone } from "../utils/audioDeviceUtils";
+import { isSecureEndpoint } from "../utils/urlUtils";
const SHORT_CLIP_DURATION_SECONDS = 2.5;
const REASONING_CACHE_TTL = 30000; // 30 seconds
@@ -1120,10 +1121,8 @@ class AudioManager {
return cacheResult(API_ENDPOINTS.TRANSCRIPTION);
}
- const isLocalhost =
- normalizedBase.includes("://localhost") || normalizedBase.includes("://127.0.0.1");
- if (!normalizedBase.startsWith("https://") && !isLocalhost) {
- console.warn("Non-HTTPS endpoint rejected for security. Using default.");
+ if (!isSecureEndpoint(normalizedBase)) {
+ console.warn("HTTPS required (HTTP allowed for local network only). Using default.");
return cacheResult(API_ENDPOINTS.TRANSCRIPTION);
}
diff --git a/src/services/ReasoningService.ts b/src/services/ReasoningService.ts
index 4df92cd04..f28350682 100644
--- a/src/services/ReasoningService.ts
+++ b/src/services/ReasoningService.ts
@@ -4,6 +4,7 @@ import { SecureCache } from "../utils/SecureCache";
import { withRetry, createApiRetryStrategy } from "../utils/retry";
import { API_ENDPOINTS, TOKEN_LIMITS, buildApiUrl, normalizeBaseUrl } from "../config/constants";
import logger from "../utils/logger";
+import { isSecureEndpoint } from "../utils/urlUtils";
export const DEFAULT_PROMPTS = {
agent: `You are {{agentName}}, a helpful AI assistant. Process and improve the following text, removing any reference to your name from the output:\n\n{{text}}\n\nImproved text:`,
@@ -55,12 +56,9 @@ class ReasoningService extends BaseReasoningService {
return API_ENDPOINTS.OPENAI_BASE;
}
- // Security: Only allow HTTPS endpoints (except localhost for development)
- const isLocalhost =
- normalized.includes("://localhost") || normalized.includes("://127.0.0.1");
- if (!normalized.startsWith("https://") && !isLocalhost) {
+ if (!isSecureEndpoint(normalized)) {
logger.logReasoning("OPENAI_BASE_REJECTED", {
- reason: "Non-HTTPS endpoint rejected for security",
+ reason: "HTTPS required (HTTP allowed for local network only)",
attempted: normalized,
});
return API_ENDPOINTS.OPENAI_BASE;
diff --git a/src/utils/urlUtils.ts b/src/utils/urlUtils.ts
new file mode 100644
index 000000000..7cc62b70a
--- /dev/null
+++ b/src/utils/urlUtils.ts
@@ -0,0 +1,48 @@
+/**
+ * URL security utilities - allows HTTP for private networks, requires HTTPS otherwise.
+ */
+
+function isPrivateHost(hostname: string): boolean {
+ const h = hostname.toLowerCase().replace(/^\[|\]$/g, "");
+
+ // Loopback
+ if (h === "localhost" || h === "0.0.0.0" || h.startsWith("127.")) return true;
+
+ // IPv6 loopback
+ if (h === "::1") return true;
+
+ // RFC 1918 private ranges
+ if (h.startsWith("10.")) return true;
+ if (h.startsWith("192.168.")) return true;
+ if (h.startsWith("172.")) {
+ const octet = parseInt(h.split(".")[1], 10);
+ if (octet >= 16 && octet <= 31) return true;
+ }
+
+ // Link-local IPv4
+ if (h.startsWith("169.254.")) return true;
+
+ // IPv6 addresses contain colons - check before matching prefixes
+ const isIPv6 = h.includes(":");
+
+ // Link-local IPv6 (fe80::/10)
+ if (isIPv6 && h.startsWith("fe80")) return true;
+
+ // IPv6 unique local (fc00::/7)
+ if (isIPv6 && (h.startsWith("fc") || h.startsWith("fd"))) return true;
+
+ // mDNS/Bonjour (.local domains)
+ if (h.endsWith(".local")) return true;
+
+ return false;
+}
+
+/** Returns true if URL is HTTPS or targets a private/local network. */
+export function isSecureEndpoint(url: string): boolean {
+ try {
+ const parsed = new URL(url);
+ return parsed.protocol === "https:" || isPrivateHost(parsed.hostname);
+ } catch {
+ return false;
+ }
+}