2013-03-25 50 views
156

我試圖用角與應用程序的列表,而且每一個都是一個鏈接,查看更詳細的應用程序(apps/app.id):角度變化的網址爲「不安全」的擴展頁

<a id="{{app.id}}" href="apps/{{app.id}}" >{{app.name}}</a> 

每次點擊其中一個鏈接時,Chrome顯示的URL爲unsafe:chrome-extension://kpbipnfncdpgejhmdneaagc.../apps/app.id

unsafe:從何而來?

+1

請記住,你應該使用' ng-href'在這種情況下,而不僅僅是'href':https://docs.angularjs.org/api/ng/directive/ngHref – hartz89 2016-07-19 16:04:05

回答

320

你需要使用正則表達式明確添加URL協議角的白名單。只有http,https,ftpmailto默認啓用。使用chrome-extension:等協議時,Angular會在未列入白名單的網址前添加unsafe:

的好地方列入白名單chrome-extension:協議將是你的模塊的配置塊:當你需要使用的協議,如file:tel:

var app = angular.module('myApp', []) 
.config([ 
    '$compileProvider', 
    function($compileProvider) 
    { 
     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension):/); 
     // Angular before v1.2 uses $compileProvider.urlSanitizationWhitelist(...) 
    } 
]); 

同樣的程序也適用。

欲知詳情,請參閱AngularJS $compileProvider API documentation

+11

在Angular 1.2中,方法名變爲'$ compileProvider.aHrefSanitizationWhitelist' – Mart 2013-08-27 23:46:36

+6

默認imgSrcSanitizationWhitelist Angular 1.2-rc2是'/^\ s *(https?| ftp | file):| data:image \ //',用於訪問本地chrome打包應用程序的文件系統'| filesystem:chrome-extension:'應該被添加到正則表達式的末尾。 – Henning 2013-09-19 12:08:28

+26

請注意,在Angular 1.2中,實際上有兩種方法 - 一種用於鏈接(aHrefSanitizationWhitelist),另一種用於圖像(imgSrcSanitizationWhitelist)。這讓我陷入了一段時間。 – mdierker 2013-12-05 23:17:57

2

Google Chrome要求其擴展程序與Content Security Policy (CSP)合作。

您需要修改您的擴展以滿足CSP的要求。

https://developer.chrome.com/extensions/contentSecurityPolicy.html

https://developer.mozilla.org/en-US/docs/Security/CSP

此外,angularJS具有ngCsp指令,它需要使用。

http://docs.angularjs.org/api/ng.directive:ngCsp

+0

我已經有了該頁面的ngCsp指令''。 這是我清單中的CSP: '「content_security_policy」:「script-src'self'https://ssl.google-analytics.com; object-src'self'」,' 是否需要更改清單中的csp? – ebi 2013-03-26 04:16:16

46

如果任何人有這個問題,圖像,以及:

app.config(['$compileProvider', function ($compileProvider) { 
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|local|data|chrome-extension):/); 
}]); 
+0

我嘗試使用正則表達式列出我使用html2canvas捕獲的圖像屏幕截圖,現在沒有錯誤提示unsafe:data;但圖像沒有被捕獲。任何想法我將使用什麼正則表達式?我正在捕獲一個圖像/ png作爲base64 url​​。現在,html看起來像:,而不是實際的base64 url – Srini 2016-02-29 17:31:40

3

如果你只需要對郵件,電話和短信使用:

app.config(['$compileProvider', function ($compileProvider) { 
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|sms|tel):/); 
}]);