You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
7.2 KiB

8 years ago
  1. ---
  2. title: Whitelist
  3. description: Whitelist external content accessible by your app.
  4. ---
  5. <!--
  6. # license: Licensed to the Apache Software Foundation (ASF) under one
  7. # or more contributor license agreements. See the NOTICE file
  8. # distributed with this work for additional information
  9. # regarding copyright ownership. The ASF licenses this file
  10. # to you under the Apache License, Version 2.0 (the
  11. # "License"); you may not use this file except in compliance
  12. # with the License. You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing,
  17. # software distributed under the License is distributed on an
  18. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19. # KIND, either express or implied. See the License for the
  20. # specific language governing permissions and limitations
  21. # under the License.
  22. -->
  23. # cordova-plugin-whitelist
  24. This plugin implements a whitelist policy for navigating the application webview on Cordova 4.0
  25. :warning: Report issues on the [Apache Cordova issue tracker](https://issues.apache.org/jira/issues/?jql=project%20%3D%20CB%20AND%20status%20in%20%28Open%2C%20%22In%20Progress%22%2C%20Reopened%29%20AND%20resolution%20%3D%20Unresolved%20AND%20component%20%3D%20%22Plugin%20Whitelist%22%20ORDER%20BY%20priority%20DESC%2C%20summary%20ASC%2C%20updatedDate%20DESC)
  26. ## Installation
  27. You can install whitelist plugin with Cordova CLI, from npm:
  28. ```
  29. $ cordova plugin add cordova-plugin-whitelist
  30. $ cordova prepare
  31. ```
  32. ## Supported Cordova Platforms
  33. * Android 4.0.0 or above
  34. ## Navigation Whitelist
  35. Controls which URLs the WebView itself can be navigated to. Applies to
  36. top-level navigations only.
  37. Quirks: on Android it also applies to iframes for non-http(s) schemes.
  38. By default, navigations only to `file://` URLs, are allowed. To allow others URLs, you must add `<allow-navigation>` tags to your `config.xml`:
  39. <!-- Allow links to example.com -->
  40. <allow-navigation href="http://example.com/*" />
  41. <!-- Wildcards are allowed for the protocol, as a prefix
  42. to the host, or as a suffix to the path -->
  43. <allow-navigation href="*://*.example.com/*" />
  44. <!-- A wildcard can be used to whitelist the entire network,
  45. over HTTP and HTTPS.
  46. *NOT RECOMMENDED* -->
  47. <allow-navigation href="*" />
  48. <!-- The above is equivalent to these three declarations -->
  49. <allow-navigation href="http://*/*" />
  50. <allow-navigation href="https://*/*" />
  51. <allow-navigation href="data:*" />
  52. ## Intent Whitelist
  53. Controls which URLs the app is allowed to ask the system to open.
  54. By default, no external URLs are allowed.
  55. On Android, this equates to sending an intent of type BROWSEABLE.
  56. This whitelist does not apply to plugins, only hyperlinks and calls to `window.open()`.
  57. In `config.xml`, add `<allow-intent>` tags, like this:
  58. <!-- Allow links to web pages to open in a browser -->
  59. <allow-intent href="http://*/*" />
  60. <allow-intent href="https://*/*" />
  61. <!-- Allow links to example.com to open in a browser -->
  62. <allow-intent href="http://example.com/*" />
  63. <!-- Wildcards are allowed for the protocol, as a prefix
  64. to the host, or as a suffix to the path -->
  65. <allow-intent href="*://*.example.com/*" />
  66. <!-- Allow SMS links to open messaging app -->
  67. <allow-intent href="sms:*" />
  68. <!-- Allow tel: links to open the dialer -->
  69. <allow-intent href="tel:*" />
  70. <!-- Allow geo: links to open maps -->
  71. <allow-intent href="geo:*" />
  72. <!-- Allow all unrecognized URLs to open installed apps
  73. *NOT RECOMMENDED* -->
  74. <allow-intent href="*" />
  75. ## Network Request Whitelist
  76. Controls which network requests (images, XHRs, etc) are allowed to be made (via cordova native hooks).
  77. Note: We suggest you use a Content Security Policy (see below), which is more secure. This whitelist is mostly historical for webviews which do not support CSP.
  78. In `config.xml`, add `<access>` tags, like this:
  79. <!-- Allow images, xhrs, etc. to google.com -->
  80. <access origin="http://google.com" />
  81. <access origin="https://google.com" />
  82. <!-- Access to the subdomain maps.google.com -->
  83. <access origin="http://maps.google.com" />
  84. <!-- Access to all the subdomains on google.com -->
  85. <access origin="http://*.google.com" />
  86. <!-- Enable requests to content: URLs -->
  87. <access origin="content:///*" />
  88. <!-- Don't block any requests -->
  89. <access origin="*" />
  90. Without any `<access>` tags, only requests to `file://` URLs are allowed. However, the default Cordova application includes `<access origin="*">` by default.
  91. Note: Whitelist cannot block network redirects from a whitelisted remote website (i.e. http or https) to a non-whitelisted website. Use CSP rules to mitigate redirects to non-whitelisted websites for webviews that support CSP.
  92. Quirk: Android also allows requests to https://ssl.gstatic.com/accessibility/javascript/android/ by default, since this is required for TalkBack to function properly.
  93. ### Content Security Policy
  94. Controls which network requests (images, XHRs, etc) are allowed to be made (via webview directly).
  95. On Android and iOS, the network request whitelist (see above) is not able to filter all types of requests (e.g. `<video>` & WebSockets are not blocked). So, in addition to the whitelist, you should use a [Content Security Policy](http://content-security-policy.com/) `<meta>` tag on all of your pages.
  96. On Android, support for CSP within the system webview starts with KitKat (but is available on all versions using Crosswalk WebView).
  97. Here are some example CSP declarations for your `.html` pages:
  98. <!-- Good default declaration:
  99. * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
  100. * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
  101. * Disables use of eval() and inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
  102. * Enable inline JS: add 'unsafe-inline' to default-src
  103. * Enable eval(): add 'unsafe-eval' to default-src
  104. -->
  105. <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *">
  106. <!-- Allow everything but only from the same origin and foo.com -->
  107. <meta http-equiv="Content-Security-Policy" content="default-src 'self' foo.com">
  108. <!-- This policy allows everything (eg CSS, AJAX, object, frame, media, etc) except that
  109. * CSS only from the same origin and inline styles,
  110. * scripts only from the same origin and inline styles, and eval()
  111. -->
  112. <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
  113. <!-- Allows XHRs only over HTTPS on the same domain. -->
  114. <meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">
  115. <!-- Allow iframe to https://cordova.apache.org/ -->
  116. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">