Checks.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Fetch/Fetching/Checks.h>
  7. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  8. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  9. #include <LibWeb/URL/URL.h>
  10. namespace Web::Fetch::Fetching {
  11. // https://fetch.spec.whatwg.org/#concept-cors-check
  12. ErrorOr<bool> cors_check(Infrastructure::Request const& request, Infrastructure::Response const& response)
  13. {
  14. // 1. Let origin be the result of getting `Access-Control-Allow-Origin` from response’s header list.
  15. auto origin = TRY(response.header_list()->get("Access-Control-Allow-Origin"sv.bytes()));
  16. // 2. If origin is null, then return failure.
  17. // NOTE: Null is not `null`.
  18. if (!origin.has_value())
  19. return false;
  20. // 3. If request’s credentials mode is not "include" and origin is `*`, then return success.
  21. if (request.credentials_mode() != Infrastructure::Request::CredentialsMode::Include && origin->span() == "*"sv.bytes())
  22. return true;
  23. // 4. If the result of byte-serializing a request origin with request is not origin, then return failure.
  24. if (TRY(request.byte_serialize_origin()) != *origin)
  25. return false;
  26. // 5. If request’s credentials mode is not "include", then return success.
  27. if (request.credentials_mode() != Infrastructure::Request::CredentialsMode::Include)
  28. return true;
  29. // 6. Let credentials be the result of getting `Access-Control-Allow-Credentials` from response’s header list.
  30. auto credentials = TRY(response.header_list()->get("Access-Control-Allow-Credentials"sv.bytes()));
  31. // 7. If credentials is `true`, then return success.
  32. if (credentials.has_value() && credentials->span() == "true"sv.bytes())
  33. return true;
  34. // 8. Return failure.
  35. return false;
  36. }
  37. // https://fetch.spec.whatwg.org/#concept-tao-check
  38. ErrorOr<bool> tao_check(Infrastructure::Request const& request, Infrastructure::Response const& response)
  39. {
  40. // 1. If request’s timing allow failed flag is set, then return failure.
  41. if (request.timing_allow_failed())
  42. return false;
  43. // 2. Let values be the result of getting, decoding, and splitting `Timing-Allow-Origin` from response’s header list.
  44. auto values = TRY(response.header_list()->get_decode_and_split("Timing-Allow-Origin"sv.bytes()));
  45. // 3. If values contains "*", then return success.
  46. if (values.has_value() && values->contains_slow("*"_short_string))
  47. return true;
  48. // 4. If values contains the result of serializing a request origin with request, then return success.
  49. if (values.has_value() && values->contains_slow(TRY(request.serialize_origin())))
  50. return true;
  51. // 5. If request’s mode is "navigate" and request’s current URL’s origin is not same origin with request’s origin, then return failure.
  52. // NOTE: This is necessary for navigations of a nested browsing context. There, request’s origin would be the
  53. // container document’s origin and the TAO check would return failure. Since navigation timing never
  54. // validates the results of the TAO check, the nested document would still have access to the full timing
  55. // information, but the container document would not.
  56. if (request.mode() == Infrastructure::Request::Mode::Navigate
  57. && request.origin().has<HTML::Origin>()
  58. && !URL::url_origin(request.current_url()).is_same_origin(request.origin().get<HTML::Origin>())) {
  59. return false;
  60. }
  61. // 6. If request’s response tainting is "basic", then return success.
  62. if (request.response_tainting() == Infrastructure::Request::ResponseTainting::Basic)
  63. return true;
  64. // 7. Return failure.
  65. return false;
  66. }
  67. }