cordova-plugin-whitelist 协议白名单配置整理

来源:互联网 发布:八爪鱼采集微博数据 编辑:程序博客网 时间:2024/06/05 17:34

一、cordova-plugin-whitelist 插件用于配置协议的白名单。在Cordova引用创建时自动添加了这个插件。

说明:

1.Cordova的版本要求 Cordova4.0以上

2.Android 的版本要求 4.0.0或更高版本

3.包括Navigation WhiteList(配置href可以打开的链接),Intent Whitelist(配置超链接或window.open()),Network Request Whitelist(配置允许请求的域名白名单),

Content Security Policy(配置单个页面的安全处理)。

4.Cordova项目中的域名白名单是一个用来控制外部域名访问的安全模块,比如http://google.com默认的安全机制是没有网络访问权限。开发者可以定义特定域名和子域名的访问权限。

规范
域名白名单是W3C Widget Access 规范的基础。在部件访问(Widget Access)规范中,<access>元素用来定义特定域名的访问权限,在以后的更新中Cordova平台白名单将趋近 W3C Widget Access规范。特别的,每个平台必须定义自身的白名单。

二、安装命令

$ cordova plugin add cordova-plugin-whitelist$ cordova prepare

三、Navigation Whitelist 导航白名单

控制WebView 本身可以导航到。只使用与顶级导航。怪癖:Android也适用于非HTTP iframe(S)方案。

默认情况下,只允许导航 file:// 本地文件。如果你想导航到其他自定义的协议的话,需要修改 config.xml

示例1:Cordova 配置WebView可以打开外部链接

示例2:

<!-- 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="*" /><!-- The above is equivalent to these three declarations --><allow-navigation href="http://*/*" /><allow-navigation href="https://*/*" /><allow-navigation href="data:*" />

四、Intent Whitelist 意图白名单

控件的URL的应用程序可以要求系统打开。默认情况下,不允许外部URL。

在Android上,这等同于发送型浏览的意图。

这个名单不适用于插件,只有超链接和调用open()窗口。

示例:

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

五、Network Request Whitelist 网络请求白名单

控制网络请求(图片、XHRs,等)是否允许。

注:建议使用Content Security Policy ,这是更安全的。这个名单是历史做不支持CSP。

默认情况下,不锁定任何请求为:

<access origin="*" />

示例:

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


六、Content Security Policy 

1.控制网络请求(图片,XHRs,等)是允许(通过WebView直接)。

2.在Android和iOS,网络请求的白名单(见上图)是不是能够过滤所有类型的请求(例如<视频>和WebSockets没有堵塞)。所以,除了白名单,你应该使用一个安全政策的内容<meta />标签上的所有页面。

示例1:Cordova页面加载外网图片失败,Refused to load the image

示例:

<!-- 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">

七、使用实例:

语法
访问 google.comhttp://google.com

访问安全连接 google.com (https://):https://google.com

访问子域名 maps.google.com:http://maps.google.com

访问 google.com上的全部子域名 (比如mail.google.com  docs.google.com):http://*.google.com

全部域名 (诸如 google.com 或 baidu.com):*

Android:细节:
白名单规则见下载包res/xml/cordova.xml,通过元素<access origin="..." />.声明
安卓支持白名单的全部语法
语法:
访问 google.com:<access origin="http://google.com" />

iOS详细

  • 打开 Cordova.plist.
    • 在 Xcode中, 见目录 AppName/Supporting Files/Cordova.plist
    • 在目录, 见AppName/Cordova.plist
  • 在 ExternalHosts key下添加新的键值 .
    • 建议使用 Xcode避免编辑原始 XML.

iOS不支持域名协议(诸如. http:// ahttps://) .
语法访问 google.com 和安全连接 google.com (https://):google.com
访问子域名 maps.google.com:maps.google.com
访问全部子域名 google.com (诸如 mail.google.com 或 docs.google.com):*.google.com
访问全部域名(诸如 google.com 或 baidu.com):*
IOS(*)通配符比W3C Widget Access 规范更灵活。
访问所有子域名和顶级域名 (.com, .net,等):*.google.*


官方文档:http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/index.html#intent-whitelist

更多:

Cordova Ajax请求跨域问题整理

cordova-plugin-themeablebrowser插件使用整理

cordova-plugin-inappbrowser插件使用整理

0 0
原创粉丝点击