1
1
/*
2
2
Copyright 2015, 2016 OpenMarket Ltd
3
- Copyright 2017 New Vector Ltd
3
+ Copyright 2017, 2018 New Vector Ltd
4
4
5
5
Licensed under the Apache License, Version 2.0 (the "License");
6
6
you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import escape from 'lodash/escape';
25
25
import emojione from 'emojione' ;
26
26
import classNames from 'classnames' ;
27
27
import MatrixClientPeg from './MatrixClientPeg' ;
28
+ import url from 'url' ;
28
29
29
30
emojione . imagePathSVG = 'emojione/svg/' ;
30
31
// Store PNG path for displaying many flags at once (for increased performance over SVG)
@@ -44,6 +45,8 @@ const SYMBOL_PATTERN = /([\u2100-\u2bff])/;
44
45
const EMOJI_REGEX = new RegExp ( emojione . unicodeRegexp + "+" , "gi" ) ;
45
46
const COLOR_REGEX = / ^ # [ 0 - 9 a - f A - F ] { 6 } $ / ;
46
47
48
+ const PERMITTED_URL_SCHEMES = [ 'http' , 'https' , 'ftp' , 'mailto' , 'magnet' ] ;
49
+
47
50
/*
48
51
* Return true if the given string contains emoji
49
52
* Uses a much, much simpler regex than emojione's so will give false
@@ -152,6 +155,25 @@ export function sanitizedHtmlNode(insaneHtml) {
152
155
return < div dangerouslySetInnerHTML = { { __html : saneHtml } } dir = "auto" /> ;
153
156
}
154
157
158
+ /**
159
+ * Tests if a URL from an untrusted source may be safely put into the DOM
160
+ * The biggest threat here is javascript: URIs.
161
+ * Note that the HTML sanitiser library has its own internal logic for
162
+ * doing this, to which we pass the same list of schemes. This is used in
163
+ * other places we need to sanitise URLs.
164
+ * @return true if permitted, otherwise false
165
+ */
166
+ export function isUrlPermitted ( inputUrl ) {
167
+ try {
168
+ const parsed = url . parse ( inputUrl ) ;
169
+ if ( ! parsed . protocol ) return false ;
170
+ // URL parser protocol includes the trailing colon
171
+ return PERMITTED_URL_SCHEMES . includes ( parsed . protocol . slice ( 0 , - 1 ) ) ;
172
+ } catch ( e ) {
173
+ return false ;
174
+ }
175
+ }
176
+
155
177
const sanitizeHtmlParams = {
156
178
allowedTags : [
157
179
'font' , // custom to matrix for IRC-style font coloring
@@ -172,7 +194,7 @@ const sanitizeHtmlParams = {
172
194
// Lots of these won't come up by default because we don't allow them
173
195
selfClosing : [ 'img' , 'br' , 'hr' , 'area' , 'base' , 'basefont' , 'input' , 'link' , 'meta' ] ,
174
196
// URL schemes we permit
175
- allowedSchemes : [ 'http' , 'https' , 'ftp' , 'mailto' , 'magnet' ] ,
197
+ allowedSchemes : PERMITTED_URL_SCHEMES ,
176
198
177
199
allowProtocolRelative : false ,
178
200
0 commit comments