How can I do something like this: $sce.trustAsResourceUrl('URL_HERE');
Globally, like in the main app's config()
or run()
functions so that any iFrames, img src etc that have URL_HERE
will work?
Docs are rather poor at explaining this.
Strict Contextual Escaping (SCE) is a mode in which AngularJS constrains bindings to only render trusted values. Its goal is to assist in writing code in a way that (a) is secure by default, and (b) makes auditing for security vulnerabilities such as XSS, clickjacking, etc. a lot easier.
trustAsResourceUrl returns a special wrapper object for the external URL to mark the URL as trusted.
The ng-controller uses $sce (Strict Contextual Escaping) service which is used to mark the HTML as trusted using the trustAsHtml method. Note: Unless the HTML content is trusted using the $sce service, it will not be displayed using ng-bind-html directive.
trustAsHtml() produces a string that is safe to use with ng-bind-html" seems catastrophically misleading and incorrect. It yields a proxy for sanitisation bypass, and the result could be completely unsafe to use with ng-bind-html.
You could use a filter. These are available globally.
angular.module('myApp') .filter('trustUrl', function ($sce) { return function(url) { return $sce.trustAsResourceUrl(url); }; });
<img ng-src={{ imageHref | trustUrl }}">
I just read your comment from the previous answer. Not sure if you found a solution yet. Seems you are looking for a whitelist type of thing. I recently found this out that there's a whitelist function for $sce.
Taken from the AngularJS docs for $sceDelegateProvider:
angular.module('myApp', []).config(function($sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ // Allow same origin resource loads. 'self', // Allow loading from our assets domain. Notice the difference between * and **. 'http://srv*.assets.example.com/**']); })
With this you can do string interpolation in iframes like this:
<iframe ng-src="{{ 'http://srv1.assets.example.com/' + url_asset }}"></iframe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With