-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
A few fixes in convertToTspans #736
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,64 +6,133 @@ var util = require('@src/lib/svg_text_utils'); | |
describe('svg+text utils', function() { | ||
'use strict'; | ||
|
||
describe('convertToTspans', function() { | ||
describe('convertToTspans should', function() { | ||
|
||
function mockTextSVGElement(txt) { | ||
return d3.select('body') | ||
.append('svg') | ||
.attr('id', 'text') | ||
.append('text') | ||
.text(txt) | ||
.call(util.convertToTspans); | ||
.call(util.convertToTspans) | ||
.attr('transform', 'translate(50,50)'); | ||
} | ||
|
||
function assertAnchorLink(node, href) { | ||
var a = node.select('a'); | ||
|
||
expect(a.attr('xlink:href')).toBe(href); | ||
expect(a.attr('xlink:show')).toBe(href === null ? null : 'new'); | ||
} | ||
|
||
function assertAnchorAttrs(node) { | ||
var a = node.select('a'); | ||
|
||
var WHITE_LIST = ['xlink:href', 'xlink:show', 'style'], | ||
attrs = listAttributes(a.node()); | ||
|
||
// check that no other attribute are found in anchor, | ||
// which can be lead to XSS attacks. | ||
|
||
var hasWrongAttr = attrs.some(function(attr) { | ||
return WHITE_LIST.indexOf(attr) === -1; | ||
}); | ||
|
||
expect(hasWrongAttr).toBe(false); | ||
} | ||
|
||
function listAttributes(node) { | ||
var items = Array.prototype.slice.call(node.attributes); | ||
|
||
var attrs = items.map(function(item) { | ||
return item.name; | ||
}); | ||
|
||
return attrs; | ||
} | ||
|
||
afterEach(function() { | ||
d3.select('#text').remove(); | ||
}); | ||
|
||
it('checks for XSS attack in href', function() { | ||
it('check for XSS attack in href', function() { | ||
var node = mockTextSVGElement( | ||
'<a href="javascript:alert(\'attack\')">XSS</a>' | ||
); | ||
|
||
expect(node.text()).toEqual('XSS'); | ||
expect(node.select('a').attr('xlink:href')).toBe(null); | ||
assertAnchorAttrs(node); | ||
assertAnchorLink(node, null); | ||
}); | ||
|
||
it('checks for XSS attack in href (with plenty of white spaces)', function() { | ||
it('check for XSS attack in href (with plenty of white spaces)', function() { | ||
var node = mockTextSVGElement( | ||
'<a href = " javascript:alert(\'attack\')">XSS</a>' | ||
); | ||
|
||
expect(node.text()).toEqual('XSS'); | ||
expect(node.select('a').attr('xlink:href')).toBe(null); | ||
assertAnchorAttrs(node); | ||
assertAnchorLink(node, null); | ||
}); | ||
|
||
it('whitelists http hrefs', function() { | ||
it('whitelist http hrefs', function() { | ||
var node = mockTextSVGElement( | ||
'<a href="http://bl.ocks.org/">bl.ocks.org</a>' | ||
); | ||
|
||
expect(node.text()).toEqual('bl.ocks.org'); | ||
expect(node.select('a').attr('xlink:href')).toEqual('http://bl.ocks.org/'); | ||
assertAnchorAttrs(node); | ||
assertAnchorLink(node, 'http://bl.ocks.org/'); | ||
}); | ||
|
||
it('whitelists https hrefs', function() { | ||
it('whitelist https hrefs', function() { | ||
var node = mockTextSVGElement( | ||
'<a href="https://plot.ly">plot.ly</a>' | ||
); | ||
|
||
expect(node.text()).toEqual('plot.ly'); | ||
expect(node.select('a').attr('xlink:href')).toEqual('https://plot.ly'); | ||
assertAnchorAttrs(node); | ||
assertAnchorLink(node, 'https://plot.ly'); | ||
}); | ||
|
||
it('whitelists mailto hrefs', function() { | ||
it('whitelist mailto hrefs', function() { | ||
var node = mockTextSVGElement( | ||
'<a href="mailto:[email protected]">support</a>' | ||
); | ||
|
||
expect(node.text()).toEqual('support'); | ||
expect(node.select('a').attr('xlink:href')).toEqual('mailto:[email protected]'); | ||
assertAnchorAttrs(node); | ||
assertAnchorLink(node, 'mailto:[email protected]'); | ||
}); | ||
|
||
it('wrap XSS attacks in href', function() { | ||
var textCases = [ | ||
'<a href="XSS\" onmouseover="alert(1)\" style="font-size:300px">Subtitle</a>', | ||
'<a href="XSS" onmouseover="alert(1)" style="font-size:300px">Subtitle</a>' | ||
]; | ||
|
||
textCases.forEach(function(textCase) { | ||
var node = mockTextSVGElement(textCase); | ||
|
||
expect(node.text()).toEqual('Subtitle'); | ||
assertAnchorAttrs(node); | ||
assertAnchorLink(node, 'XSS onmouseover=alert(1) style=font-size:300px'); | ||
}); | ||
}); | ||
|
||
it('should keep query parameters in href', function() { | ||
var textCases = [ | ||
'<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>', | ||
'<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>' | ||
]; | ||
|
||
textCases.forEach(function(textCase) { | ||
var node = mockTextSVGElement(textCase); | ||
|
||
assertAnchorAttrs(node); | ||
expect(node.text()).toEqual('abc.com?shared-key'); | ||
assertAnchorLink(node, 'https://abc.com/myFeature.jsp?name=abc&pwd=def'); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scjody
The previous commit d58d3be also appears to have fix the XSS issue discovered in https://github.com/plotly/streambed/issues/7056 - would you mind reviewing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Let me know when it's on prod and I'll play with it a bit to be extra sure.