允許清單
網域允許清單是一種安全模型,用於控制對您的應用程式無法控制的外部網域的存取。Cordova 提供可設定的安全原則,以定義可以存取哪些外部網站。
預設情況下,新的應用程式會設定為允許存取任何網站。在將應用程式移至生產環境之前,您應制定允許清單,以提供對特定網路網域和子網域的存取權。
雖然可以實作您自己的允許清單外掛程式,但不建議這樣做,除非您的應用程式需要非常特定的安全原則。
網路請求允許清單
Cordova 遵循 W3C Widget Access 規格,該規格依賴於應用程式 config.xml
檔案中的 <access>
元素,該檔案位於專案的最上層目錄中。
這會控制允許對特定網域發出哪些網路請求(圖片、XHR 等)(透過 Cordova 原生鉤子)。
注意:建議使用內容安全原則 (CSP)(請參閱下方),該原則更安全。此網路請求允許清單主要用於不支援 CSP 的網頁檢視的歷史用途。
在 config.xml
中,新增 <access>
標籤,如下所示
<!-- Allow images, xhrs, etc. to google.com -->
<access origin="http://google.com" />
<access origin="https://google.com" />
<!-- Access to the subdomain maps.google.com -->
<access origin="http://maps.google.com" />
<!-- Access to all the subdomains on google.com -->
<access origin="http://*.google.com" />
<!-- Enable requests to content: URLs -->
<access origin="content:///*" />
<!-- Don't block any requests -->
<access origin="*" />
若沒有任何 <access>
標籤,則只允許對應用程式內容所在的伺服器位置發出請求。
注意:允許清單無法封鎖從允許清單中的遠端網站(例如 http
或 https
)到未列入允許清單的網站的網路重新導向。對於支援 CSP 的網頁檢視,請使用 CSP 規則來減輕重新導向到未列入允許清單網站的影響。
請注意,某些網站可能會自動將其首頁重新導向到不同的 URL。範例情境可能包括但不限於
- 將
http
通訊協定請求重新導向到安全的https
SSL/TSL 通訊協定。 - 重新導向到特定國家/地區的網域。例如,根據裝置的地理位置,將
https://www.google.com
重新導向到https://www.google.co.uk
。
這些情境可能需要修改或在允許清單中新增超出您初始要求的其他項目。在建立應用程式的允許清單時,請考慮這一點。
特殊情況:Android 預設也允許對 https://ssl.gstatic.com/accessibility/javascript/android/ 發出請求,因為這是 TalkBack 正常運作所必需的。
導覽允許清單
這會控制網頁檢視本身可以導覽到哪些 URL。它僅適用於頂層導覽。
預設情況下,只允許導覽到 file://
URL。若要允許其他 URL,您必須將 <allow-navigation>
標籤新增至 config.xml
。
<!-- Allow links to example.com -->
<allow-navigation href="http://example.com/*" />
<!-- Wildcards are allowed for the protocol, as a prefix
to the host, or as a suffix to the path -->
<allow-navigation href="*://*.example.com/*" />
<!--
A wildcard can be used to allow the entire network, over HTTP and HTTPS.
This is *NOT RECOMMENDED*
-->
<allow-navigation href="*" />
<!-- The above is equivalent to these three declarations -->
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="data:*" />
特殊情況:在 Android 上,它也適用於非 http(s) 配置的 iframe。
意圖允許清單
這會控制允許應用程式要求系統開啟哪些 URL。
在 config.xml
中,新增 <allow-intent>
標籤,如下所示
<!-- Allow links to web pages to open in a browser -->
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<!-- Allow links to example.com to open in a browser -->
<allow-intent href="http://example.com/*" />
<!-- Wildcards are allowed for the protocol, as a prefix
to the host, or as a suffix to the path -->
<allow-intent href="*://*.example.com/*" />
<!-- Allow SMS links to open messaging app -->
<allow-intent href="sms:*" />
<!-- Allow tel: links to open the dialer -->
<allow-intent href="tel:*" />
<!-- Allow geo: links to open maps -->
<allow-intent href="geo:*" />
<!-- Allow all unrecognized URLs to open installed apps
*NOT RECOMMENDED* -->
<allow-intent href="*" />
若沒有任何 <allow-intent>
標籤,則不允許對外部 URL 發出任何請求。不過,預設 Cordova 應用程式預設包含一組相當寬鬆的 allow-intent
項目。建議根據每個應用程式的需求縮減此範圍。
在 Android 上,這相當於傳送類型為 BROWSEABLE 的意圖。
此允許清單僅適用於主要的 Cordova 網頁檢視,不適用於任何外掛程式,例如 InAppBrowser 網頁檢視或在系統網頁瀏覽器中開啟連結。它僅適用於 超連結 和對 window.open()
的呼叫。
注意:allow-navigation
的優先順序高於 allow-intent
。例如,允許使用 <allow-navigation href="*" />
導覽到所有 URL,會產生「擷取」所有意圖的副作用,因此網頁檢視會導覽到這些意圖,而不是觸發外部應用程式。
內容安全原則 (CSP)
控制允許發出哪些網路請求(圖片、XHR 等)(直接透過網頁檢視)。
在 Android 和 iOS 上,網路請求允許清單(請參閱上方)無法篩選所有類型的請求(例如,<video>
& WebSockets 未被封鎖)。因此,除了允許清單之外,您還應該在所有頁面上使用 內容安全原則 <meta>
標籤。
以下是一些用於 .html
頁面的範例 CSP 宣告
<!-- Good default declaration:
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of eval() and inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
* Enable eval(): add 'unsafe-eval' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *">
<!-- Allow everything but only from the same origin and foo.com -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' foo.com">
<!-- This policy allows everything (eg CSS, AJAX, object, frame, media, etc) except that
* CSS only from the same origin and inline styles,
* scripts only from the same origin and inline styles, and eval()
-->
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<!-- Allows XHRs only over HTTPS on the same domain. -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">
<!-- Allow iframe to https://cordova.dev.org.tw/ -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.dev.org.tw">
其他注意事項
應用程式傳輸安全性 (ATS) 是 iOS 9 (Xcode 7) 中的新功能。此新功能可作為應用程式的允許清單。Cordova CLI 會自動將 <access>
和 <allow-navigation>
標籤轉換為適當的 ATS 指示。
<access>
和 <allow-navigation>
標籤支援以下三個屬性,這些屬性在 ATS 中具有對應的屬性
minimum-tls-version
(字串,預設值為 'TLSv1.2')requires-forward-secrecy
(布林值,預設值為 'true')requires-certificate-transparency
(布林值,預設值為 'false',iOS 10 中的新功能)
範例
<access origin='https://cordova.dev.org.tw' minimum-tls-version='TLSv1.1' requires-forward-secrecy='false' requires-certificate-transparency='true' />
在 iOS 10 及更新版本中,<access>
標籤還支援以下三個屬性(如下所述),當與來源萬用字元 *
配對時。這些屬性在 ATS 中也有對應的屬性
allows-arbitrary-loads-for-media
(布林值,預設值為 'false',iOS 10 中的新功能。cordova-ios@4.5.0 中的新功能,已修正為使用正確的屬性名稱)。舊的屬性allows-arbitrary-loads-in-media
現在已過時。allows-arbitrary-loads-in-web-content
(布林值,預設值為 'false',iOS 10 中的新功能)allows-local-networking
(布林值,預設值為 'false',iOS 10 中的新功能)
範例
<access origin='*' allows-arbitrary-loads-for-media='true' allows-arbitrary-loads-in-web-content='true' allows-local-networking='true' />
如需更多詳細資訊,請參閱 ATS 技術說明。