As I understand $sce
in Angular, the default behavior is to:
AngularJS will automatically run security checks on [sensitive HTML] (sanitizations, whitelists, depending on context
Also, as I understand Whitelisting (via $sceDelegateProvider.resourceUrlWhitelist
)prevents sanitization.
Basically, I've got an app where trustasresourceurl is globally enabled (see accepted answer). Now, I want to exclude 1 controller from this global trust. In other words, I want to display this content as plain text with none of the styling bound to ng-bind-html
:
<span style="font-family: Arial; font-size: 24px;"> A wizard is never late hardcode.</span>
I may need to create a filter to manually remove any HTML, but I hate to do that just to get back to the default angular behavior. Parsing HTML with regex is ugly and fragile. What do you suggest?
This code is close to what I want, but for whatever the style is removed so it fails to properly reproduce the problem. I'm guessing the whitelist failed so it's still sanitized.
var mainMod = angular.module('MainApp', ['ngSanitize']);
mainMod.controller('MainCtrl', ['$scope',
function ($scope) {
$scope.text = '<p>Hello! <a href="#">Link</a></p>';
$scope.htmlContent = '<span style="font-family: Arial; font-size: 24px;"> A wizard is never late.</span>';
}]);
angular.module('MainCtrl', []).config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
// Allow same origin resource loads.
'self',
// Allow loading from our assets domain. Notice the difference between * and **.
'https://codepen.io/**'
]);
// The blacklist overrides the whitelist so the open redirect here is blocked. I don't want to blocklist the whole resource. Just this one controller.
$sceDelegateProvider.resourceUrlBlacklist([
''
]);
});
body{
padding:20px;
}
.search{
margin-left:10px;
}
<script src="https://code.angularjs.org/1.5.11/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.11/angular-sanitize.min.js"></script>
<body ng-app='MainApp'>
<div ng-controller="MainCtrl">
<div ng-bind-html="htmlContent"></div>
<div ng-bind-html='text'></div>
<span style="font-family: Arial; font-size: 24px;"> A wizard is never late hardcode. In real app this is what I get. </span>
</div>
</body>
Angular 1.5.11