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

Commit 712bdba

Browse files
authored
Merge pull request #6026 from matrix-org/t3chguy/fix/17281
Wrap decodeURIComponent in try-catch to protect against malformed URIs
2 parents 52420fe + dd04b47 commit 712bdba

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/linkify-matrix.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,15 @@ matrixLinkify.options = {
254254

255255
target: function(href, type) {
256256
if (type === 'url') {
257-
const transformed = tryTransformPermalinkToLocalHref(href);
258-
if (transformed !== href || decodeURIComponent(href).match(matrixLinkify.ELEMENT_URL_PATTERN)) {
259-
return null;
260-
} else {
261-
return '_blank';
257+
try {
258+
const transformed = tryTransformPermalinkToLocalHref(href);
259+
if (transformed !== href || decodeURIComponent(href).match(matrixLinkify.ELEMENT_URL_PATTERN)) {
260+
return null;
261+
} else {
262+
return '_blank';
263+
}
264+
} catch (e) {
265+
// malformed URI
262266
}
263267
}
264268
return null;

src/utils/permalinks/Permalinks.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,14 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string {
346346
return permalink;
347347
}
348348

349-
const m = decodeURIComponent(permalink).match(matrixLinkify.ELEMENT_URL_PATTERN);
350-
if (m) {
351-
return m[1];
349+
try {
350+
const m = decodeURIComponent(permalink).match(matrixLinkify.ELEMENT_URL_PATTERN);
351+
if (m) {
352+
return m[1];
353+
}
354+
} catch (e) {
355+
// Not a valid URI
356+
return permalink;
352357
}
353358

354359
// A bit of a hack to convert permalinks of unknown origin to Element links

0 commit comments

Comments
 (0)