AbstractOperations.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/HTML/Origin.h>
  10. #include <LibWeb/SecureContexts/AbstractOperations.h>
  11. #include <LibWeb/URL/URL.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. if (auto ipv4_address = IPv4Address::from_string(origin.host()); ipv4_address.has_value() && (ipv4_address->to_u32() & 0xff000000) != 0)
  26. return Trustworthiness::PotentiallyTrustworthy;
  27. if (auto ipv6_address = IPv6Address::from_string(origin.host()); ipv6_address.has_value() && ipv6_address->to_deprecated_string() == "::1")
  28. return Trustworthiness::PotentiallyTrustworthy;
  29. // 5. If the user agent conforms to the name resolution rules in [let-localhost-be-localhost] and one of the following is true:
  30. // - origin’s host is "localhost" or "localhost."
  31. // - origin’s host ends with ".localhost" or ".localhost."
  32. // then return "Potentially Trustworthy".
  33. // Note: See § 5.2 localhost for details on the requirements here.
  34. if (origin.host().is_one_of("localhost"sv, "localhost.")
  35. || origin.host().ends_with(".localhost"sv)
  36. || origin.host().ends_with(".localhost."sv)) {
  37. return Trustworthiness::PotentiallyTrustworthy;
  38. }
  39. // 6. If origin’s scheme is "file", return "Potentially Trustworthy".
  40. if (origin.scheme() == "file"sv)
  41. return Trustworthiness::PotentiallyTrustworthy;
  42. // 7. If origin’s scheme component is one which the user agent considers to be authenticated, return "Potentially Trustworthy".
  43. // Note: See § 7.1 Packaged Applications for detail here.
  44. // 8. If origin has been configured as a trustworthy origin, return "Potentially Trustworthy".
  45. // Note: See § 7.2 Development Environments for detail here.
  46. // 9. Return "Not Trustworthy".
  47. return Trustworthiness::NotTrustworthy;
  48. }
  49. // https://w3c.github.io/webappsec-secure-contexts/#is-url-trustworthy
  50. Trustworthiness is_url_potentially_trustworthy(AK::URL const& url)
  51. {
  52. // 1. If url is "about:blank" or "about:srcdoc", return "Potentially Trustworthy".
  53. if (url == "about:blank"sv || url == "about:srcdoc"sv)
  54. return Trustworthiness::PotentiallyTrustworthy;
  55. // 2. If url’s scheme is "data", return "Potentially Trustworthy".
  56. if (url.scheme() == "data"sv)
  57. return Trustworthiness::PotentiallyTrustworthy;
  58. // 3. Return the result of executing § 3.1 Is origin potentially trustworthy? on url’s origin.
  59. return is_origin_potentially_trustworthy(URL::url_origin(url));
  60. }
  61. }