@@ -604,14 +604,9 @@ function buildSVGText(containerNode, str) {
604
604
var href = getQuotedMatch ( extra , HREFMATCH ) ;
605
605
606
606
if ( href ) {
607
- // check safe protocols
608
- var dummyAnchor = document . createElement ( 'a' ) ;
609
- dummyAnchor . href = href ;
610
- if ( PROTOCOLS . indexOf ( dummyAnchor . protocol ) !== - 1 ) {
611
- // Decode href to allow both already encoded and not encoded
612
- // URIs. Without decoding prior encoding, an already encoded
613
- // URI would be encoded twice producing a semantically different URI.
614
- nodeSpec . href = encodeURI ( decodeURI ( href ) ) ;
607
+ var safeHref = sanitizeHref ( href ) ;
608
+ if ( safeHref ) {
609
+ nodeSpec . href = safeHref ;
615
610
nodeSpec . target = getQuotedMatch ( extra , TARGETMATCH ) || '_blank' ;
616
611
nodeSpec . popup = getQuotedMatch ( extra , POPUPMATCH ) ;
617
612
}
@@ -626,6 +621,27 @@ function buildSVGText(containerNode, str) {
626
621
return hasLink ;
627
622
}
628
623
624
+ function sanitizeHref ( href ) {
625
+ var decodedHref = encodeURI ( decodeURI ( href ) ) ;
626
+ var dummyAnchor1 = document . createElement ( 'a' ) ;
627
+ var dummyAnchor2 = document . createElement ( 'a' ) ;
628
+ dummyAnchor1 . href = href ;
629
+ dummyAnchor2 . href = decodedHref ;
630
+
631
+ var p1 = dummyAnchor1 . protocol ;
632
+ var p2 = dummyAnchor2 . protocol ;
633
+
634
+ // check safe protocols
635
+ if (
636
+ PROTOCOLS . indexOf ( p1 ) !== - 1 &&
637
+ PROTOCOLS . indexOf ( p2 ) !== - 1
638
+ ) {
639
+ return decodedHref ;
640
+ } else {
641
+ return '' ;
642
+ }
643
+ }
644
+
629
645
/*
630
646
* sanitizeHTML: port of buildSVGText aimed at providing a clean subset of HTML
631
647
* @param {string } str: the html string to clean
@@ -660,10 +676,9 @@ exports.sanitizeHTML = function sanitizeHTML(str) {
660
676
var href = getQuotedMatch ( extra , HREFMATCH ) ;
661
677
662
678
if ( href ) {
663
- var dummyAnchor = document . createElement ( 'a' ) ;
664
- dummyAnchor . href = href ;
665
- if ( PROTOCOLS . indexOf ( dummyAnchor . protocol ) !== - 1 ) {
666
- nodeAttrs . href = encodeURI ( decodeURI ( href ) ) ;
679
+ var safeHref = sanitizeHref ( href ) ;
680
+ if ( safeHref ) {
681
+ nodeAttrs . href = safeHref ;
667
682
var target = getQuotedMatch ( extra , TARGETMATCH ) ;
668
683
if ( target ) {
669
684
nodeAttrs . target = target ;
0 commit comments