Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XSS in trace name on hover #1307

Merged
merged 2 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ module.exports = function(text, displayLength) {
note.transition().call(killNote);
});

note.append('p').html(thisText);
var p = note.append('p');
var lines = thisText.split(/<br\s*\/?>/g);
for(var i = 0; i < lines.length; i++) {
if(i) p.append('br');
p.append('span').text(lines[i]);
}

note.transition()
.duration(700)
Expand Down
1 change: 1 addition & 0 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports.html_entity_decode = function(s) {
var replaced = s.replace(/(&[^;]*;)/gi, function(d) {
if(d === '&lt;') { return '&#60;'; } // special handling for brackets
if(d === '&rt;') { return '&#62;'; }
if(d.indexOf('<') !== -1 || d.indexOf('>') !== -1) { return ''; }
return hiddenDiv.html(d).text(); // everything else, let the browser decode it to unicode
});
hiddenDiv.remove();
Expand Down
8 changes: 2 additions & 6 deletions src/plots/cartesian/graph_interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,8 @@ function createHoverText(hoverData, opts) {


if(d.name && d.zLabelVal === undefined) {
// strip out any html elements from d.name (if it exists at all)
// Note that this isn't an XSS vector, only because it never gets
// attached to the DOM
var tmp = document.createElement('p');
tmp.innerHTML = d.name;
name = tmp.textContent || '';
// strip out our pseudo-html elements from d.name (if it exists at all)
name = svgTextUtils.plainText(d.name || '');

if(name.length > 15) name = name.substr(0, 12) + '...';
}
Expand Down
39 changes: 39 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,45 @@ describe('hover info', function() {
});
});

describe('hover info with bad name', function() {
var mockCopy = Lib.extendDeep({}, mock);

mockCopy.data[0].text = [];
mockCopy.data[0].text[17] = 'hover text';
mockCopy.data[0].hoverinfo = 'all';
mockCopy.data[0].name = '<img src=x onerror=y>';
mockCopy.data.push({
x: [0.002, 0.004],
y: [12.5, 16.25],
mode: 'lines+markers',
name: 'another trace'
});

beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});

it('cleans the name', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');

var hoverTrace = gd._hoverdata[0];

expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);

expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
expect(d3.selectAll('g.hovertext').select('text.nums').selectAll('tspan').size()).toEqual(2);
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][0].innerHTML).toEqual('1');
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][1].innerHTML).toEqual('hover text');
expect(d3.selectAll('g.hovertext').selectAll('text.name').node().innerHTML).toEqual('&lt;img src=x o...');
});
});

describe('hover info y+text', function() {
var mockCopy = Lib.extendDeep({}, mock);

Expand Down