Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 186497a

Browse files
authored
Handle all permitted url schemes in linkify (#11215)
* Handle all permitted url schemes in linkify * Correctly handle optional slash protocols * Iterate
1 parent 58710d1 commit 186497a

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-30
lines changed

src/HtmlUtils.tsx

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { tryTransformPermalinkToLocalHref } from "./utils/permalinks/Permalinks"
4242
import { getEmojiFromUnicode } from "./emoji";
4343
import { mediaFromMxc } from "./customisations/Media";
4444
import { stripHTMLReply, stripPlainReply } from "./utils/Reply";
45+
import { PERMITTED_URL_SCHEMES } from "./utils/UrlUtils";
4546

4647
// Anything outside the basic multilingual plane will be a surrogate pair
4748
const SURROGATE_PAIR_PATTERN = /([\ud800-\udbff])([\udc00-\udfff])/;
@@ -58,34 +59,6 @@ const BIGEMOJI_REGEX = new RegExp(`^(${EMOJIBASE_REGEX.source})+$`, "i");
5859

5960
const COLOR_REGEX = /^#[0-9a-fA-F]{6}$/;
6061

61-
export const PERMITTED_URL_SCHEMES = [
62-
"bitcoin",
63-
"ftp",
64-
"geo",
65-
"http",
66-
"https",
67-
"im",
68-
"irc",
69-
"ircs",
70-
"magnet",
71-
"mailto",
72-
"matrix",
73-
"mms",
74-
"news",
75-
"nntp",
76-
"openpgp4fpr",
77-
"sip",
78-
"sftp",
79-
"sms",
80-
"smsto",
81-
"ssh",
82-
"tel",
83-
"urn",
84-
"webcal",
85-
"wtai",
86-
"xmpp",
87-
];
88-
8962
const MEDIA_API_MXC_REGEX = /\/_matrix\/media\/r0\/(?:download|thumbnail)\/(.+?)\/(.+?)(?:[?/]|$)/;
9063

9164
/*

src/linkify-matrix.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Action } from "./dispatcher/actions";
3131
import { ViewUserPayload } from "./dispatcher/payloads/ViewUserPayload";
3232
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
3333
import { MatrixClientPeg } from "./MatrixClientPeg";
34+
import { PERMITTED_URL_SCHEMES } from "./utils/UrlUtils";
3435

3536
export enum Type {
3637
URL = "url",
@@ -242,7 +243,32 @@ registerPlugin(Type.UserId, ({ scanner, parser }) => {
242243
});
243244
});
244245

245-
registerCustomProtocol("matrix", true);
246+
// Linkify supports some common protocols but not others, register all permitted url schemes if unsupported
247+
// https://github.com/Hypercontext/linkifyjs/blob/f4fad9df1870259622992bbfba38bfe3d0515609/packages/linkifyjs/src/scanner.js#L133-L141
248+
// This also handles registering the `matrix:` protocol scheme
249+
const linkifySupportedProtocols = ["file", "mailto", "http", "https", "ftp", "ftps"];
250+
const optionalSlashProtocols = [
251+
"bitcoin",
252+
"geo",
253+
"im",
254+
"magnet",
255+
"mailto",
256+
"matrix",
257+
"news",
258+
"openpgp4fpr",
259+
"sip",
260+
"sms",
261+
"smsto",
262+
"tel",
263+
"urn",
264+
"xmpp",
265+
];
266+
267+
PERMITTED_URL_SCHEMES.forEach((scheme) => {
268+
if (!linkifySupportedProtocols.includes(scheme)) {
269+
registerCustomProtocol(scheme, optionalSlashProtocols.includes(scheme));
270+
}
271+
});
246272

247273
export const linkify = linkifyjs;
248274
export const _linkifyElement = linkifyElement;

src/utils/Reply.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
2424
import { M_POLL_END, M_POLL_START } from "matrix-js-sdk/src/@types/polls";
2525
import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent";
2626

27-
import { PERMITTED_URL_SCHEMES } from "../HtmlUtils";
27+
import { PERMITTED_URL_SCHEMES } from "./UrlUtils";
2828
import { makeUserPermalink, RoomPermalinkCreator } from "./permalinks/Permalinks";
2929
import { isSelfLocation } from "./location";
3030

src/utils/UrlUtils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,31 @@ export function parseUrl(u: string): URL {
5757
}
5858
return new URL(u);
5959
}
60+
61+
export const PERMITTED_URL_SCHEMES = [
62+
"bitcoin",
63+
"ftp",
64+
"geo",
65+
"http",
66+
"https",
67+
"im",
68+
"irc",
69+
"ircs",
70+
"magnet",
71+
"mailto",
72+
"matrix",
73+
"mms",
74+
"news",
75+
"nntp",
76+
"openpgp4fpr",
77+
"sip",
78+
"sftp",
79+
"sms",
80+
"smsto",
81+
"ssh",
82+
"tel",
83+
"urn",
84+
"webcal",
85+
"wtai",
86+
"xmpp",
87+
];

0 commit comments

Comments
 (0)