cordova開發日記05 白名單的配置和說明(Whitelist)

Whitelist是cordova爲了解決同源策略的方案,配置方法以下:html

官網地址:web

http://cordova.apache.org/docs/en/latest/guide/appdev/whitelist/index.htmlapache

http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/index.html安全

config.xml access配置

只容許google.com Access to google.com:網絡

<access origin="http://google.com" />

只容許google.com的https協議 Access to the secure google.com (https://):app

<access origin="https://google.com" />

二級域名(maps) Access to the subdomain maps.google.com:dom

<access origin="http://maps.google.com" />

全部二級域名 Access to all the subdomains on google.com, for example mail.google.com and docs.google.com:ide

<access origin="http://*.google.com" />

全部域名 Access to all domains, for example, google.com and developer.mozilla.org:字體

<access origin="*" />

config.xml Navigation Whitelist

說明:webview能夠跳轉至的URLui

<!-- 容許全部到example.com的連接 -->
<!-- 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 whitelist the entire network,
     over HTTP and HTTPS.
     *NOT RECOMMENDED* -->
<allow-navigation href="*" />

<!-- 上面的寫法與下面3句等價 -->
<!-- The above is equivalent to these three declarations -->
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="data:*" />

config.xml Intent Whitelist

說明:系統能夠打開的連接

<!-- 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="*" />

config.xml Network Request Whitelist

說明:網絡請求(如XHR等)白名單

<!-- 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="*" />

index.html Content Security Policy

說明:頁面上的資源白名單

主要分這幾類:default-src,style-src,script-src,img-src,font-src,media-src 等

參數值能夠是:*,'self','unsafe-inline',data: 等

我使用的是很是寬鬆的策略:

容許全部域名的數據,容許不安全的內聯,容許data:(主要用於BASE64形式的圖片,字體等)

<meta http-equiv="Content-Security-Policy" content="default-src * 'self' 'unsafe-inline';img-src * 'self' data:;font-src 'self' data:">

下面是官方示例:

<!-- Good default declaration:
    * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
    * 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: gap: 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.apache.org/ -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">
相關文章
相關標籤/搜索