Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

Whitelist.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 java.net.MalformedURLException;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.apache.cordova.LOG;
  24. import android.net.Uri;
  25. public class Whitelist {
  26. private static class URLPattern {
  27. public Pattern scheme;
  28. public Pattern host;
  29. public Integer port;
  30. public Pattern path;
  31. private String regexFromPattern(String pattern, boolean allowWildcards) {
  32. final String toReplace = "\\.[]{}()^$?+|";
  33. StringBuilder regex = new StringBuilder();
  34. for (int i=0; i < pattern.length(); i++) {
  35. char c = pattern.charAt(i);
  36. if (c == '*' && allowWildcards) {
  37. regex.append(".");
  38. } else if (toReplace.indexOf(c) > -1) {
  39. regex.append('\\');
  40. }
  41. regex.append(c);
  42. }
  43. return regex.toString();
  44. }
  45. public URLPattern(String scheme, String host, String port, String path) throws MalformedURLException {
  46. try {
  47. if (scheme == null || "*".equals(scheme)) {
  48. this.scheme = null;
  49. } else {
  50. this.scheme = Pattern.compile(regexFromPattern(scheme, false), Pattern.CASE_INSENSITIVE);
  51. }
  52. if ("*".equals(host)) {
  53. this.host = null;
  54. } else if (host.startsWith("*.")) {
  55. this.host = Pattern.compile("([a-z0-9.-]*\\.)?" + regexFromPattern(host.substring(2), false), Pattern.CASE_INSENSITIVE);
  56. } else {
  57. this.host = Pattern.compile(regexFromPattern(host, false), Pattern.CASE_INSENSITIVE);
  58. }
  59. if (port == null || "*".equals(port)) {
  60. this.port = null;
  61. } else {
  62. this.port = Integer.parseInt(port,10);
  63. }
  64. if (path == null || "/*".equals(path)) {
  65. this.path = null;
  66. } else {
  67. this.path = Pattern.compile(regexFromPattern(path, true));
  68. }
  69. } catch (NumberFormatException e) {
  70. throw new MalformedURLException("Port must be a number");
  71. }
  72. }
  73. public boolean matches(Uri uri) {
  74. try {
  75. return ((scheme == null || scheme.matcher(uri.getScheme()).matches()) &&
  76. (host == null || host.matcher(uri.getHost()).matches()) &&
  77. (port == null || port.equals(uri.getPort())) &&
  78. (path == null || path.matcher(uri.getPath()).matches()));
  79. } catch (Exception e) {
  80. LOG.d(TAG, e.toString());
  81. return false;
  82. }
  83. }
  84. }
  85. private ArrayList<URLPattern> whiteList;
  86. public static final String TAG = "Whitelist";
  87. public Whitelist() {
  88. this.whiteList = new ArrayList<URLPattern>();
  89. }
  90. /* Match patterns (from http://developer.chrome.com/extensions/match_patterns.html)
  91. *
  92. * <url-pattern> := <scheme>://<host><path>
  93. * <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'chrome-extension'
  94. * <host> := '*' | '*.' <any char except '/' and '*'>+
  95. * <path> := '/' <any chars>
  96. *
  97. * We extend this to explicitly allow a port attached to the host, and we allow
  98. * the scheme to be omitted for backwards compatibility. (Also host is not required
  99. * to begin with a "*" or "*.".)
  100. */
  101. public void addWhiteListEntry(String origin, boolean subdomains) {
  102. if (whiteList != null) {
  103. try {
  104. // Unlimited access to network resources
  105. if (origin.compareTo("*") == 0) {
  106. LOG.d(TAG, "Unlimited access to network resources");
  107. whiteList = null;
  108. }
  109. else { // specific access
  110. Pattern parts = Pattern.compile("^((\\*|[A-Za-z-]+):(//)?)?(\\*|((\\*\\.)?[^*/:]+))?(:(\\d+))?(/.*)?");
  111. Matcher m = parts.matcher(origin);
  112. if (m.matches()) {
  113. String scheme = m.group(2);
  114. String host = m.group(4);
  115. // Special case for two urls which are allowed to have empty hosts
  116. if (("file".equals(scheme) || "content".equals(scheme)) && host == null) host = "*";
  117. String port = m.group(8);
  118. String path = m.group(9);
  119. if (scheme == null) {
  120. // XXX making it stupid friendly for people who forget to include protocol/SSL
  121. whiteList.add(new URLPattern("http", host, port, path));
  122. whiteList.add(new URLPattern("https", host, port, path));
  123. } else {
  124. whiteList.add(new URLPattern(scheme, host, port, path));
  125. }
  126. }
  127. }
  128. } catch (Exception e) {
  129. LOG.d(TAG, "Failed to add origin %s", origin);
  130. }
  131. }
  132. }
  133. /**
  134. * Determine if URL is in approved list of URLs to load.
  135. *
  136. * @param uri
  137. * @return true if wide open or whitelisted
  138. */
  139. public boolean isUrlWhiteListed(String uri) {
  140. // If there is no whitelist, then it's wide open
  141. if (whiteList == null) return true;
  142. Uri parsedUri = Uri.parse(uri);
  143. // Look for match in white list
  144. Iterator<URLPattern> pit = whiteList.iterator();
  145. while (pit.hasNext()) {
  146. URLPattern p = pit.next();
  147. if (p.matches(parsedUri)) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. }