From de32da49f310410c994d1b589b7c5de434ca49d4 Mon Sep 17 00:00:00 2001 From: or-else Date: Fri, 15 Dec 2023 14:27:05 -0800 Subject: [PATCH] make sure only safe schemes are clickable in URLs --- src/config.js | 3 +++ src/lib/formatters.js | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index 146422d3..66e8d34f 100644 --- a/src/config.js +++ b/src/config.js @@ -126,3 +126,6 @@ export const LINK_TERMS_OF_SERVICE = 'https://tinode.co/terms.html'; // happen until the first message. Setting this option to true enables // immediate subscription. export const IMMEDIATE_P2P_SUBSCRIPTION = false; + +// URL schemes permitted to appear in links (i.e. no javascript: scheme). +export const CLICKABLE_URL_SCHEMES = ['http', 'https', 'ftp', 'ftps']; diff --git a/src/lib/formatters.js b/src/lib/formatters.js index 1b13322b..8ef1b714 100644 --- a/src/lib/formatters.js +++ b/src/lib/formatters.js @@ -10,10 +10,11 @@ import InlineVideo from '../widgets/inline-video.jsx'; import LazyImage from '../widgets/lazy-image.jsx' import UploadingImage from '../widgets/uploading-image.jsx' -import { BROKEN_IMAGE_SIZE, IMAGE_THUMBNAIL_DIM, NO_DIMENSIONS_VIDEO, REM_SIZE, VIDEO_THUMBNAIL_WIDTH } from '../config.js'; +import { BROKEN_IMAGE_SIZE, CLICKABLE_URL_SCHEMES, IMAGE_THUMBNAIL_DIM, NO_DIMENSIONS_VIDEO, REM_SIZE, + VIDEO_THUMBNAIL_WIDTH } from '../config.js'; import { base64ToBlob, blobToBase64, fitImageSize, imageScaled } from './blob-helpers.js'; import { idToColorClass, secondsToTime, shortenFileName } from './strformat.js'; -import { cancelablePromise, sanitizeUrlForMime } from './utils.js'; +import { cancelablePromise, sanitizeUrl, sanitizeUrlForMime } from './utils.js'; const messages = defineMessages({ drafty_form: { @@ -109,6 +110,12 @@ export function fullFormatter(style, data, values, key, stack) { // Get text which will be sent back when the button is clicked. attr['data-title'] = inner.join(''); break; + case 'LN': + if (attr) { + // Ensure only safe URL schemes are clickable. + attr.href = typeof attr.href == 'string' ? sanitizeUrl(attr.href, CLICKABLE_URL_SCHEMES) : ''; + } + break; case 'MN': // Mention attr.className = 'mention'