From 438fb753ab24d91d352db6fa8433ee967d6306be Mon Sep 17 00:00:00 2001 From: PM <76590223+pqminh27@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:07:14 +0700 Subject: [PATCH] fix(terminal): preserve IPv6 host brackets in HTTP links (#37449) Terminal link detection treated the closing `]` of a bracketed IPv6 host (e.g. http://[::1]:5173) as trailing punctuation and dropped it, producing unopenable links. Match an optional bracketed IPv6 authority after the scheme, and never trim trailing punctuation back past that authority. Avoids regex lookbehind so the mobile terminal WebView can still parse it. Co-authored-by: Neil --- src/shared/prose-url-boundary.ts | 12 +++++++++--- src/shared/terminal-http-url-extraction.test.ts | 17 ++++++++++++++++- src/shared/terminal-http-url-extraction.ts | 16 ++++++++++++++-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/shared/prose-url-boundary.ts b/src/shared/prose-url-boundary.ts index f7071462576..ef0cab5d840 100644 --- a/src/shared/prose-url-boundary.ts +++ b/src/shared/prose-url-boundary.ts @@ -5,11 +5,17 @@ const PROSE_URL_TERMINATOR_SOURCE = String.raw`(?![\0-\x7f])[\p{Ps}\p{Pe}\p{Pi}\ const NOT_PROSE_URL_TERMINATOR = `(?!${PROSE_URL_TERMINATOR_SOURCE})` -/** xterm's default web-links url pattern, also ended by prose punctuation; needs the `u` flag. */ -export const PROSE_HTTP_URL_SOURCE = - String.raw`https?:\/\/(?:${NOT_PROSE_URL_TERMINATOR}[^\s"'!*(){}|\\^<>\x60])*` + +// Why no lookbehind: the mobile terminal WebView bundles this, and WebKit before Safari 16.4 cannot parse it. +/** A bracketed IPv6 host after the scheme, e.g. `user@[::1]`; its `]` is not trailing punctuation. */ +export const IPV6_HTTP_HOST_SOURCE = String.raw`(?:(?:${NOT_PROSE_URL_TERMINATOR}[^\s"'!*(){}|\\^<>\x60\/?#\[\]])*@)?\[[\dA-Fa-f:.]+\]` + +const PROSE_HTTP_URL_BODY_SOURCE = + String.raw`(?:${NOT_PROSE_URL_TERMINATOR}[^\s"'!*(){}|\\^<>\x60])*` + String.raw`${NOT_PROSE_URL_TERMINATOR}[^\s"':,.!?{}|\\^~\[\]\x60()<>]` +/** xterm's default web-links url pattern, also ended by prose punctuation; needs the `u` flag. */ +export const PROSE_HTTP_URL_SOURCE = String.raw`https?:\/\/(?:${IPV6_HTTP_HOST_SOURCE}(?:${PROSE_HTTP_URL_BODY_SOURCE})?|${PROSE_HTTP_URL_BODY_SOURCE})` + const PROSE_URL_TERMINATOR = new RegExp(PROSE_URL_TERMINATOR_SOURCE, 'u') const PROSE_URL_END = new RegExp(String.raw`\s|${PROSE_URL_TERMINATOR_SOURCE}`, 'u') const PROSE_URL_TERMINATOR_RUN = new RegExp(`((?:${PROSE_URL_TERMINATOR_SOURCE})+)`, 'u') diff --git a/src/shared/terminal-http-url-extraction.test.ts b/src/shared/terminal-http-url-extraction.test.ts index 5fbf08d38f2..b00f4d579b9 100644 --- a/src/shared/terminal-http-url-extraction.test.ts +++ b/src/shared/terminal-http-url-extraction.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { extractTerminalHttpLinks } from './terminal-http-url-extraction' +import { extractTerminalHttpLinks, TERMINAL_HTTP_URL_REGEX } from './terminal-http-url-extraction' const urlsIn = (line: string): string[] => extractTerminalHttpLinks(line).map((link) => link.url) @@ -32,4 +32,19 @@ describe('extractTerminalHttpLinks', () => { expect(urlsIn('https://example.com/b。')).toEqual(['https://example.com/b']) expect(urlsIn('詳細はhttps://example.com/docs……')).toEqual(['https://example.com/docs']) }) + + it.each([ + ['http://[::1]', 'http://[::1]'], + ['see http://[::1]. now', 'http://[::1]'], + ['Server at http://[::1]: ready', 'http://[::1]'], + ['(http://user@[::1])', 'http://user@[::1]'], + ['http://[::1]:5173/app]', 'http://[::1]:5173/app'], + ['[see http://example.com/a[b]]', 'http://example.com/a[b'] + ])('keeps only an IPv6 host closing bracket in %j', (line, expected) => { + // Why: xterm's WebLinksAddon matches with the regex; clicks and hard wraps use the extractor. + expect(line.match(TERMINAL_HTTP_URL_REGEX)?.[0]).toBe(expected) + expect( + extractTerminalHttpLinks(line).map((link) => line.slice(link.startIndex, link.endIndex)) + ).toEqual([expected]) + }) }) diff --git a/src/shared/terminal-http-url-extraction.ts b/src/shared/terminal-http-url-extraction.ts index d00470bc3ee..8ce625581db 100644 --- a/src/shared/terminal-http-url-extraction.ts +++ b/src/shared/terminal-http-url-extraction.ts @@ -1,5 +1,11 @@ import { TERMINAL_HTTP_URL_MAX_LENGTH } from './terminal-http-link-limits' -import { isProseUrlTerminator, PROSE_HTTP_URL_SOURCE } from './prose-url-boundary' +import { + IPV6_HTTP_HOST_SOURCE, + isProseUrlTerminator, + PROSE_HTTP_URL_SOURCE +} from './prose-url-boundary' + +const IPV6_HTTP_AUTHORITY = new RegExp(String.raw`^https?:\/\/${IPV6_HTTP_HOST_SOURCE}`, 'iu') type ParsedTerminalHttpLink = { url: string @@ -82,8 +88,14 @@ function trimHttpUrlTrailingPunctuation( startIndex: number, rawEndIndex: number ): number { + const minEndIndex = + startIndex + + (IPV6_HTTP_AUTHORITY.exec(lineText.slice(startIndex, rawEndIndex))?.[0].length ?? 0) let endIndex = rawEndIndex - while (endIndex > startIndex && isHttpUrlTrailingPunctuation(lineText.charCodeAt(endIndex - 1))) { + while ( + endIndex > minEndIndex && + isHttpUrlTrailingPunctuation(lineText.charCodeAt(endIndex - 1)) + ) { endIndex -= 1 } return endIndex