通过$ sceDelegateProvider进行OpenRedirect预防

时间:2016-08-04 10:41:35

标签: javascript angularjs

$ sceDelegateProvider文档https://docs.angularjs.org/api/ng/provider/ $ sceDelegateProvider声明您可以使用resourceUrlBlacklist来阻止打开的指示。

根据我的理解,$ sceDelegateProvider仅用于从ng-include渲染模板时,如果将这些资源包含在黑名单中,它会停止加载这些资源。

对我来说令人困惑的部分是他们的文档看起来像这样:

示例:请考虑以下情况。

他们的例子如下:

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/**'
  ]);

  // The blacklist overrides the whitelist so the open redirect here is blocked.
  $sceDelegateProvider.resourceUrlBlacklist([
    'http://myapp.example.com/clickThru**'
  ]);
});

你通常在AngularJS中进行重定向的方法是通过$ window.location.href,如果实现不正确,可以让某人进行开放重定向。

我创建了一个示例应用程序,以反映我认为应该是这样的内容:http://plnkr.co/edit/cjXsCVcRGwgtgqZr13mE?p=preview

的index.html:

<!doctype html>
<html lang="en" ng-app="app">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example85-production</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-animate.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-sanitize.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.js"></script>
  <script src="script.js"></script>
</head>
<body>
    <h1>Redirect Example</h1>
    <p>Why doesn't this work?.</p>

    <h2>Demo</h2>
    <div>
        <h3>redirect</h3>
        <ng-view></ng-view>
        <ul>
            <li >
                <a href="/redirect?url=https://angularjs.org">
                    AngularJS (https)
                </a>
            </li>
            <li>
                <a href="/redirect?url=http://angularjs.org">
                    AngularJS (http)
                </a>
            </li>
        </ul>
    </div>

</body>
</html>

script.js:

// script.js
(function() {
    'use strict';

    angular
        .module('app', [
            'ngSanitize',
            'ngRoute'
        ]);
})();

// app.config.js
(function() {
    'use strict';

    angular
        .module('app')
        .config(function ( $sceDelegateProvider, $routeProvider ) {
      $sceDelegateProvider.resourceUrlWhitelist([
      // Allow same origin resource loads.
        'self',
        // Allow loading from our assets domain.  Notice the difference between * and **.
        'http://srv*.assets.example.com/**'
      ]);

      // The blacklist overrides the whitelist so the open redirect here is blocked.
      $sceDelegateProvider.resourceUrlBlacklist([
        'http://example.com/redirect?url=https://angularjs.org',
        'http://example.com/redirect?url=http://angularjs.org'
      ]);

            $routeProvider
                .when('/redirect', {
                    template: '<em>redirecting',
                    controller: function ($scope, $location,$window) {
                        $scope.url = $location.$$search['url'];
                        $window.location.href = $scope.url;
                    }
        })
            }
        );
})();

如果文档意味着什么,有人可以给我一些清晰度吗?

2 个答案:

答案 0 :(得分:1)

$ sceDelegateProvider仅沙箱模板网址解析和打开流程中发生的重定向。例如,如果您ng-include白名单资源301重定向到列入白名单的其他资源,则会被拒绝。

它不会干涉$ window.location(或$ location)。

答案 1 :(得分:0)

回答我自己的问题......

使用$ location和$ window时,$ sceDelegateProvider不用于防止重定向。在为ngRoute,directives,ngSrc,ngInclude等加载模板时,严格使用$ sceDelegateProvider,并且仅针对初始URL而不是最终目标进行检查。

例如,如果您将facebook.com列入白名单并且其中包含重定向到未列入白名单的网页(例如malicious.com),则只要它被恶意网站的CORS政策批准,它仍会成功加载。