From 7309ec948fbbf30ef9ad273bdf7794a0fc58c1b4 Mon Sep 17 00:00:00 2001 From: SparkLabScout Date: Sat, 14 Mar 2026 02:15:54 +0800 Subject: [PATCH] fix: handle relative URLs in WebSocket domain filter Resolves TypeError when WebSocket connections use relative URLs like /__webpack_hmr (webpack HMR endpoints). - domain-filter.ts: Use location.href as base URL for new URL() - network.rs: Same fix for inline WebSocket filter script Previously, new URL(url) threw TypeError on relative URLs because it requires an absolute URL. Now we resolve against location.href to properly handle relative paths. --- cli/src/native/network.rs | 2 +- src/domain-filter.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/src/native/network.rs b/cli/src/native/network.rs index 0f178d7b..48226e2a 100644 --- a/cli/src/native/network.rs +++ b/cli/src/native/network.rs @@ -184,7 +184,7 @@ pub async fn install_domain_filter_script( const OrigWS = window.WebSocket; window.WebSocket = function(url, protocols) {{ try {{ - const u = new URL(url); + const u = new URL(url, location.href); if (!_isDomainAllowed(u.hostname)) throw new DOMException('WebSocket blocked: ' + u.hostname, 'SecurityError'); }} catch(e) {{ if (e instanceof DOMException) throw e; }} return new OrigWS(url, protocols); diff --git a/src/domain-filter.ts b/src/domain-filter.ts index 19dbff8a..77749921 100644 --- a/src/domain-filter.ts +++ b/src/domain-filter.ts @@ -51,7 +51,8 @@ export function buildWebSocketFilterScript(allowedDomains: string[]): string { } function _checkUrl(url) { try { - var parsed = new URL(url); + // Resolve relative URLs against current page origin (e.g., /__webpack_hmr) + var parsed = new URL(url, location.href); return _isDomainAllowed(parsed.hostname); } catch(e) { return false;