Пытаюсь реализовать angular sanitizer в моем приложении с помощью angular sanitizer, но он работает не так, как ожидалось.
angular.module('sanitizeExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', '$sce', function($scope, $sce) {
//$scope.snippet = "<script type='text/javascript'>alert(1)</script>";
$scope.snippet = "alert(1)";
$scope.deliberatelyTrustDangerousSnippet = function() {
return $sce.trustAsHtml($scope.snippet);
};
$scope.escape = function(input) {
return $sce.trustAsHtml(input);
}
}]);
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
<div ng-app = "sanitizeExample">
<div ng-controller = "ExampleController">
Snippet: <textarea ng-model = "snippet" cols = "60" rows = "3"></textarea>
<table>
<tr>
<td>Directive</td>
<td>How</td>
<td>Source</td>
<td>Rendered</td>
</tr>
<tr id = "bind-html-with-sanitize">
<td>ng-bind-html</td>
<td>Automatically uses $sanitize</td>
<td><pre><div ng-bind-html = "snippet"><br/></div></pre></td>
<td><div ng-bind-html = "snippet"></div></td>
</tr>
<tr id = "bind-html-with-trust">
<td>ng-bind-html</td>
<td>Bypass $sanitize by explicitly trusting the dangerous value</td>
<td>
<pre><div ng-bind-html = "deliberatelyTrustDangerousSnippet()">
</div></pre>
</td>
<td><div ng-bind-html = "deliberatelyTrustDangerousSnippet()"></div></td>
</tr>
<tr id = "bind-default">
<td>ng-bind</td>
<td>Automatically escapes</td>
<td><pre><div ng-bind = "snippet"><br/></div></pre></td> <td><div ng-bind = "snippet">ggg</div></td>
</tr>
</table>
<p>{{ escape("<script type='text/javascript'>alert(1)</script>") }}</p>
</div>
</div>
Выдает Uncaught SyntaxError: недопустимая или неожиданная ошибка токена при использовании скрипта
$scope.snippet = "<script type='text/javascript'>alert(1)</script>"
Прекрасно работает без скрипта
$scope.snippet = "alert(1)"
Он выдает предупреждение в браузере, но ожидает внутренний текст для тега p.
<p>{{ escape("<script type='text/javascript'>alert(1)</script>") }}</p>
также получение вывода в html
{{ escape("") }}
Как выполнить дезинфекцию переменной с тегом сценария, также вызвав функцию контроллера из шаблона html.
Я ищу исправление очистки атаки xss.
Мое требование - отображать сценарий также как текст внутри шаблона html.
это работает?
angular.module("sanitizeExample", ['ngSanitize'])
.controller("ExampleController", ['$scope', '$sce', function($scope, $sce){
/* $scope.snippet = "alert(1)"; */
$scope.snippet = "<script " + "type='text/javascript'"+" >alert(1) <" + "/script>";
$scope.deliberatelyTrustDangerousSnippet = function(){
return $sce.trustAsHtml($scope.snippet);
};
$scope.escape = function(input) {
return $sce.trustAsHtml(input);
}
}]);
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app = "sanitizeExample">
<div ng-controller = "ExampleController">
Snippet: <textarea ng-model = "snippet" cols = "60" rows = "3"></textarea>
<table>
<tr>
<td>Directive</td>
<td>How</td>
<td>Source</td>
<td>Rendered</td>
</tr>
<tr id = "bind-html-with-sanitize">
<td>ng-bind-html</td>
<td>Automatically uses $sanitize</td>
<td><pre><div ng-bind-html = "snippet"><br/></div></pre></td>
<td><div ng-bind-html = "snippet"></div></td>
</tr>
<tr id = "bind-html-with-trust">
<td>ng-bind-html</td>
<td>Bypass $sanitize by explicitly trusting the dangerous value</td>
<td>
<pre><div ng-bind-html = "deliberatelyTrustDangerousSnippet()">
</div></pre>
</td>
<td><div ng-bind-html = "deliberatelyTrustDangerousSnippet()"></div></td>
</tr>
<tr id = "bind-default">
<td>ng-bind</td>
<td>Automatically escapes</td>
<td><pre><div ng-bind = "snippet"><br/></div></pre></td> <td><div ng-bind = "snippet">ggg</div></td>
</tr>
</table>
<!--
-->
<p>{{ escape("<script type='text/javascript'>alert(1)</script>") }}</p>
</div>
</div>
Включая только модуль ngSanitize, достаточный для выполнения дезинфекции в представлении.
angular.module("sanitizeExample", ['ngSanitize'])
Это неправильный способ проверки очистки от представления. Он передается как переменная в angular, чтобы он выполнялся и выдает предупреждение.
{{ escape("<script type='text/javascript'>alert(1)</script>") }}
Когда мы тестируем из служб данных, sanitize работает должным образом.
взгляните на stackoverflow.com/questions/12197880/…