javascript.angular.security.detect-angular-trust-as-js-method.detect-angular-trust-as-js-method

Author
1,327
Download Count*
License
The use of $sce.trustAsJs can be dangerous if unsanitized user input flows through this API.
Run Locally
Run in CI
Defintion
rules:
- id: detect-angular-trust-as-js-method
message: The use of $sce.trustAsJs can be dangerous if unsanitized user input
flows through this API.
metadata:
owasp:
- A07:2017 - Cross-Site Scripting (XSS)
- A03:2021 - Injection
references:
- https://docs.angularjs.org/api/ng/service/$sce#trustAsJs
- https://owasp.org/www-chapter-london/assets/slides/OWASPLondon20170727_AngularJS.pdf
cwe:
- "CWE-79: Improper Neutralization of Input During Web Page Generation
('Cross-site Scripting')"
category: security
technology:
- angular
cwe2022-top25: true
cwe2021-top25: true
subcategory:
- audit
likelihood: LOW
impact: MEDIUM
confidence: LOW
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
languages:
- javascript
- typescript
severity: WARNING
patterns:
- pattern-either:
- pattern: |
$SOURCE = $scope.$INPUT;
$sce.trustAsJs($SOURCE);
- pattern: |
$sce.trustAsJs($scope.$INPUT);
- pattern-inside: |
app.controller(..., function($scope,$sce){
...
});
Examples
detect-angular-trust-as-js-method.js
var app = angular.module('MyApp', []);
app.controller('myCtrl', function($scope, $sce) {
$scope.userInput = 'foo';
$scope.sayHello = function() {
// ruleid:detect-angular-trust-as-js-method
$scope.trustedurl = $sce.trustAsJs($scope.html);
// ruleid:detect-angular-trust-as-js-method
input = $scope.html
$scope.trustedurl = $sce.trustAsJs(input);
//Data is not coming from user input
$scope.trustedurl = $sce.trustAsJs('stringLiteral');
};
});
Short Link: https://sg.run/x1nA