CordovaBridge.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. import android.annotation.SuppressLint;
  19. import java.security.SecureRandom;
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. /**
  23. * Contains APIs that the JS can call. All functions in here should also have
  24. * an equivalent entry in CordovaChromeClient.java, and be added to
  25. * cordova-js/lib/android/plugin/android/promptbasednativeapi.js
  26. */
  27. public class CordovaBridge {
  28. private static final String LOG_TAG = "CordovaBridge";
  29. private PluginManager pluginManager;
  30. private NativeToJsMessageQueue jsMessageQueue;
  31. private volatile int expectedBridgeSecret = -1; // written by UI thread, read by JS thread.
  32. public CordovaBridge(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue) {
  33. this.pluginManager = pluginManager;
  34. this.jsMessageQueue = jsMessageQueue;
  35. }
  36. public String jsExec(int bridgeSecret, String service, String action, String callbackId, String arguments) throws JSONException, IllegalAccessException {
  37. if (!verifySecret("exec()", bridgeSecret)) {
  38. return null;
  39. }
  40. // If the arguments weren't received, send a message back to JS. It will switch bridge modes and try again. See CB-2666.
  41. // We send a message meant specifically for this case. It starts with "@" so no other message can be encoded into the same string.
  42. if (arguments == null) {
  43. return "@Null arguments.";
  44. }
  45. jsMessageQueue.setPaused(true);
  46. try {
  47. // Tell the resourceApi what thread the JS is running on.
  48. CordovaResourceApi.jsThread = Thread.currentThread();
  49. pluginManager.exec(service, action, callbackId, arguments);
  50. String ret = null;
  51. if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING) {
  52. ret = jsMessageQueue.popAndEncode(false);
  53. }
  54. return ret;
  55. } catch (Throwable e) {
  56. e.printStackTrace();
  57. return "";
  58. } finally {
  59. jsMessageQueue.setPaused(false);
  60. }
  61. }
  62. public void jsSetNativeToJsBridgeMode(int bridgeSecret, int value) throws IllegalAccessException {
  63. if (!verifySecret("setNativeToJsBridgeMode()", bridgeSecret)) {
  64. return;
  65. }
  66. jsMessageQueue.setBridgeMode(value);
  67. }
  68. public String jsRetrieveJsMessages(int bridgeSecret, boolean fromOnlineEvent) throws IllegalAccessException {
  69. if (!verifySecret("retrieveJsMessages()", bridgeSecret)) {
  70. return null;
  71. }
  72. return jsMessageQueue.popAndEncode(fromOnlineEvent);
  73. }
  74. private boolean verifySecret(String action, int bridgeSecret) throws IllegalAccessException {
  75. if (!jsMessageQueue.isBridgeEnabled()) {
  76. if (bridgeSecret == -1) {
  77. LOG.d(LOG_TAG, action + " call made before bridge was enabled.");
  78. } else {
  79. LOG.d(LOG_TAG, "Ignoring " + action + " from previous page load.");
  80. }
  81. return false;
  82. }
  83. // Bridge secret wrong and bridge not due to it being from the previous page.
  84. if (expectedBridgeSecret < 0 || bridgeSecret != expectedBridgeSecret) {
  85. LOG.e(LOG_TAG, "Bridge access attempt with wrong secret token, possibly from malicious code. Disabling exec() bridge!");
  86. clearBridgeSecret();
  87. throw new IllegalAccessException();
  88. }
  89. return true;
  90. }
  91. /** Called on page transitions */
  92. void clearBridgeSecret() {
  93. expectedBridgeSecret = -1;
  94. }
  95. public boolean isSecretEstablished() {
  96. return expectedBridgeSecret != -1;
  97. }
  98. /** Called by cordova.js to initialize the bridge. */
  99. //On old Androids SecureRandom isn't really secure, this is the least of your problems if
  100. //you're running Android 4.3 and below in 2017
  101. @SuppressLint("TrulyRandom")
  102. int generateBridgeSecret() {
  103. SecureRandom randGen = new SecureRandom();
  104. expectedBridgeSecret = randGen.nextInt(Integer.MAX_VALUE);
  105. return expectedBridgeSecret;
  106. }
  107. public void reset() {
  108. jsMessageQueue.reset();
  109. clearBridgeSecret();
  110. }
  111. public String promptOnJsPrompt(String origin, String message, String defaultValue) {
  112. if (defaultValue != null && defaultValue.length() > 3 && defaultValue.startsWith("gap:")) {
  113. JSONArray array;
  114. try {
  115. array = new JSONArray(defaultValue.substring(4));
  116. int bridgeSecret = array.getInt(0);
  117. String service = array.getString(1);
  118. String action = array.getString(2);
  119. String callbackId = array.getString(3);
  120. String r = jsExec(bridgeSecret, service, action, callbackId, message);
  121. return r == null ? "" : r;
  122. } catch (JSONException e) {
  123. e.printStackTrace();
  124. } catch (IllegalAccessException e) {
  125. e.printStackTrace();
  126. }
  127. return "";
  128. }
  129. // Sets the native->JS bridge mode.
  130. else if (defaultValue != null && defaultValue.startsWith("gap_bridge_mode:")) {
  131. try {
  132. int bridgeSecret = Integer.parseInt(defaultValue.substring(16));
  133. jsSetNativeToJsBridgeMode(bridgeSecret, Integer.parseInt(message));
  134. } catch (NumberFormatException e){
  135. e.printStackTrace();
  136. } catch (IllegalAccessException e) {
  137. e.printStackTrace();
  138. }
  139. return "";
  140. }
  141. // Polling for JavaScript messages
  142. else if (defaultValue != null && defaultValue.startsWith("gap_poll:")) {
  143. int bridgeSecret = Integer.parseInt(defaultValue.substring(9));
  144. try {
  145. String r = jsRetrieveJsMessages(bridgeSecret, "1".equals(message));
  146. return r == null ? "" : r;
  147. } catch (IllegalAccessException e) {
  148. e.printStackTrace();
  149. }
  150. return "";
  151. }
  152. else if (defaultValue != null && defaultValue.startsWith("gap_init:")) {
  153. // Protect against random iframes being able to talk through the bridge.
  154. // Trust only pages which the app would have been allowed to navigate to anyway.
  155. if (pluginManager.shouldAllowBridgeAccess(origin)) {
  156. // Enable the bridge
  157. int bridgeMode = Integer.parseInt(defaultValue.substring(9));
  158. jsMessageQueue.setBridgeMode(bridgeMode);
  159. // Tell JS the bridge secret.
  160. int secret = generateBridgeSecret();
  161. return ""+secret;
  162. } else {
  163. LOG.e(LOG_TAG, "gap_init called from restricted origin: " + origin);
  164. }
  165. return "";
  166. }
  167. return null;
  168. }
  169. }