Skip to content

safer construction of popup click handler #1888

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 1 commit into from
Jul 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ var BR_TAG = /<br(\s+.*)?>/i;
var STYLEMATCH = /(^|[\s"'])style\s*=\s*("([^"]*);?"|'([^']*);?')/i;
var HREFMATCH = /(^|[\s"'])href\s*=\s*("([^"]*)"|'([^']*)')/i;
var TARGETMATCH = /(^|[\s"'])target\s*=\s*("([^"\s]*)"|'([^'\s]*)')/i;
var POPUPMATCH = /(^|[\s"'])popup\s*=\s*("([^"\s]*)"|'([^'\s]*)')/i;
var POPUPMATCH = /(^|[\s"'])popup\s*=\s*("([\w=,]*)"|'([\w=,]*)')/i;

// dedicated matcher for these quoted regexes, that can return their results
// in two different places
Expand Down Expand Up @@ -360,7 +360,9 @@ function buildSVGText(containerNode, str) {
'xlink:xlink:href': href
};
if(popup) {
nodeAttrs.onclick = 'window.open("' + href + '","' + target + '","' +
// security: href and target are not inserted as code but
// as attributes. popup is, but limited to /[A-Za-z0-9_=,]/
nodeAttrs.onclick = 'window.open(this.href.baseVal,this.target.baseVal,"' +
popup + '");return false;';
}
}
Expand Down Expand Up @@ -459,7 +461,7 @@ function buildSVGText(containerNode, str) {
var dummyAnchor = document.createElement('a');
dummyAnchor.href = href;
if(PROTOCOLS.indexOf(dummyAnchor.protocol) !== -1) {
nodeSpec.href = href;
nodeSpec.href = encodeURI(href);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I couldn't identify any vulnerability due to not calling encodeURI here - quotes and other special characters embedded in href still can't break you out of the href attribute (while constructing the onclick string or otherwise) to where you can cause more damage - but it makes strange items look more url-like.

nodeSpec.target = getQuotedMatch(extra, TARGETMATCH) || '_blank';
nodeSpec.popup = getQuotedMatch(extra, POPUPMATCH);
}
Expand Down
33 changes: 32 additions & 1 deletion test/jasmine/tests/svg_text_utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,38 @@ describe('svg+text utils', function() {
it('attaches onclick if popup is specified', function() {
var node = mockTextSVGElement('<a href="x" target="fred" popup="width=500,height=400">link</a>');
assertAnchorLink(node, 'x', 'fred', 'new');
assertAnchorAttrs(node, {onclick: 'window.open("x","fred","width=500,height=400");return false;'});
assertAnchorAttrs(node, {onclick: 'window.open(this.href.baseVal,this.target.baseVal,"width=500,height=400");return false;'});
});

it('drops XSS attacks via popup script', function() {
var textCases = [
[
'<a href=\'#\' target=\'b\' popup=\'1");alert(document.cookie);//\'>XSS</a>',
'#', 'b', null
],
[
'<a href=\'#\' target=\'b");alert(document.cookie);//\' popup=\'1\'>XSS</a>',
'#', 'b");alert(document.cookie);//', '1'
],
[
'<a href=\'#");alert(document.cookie);//\' target=\'b\' popup=\'1\'>XSS</a>',
'#%22);alert(document.cookie);//', 'b', '1'
]
];

textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase[0]);

var attrs = {};
if(textCase[3]) {
attrs.onclick = 'window.open(this.href.baseVal,this.target.baseVal,"' +
textCase[3] + '");return false;';
}

expect(node.text()).toEqual('XSS');
assertAnchorAttrs(node, attrs, textCase[0]);
assertAnchorLink(node, textCase[1], textCase[2], 'new', textCase[0]);
});
});

it('keeps query parameters in href', function() {
Expand Down