1- // Must start with a valid scheme
2- // ^
3- // Schemes that are considered safe
4- // (https?|s?ftp|mailto|spotify|ssh|teamspeak|tel):|
5- // Relative schemes (//:) are considered safe
6- // (\\/\\/)|
7- // Image data URI's are considered safe
8- // data:image\\/(png|bmp|gif|p?jpe?g);
9- var VALID_SCHEME_REGEX =
10- / ^ ( h t t p s ? | s ? f t p | m a i l t o | s p o t i f y | s s h | t e a m s p e a k | t e l ) : | ( \/ \/ ) | d a t a : i m a g e \/ ( p n g | b m p | g i f | p ? j p e ? g ) ; / i;
1+ // Regex used by DOMPurify to filter URLs. Might as well match here as otherwise
2+ // URLs will be filtered out by DOMPurify anyway
3+ var VALID_URI_REGEX = / ^ (?: (?: (?: f | h t ) t p s ? | m a i l t o | t e l | c a l l t o | s m s | c i d | x m p p | m a t r i x ) : | [ ^ a - z ] | [ a - z + . \- ] + (?: [ ^ a - z + . \- : ] | $ ) ) / i;
4+ // Safe image data URIs
5+ var VALID_DATA_REGEX = / ^ d a t a : i m a g e \/ ( p n g | b m p | g i f | p ? j p e ? g ) ; / i;
6+ var WHITESPACE_REGEX = / [ \u0000 - \u0020 \u00A0 \u1680 \u180E \u2000 - \u2029 \u205F \u3000 ] / g;
117
128/**
139 * Escapes a string so it's safe to use in regex
@@ -66,14 +62,18 @@ export function entities(str, noQuotes) {
6662 *
6763 * http
6864 * https
69- * sftp
65+ * ftps
7066 * ftp
7167 * mailto
7268 * spotify
7369 * ssh
7470 * teamspeak
7571 * tel
76- * //
72+ * callto
73+ * sms
74+ * cid
75+ * xmpp
76+ * matrix
7777 * data:image/(png|jpeg|jpg|pjpeg|bmp|gif);
7878 *
7979 * **IMPORTANT**: This does not escape any HTML in a url, for
@@ -84,21 +84,28 @@ export function entities(str, noQuotes) {
8484 * @since 1.4.5
8585 */
8686export function uriScheme ( url ) {
87- const hasScheme = / ^ [ ^ \/ ] * : / i ;
88- const location = window . location ;
87+ var path ,
88+ location = window . location ;
8989
90- // Has no scheme or a valid scheme
91- if ( ( ! url || ! hasScheme . test ( url ) ) || VALID_SCHEME_REGEX . test ( url ) ) {
90+ // Match previous behaviour for empty or data: URIs
91+ if ( ! url || VALID_DATA_REGEX . test ( url ) ) {
9292 return url ;
9393 }
9494
95- const path = location . pathname . split ( '/' ) ;
96- path . pop ( ) ;
95+ // Invalid scheme so make relative
96+ if ( ! VALID_URI_REGEX . test ( url . replace ( WHITESPACE_REGEX , '' ) ) ) {
97+ path = location . pathname . split ( '/' ) ;
98+ path . pop ( ) ;
9799
98- return location . protocol +
99- '//' +
100- location . host +
101- path . join ( '/' ) +
102- '/' +
103- url ;
100+ url = location . protocol + '//' +
101+ location . host +
102+ path . join ( '/' ) + '/' +
103+ url ;
104+
105+ if ( ! VALID_URI_REGEX . test ( url . replace ( WHITESPACE_REGEX , '' ) ) ) {
106+ return '' ;
107+ }
108+ }
109+
110+ return url ;
104111} ;
0 commit comments