AbstractOperations.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/IPv4Address.h>
  7. #include <AK/IPv6Address.h>
  8. #include <AK/URL.h>
  9. #include <LibWeb/DOMURL/DOMURL.h>
  10. #include <LibWeb/HTML/Origin.h>
  11. #include <LibWeb/SecureContexts/AbstractOperations.h>
  12. namespace Web::SecureContexts {
  13. // https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy
  14. Trustworthiness is_origin_potentially_trustworthy(HTML::Origin const& origin)
  15. {
  16. // 1. If origin is an opaque origin, return "Not Trustworthy".
  17. if (origin.is_opaque())
  18. return Trustworthiness::NotTrustworthy;
  19. // 2. Assert: origin is a tuple origin.
  20. // 3. If origin’s scheme is either "https" or "wss", return "Potentially Trustworthy".
  21. // Note: This is meant to be analog to the a priori authenticated URL concept in [MIX].
  22. if (origin.scheme().is_one_of("https"sv, "wss"sv))
  23. return Trustworthiness::PotentiallyTrustworthy;
  24. // 4. If origin’s host matches one of the CIDR notations 127.0.0.0/8 or ::1/128 [RFC4632], return "Potentially Trustworthy".
  25. // FIXME: This would be nicer if URL::IPv4Address and URL::IPv6Address were instances of AK::IPv4Address and AK::IPv6Address
  26. if (origin.host().has<URL::IPv4Address>()) {
  27. if ((origin.host().get<URL::IPv4Address>() & 0xff000000) != 0)
  28. return Trustworthiness::PotentiallyTrustworthy;
  29. } else if (origin.host().has<URL::IPv6Address>()) {
  30. auto ipv6_address = origin.host().get<URL::IPv6Address>();
  31. static constexpr URL::IPv6Address loopback { 0, 0, 0, 0, 0, 0, 0, 1 };
  32. if (ipv6_address == loopback)
  33. return Trustworthiness::PotentiallyTrustworthy;
  34. }
  35. // 5. If the user agent conforms to the name resolution rules in [let-localhost-be-localhost] and one of the following is true:
  36. // - origin’s host is "localhost" or "localhost."
  37. // - origin’s host ends with ".localhost" or ".localhost."
  38. // then return "Potentially Trustworthy".
  39. // Note: See § 5.2 localhost for details on the requirements here.
  40. if (origin.host().has<String>()) {
  41. auto const& host = origin.host().get<String>();
  42. if (host.is_one_of("localhost"sv, "localhost.")
  43. || host.ends_with_bytes(".localhost"sv)
  44. || host.ends_with_bytes(".localhost."sv)) {
  45. return Trustworthiness::PotentiallyTrustworthy;
  46. }
  47. }
  48. // 6. If origin’s scheme is "file", return "Potentially Trustworthy".
  49. if (origin.scheme() == "file"sv)
  50. return Trustworthiness::PotentiallyTrustworthy;
  51. // 7. If origin’s scheme component is one which the user agent considers to be authenticated, return "Potentially Trustworthy".
  52. // Note: See § 7.1 Packaged Applications for detail here.
  53. // 8. If origin has been configured as a trustworthy origin, return "Potentially Trustworthy".
  54. // Note: See § 7.2 Development Environments for detail here.
  55. // 9. Return "Not Trustworthy".
  56. return Trustworthiness::NotTrustworthy;
  57. }
  58. // https://w3c.github.io/webappsec-secure-contexts/#is-url-trustworthy
  59. Trustworthiness is_url_potentially_trustworthy(URL const& url)
  60. {
  61. // 1. If url is "about:blank" or "about:srcdoc", return "Potentially Trustworthy".
  62. if (url == "about:blank"sv || url == "about:srcdoc"sv)
  63. return Trustworthiness::PotentiallyTrustworthy;
  64. // 2. If url’s scheme is "data", return "Potentially Trustworthy".
  65. if (url.scheme() == "data"sv)
  66. return Trustworthiness::PotentiallyTrustworthy;
  67. // 3. Return the result of executing § 3.1 Is origin potentially trustworthy? on url’s origin.
  68. return is_origin_potentially_trustworthy(DOMURL::url_origin(url));
  69. }
  70. }