9952 explained code solutions for 126 technologies

angularjsHow can I use AngularJS to prevent cross-site scripting attacks?

AngularJS provides built-in protection against cross-site scripting (XSS) attacks. It does this by sanitizing untrusted values that are bound to HTML elements. This sanitization process is done by using a built-in security context called a “Strict Contextual Escaping” (SCE).

The SCE works by validating untrusted values and converting them into a safe form before they are bound to HTML elements. This process helps to prevent malicious code from being injected into the HTML.

Here is an example of how to use AngularJS to prevent XSS attacks:

// Create a module
var app = angular.module('myApp', []);

// Configure the module
app.config(function($sceProvider) {

// Bind the untrusted value to an HTML element
app.controller('myController', function($scope) {
  $scope.unsafeValue = '<script>alert("Hello World!");</script>';
  $scope.trustedValue = $sce.trustAsHtml($scope.unsafeValue);

The code above creates a module and configures it to enable the SCE. It then binds an untrusted value to an HTML element. The $sce.trustAsHtml method is used to sanitize the untrusted value before it is bound to the HTML element.

Code explanation

  • var app = angular.module('myApp', []): Creates a new AngularJS module called myApp.
  • $sceProvider.enabled(true): Enables the SCE.
  • $scope.unsafeValue = '<script>alert("Hello World!");</script>': Sets an untrusted value.
  • $scope.trustedValue = $sce.trustAsHtml($scope.unsafeValue): Sanitizes the untrusted value using the $sce.trustAsHtml method.

Helpful links

Edit this code on GitHub