javascript - Handling XSS in AngularJS -
i in need of assistance , help/guidance/suggestion highly appreciated.
as title says, it's related xss, when user enters - ">alert("hello") input application throwing alert.
scenario:
i have gridview list data, in gridview not display data instead display links enity user can click on individual entity , user navigated respective entity.
this gridview generic, takes headers, number of columns diaplayed loaded dynamically. below sample code snippet:
angular.module("pt-sample").directive("ptstaticgridcell", [ "$compile", "$timeout", "$log","$sce", function($compile, $timeout, $log, $sce) { return { scope: { column: "=staticgridcell", object: "=", renderers: "=?" }, replace: !0, link: function (scope, element) { function render(template) { element.empty(); if (_.isobject(template) && template.hasownproperty("value")) { if (_.isstring(template.value) && template.value.length > 0) { element.append($compile("<div>" + template.value + "</div>")(scope)); } } } render(template); } } }
}
problem: in above directive problem is, "template.value". sample data of "template.value": case 1 = "><script >alert("hello")</script >
case 2 = `<div pt-token="object"></div>` case 3 = `<i ng-class="geticonclass()" pt-metadata-css="object" pt-trigger-tooltip="entitytypename"></i>` case 4 = `<a type="button" pt-trigger-tooltip="value.dictkey | i18n" ng-click="value.action()" ' + cssclass + '><i ng-class="value.icon"></i></a>`
here, "template.value" value or html element or anchor tag. need same directive work in these cases. issue here below line
element.append($compile("<div>" + template.value + "</div>")(scope));
'$compile' executing ever comes in , in case alert() popped in "case1".
approaches tried: 1) altered case below, in case inner html elements not executed, gets rendered (like,<div pt-token="object"></div>
)
var e = angular.element("<div>").text(template.value); element.append($compile(e)(scope));
2) tried changing below, works fine but, need go across application , pass additional property 'nonexecutable' specify should executed or not. not willing this.
if (scope.column.nonexecutable === true) { var elem = angular.element("<div>").text(template.value); element.append(elem); } else { var e = angular.element("<div>").html(template.value); element.append($compile(e)(scope)); }
can please me this? how should handle this?
thanks in advance!
Comments
Post a Comment