Skip to content

Commit

Permalink
Mitigation of Mutation XSS attacks [WIP] [fixes #88]
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtech-dobes committed May 24, 2014
1 parent 1a80224 commit 90199b0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions nette.ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,53 @@ $.nette.ext('snippets', {
} else if (!back && $el.is('[data-ajax-prepend]')) {
$el.prepend(html);
} else {
this.setHtml($el, html);
}
},
setHtml: function ($el, html) {
if (this.isMXSSMitigationPossible()) {
this.mitigateMXSS($el.get(0));
$el.html(html);
} else {
// ... @todo
}
},
escapeSelector: function (selector) {
// thx to @uestla (https://github.com/uestla)
return selector.replace(/[\!"#\$%&'\(\)\*\+,\.\/:;<=>\?@\[\\\]\^`\{\|\}~]/g, '\\$&');
},
mitigateMXSS: function (element) {
var that = this;
if (typeof element.innerHTML === 'string') {
Object.defineProperty(element, 'innerHTML', {
get: function () { return that.changeInnerHtmlHandler(this, 'innerHTML') },
set: function (html) {
while (this.firstChild) {
this.removeChild(this.lastChild);
}
this.insertAdjacentHTML('afterBegin', html);
}
});
}
},
changeInnerHtmlHandler: function (element, type) {
var serializer = new XMLSerializer();
var domstring = '';
if (type === 'outerHTML') {
try {
domstring += serializer.serializeToString(element);
} catch(e) {}
} else {
for (var i in element.childNodes) {
try {
domstring += serializer.serializeToString(element.childNodes[i]);
} catch(e) {}
}
}
return domstring;
},
isMXSSMitigationPossible: function () {
// ... @todo
}
});

Expand Down

1 comment on commit 90199b0

@xeno6696
Copy link

Choose a reason for hiding this comment

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

On line 448 you make a call to check if a mitigation is possible, but the defined function is blank if you look at line 489. If you're relying upon javascript to default a return value of false, I'd recommend explicitly setting a return value here.

Please sign in to comment.