Skip to content

Commit 902da28

Browse files
authored
Merge pull request #4324 from Ry0taK/feature/secure-links-by-default
Add allowNonHttpProtocols option to ILinkHandler
2 parents 0b56255 + b3d3647 commit 902da28

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/browser/OscLinkProvider.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,30 @@ export class OscLinkProvider implements ILinkProvider {
6666
y
6767
}
6868
};
69-
// OSC links always use underline and pointer decorations
70-
result.push({
71-
text,
72-
range,
73-
activate: (e, text) => (linkHandler ? linkHandler.activate(e, text, range) : defaultActivate(e, text)),
74-
hover: (e, text) => linkHandler?.hover?.(e, text, range),
75-
leave: (e, text) => linkHandler?.leave?.(e, text, range)
76-
});
69+
70+
let ignoreLink = false;
71+
if (!linkHandler?.allowNonHttpProtocols) {
72+
try {
73+
const parsed = new URL(text);
74+
if (!['http:', 'https:'].includes(parsed.protocol)) {
75+
ignoreLink = true;
76+
}
77+
} catch (e) {
78+
// Ignore invalid URLs to prevent unexpected behaviors
79+
ignoreLink = true;
80+
}
81+
}
82+
83+
if (!ignoreLink) {
84+
// OSC links always use underline and pointer decorations
85+
result.push({
86+
text,
87+
range,
88+
activate: (e, text) => (linkHandler ? linkHandler.activate(e, text, range) : defaultActivate(e, text)),
89+
hover: (e, text) => linkHandler?.hover?.(e, text, range),
90+
leave: (e, text) => linkHandler?.leave?.(e, text, range)
91+
});
92+
}
7793
}
7894
finishLink = false;
7995

typings/xterm.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,13 @@ declare module 'xterm' {
11791179
* @param range The buffer range of the link.
11801180
*/
11811181
leave?(event: MouseEvent, text: string, range: IBufferRange): void;
1182+
1183+
/**
1184+
* Whether to receive non-HTTP URLs from LinkProvider. When false, any usage of non-HTTP URLs
1185+
* will be ignored. Enabling this option without proper protection in `activate` function
1186+
* may cause security issues such as XSS.
1187+
*/
1188+
allowNonHttpProtocols?: boolean;
11821189
}
11831190

11841191
/**

0 commit comments

Comments
 (0)