棱角分明.js aHrefSanitization白名单不起作用

Angular.js aHrefSanitizationWhitelist not working?

本文关键字:名单 不起作用 白名单 aHrefSanitization js      更新时间:2023-09-26

我正在尝试基本的 Angular.js教程,但我在尝试设置 url-schema 的白名单时遇到了麻烦。

基本上,我正在尝试将自定义方案(cust-scheme)添加到angular的白名单中,以避免它在URL前面加上unsafe:

根据这个 StackOverflow 的答案,我只需要添加$compileProvider.aHrefSanitizationWhitelist(/^'s*(https?|cust-scheme):/);到应用程序的配置参数。

我尝试了以下方法:

'use strict';
/* App Module */
var phonecatApp = angular.module('phonecatApp', [
  'ngRoute',
  'ngSanitize',
  'phonecatAnimations',
  'phonecatControllers',
  'phonecatFilters',
  'phonecatServices'
]);
phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
  }],
  ['$compileProvider',
  function( $compileProvider ) {   
    $compileProvider.aHrefSanitizationWhitelist(/^'s*(https?|cust-scheme):/);
    $compileProvider.imgSrcSanitizationWhitelist(/^'s*(https?|cust-scheme):/);
  }
]);

但它不起作用。路由正常,但客户方案架构未列入白名单。我错过了什么吗?也许我做错了多个配置?

我还尝试了以下方法:

'use strict';
/* App Module */
var phonecatApp = angular.module('phonecatApp', [
  'ngRoute',
  'ngSanitize',
  'phonecatAnimations',
  'phonecatControllers',
  'phonecatFilters',
  'phonecatServices'
]);
phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
  }]
);
phonecatApp.config(function( $compileProvider ) {   
    $compileProvider.aHrefSanitizationWhitelist(/^'s*(https?|cust-scheme):/);
    $compileProvider.imgSrcSanitizationWhitelist(/^'s*(https?|cust-scheme):/);
  }
);

在第二种情况下,架构已列入白名单,但路由不再起作用。

任何帮助不胜感激!

你的真诚,一个棱角分明.js新手:)

看起来你在语法上有点偏离,所以让我们尝试将这两个代码片段"合并"在一起。希望这段代码是自我描述的,你可以看到我们正在以一种最小安全和冗长的方式将$routeProvider$compileProvider一起注入,以立即调用我们的应用程序.config

phonecatApp.config(['$routeProvider', '$compileProvider', function($routeProvider, $compileProvider) {
    $compileProvider.aHrefSanitizationWhitelist(/^'s*(https?|ftp|mailto|chrome-extension):/);
    $compileProvider.imgSrcSanitizationWhitelist(/^'s*(https?|ftp|mailto|chrome-extension):/);
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
}]);