SchemeRegistry.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. */
  26. #include "config.h"
  27. #include "SchemeRegistry.h"
  28. #include <wtf/MainThread.h>
  29. namespace WebCore {
  30. static URLSchemesMap& localURLSchemes()
  31. {
  32. DEFINE_STATIC_LOCAL(URLSchemesMap, localSchemes, ());
  33. if (localSchemes.isEmpty()) {
  34. localSchemes.add("file");
  35. #if PLATFORM(MAC)
  36. localSchemes.add("applewebdata");
  37. #endif
  38. #if PLATFORM(QT)
  39. localSchemes.add("qrc");
  40. #endif
  41. }
  42. return localSchemes;
  43. }
  44. static URLSchemesMap& displayIsolatedURLSchemes()
  45. {
  46. DEFINE_STATIC_LOCAL(URLSchemesMap, displayIsolatedSchemes, ());
  47. return displayIsolatedSchemes;
  48. }
  49. static URLSchemesMap& secureSchemes()
  50. {
  51. DEFINE_STATIC_LOCAL(URLSchemesMap, secureSchemes, ());
  52. if (secureSchemes.isEmpty()) {
  53. secureSchemes.add("https");
  54. secureSchemes.add("about");
  55. secureSchemes.add("data");
  56. }
  57. return secureSchemes;
  58. }
  59. static URLSchemesMap& schemesWithUniqueOrigins()
  60. {
  61. DEFINE_STATIC_LOCAL(URLSchemesMap, schemesWithUniqueOrigins, ());
  62. if (schemesWithUniqueOrigins.isEmpty()) {
  63. schemesWithUniqueOrigins.add("about");
  64. schemesWithUniqueOrigins.add("javascript");
  65. // This is a willful violation of HTML5.
  66. // See https://bugs.webkit.org/show_bug.cgi?id=11885
  67. schemesWithUniqueOrigins.add("data");
  68. }
  69. return schemesWithUniqueOrigins;
  70. }
  71. static URLSchemesMap& emptyDocumentSchemes()
  72. {
  73. DEFINE_STATIC_LOCAL(URLSchemesMap, emptyDocumentSchemes, ());
  74. if (emptyDocumentSchemes.isEmpty())
  75. emptyDocumentSchemes.add("about");
  76. return emptyDocumentSchemes;
  77. }
  78. static HashSet<String>& schemesForbiddenFromDomainRelaxation()
  79. {
  80. DEFINE_STATIC_LOCAL(HashSet<String>, schemes, ());
  81. return schemes;
  82. }
  83. static URLSchemesMap& canDisplayOnlyIfCanRequestSchemes()
  84. {
  85. DEFINE_STATIC_LOCAL(URLSchemesMap, canDisplayOnlyIfCanRequestSchemes, ());
  86. #if ENABLE(BLOB)
  87. if (canDisplayOnlyIfCanRequestSchemes.isEmpty()) {
  88. canDisplayOnlyIfCanRequestSchemes.add("blob");
  89. #if ENABLE(FILE_SYSTEM)
  90. canDisplayOnlyIfCanRequestSchemes.add("filesystem");
  91. #endif
  92. }
  93. #endif // ENABLE(BLOB)
  94. return canDisplayOnlyIfCanRequestSchemes;
  95. }
  96. static URLSchemesMap& notAllowingJavascriptURLsSchemes()
  97. {
  98. DEFINE_STATIC_LOCAL(URLSchemesMap, notAllowingJavascriptURLsSchemes, ());
  99. return notAllowingJavascriptURLsSchemes;
  100. }
  101. void SchemeRegistry::registerURLSchemeAsLocal(const String& scheme)
  102. {
  103. localURLSchemes().add(scheme);
  104. }
  105. void SchemeRegistry::removeURLSchemeRegisteredAsLocal(const String& scheme)
  106. {
  107. if (scheme == "file")
  108. return;
  109. #if PLATFORM(MAC)
  110. if (scheme == "applewebdata")
  111. return;
  112. #endif
  113. localURLSchemes().remove(scheme);
  114. }
  115. const URLSchemesMap& SchemeRegistry::localSchemes()
  116. {
  117. return localURLSchemes();
  118. }
  119. static URLSchemesMap& schemesAllowingLocalStorageAccessInPrivateBrowsing()
  120. {
  121. DEFINE_STATIC_LOCAL(URLSchemesMap, schemesAllowingLocalStorageAccessInPrivateBrowsing, ());
  122. return schemesAllowingLocalStorageAccessInPrivateBrowsing;
  123. }
  124. static URLSchemesMap& schemesAllowingDatabaseAccessInPrivateBrowsing()
  125. {
  126. DEFINE_STATIC_LOCAL(URLSchemesMap, schemesAllowingDatabaseAccessInPrivateBrowsing, ());
  127. return schemesAllowingDatabaseAccessInPrivateBrowsing;
  128. }
  129. static URLSchemesMap& CORSEnabledSchemes()
  130. {
  131. // FIXME: http://bugs.webkit.org/show_bug.cgi?id=77160
  132. DEFINE_STATIC_LOCAL(URLSchemesMap, CORSEnabledSchemes, ());
  133. if (CORSEnabledSchemes.isEmpty()) {
  134. CORSEnabledSchemes.add("http");
  135. CORSEnabledSchemes.add("https");
  136. }
  137. return CORSEnabledSchemes;
  138. }
  139. static URLSchemesMap& ContentSecurityPolicyBypassingSchemes()
  140. {
  141. DEFINE_STATIC_LOCAL(URLSchemesMap, schemes, ());
  142. return schemes;
  143. }
  144. bool SchemeRegistry::shouldTreatURLSchemeAsLocal(const String& scheme)
  145. {
  146. if (scheme.isEmpty())
  147. return false;
  148. return localURLSchemes().contains(scheme);
  149. }
  150. void SchemeRegistry::registerURLSchemeAsNoAccess(const String& scheme)
  151. {
  152. schemesWithUniqueOrigins().add(scheme);
  153. }
  154. bool SchemeRegistry::shouldTreatURLSchemeAsNoAccess(const String& scheme)
  155. {
  156. if (scheme.isEmpty())
  157. return false;
  158. return schemesWithUniqueOrigins().contains(scheme);
  159. }
  160. void SchemeRegistry::registerURLSchemeAsDisplayIsolated(const String& scheme)
  161. {
  162. displayIsolatedURLSchemes().add(scheme);
  163. }
  164. bool SchemeRegistry::shouldTreatURLSchemeAsDisplayIsolated(const String& scheme)
  165. {
  166. if (scheme.isEmpty())
  167. return false;
  168. return displayIsolatedURLSchemes().contains(scheme);
  169. }
  170. void SchemeRegistry::registerURLSchemeAsSecure(const String& scheme)
  171. {
  172. secureSchemes().add(scheme);
  173. }
  174. bool SchemeRegistry::shouldTreatURLSchemeAsSecure(const String& scheme)
  175. {
  176. if (scheme.isEmpty())
  177. return false;
  178. return secureSchemes().contains(scheme);
  179. }
  180. void SchemeRegistry::registerURLSchemeAsEmptyDocument(const String& scheme)
  181. {
  182. emptyDocumentSchemes().add(scheme);
  183. }
  184. bool SchemeRegistry::shouldLoadURLSchemeAsEmptyDocument(const String& scheme)
  185. {
  186. if (scheme.isEmpty())
  187. return false;
  188. return emptyDocumentSchemes().contains(scheme);
  189. }
  190. void SchemeRegistry::setDomainRelaxationForbiddenForURLScheme(bool forbidden, const String& scheme)
  191. {
  192. if (scheme.isEmpty())
  193. return;
  194. if (forbidden)
  195. schemesForbiddenFromDomainRelaxation().add(scheme);
  196. else
  197. schemesForbiddenFromDomainRelaxation().remove(scheme);
  198. }
  199. bool SchemeRegistry::isDomainRelaxationForbiddenForURLScheme(const String& scheme)
  200. {
  201. if (scheme.isEmpty())
  202. return false;
  203. return schemesForbiddenFromDomainRelaxation().contains(scheme);
  204. }
  205. bool SchemeRegistry::canDisplayOnlyIfCanRequest(const String& scheme)
  206. {
  207. if (scheme.isEmpty())
  208. return false;
  209. return canDisplayOnlyIfCanRequestSchemes().contains(scheme);
  210. }
  211. void SchemeRegistry::registerAsCanDisplayOnlyIfCanRequest(const String& scheme)
  212. {
  213. canDisplayOnlyIfCanRequestSchemes().add(scheme);
  214. }
  215. void SchemeRegistry::registerURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
  216. {
  217. notAllowingJavascriptURLsSchemes().add(scheme);
  218. }
  219. bool SchemeRegistry::shouldTreatURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
  220. {
  221. if (scheme.isEmpty())
  222. return false;
  223. return notAllowingJavascriptURLsSchemes().contains(scheme);
  224. }
  225. void SchemeRegistry::registerURLSchemeAsAllowingLocalStorageAccessInPrivateBrowsing(const String& scheme)
  226. {
  227. schemesAllowingLocalStorageAccessInPrivateBrowsing().add(scheme);
  228. }
  229. bool SchemeRegistry::allowsLocalStorageAccessInPrivateBrowsing(const String& scheme)
  230. {
  231. if (scheme.isEmpty())
  232. return false;
  233. return schemesAllowingLocalStorageAccessInPrivateBrowsing().contains(scheme);
  234. }
  235. void SchemeRegistry::registerURLSchemeAsAllowingDatabaseAccessInPrivateBrowsing(const String& scheme)
  236. {
  237. schemesAllowingDatabaseAccessInPrivateBrowsing().add(scheme);
  238. }
  239. bool SchemeRegistry::allowsDatabaseAccessInPrivateBrowsing(const String& scheme)
  240. {
  241. if (scheme.isEmpty())
  242. return false;
  243. return schemesAllowingDatabaseAccessInPrivateBrowsing().contains(scheme);
  244. }
  245. void SchemeRegistry::registerURLSchemeAsCORSEnabled(const String& scheme)
  246. {
  247. CORSEnabledSchemes().add(scheme);
  248. }
  249. bool SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(const String& scheme)
  250. {
  251. if (scheme.isEmpty())
  252. return false;
  253. return CORSEnabledSchemes().contains(scheme);
  254. }
  255. void SchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme)
  256. {
  257. ContentSecurityPolicyBypassingSchemes().add(scheme);
  258. }
  259. void SchemeRegistry::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme)
  260. {
  261. ContentSecurityPolicyBypassingSchemes().remove(scheme);
  262. }
  263. bool SchemeRegistry::schemeShouldBypassContentSecurityPolicy(const String& scheme)
  264. {
  265. if (scheme.isEmpty())
  266. return false;
  267. return ContentSecurityPolicyBypassingSchemes().contains(scheme);
  268. }
  269. bool SchemeRegistry::shouldCacheResponsesFromURLSchemeIndefinitely(const String& scheme)
  270. {
  271. #if PLATFORM(MAC)
  272. if (equalIgnoringCase(scheme, "applewebdata"))
  273. return true;
  274. #endif
  275. return equalIgnoringCase(scheme, "data");
  276. }
  277. } // namespace WebCore