fix: use URL API for WebSocket URL construction in testWs

Parse base URL with new URL() instead of regex stripping so
paths/query/fragments in user-entered URLs cannot corrupt the
WebSocket endpoint.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
snomiao
2026-04-14 00:20:49 +09:00
parent 5f47cc7997
commit 8fd6531c57

View File

@@ -278,9 +278,12 @@ async function testHttp(base: string): Promise<boolean> {
function testWs(base: string): Promise<boolean> {
return new Promise((resolve) => {
const protocol = base.startsWith('https') ? 'wss' : 'ws'
const host = base.replace(/^https?:\/\//, '')
const ws = new WebSocket(`${protocol}://${host}/ws`)
const wsUrl = new URL(base)
wsUrl.protocol = wsUrl.protocol === 'https:' ? 'wss:' : 'ws:'
wsUrl.pathname = '/ws'
wsUrl.search = ''
wsUrl.hash = ''
const ws = new WebSocket(wsUrl.toString())
const timeout = setTimeout(() => {
ws.close()
resolve(false)