url_util.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "url/url_util.h"
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <atomic>
  8. #include <ostream>
  9. #include "base/check_op.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/no_destructor.h"
  12. #include "base/strings/string_util.h"
  13. #include "url/url_canon_internal.h"
  14. #include "url/url_constants.h"
  15. #include "url/url_file.h"
  16. #include "url/url_util_internal.h"
  17. namespace url {
  18. namespace {
  19. // A pair for representing a standard scheme name and the SchemeType for it.
  20. struct SchemeWithType {
  21. std::string scheme;
  22. SchemeType type;
  23. };
  24. // A pair for representing a scheme and a custom protocol handler for it.
  25. //
  26. // This pair of strings must be normalized protocol handler parameters as
  27. // described in the Custom Handler specification.
  28. // https://html.spec.whatwg.org/multipage/system-state.html#normalize-protocol-handler-parameters
  29. struct SchemeWithHandler {
  30. std::string scheme;
  31. std::string handler;
  32. };
  33. // List of currently registered schemes and associated properties.
  34. struct SchemeRegistry {
  35. // Standard format schemes (see header for details).
  36. std::vector<SchemeWithType> standard_schemes = {
  37. {kHttpsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
  38. {kHttpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
  39. // Yes, file URLs can have a hostname, so file URLs should be handled as
  40. // "standard". File URLs never have a port as specified by the SchemeType
  41. // field. Unlike other SCHEME_WITH_HOST schemes, the 'host' in a file
  42. // URL may be empty, a behavior which is special-cased during
  43. // canonicalization.
  44. {kFileScheme, SCHEME_WITH_HOST},
  45. {kFtpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
  46. {kWssScheme,
  47. SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION}, // WebSocket secure.
  48. {kWsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION}, // WebSocket.
  49. {kFileSystemScheme, SCHEME_WITHOUT_AUTHORITY},
  50. {kQuicTransportScheme, SCHEME_WITH_HOST_AND_PORT},
  51. };
  52. // Schemes that are allowed for referrers.
  53. //
  54. // WARNING: Adding (1) a non-"standard" scheme or (2) a scheme whose URLs have
  55. // opaque origins could lead to surprising behavior in some of the referrer
  56. // generation logic. In order to avoid surprises, be sure to have adequate
  57. // test coverage in each of the multiple code locations that compute
  58. // referrers.
  59. std::vector<SchemeWithType> referrer_schemes = {
  60. {kHttpsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
  61. {kHttpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
  62. };
  63. // Schemes that do not trigger mixed content warning.
  64. std::vector<std::string> secure_schemes = {
  65. kHttpsScheme, kAboutScheme, kDataScheme, kQuicTransportScheme, kWssScheme,
  66. };
  67. // Schemes that normal pages cannot link to or access (i.e., with the same
  68. // security rules as those applied to "file" URLs).
  69. std::vector<std::string> local_schemes = {
  70. kFileScheme,
  71. };
  72. // Schemes that cause pages loaded with them to not have access to pages
  73. // loaded with any other URL scheme.
  74. std::vector<std::string> no_access_schemes = {
  75. kAboutScheme,
  76. kJavaScriptScheme,
  77. kDataScheme,
  78. };
  79. // Schemes that can be sent CORS requests.
  80. std::vector<std::string> cors_enabled_schemes = {
  81. kHttpsScheme,
  82. kHttpScheme,
  83. kDataScheme,
  84. };
  85. // Schemes that can be used by web to store data (local storage, etc).
  86. std::vector<std::string> web_storage_schemes = {
  87. kHttpsScheme, kHttpScheme, kFileScheme, kFtpScheme, kWssScheme, kWsScheme,
  88. };
  89. // Schemes that can bypass the Content-Security-Policy (CSP) checks.
  90. std::vector<std::string> csp_bypassing_schemes = {};
  91. // Schemes that are strictly empty documents, allowing them to commit
  92. // synchronously.
  93. std::vector<std::string> empty_document_schemes = {
  94. kAboutScheme,
  95. };
  96. // Schemes with a predefined default custom handler.
  97. std::vector<SchemeWithHandler> predefined_handler_schemes;
  98. bool allow_non_standard_schemes = false;
  99. };
  100. // See the LockSchemeRegistries declaration in the header.
  101. bool scheme_registries_locked = false;
  102. // Ensure that the schemes aren't modified after first use.
  103. static std::atomic<bool> g_scheme_registries_used{false};
  104. // Gets the scheme registry without locking the schemes. This should *only* be
  105. // used for adding schemes to the registry.
  106. SchemeRegistry* GetSchemeRegistryWithoutLocking() {
  107. static base::NoDestructor<SchemeRegistry> registry;
  108. return registry.get();
  109. }
  110. const SchemeRegistry& GetSchemeRegistry() {
  111. #if DCHECK_IS_ON()
  112. g_scheme_registries_used.store(true);
  113. #endif
  114. return *GetSchemeRegistryWithoutLocking();
  115. }
  116. // Pass this enum through for methods which would like to know if whitespace
  117. // removal is necessary.
  118. enum WhitespaceRemovalPolicy {
  119. REMOVE_WHITESPACE,
  120. DO_NOT_REMOVE_WHITESPACE,
  121. };
  122. // This template converts a given character type to the corresponding
  123. // StringPiece type.
  124. template<typename CHAR> struct CharToStringPiece {
  125. };
  126. template<> struct CharToStringPiece<char> {
  127. typedef base::StringPiece Piece;
  128. };
  129. template <>
  130. struct CharToStringPiece<char16_t> {
  131. typedef base::StringPiece16 Piece;
  132. };
  133. // Given a string and a range inside the string, compares it to the given
  134. // lower-case |compare_to| buffer.
  135. template<typename CHAR>
  136. inline bool DoCompareSchemeComponent(const CHAR* spec,
  137. const Component& component,
  138. const char* compare_to) {
  139. if (!component.is_nonempty())
  140. return compare_to[0] == 0; // When component is empty, match empty scheme.
  141. return base::EqualsCaseInsensitiveASCII(
  142. typename CharToStringPiece<CHAR>::Piece(&spec[component.begin],
  143. component.len),
  144. compare_to);
  145. }
  146. // Returns true and sets |type| to the SchemeType of the given scheme
  147. // identified by |scheme| within |spec| if in |schemes|.
  148. template<typename CHAR>
  149. bool DoIsInSchemes(const CHAR* spec,
  150. const Component& scheme,
  151. SchemeType* type,
  152. const std::vector<SchemeWithType>& schemes) {
  153. if (!scheme.is_nonempty())
  154. return false; // Empty or invalid schemes are non-standard.
  155. for (const SchemeWithType& scheme_with_type : schemes) {
  156. if (base::EqualsCaseInsensitiveASCII(
  157. typename CharToStringPiece<CHAR>::Piece(&spec[scheme.begin],
  158. scheme.len),
  159. scheme_with_type.scheme)) {
  160. *type = scheme_with_type.type;
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. template<typename CHAR>
  167. bool DoIsStandard(const CHAR* spec, const Component& scheme, SchemeType* type) {
  168. return DoIsInSchemes(spec, scheme, type,
  169. GetSchemeRegistry().standard_schemes);
  170. }
  171. template<typename CHAR>
  172. bool DoFindAndCompareScheme(const CHAR* str,
  173. int str_len,
  174. const char* compare,
  175. Component* found_scheme) {
  176. // Before extracting scheme, canonicalize the URL to remove any whitespace.
  177. // This matches the canonicalization done in DoCanonicalize function.
  178. STACK_UNINITIALIZED RawCanonOutputT<CHAR> whitespace_buffer;
  179. int spec_len;
  180. const CHAR* spec =
  181. RemoveURLWhitespace(str, str_len, &whitespace_buffer, &spec_len, nullptr);
  182. Component our_scheme;
  183. if (!ExtractScheme(spec, spec_len, &our_scheme)) {
  184. // No scheme.
  185. if (found_scheme)
  186. *found_scheme = Component();
  187. return false;
  188. }
  189. if (found_scheme)
  190. *found_scheme = our_scheme;
  191. return DoCompareSchemeComponent(spec, our_scheme, compare);
  192. }
  193. template <typename CHAR>
  194. bool DoCanonicalize(const CHAR* spec,
  195. int spec_len,
  196. bool trim_path_end,
  197. WhitespaceRemovalPolicy whitespace_policy,
  198. CharsetConverter* charset_converter,
  199. CanonOutput* output,
  200. Parsed* output_parsed) {
  201. // Trim leading C0 control characters and spaces.
  202. int begin = 0;
  203. TrimURL(spec, &begin, &spec_len, trim_path_end);
  204. DCHECK(0 <= begin && begin <= spec_len);
  205. spec += begin;
  206. spec_len -= begin;
  207. output->ReserveSizeIfNeeded(spec_len);
  208. // Remove any whitespace from the middle of the relative URL if necessary.
  209. // Possibly this will result in copying to the new buffer.
  210. STACK_UNINITIALIZED RawCanonOutputT<CHAR> whitespace_buffer;
  211. if (whitespace_policy == REMOVE_WHITESPACE) {
  212. spec = RemoveURLWhitespace(spec, spec_len, &whitespace_buffer, &spec_len,
  213. &output_parsed->potentially_dangling_markup);
  214. }
  215. Parsed parsed_input;
  216. #ifdef WIN32
  217. // For Windows, we allow things that look like absolute Windows paths to be
  218. // fixed up magically to file URLs. This is done for IE compatibility. For
  219. // example, this will change "c:/foo" into a file URL rather than treating
  220. // it as a URL with the protocol "c". It also works for UNC ("\\foo\bar.txt").
  221. // There is similar logic in url_canon_relative.cc for
  222. //
  223. // For Max & Unix, we don't do this (the equivalent would be "/foo/bar" which
  224. // has no meaning as an absolute path name. This is because browsers on Mac
  225. // & Unix don't generally do this, so there is no compatibility reason for
  226. // doing so.
  227. if (DoesBeginUNCPath(spec, 0, spec_len, false) ||
  228. DoesBeginWindowsDriveSpec(spec, 0, spec_len)) {
  229. ParseFileURL(spec, spec_len, &parsed_input);
  230. return CanonicalizeFileURL(spec, spec_len, parsed_input, charset_converter,
  231. output, output_parsed);
  232. }
  233. #endif
  234. Component scheme;
  235. if (!ExtractScheme(spec, spec_len, &scheme))
  236. return false;
  237. // This is the parsed version of the input URL, we have to canonicalize it
  238. // before storing it in our object.
  239. bool success;
  240. SchemeType scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
  241. if (DoCompareSchemeComponent(spec, scheme, url::kFileScheme)) {
  242. // File URLs are special.
  243. ParseFileURL(spec, spec_len, &parsed_input);
  244. success = CanonicalizeFileURL(spec, spec_len, parsed_input,
  245. charset_converter, output, output_parsed);
  246. } else if (DoCompareSchemeComponent(spec, scheme, url::kFileSystemScheme)) {
  247. // Filesystem URLs are special.
  248. ParseFileSystemURL(spec, spec_len, &parsed_input);
  249. success = CanonicalizeFileSystemURL(spec, spec_len, parsed_input,
  250. charset_converter, output,
  251. output_parsed);
  252. } else if (DoIsStandard(spec, scheme, &scheme_type)) {
  253. // All "normal" URLs.
  254. ParseStandardURL(spec, spec_len, &parsed_input);
  255. success = CanonicalizeStandardURL(spec, spec_len, parsed_input, scheme_type,
  256. charset_converter, output, output_parsed);
  257. } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) {
  258. // Mailto URLs are treated like standard URLs, with only a scheme, path,
  259. // and query.
  260. ParseMailtoURL(spec, spec_len, &parsed_input);
  261. success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output,
  262. output_parsed);
  263. } else {
  264. // "Weird" URLs like data: and javascript:.
  265. ParsePathURL(spec, spec_len, trim_path_end, &parsed_input);
  266. success = CanonicalizePathURL(spec, spec_len, parsed_input, output,
  267. output_parsed);
  268. }
  269. return success;
  270. }
  271. template<typename CHAR>
  272. bool DoResolveRelative(const char* base_spec,
  273. int base_spec_len,
  274. const Parsed& base_parsed,
  275. const CHAR* in_relative,
  276. int in_relative_length,
  277. CharsetConverter* charset_converter,
  278. CanonOutput* output,
  279. Parsed* output_parsed) {
  280. // Remove any whitespace from the middle of the relative URL, possibly
  281. // copying to the new buffer.
  282. STACK_UNINITIALIZED RawCanonOutputT<CHAR> whitespace_buffer;
  283. int relative_length;
  284. const CHAR* relative = RemoveURLWhitespace(
  285. in_relative, in_relative_length, &whitespace_buffer, &relative_length,
  286. &output_parsed->potentially_dangling_markup);
  287. bool base_is_authority_based = false;
  288. bool base_is_hierarchical = false;
  289. if (base_spec &&
  290. base_parsed.scheme.is_nonempty()) {
  291. int after_scheme = base_parsed.scheme.end() + 1; // Skip past the colon.
  292. int num_slashes = CountConsecutiveSlashes(base_spec, after_scheme,
  293. base_spec_len);
  294. base_is_authority_based = num_slashes > 1;
  295. base_is_hierarchical = num_slashes > 0;
  296. }
  297. SchemeType unused_scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
  298. bool standard_base_scheme =
  299. base_parsed.scheme.is_nonempty() &&
  300. DoIsStandard(base_spec, base_parsed.scheme, &unused_scheme_type);
  301. bool is_relative;
  302. Component relative_component;
  303. if (!IsRelativeURL(base_spec, base_parsed, relative, relative_length,
  304. (base_is_hierarchical || standard_base_scheme),
  305. &is_relative, &relative_component)) {
  306. // Error resolving.
  307. return false;
  308. }
  309. // Don't reserve buffer space here. Instead, reserve in DoCanonicalize and
  310. // ReserveRelativeURL, to enable more accurate buffer sizes.
  311. // Pretend for a moment that |base_spec| is a standard URL. Normally
  312. // non-standard URLs are treated as PathURLs, but if the base has an
  313. // authority we would like to preserve it.
  314. if (is_relative && base_is_authority_based && !standard_base_scheme) {
  315. Parsed base_parsed_authority;
  316. ParseStandardURL(base_spec, base_spec_len, &base_parsed_authority);
  317. if (base_parsed_authority.host.is_nonempty()) {
  318. STACK_UNINITIALIZED RawCanonOutputT<char> temporary_output;
  319. bool did_resolve_succeed =
  320. ResolveRelativeURL(base_spec, base_parsed_authority, false, relative,
  321. relative_component, charset_converter,
  322. &temporary_output, output_parsed);
  323. // The output_parsed is incorrect at this point (because it was built
  324. // based on base_parsed_authority instead of base_parsed) and needs to be
  325. // re-created.
  326. DoCanonicalize(temporary_output.data(), temporary_output.length(), true,
  327. REMOVE_WHITESPACE, charset_converter, output,
  328. output_parsed);
  329. return did_resolve_succeed;
  330. }
  331. } else if (is_relative) {
  332. // Relative, resolve and canonicalize.
  333. bool file_base_scheme = base_parsed.scheme.is_nonempty() &&
  334. DoCompareSchemeComponent(base_spec, base_parsed.scheme, kFileScheme);
  335. return ResolveRelativeURL(base_spec, base_parsed, file_base_scheme, relative,
  336. relative_component, charset_converter, output,
  337. output_parsed);
  338. }
  339. // Not relative, canonicalize the input.
  340. return DoCanonicalize(relative, relative_length, true,
  341. DO_NOT_REMOVE_WHITESPACE, charset_converter, output,
  342. output_parsed);
  343. }
  344. template<typename CHAR>
  345. bool DoReplaceComponents(const char* spec,
  346. int spec_len,
  347. const Parsed& parsed,
  348. const Replacements<CHAR>& replacements,
  349. CharsetConverter* charset_converter,
  350. CanonOutput* output,
  351. Parsed* out_parsed) {
  352. // If the scheme is overridden, just do a simple string substitution and
  353. // re-parse the whole thing. There are lots of edge cases that we really don't
  354. // want to deal with. Like what happens if I replace "http://e:8080/foo"
  355. // with a file. Does it become "file:///E:/8080/foo" where the port number
  356. // becomes part of the path? Parsing that string as a file URL says "yes"
  357. // but almost no sane rule for dealing with the components individually would
  358. // come up with that.
  359. //
  360. // Why allow these crazy cases at all? Programatically, there is almost no
  361. // case for replacing the scheme. The most common case for hitting this is
  362. // in JS when building up a URL using the location object. In this case, the
  363. // JS code expects the string substitution behavior:
  364. // http://www.w3.org/TR/2008/WD-html5-20080610/structured.html#common3
  365. if (replacements.IsSchemeOverridden()) {
  366. // Canonicalize the new scheme so it is 8-bit and can be concatenated with
  367. // the existing spec.
  368. STACK_UNINITIALIZED RawCanonOutput<128> scheme_replaced;
  369. Component scheme_replaced_parsed;
  370. CanonicalizeScheme(replacements.sources().scheme,
  371. replacements.components().scheme,
  372. &scheme_replaced, &scheme_replaced_parsed);
  373. // We can assume that the input is canonicalized, which means it always has
  374. // a colon after the scheme (or where the scheme would be).
  375. int spec_after_colon = parsed.scheme.is_valid() ? parsed.scheme.end() + 1
  376. : 1;
  377. if (spec_len - spec_after_colon > 0) {
  378. scheme_replaced.Append(&spec[spec_after_colon],
  379. spec_len - spec_after_colon);
  380. }
  381. // We now need to completely re-parse the resulting string since its meaning
  382. // may have changed with the different scheme.
  383. STACK_UNINITIALIZED RawCanonOutput<128> recanonicalized;
  384. Parsed recanonicalized_parsed;
  385. DoCanonicalize(scheme_replaced.data(), scheme_replaced.length(), true,
  386. REMOVE_WHITESPACE, charset_converter, &recanonicalized,
  387. &recanonicalized_parsed);
  388. // Recurse using the version with the scheme already replaced. This will now
  389. // use the replacement rules for the new scheme.
  390. //
  391. // Warning: this code assumes that ReplaceComponents will re-check all
  392. // components for validity. This is because we can't fail if DoCanonicalize
  393. // failed above since theoretically the thing making it fail could be
  394. // getting replaced here. If ReplaceComponents didn't re-check everything,
  395. // we wouldn't know if something *not* getting replaced is a problem.
  396. // If the scheme-specific replacers are made more intelligent so they don't
  397. // re-check everything, we should instead re-canonicalize the whole thing
  398. // after this call to check validity (this assumes replacing the scheme is
  399. // much much less common than other types of replacements, like clearing the
  400. // ref).
  401. Replacements<CHAR> replacements_no_scheme = replacements;
  402. replacements_no_scheme.SetScheme(NULL, Component());
  403. // If the input URL has potentially dangling markup, set the flag on the
  404. // output too. Note that in some cases the replacement gets rid of the
  405. // potentially dangling markup, but this ok since the check will fail
  406. // closed.
  407. if (parsed.potentially_dangling_markup) {
  408. out_parsed->potentially_dangling_markup = true;
  409. }
  410. return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(),
  411. recanonicalized_parsed, replacements_no_scheme,
  412. charset_converter, output, out_parsed);
  413. }
  414. // TODO(csharrison): We could be smarter about size to reserve if this is done
  415. // in callers below, and the code checks to see which components are being
  416. // replaced, and with what length. If this ends up being a hot spot it should
  417. // be changed.
  418. output->ReserveSizeIfNeeded(spec_len);
  419. // If we get here, then we know the scheme doesn't need to be replaced, so can
  420. // just key off the scheme in the spec to know how to do the replacements.
  421. if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileScheme)) {
  422. return ReplaceFileURL(spec, parsed, replacements, charset_converter, output,
  423. out_parsed);
  424. }
  425. if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileSystemScheme)) {
  426. return ReplaceFileSystemURL(spec, parsed, replacements, charset_converter,
  427. output, out_parsed);
  428. }
  429. SchemeType scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
  430. if (DoIsStandard(spec, parsed.scheme, &scheme_type)) {
  431. return ReplaceStandardURL(spec, parsed, replacements, scheme_type,
  432. charset_converter, output, out_parsed);
  433. }
  434. if (DoCompareSchemeComponent(spec, parsed.scheme, url::kMailToScheme)) {
  435. return ReplaceMailtoURL(spec, parsed, replacements, output, out_parsed);
  436. }
  437. // Default is a path URL.
  438. return ReplacePathURL(spec, parsed, replacements, output, out_parsed);
  439. }
  440. void DoSchemeModificationPreamble() {
  441. // If this assert triggers, it means you've called Add*Scheme after
  442. // the SchemeRegistry has been used.
  443. //
  444. // This normally means you're trying to set up a new scheme too late or using
  445. // the SchemeRegistry too early in your application's init process.
  446. DCHECK(!g_scheme_registries_used.load())
  447. << "Trying to add a scheme after the lists have been used. "
  448. "Make sure that you haven't added any static GURL initializers in tests.";
  449. // If this assert triggers, it means you've called Add*Scheme after
  450. // LockSchemeRegistries has been called (see the header file for
  451. // LockSchemeRegistries for more).
  452. //
  453. // This normally means you're trying to set up a new scheme too late in your
  454. // application's init process. Locate where your app does this initialization
  455. // and calls LockSchemeRegistries, and add your new scheme there.
  456. DCHECK(!scheme_registries_locked)
  457. << "Trying to add a scheme after the lists have been locked.";
  458. }
  459. void DoAddSchemeWithHandler(const char* new_scheme,
  460. const char* handler,
  461. std::vector<SchemeWithHandler>* schemes) {
  462. DoSchemeModificationPreamble();
  463. DCHECK(schemes);
  464. DCHECK(strlen(new_scheme) > 0);
  465. DCHECK(strlen(handler) > 0);
  466. DCHECK_EQ(base::ToLowerASCII(new_scheme), new_scheme);
  467. DCHECK(std::find_if(schemes->begin(), schemes->end(),
  468. [&new_scheme](const SchemeWithHandler& scheme) {
  469. return scheme.scheme == new_scheme;
  470. }) == schemes->end());
  471. schemes->push_back({new_scheme, handler});
  472. }
  473. void DoAddScheme(const char* new_scheme, std::vector<std::string>* schemes) {
  474. DoSchemeModificationPreamble();
  475. DCHECK(schemes);
  476. DCHECK(strlen(new_scheme) > 0);
  477. DCHECK_EQ(base::ToLowerASCII(new_scheme), new_scheme);
  478. DCHECK(std::find(schemes->begin(), schemes->end(), new_scheme) ==
  479. schemes->end());
  480. schemes->push_back(new_scheme);
  481. }
  482. void DoAddSchemeWithType(const char* new_scheme,
  483. SchemeType type,
  484. std::vector<SchemeWithType>* schemes) {
  485. DoSchemeModificationPreamble();
  486. DCHECK(schemes);
  487. DCHECK(strlen(new_scheme) > 0);
  488. DCHECK_EQ(base::ToLowerASCII(new_scheme), new_scheme);
  489. DCHECK(std::find_if(schemes->begin(), schemes->end(),
  490. [&new_scheme](const SchemeWithType& scheme) {
  491. return scheme.scheme == new_scheme;
  492. }) == schemes->end());
  493. schemes->push_back({new_scheme, type});
  494. }
  495. } // namespace
  496. void ClearSchemesForTests() {
  497. DCHECK(!g_scheme_registries_used.load())
  498. << "Schemes already used "
  499. << "(use ScopedSchemeRegistryForTests to relax for tests).";
  500. DCHECK(!scheme_registries_locked)
  501. << "Schemes already locked "
  502. << "(use ScopedSchemeRegistryForTests to relax for tests).";
  503. *GetSchemeRegistryWithoutLocking() = SchemeRegistry();
  504. }
  505. class ScopedSchemeRegistryInternal {
  506. public:
  507. ScopedSchemeRegistryInternal()
  508. : registry_(std::make_unique<SchemeRegistry>(
  509. *GetSchemeRegistryWithoutLocking())) {
  510. g_scheme_registries_used.store(false);
  511. scheme_registries_locked = false;
  512. }
  513. ~ScopedSchemeRegistryInternal() {
  514. *GetSchemeRegistryWithoutLocking() = *registry_;
  515. g_scheme_registries_used.store(true);
  516. scheme_registries_locked = true;
  517. }
  518. private:
  519. std::unique_ptr<SchemeRegistry> registry_;
  520. };
  521. ScopedSchemeRegistryForTests::ScopedSchemeRegistryForTests()
  522. : internal_(std::make_unique<ScopedSchemeRegistryInternal>()) {}
  523. ScopedSchemeRegistryForTests::~ScopedSchemeRegistryForTests() = default;
  524. void EnableNonStandardSchemesForAndroidWebView() {
  525. DoSchemeModificationPreamble();
  526. GetSchemeRegistryWithoutLocking()->allow_non_standard_schemes = true;
  527. }
  528. bool AllowNonStandardSchemesForAndroidWebView() {
  529. return GetSchemeRegistry().allow_non_standard_schemes;
  530. }
  531. void AddStandardScheme(const char* new_scheme, SchemeType type) {
  532. DoAddSchemeWithType(new_scheme, type,
  533. &GetSchemeRegistryWithoutLocking()->standard_schemes);
  534. }
  535. std::vector<std::string> GetStandardSchemes() {
  536. std::vector<std::string> result;
  537. result.reserve(GetSchemeRegistry().standard_schemes.size());
  538. for (const auto& entry : GetSchemeRegistry().standard_schemes) {
  539. result.push_back(entry.scheme);
  540. }
  541. return result;
  542. }
  543. void AddReferrerScheme(const char* new_scheme, SchemeType type) {
  544. DoAddSchemeWithType(new_scheme, type,
  545. &GetSchemeRegistryWithoutLocking()->referrer_schemes);
  546. }
  547. void AddSecureScheme(const char* new_scheme) {
  548. DoAddScheme(new_scheme, &GetSchemeRegistryWithoutLocking()->secure_schemes);
  549. }
  550. const std::vector<std::string>& GetSecureSchemes() {
  551. return GetSchemeRegistry().secure_schemes;
  552. }
  553. void AddLocalScheme(const char* new_scheme) {
  554. DoAddScheme(new_scheme, &GetSchemeRegistryWithoutLocking()->local_schemes);
  555. }
  556. const std::vector<std::string>& GetLocalSchemes() {
  557. return GetSchemeRegistry().local_schemes;
  558. }
  559. void AddNoAccessScheme(const char* new_scheme) {
  560. DoAddScheme(new_scheme,
  561. &GetSchemeRegistryWithoutLocking()->no_access_schemes);
  562. }
  563. const std::vector<std::string>& GetNoAccessSchemes() {
  564. return GetSchemeRegistry().no_access_schemes;
  565. }
  566. void AddCorsEnabledScheme(const char* new_scheme) {
  567. DoAddScheme(new_scheme,
  568. &GetSchemeRegistryWithoutLocking()->cors_enabled_schemes);
  569. }
  570. const std::vector<std::string>& GetCorsEnabledSchemes() {
  571. return GetSchemeRegistry().cors_enabled_schemes;
  572. }
  573. void AddWebStorageScheme(const char* new_scheme) {
  574. DoAddScheme(new_scheme,
  575. &GetSchemeRegistryWithoutLocking()->web_storage_schemes);
  576. }
  577. const std::vector<std::string>& GetWebStorageSchemes() {
  578. return GetSchemeRegistry().web_storage_schemes;
  579. }
  580. void AddCSPBypassingScheme(const char* new_scheme) {
  581. DoAddScheme(new_scheme,
  582. &GetSchemeRegistryWithoutLocking()->csp_bypassing_schemes);
  583. }
  584. const std::vector<std::string>& GetCSPBypassingSchemes() {
  585. return GetSchemeRegistry().csp_bypassing_schemes;
  586. }
  587. void AddEmptyDocumentScheme(const char* new_scheme) {
  588. DoAddScheme(new_scheme,
  589. &GetSchemeRegistryWithoutLocking()->empty_document_schemes);
  590. }
  591. const std::vector<std::string>& GetEmptyDocumentSchemes() {
  592. return GetSchemeRegistry().empty_document_schemes;
  593. }
  594. void AddPredefinedHandlerScheme(const char* new_scheme, const char* handler) {
  595. DoAddSchemeWithHandler(
  596. new_scheme, handler,
  597. &GetSchemeRegistryWithoutLocking()->predefined_handler_schemes);
  598. }
  599. std::vector<std::pair<std::string, std::string>> GetPredefinedHandlerSchemes() {
  600. std::vector<std::pair<std::string, std::string>> result;
  601. result.reserve(GetSchemeRegistry().predefined_handler_schemes.size());
  602. for (const SchemeWithHandler& entry :
  603. GetSchemeRegistry().predefined_handler_schemes) {
  604. result.emplace_back(entry.scheme, entry.handler);
  605. }
  606. return result;
  607. }
  608. void LockSchemeRegistries() {
  609. scheme_registries_locked = true;
  610. }
  611. bool IsStandard(const char* spec, const Component& scheme) {
  612. SchemeType unused_scheme_type;
  613. return DoIsStandard(spec, scheme, &unused_scheme_type);
  614. }
  615. bool GetStandardSchemeType(const char* spec,
  616. const Component& scheme,
  617. SchemeType* type) {
  618. return DoIsStandard(spec, scheme, type);
  619. }
  620. bool GetStandardSchemeType(const char16_t* spec,
  621. const Component& scheme,
  622. SchemeType* type) {
  623. return DoIsStandard(spec, scheme, type);
  624. }
  625. bool IsStandard(const char16_t* spec, const Component& scheme) {
  626. SchemeType unused_scheme_type;
  627. return DoIsStandard(spec, scheme, &unused_scheme_type);
  628. }
  629. bool IsReferrerScheme(const char* spec, const Component& scheme) {
  630. SchemeType unused_scheme_type;
  631. return DoIsInSchemes(spec, scheme, &unused_scheme_type,
  632. GetSchemeRegistry().referrer_schemes);
  633. }
  634. bool FindAndCompareScheme(const char* str,
  635. int str_len,
  636. const char* compare,
  637. Component* found_scheme) {
  638. return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
  639. }
  640. bool FindAndCompareScheme(const char16_t* str,
  641. int str_len,
  642. const char* compare,
  643. Component* found_scheme) {
  644. return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
  645. }
  646. bool DomainIs(base::StringPiece canonical_host,
  647. base::StringPiece canonical_domain) {
  648. if (canonical_host.empty() || canonical_domain.empty())
  649. return false;
  650. // If the host name ends with a dot but the input domain doesn't, then we
  651. // ignore the dot in the host name.
  652. size_t host_len = canonical_host.length();
  653. if (canonical_host.back() == '.' && canonical_domain.back() != '.')
  654. --host_len;
  655. if (host_len < canonical_domain.length())
  656. return false;
  657. // |host_first_pos| is the start of the compared part of the host name, not
  658. // start of the whole host name.
  659. const char* host_first_pos =
  660. canonical_host.data() + host_len - canonical_domain.length();
  661. if (base::StringPiece(host_first_pos, canonical_domain.length()) !=
  662. canonical_domain) {
  663. return false;
  664. }
  665. // Make sure there aren't extra characters in host before the compared part;
  666. // if the host name is longer than the input domain name, then the character
  667. // immediately before the compared part should be a dot. For example,
  668. // www.google.com has domain "google.com", but www.iamnotgoogle.com does not.
  669. if (canonical_domain[0] != '.' && host_len > canonical_domain.length() &&
  670. *(host_first_pos - 1) != '.') {
  671. return false;
  672. }
  673. return true;
  674. }
  675. bool HostIsIPAddress(base::StringPiece host) {
  676. STACK_UNINITIALIZED url::RawCanonOutputT<char, 128> ignored_output;
  677. url::CanonHostInfo host_info;
  678. url::CanonicalizeIPAddress(host.data(), Component(0, host.length()),
  679. &ignored_output, &host_info);
  680. return host_info.IsIPAddress();
  681. }
  682. bool Canonicalize(const char* spec,
  683. int spec_len,
  684. bool trim_path_end,
  685. CharsetConverter* charset_converter,
  686. CanonOutput* output,
  687. Parsed* output_parsed) {
  688. return DoCanonicalize(spec, spec_len, trim_path_end, REMOVE_WHITESPACE,
  689. charset_converter, output, output_parsed);
  690. }
  691. bool Canonicalize(const char16_t* spec,
  692. int spec_len,
  693. bool trim_path_end,
  694. CharsetConverter* charset_converter,
  695. CanonOutput* output,
  696. Parsed* output_parsed) {
  697. return DoCanonicalize(spec, spec_len, trim_path_end, REMOVE_WHITESPACE,
  698. charset_converter, output, output_parsed);
  699. }
  700. bool ResolveRelative(const char* base_spec,
  701. int base_spec_len,
  702. const Parsed& base_parsed,
  703. const char* relative,
  704. int relative_length,
  705. CharsetConverter* charset_converter,
  706. CanonOutput* output,
  707. Parsed* output_parsed) {
  708. return DoResolveRelative(base_spec, base_spec_len, base_parsed,
  709. relative, relative_length,
  710. charset_converter, output, output_parsed);
  711. }
  712. bool ResolveRelative(const char* base_spec,
  713. int base_spec_len,
  714. const Parsed& base_parsed,
  715. const char16_t* relative,
  716. int relative_length,
  717. CharsetConverter* charset_converter,
  718. CanonOutput* output,
  719. Parsed* output_parsed) {
  720. return DoResolveRelative(base_spec, base_spec_len, base_parsed,
  721. relative, relative_length,
  722. charset_converter, output, output_parsed);
  723. }
  724. bool ReplaceComponents(const char* spec,
  725. int spec_len,
  726. const Parsed& parsed,
  727. const Replacements<char>& replacements,
  728. CharsetConverter* charset_converter,
  729. CanonOutput* output,
  730. Parsed* out_parsed) {
  731. return DoReplaceComponents(spec, spec_len, parsed, replacements,
  732. charset_converter, output, out_parsed);
  733. }
  734. bool ReplaceComponents(const char* spec,
  735. int spec_len,
  736. const Parsed& parsed,
  737. const Replacements<char16_t>& replacements,
  738. CharsetConverter* charset_converter,
  739. CanonOutput* output,
  740. Parsed* out_parsed) {
  741. return DoReplaceComponents(spec, spec_len, parsed, replacements,
  742. charset_converter, output, out_parsed);
  743. }
  744. void DecodeURLEscapeSequences(const char* input,
  745. int length,
  746. DecodeURLMode mode,
  747. CanonOutputW* output) {
  748. if (length <= 0)
  749. return;
  750. STACK_UNINITIALIZED RawCanonOutputT<char> unescaped_chars;
  751. size_t length_size_t = static_cast<size_t>(length);
  752. for (size_t i = 0; i < length_size_t; i++) {
  753. if (input[i] == '%') {
  754. unsigned char ch;
  755. if (DecodeEscaped(input, &i, length_size_t, &ch)) {
  756. unescaped_chars.push_back(ch);
  757. } else {
  758. // Invalid escape sequence, copy the percent literal.
  759. unescaped_chars.push_back('%');
  760. }
  761. } else {
  762. // Regular non-escaped 8-bit character.
  763. unescaped_chars.push_back(input[i]);
  764. }
  765. }
  766. int output_initial_length = output->length();
  767. // Convert that 8-bit to UTF-16. It's not clear IE does this at all to
  768. // JavaScript URLs, but Firefox and Safari do.
  769. size_t unescaped_length = unescaped_chars.length();
  770. for (size_t i = 0; i < unescaped_length; i++) {
  771. unsigned char uch = static_cast<unsigned char>(unescaped_chars.at(i));
  772. if (uch < 0x80) {
  773. // Non-UTF-8, just append directly
  774. output->push_back(uch);
  775. } else {
  776. // next_ch will point to the last character of the decoded
  777. // character.
  778. size_t next_character = i;
  779. base_icu::UChar32 code_point;
  780. if (ReadUTFChar(unescaped_chars.data(), &next_character, unescaped_length,
  781. &code_point)) {
  782. // Valid UTF-8 character, convert to UTF-16.
  783. AppendUTF16Value(code_point, output);
  784. i = next_character;
  785. } else if (mode == DecodeURLMode::kUTF8) {
  786. DCHECK_EQ(code_point, 0xFFFD);
  787. AppendUTF16Value(code_point, output);
  788. i = next_character;
  789. } else {
  790. // If there are any sequences that are not valid UTF-8, we
  791. // revert |output| changes, and promote any bytes to UTF-16. We
  792. // copy all characters from the beginning to the end of the
  793. // identified sequence.
  794. output->set_length(output_initial_length);
  795. for (size_t j = 0; j < unescaped_chars.length(); ++j)
  796. output->push_back(static_cast<unsigned char>(unescaped_chars.at(j)));
  797. break;
  798. }
  799. }
  800. }
  801. }
  802. void EncodeURIComponent(const char* input, int length, CanonOutput* output) {
  803. for (int i = 0; i < length; ++i) {
  804. unsigned char c = static_cast<unsigned char>(input[i]);
  805. if (IsComponentChar(c))
  806. output->push_back(c);
  807. else
  808. AppendEscapedChar(c, output);
  809. }
  810. }
  811. bool CompareSchemeComponent(const char* spec,
  812. const Component& component,
  813. const char* compare_to) {
  814. return DoCompareSchemeComponent(spec, component, compare_to);
  815. }
  816. bool CompareSchemeComponent(const char16_t* spec,
  817. const Component& component,
  818. const char* compare_to) {
  819. return DoCompareSchemeComponent(spec, component, compare_to);
  820. }
  821. } // namespace url