Skip to content

Commit 9e2f251

Browse files
committed
HTML encode attributes in <tspan>s and <a>s
I don't believe this is necessary for security, but it makes our code more obviously secure.
1 parent 5904af2 commit 9e2f251

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/lib/svg_text_utils.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ util.plainText = function(_str) {
242242
return (_str || '').replace(STRIP_TAGS, ' ');
243243
};
244244

245+
function encodeForHTML(_str) {
246+
return (_str || '').replace(/&/g, '&amp;')
247+
.replace(/</g, '&lt;')
248+
.replace(/>/g, '&gt;')
249+
.replace(/"/g, '&quot;')
250+
.replace(/'/g, '&#x27;')
251+
.replace(/\//g, '&#x2F;');
252+
}
253+
245254
function convertToSVG(_str) {
246255
var htmlEntitiesDecoded = Plotly.util.html_entity_decode(_str);
247256
var result = htmlEntitiesDecoded
@@ -270,15 +279,14 @@ function convertToSVG(_str) {
270279
// remove quotes, leading '=', replace '&' with '&amp;'
271280
var href = extra.substr(4)
272281
.replace(/["']/g, '')
273-
.replace(/=/, '')
274-
.replace(/&/g, '&amp;');
282+
.replace(/=/, '');
275283

276284
// check protocol
277285
var dummyAnchor = document.createElement('a');
278286
dummyAnchor.href = href;
279287
if(PROTOCOLS.indexOf(dummyAnchor.protocol) === -1) return '<a>';
280288

281-
return '<a xlink:show="new" xlink:href="' + href + '">';
289+
return '<a xlink:show="new" xlink:href="' + encodeForHTML(href) + '">';
282290
}
283291
}
284292
else if(tag === 'br') return '<br>';
@@ -302,7 +310,7 @@ function convertToSVG(_str) {
302310
// most of the svg css users will care about is just like html,
303311
// but font color is different. Let our users ignore this.
304312
extraStyle = extraStyle[1].replace(/(^|;)\s*color:/, '$1 fill:');
305-
style = (style ? style + ';' : '') + extraStyle;
313+
style = (style ? style + ';' : '') + encodeForHTML(extraStyle);
306314
}
307315

308316
return tspanStart + (style ? ' style="' + style + '"' : '') + '>';

0 commit comments

Comments
 (0)