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 <neil@stably.ai>
This commit is contained in:
PM
2026-11-02 20:07:14 -08:00
committed by GitHub
co-authored by Neil
parent 8fba3704bd
commit 438fb753ab
3 changed files with 39 additions and 6 deletions
+9 -3
View File
@@ -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')
@@ -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])
})
})
+14 -2
View File
@@ -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