Requests.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibWeb/DOMURL/DOMURL.h>
  10. #include <LibWeb/Fetch/Fetching/PendingResponse.h>
  11. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  12. namespace Web::Fetch::Infrastructure {
  13. JS_DEFINE_ALLOCATOR(Request);
  14. Request::Request(JS::NonnullGCPtr<HeaderList> header_list)
  15. : m_header_list(header_list)
  16. {
  17. }
  18. void Request::visit_edges(JS::Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(m_header_list);
  22. visitor.visit(m_client);
  23. m_body.visit(
  24. [&](JS::NonnullGCPtr<Body>& body) { visitor.visit(body); },
  25. [](auto&) {});
  26. m_reserved_client.visit(
  27. [&](JS::GCPtr<HTML::EnvironmentSettingsObject> const& value) { visitor.visit(value); },
  28. [](auto const&) {});
  29. m_window.visit(
  30. [&](JS::GCPtr<HTML::EnvironmentSettingsObject> const& value) { visitor.visit(value); },
  31. [](auto const&) {});
  32. visitor.visit(m_pending_responses);
  33. }
  34. JS::NonnullGCPtr<Request> Request::create(JS::VM& vm)
  35. {
  36. return vm.heap().allocate_without_realm<Request>(HeaderList::create(vm));
  37. }
  38. // https://fetch.spec.whatwg.org/#concept-request-url
  39. URL::URL& Request::url()
  40. {
  41. // A request has an associated URL (a URL).
  42. // NOTE: Implementations are encouraged to make this a pointer to the first URL in request’s URL list. It is provided as a distinct field solely for the convenience of other standards hooking into Fetch.
  43. VERIFY(!m_url_list.is_empty());
  44. return m_url_list.first();
  45. }
  46. // https://fetch.spec.whatwg.org/#concept-request-url
  47. URL::URL const& Request::url() const
  48. {
  49. return const_cast<Request&>(*this).url();
  50. }
  51. // https://fetch.spec.whatwg.org/#concept-request-current-url
  52. URL::URL& Request::current_url()
  53. {
  54. // A request has an associated current URL. It is a pointer to the last URL in request’s URL list.
  55. VERIFY(!m_url_list.is_empty());
  56. return m_url_list.last();
  57. }
  58. // https://fetch.spec.whatwg.org/#concept-request-current-url
  59. URL::URL const& Request::current_url() const
  60. {
  61. return const_cast<Request&>(*this).current_url();
  62. }
  63. void Request::set_url(URL::URL url)
  64. {
  65. // Sometimes setting the URL and URL list are done as two distinct steps in the spec,
  66. // but since we know the URL is always the URL list's first item and doesn't change later
  67. // on, we can combine them.
  68. if (!m_url_list.is_empty())
  69. m_url_list.clear();
  70. m_url_list.append(move(url));
  71. }
  72. // https://fetch.spec.whatwg.org/#request-destination-script-like
  73. bool Request::destination_is_script_like() const
  74. {
  75. // A request’s destination is script-like if it is "audioworklet", "paintworklet", "script", "serviceworker", "sharedworker", or "worker".
  76. static constexpr Array script_like_destinations = {
  77. Destination::AudioWorklet,
  78. Destination::PaintWorklet,
  79. Destination::Script,
  80. Destination::ServiceWorker,
  81. Destination::SharedWorker,
  82. Destination::Worker,
  83. };
  84. return any_of(script_like_destinations, [this](auto destination) {
  85. return m_destination == destination;
  86. });
  87. }
  88. // https://fetch.spec.whatwg.org/#subresource-request
  89. bool Request::is_subresource_request() const
  90. {
  91. // A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string.
  92. static constexpr Array subresource_request_destinations = {
  93. Destination::Audio,
  94. Destination::AudioWorklet,
  95. Destination::Font,
  96. Destination::Image,
  97. Destination::Manifest,
  98. Destination::PaintWorklet,
  99. Destination::Script,
  100. Destination::Style,
  101. Destination::Track,
  102. Destination::Video,
  103. Destination::XSLT,
  104. };
  105. return any_of(subresource_request_destinations, [this](auto destination) {
  106. return m_destination == destination;
  107. }) || !m_destination.has_value();
  108. }
  109. // https://fetch.spec.whatwg.org/#non-subresource-request
  110. bool Request::is_non_subresource_request() const
  111. {
  112. // A non-subresource request is a request whose destination is "document", "embed", "frame", "iframe", "object", "report", "serviceworker", "sharedworker", or "worker".
  113. static constexpr Array non_subresource_request_destinations = {
  114. Destination::Document,
  115. Destination::Embed,
  116. Destination::Frame,
  117. Destination::IFrame,
  118. Destination::Object,
  119. Destination::Report,
  120. Destination::ServiceWorker,
  121. Destination::SharedWorker,
  122. Destination::Worker,
  123. };
  124. return any_of(non_subresource_request_destinations, [this](auto destination) {
  125. return m_destination == destination;
  126. });
  127. }
  128. // https://fetch.spec.whatwg.org/#navigation-request
  129. bool Request::is_navigation_request() const
  130. {
  131. // A navigation request is a request whose destination is "document", "embed", "frame", "iframe", or "object".
  132. static constexpr Array navigation_request_destinations = {
  133. Destination::Document,
  134. Destination::Embed,
  135. Destination::Frame,
  136. Destination::IFrame,
  137. Destination::Object,
  138. };
  139. return any_of(navigation_request_destinations, [this](auto destination) {
  140. return m_destination == destination;
  141. });
  142. }
  143. // https://fetch.spec.whatwg.org/#concept-request-tainted-origin
  144. bool Request::has_redirect_tainted_origin() const
  145. {
  146. // A request request has a redirect-tainted origin if these steps return true:
  147. // 1. Let lastURL be null.
  148. Optional<URL::URL const&> last_url;
  149. // 2. For each url of request’s URL list:
  150. for (auto const& url : m_url_list) {
  151. // 1. If lastURL is null, then set lastURL to url and continue.
  152. if (!last_url.has_value()) {
  153. last_url = url;
  154. continue;
  155. }
  156. // 2. If url’s origin is not same origin with lastURL’s origin and request’s origin is not same origin with lastURL’s origin, then return true.
  157. auto const* request_origin = m_origin.get_pointer<HTML::Origin>();
  158. if (!DOMURL::url_origin(url).is_same_origin(DOMURL::url_origin(*last_url))
  159. && (request_origin == nullptr || !request_origin->is_same_origin(DOMURL::url_origin(*last_url)))) {
  160. return true;
  161. }
  162. // 3. Set lastURL to url.
  163. last_url = url;
  164. }
  165. // 3. Return false.
  166. return false;
  167. }
  168. // https://fetch.spec.whatwg.org/#serializing-a-request-origin
  169. ErrorOr<String> Request::serialize_origin() const
  170. {
  171. // 1. If request has a redirect-tainted origin, then return "null".
  172. if (has_redirect_tainted_origin())
  173. return "null"_string;
  174. // 2. Return request’s origin, serialized.
  175. return String::from_byte_string(m_origin.get<HTML::Origin>().serialize());
  176. }
  177. // https://fetch.spec.whatwg.org/#byte-serializing-a-request-origin
  178. ErrorOr<ByteBuffer> Request::byte_serialize_origin() const
  179. {
  180. // Byte-serializing a request origin, given a request request, is to return the result of serializing a request origin with request, isomorphic encoded.
  181. return ByteBuffer::copy(TRY(serialize_origin()).bytes());
  182. }
  183. // https://fetch.spec.whatwg.org/#concept-request-clone
  184. JS::NonnullGCPtr<Request> Request::clone(JS::Realm& realm) const
  185. {
  186. // To clone a request request, run these steps:
  187. auto& vm = realm.vm();
  188. // 1. Let newRequest be a copy of request, except for its body.
  189. auto new_request = Infrastructure::Request::create(vm);
  190. new_request->set_method(m_method);
  191. new_request->set_local_urls_only(m_local_urls_only);
  192. for (auto const& header : *m_header_list)
  193. MUST(new_request->header_list()->append(header));
  194. new_request->set_unsafe_request(m_unsafe_request);
  195. new_request->set_client(m_client);
  196. new_request->set_reserved_client(m_reserved_client);
  197. new_request->set_replaces_client_id(m_replaces_client_id);
  198. new_request->set_window(m_window);
  199. new_request->set_keepalive(m_keepalive);
  200. new_request->set_initiator_type(m_initiator_type);
  201. new_request->set_service_workers_mode(m_service_workers_mode);
  202. new_request->set_initiator(m_initiator);
  203. new_request->set_destination(m_destination);
  204. new_request->set_priority(m_priority);
  205. new_request->set_origin(m_origin);
  206. new_request->set_policy_container(m_policy_container);
  207. new_request->set_referrer(m_referrer);
  208. new_request->set_referrer_policy(m_referrer_policy);
  209. new_request->set_mode(m_mode);
  210. new_request->set_use_cors_preflight(m_use_cors_preflight);
  211. new_request->set_credentials_mode(m_credentials_mode);
  212. new_request->set_use_url_credentials(m_use_url_credentials);
  213. new_request->set_cache_mode(m_cache_mode);
  214. new_request->set_redirect_mode(m_redirect_mode);
  215. new_request->set_integrity_metadata(m_integrity_metadata);
  216. new_request->set_cryptographic_nonce_metadata(m_cryptographic_nonce_metadata);
  217. new_request->set_parser_metadata(m_parser_metadata);
  218. new_request->set_reload_navigation(m_reload_navigation);
  219. new_request->set_history_navigation(m_history_navigation);
  220. new_request->set_user_activation(m_user_activation);
  221. new_request->set_render_blocking(m_render_blocking);
  222. new_request->set_url_list(m_url_list);
  223. new_request->set_redirect_count(m_redirect_count);
  224. new_request->set_response_tainting(m_response_tainting);
  225. new_request->set_prevent_no_cache_cache_control_header_modification(m_prevent_no_cache_cache_control_header_modification);
  226. new_request->set_done(m_done);
  227. new_request->set_timing_allow_failed(m_timing_allow_failed);
  228. // 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
  229. if (auto const* body = m_body.get_pointer<JS::NonnullGCPtr<Body>>())
  230. new_request->set_body((*body)->clone(realm));
  231. // 3. Return newRequest.
  232. return new_request;
  233. }
  234. // https://fetch.spec.whatwg.org/#concept-request-add-range-header
  235. ErrorOr<void> Request::add_range_header(u64 first, Optional<u64> const& last)
  236. {
  237. // To add a range header to a request request, with an integer first, and an optional integer last, run these steps:
  238. // 1. Assert: last is not given, or first is less than or equal to last.
  239. VERIFY(!last.has_value() || first <= last.value());
  240. // 2. Let rangeValue be `bytes=`.
  241. auto range_value = MUST(ByteBuffer::copy("bytes"sv.bytes()));
  242. // 3. Serialize and isomorphic encode first, and append the result to rangeValue.
  243. TRY(range_value.try_append(TRY(String::number(first)).bytes()));
  244. // 4. Append 0x2D (-) to rangeValue.
  245. TRY(range_value.try_append('-'));
  246. // 5. If last is given, then serialize and isomorphic encode it, and append the result to rangeValue.
  247. if (last.has_value())
  248. TRY(range_value.try_append(TRY(String::number(*last)).bytes()));
  249. // 6. Append (`Range`, rangeValue) to request’s header list.
  250. auto header = Header {
  251. .name = MUST(ByteBuffer::copy("Range"sv.bytes())),
  252. .value = move(range_value),
  253. };
  254. TRY(m_header_list->append(move(header)));
  255. return {};
  256. }
  257. // https://fetch.spec.whatwg.org/#append-a-request-origin-header
  258. ErrorOr<void> Request::add_origin_header()
  259. {
  260. // 1. Let serializedOrigin be the result of byte-serializing a request origin with request.
  261. auto serialized_origin = TRY(byte_serialize_origin());
  262. // 2. If request’s response tainting is "cors" or request’s mode is "websocket", then append (`Origin`, serializedOrigin) to request’s header list.
  263. if (m_response_tainting == ResponseTainting::CORS || m_mode == Mode::WebSocket) {
  264. auto header = Header {
  265. .name = MUST(ByteBuffer::copy("Origin"sv.bytes())),
  266. .value = move(serialized_origin),
  267. };
  268. TRY(m_header_list->append(move(header)));
  269. }
  270. // 3. Otherwise, if request’s method is neither `GET` nor `HEAD`, then:
  271. else if (!StringView { m_method }.is_one_of("GET"sv, "HEAD"sv)) {
  272. // 1. If request’s mode is not "cors", then switch on request’s referrer policy:
  273. if (m_mode != Mode::CORS) {
  274. switch (m_referrer_policy) {
  275. // -> "no-referrer"
  276. case ReferrerPolicy::ReferrerPolicy::NoReferrer:
  277. // Set serializedOrigin to `null`.
  278. serialized_origin = MUST(ByteBuffer::copy("null"sv.bytes()));
  279. break;
  280. // -> "no-referrer-when-downgrade"
  281. // -> "strict-origin"
  282. // -> "strict-origin-when-cross-origin"
  283. case ReferrerPolicy::ReferrerPolicy::NoReferrerWhenDowngrade:
  284. case ReferrerPolicy::ReferrerPolicy::StrictOrigin:
  285. case ReferrerPolicy::ReferrerPolicy::StrictOriginWhenCrossOrigin:
  286. // If request’s origin is a tuple origin, its scheme is "https", and request’s current URL’s scheme is
  287. // not "https", then set serializedOrigin to `null`.
  288. if (m_origin.has<HTML::Origin>() && m_origin.get<HTML::Origin>().scheme() == "https"sv && current_url().scheme() != "https"sv)
  289. serialized_origin = MUST(ByteBuffer::copy("null"sv.bytes()));
  290. break;
  291. // -> "same-origin"
  292. case ReferrerPolicy::ReferrerPolicy::SameOrigin:
  293. // If request’s origin is not same origin with request’s current URL’s origin, then set serializedOrigin
  294. // to `null`.
  295. if (m_origin.has<HTML::Origin>() && !m_origin.get<HTML::Origin>().is_same_origin(DOMURL::url_origin(current_url())))
  296. serialized_origin = MUST(ByteBuffer::copy("null"sv.bytes()));
  297. break;
  298. // -> Otherwise
  299. default:
  300. // Do nothing.
  301. break;
  302. }
  303. }
  304. // 2. Append (`Origin`, serializedOrigin) to request’s header list.
  305. auto header = Header {
  306. .name = MUST(ByteBuffer::copy("Origin"sv.bytes())),
  307. .value = move(serialized_origin),
  308. };
  309. TRY(m_header_list->append(move(header)));
  310. }
  311. return {};
  312. }
  313. // https://fetch.spec.whatwg.org/#cross-origin-embedder-policy-allows-credentials
  314. bool Request::cross_origin_embedder_policy_allows_credentials() const
  315. {
  316. // 1. If request’s mode is not "no-cors", then return true.
  317. if (m_mode != Mode::NoCORS)
  318. return true;
  319. // 2. If request’s client is null, then return true.
  320. if (m_client == nullptr)
  321. return true;
  322. // FIXME: 3. If request’s client’s policy container’s embedder policy’s value is not "credentialless", then return true.
  323. // 4. If request’s origin is same origin with request’s current URL’s origin and request does not have a redirect-tainted origin, then return true.
  324. // 5. Return false.
  325. auto const* request_origin = m_origin.get_pointer<HTML::Origin>();
  326. if (request_origin == nullptr)
  327. return false;
  328. return request_origin->is_same_origin(DOMURL::url_origin(current_url())) && !has_redirect_tainted_origin();
  329. }
  330. }