AbstractOperations.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Fetch/Response.h>
  7. #include <LibWeb/HTML/Window.h>
  8. #include <LibWeb/MixedContent/AbstractOperations.h>
  9. #include <LibWeb/SecureContexts/AbstractOperations.h>
  10. namespace Web::MixedContent {
  11. // https://w3c.github.io/webappsec-mixed-content/#upgrade-algorithm
  12. void upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate(Fetch::Infrastructure::Request& request)
  13. {
  14. // 1. If one or more of the following conditions is met, return without modifying request:
  15. if (
  16. // 1. request’s URL is a potentially trustworthy URL.
  17. SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy
  18. // 2. request’s URL’s host is an IP address.
  19. || request.url().host().has<URL::IPv4Address>() || request.url().host().has<URL::IPv6Address>()
  20. // 3. § 4.3 Does settings prohibit mixed security contexts? returns "Does Not Restrict Mixed Security Contents" when applied to request’s client.
  21. || does_settings_prohibit_mixed_security_contexts(request.client()) == ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts
  22. // 4. request’s destination is not "image", "audio", or "video".
  23. || (request.destination() != Fetch::Infrastructure::Request::Destination::Image
  24. && request.destination() != Fetch::Infrastructure::Request::Destination::Audio
  25. && request.destination() != Fetch::Infrastructure::Request::Destination::Video)
  26. // 5. request’s destination is "image" and request’s initiator is "imageset".
  27. || (request.destination() == Fetch::Infrastructure::Request::Destination::Image
  28. && request.initiator() == Fetch::Infrastructure::Request::Initiator::ImageSet)) {
  29. return;
  30. }
  31. // 2. If request’s URL’s scheme is http, set request’s URL’s scheme to https, and return.
  32. if (request.url().scheme() == "http")
  33. request.url().set_scheme("https"_string);
  34. }
  35. // https://w3c.github.io/webappsec-mixed-content/#categorize-settings-object
  36. ProhibitsMixedSecurityContexts does_settings_prohibit_mixed_security_contexts(JS::GCPtr<HTML::EnvironmentSettingsObject> settings)
  37. {
  38. // 1. If settings’ origin is a potentially trustworthy origin, then return "Prohibits Mixed Security Contexts".
  39. if (SecureContexts::is_origin_potentially_trustworthy(settings->origin()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  40. return ProhibitsMixedSecurityContexts::ProhibitsMixedSecurityContexts;
  41. // 2. If settings’ global object is a window, then:
  42. if (is<HTML::Window>(settings->global_object())) {
  43. // 1. Set document to settings’ global object's associated Document.
  44. auto document = verify_cast<HTML::Window>(settings->global_object()).document();
  45. // 2. For each navigable navigable in document’s ancestor navigables:
  46. for (auto const& navigable : document->ancestor_navigables()) {
  47. // 1. If navigable’s active document's origin is a potentially trustworthy origin, then return "Prohibits Mixed Security Contexts".
  48. if (SecureContexts::is_origin_potentially_trustworthy(navigable->active_document()->origin()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  49. return ProhibitsMixedSecurityContexts::ProhibitsMixedSecurityContexts;
  50. }
  51. }
  52. // 3. Return "Does Not Restrict Mixed Security Contexts".
  53. return ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts;
  54. }
  55. // https://w3c.github.io/webappsec-mixed-content/#should-block-fetch
  56. Fetch::Infrastructure::RequestOrResponseBlocking should_fetching_request_be_blocked_as_mixed_content(Fetch::Infrastructure::Request& request)
  57. {
  58. // 1. Return allowed if one or more of the following conditions are met:
  59. if (
  60. // 1. § 4.3 Does settings prohibit mixed security contexts? returns "Does Not Restrict Mixed Security Contexts" when applied to request’s client.
  61. does_settings_prohibit_mixed_security_contexts(request.client()) == ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts
  62. // 2. request’s URL is a potentially trustworthy URL.
  63. || SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy
  64. // FIXME: 3. The user agent has been instructed to allow mixed content, as described in § 7.2 User Controls).
  65. || false
  66. // 4. request’s destination is "document", and request’s target browsing context has no parent browsing context.
  67. || (request.destination() == Fetch::Infrastructure::Request::Destination::Document && !request.client()->target_browsing_context->parent())) {
  68. return Fetch::Infrastructure::RequestOrResponseBlocking::Allowed;
  69. }
  70. // 2. Return blocked.
  71. dbgln("MixedContent: Blocked '{}' (request)", MUST(request.url().to_string()));
  72. return Fetch::Infrastructure::RequestOrResponseBlocking::Blocked;
  73. }
  74. // https://w3c.github.io/webappsec-mixed-content/#should-block-response
  75. Web::Fetch::Infrastructure::RequestOrResponseBlocking should_response_to_request_be_blocked_as_mixed_content(Fetch::Infrastructure::Request& request, JS::NonnullGCPtr<Fetch::Infrastructure::Response>& response)
  76. {
  77. // 1. Return allowed if one or more of the following conditions are met:
  78. if (
  79. // 1. § 4.3 Does settings prohibit mixed security contexts? returns Does Not Restrict Mixed Content when applied to request’s client.
  80. does_settings_prohibit_mixed_security_contexts(request.client()) == ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts
  81. // 2. response’s url is a potentially trustworthy URL.
  82. || (response->url().has_value() && SecureContexts::is_url_potentially_trustworthy(response->url().value()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  83. // FIXME: 3. The user agent has been instructed to allow mixed content, as described in § 7.2 User Controls).
  84. || false
  85. // 4. request’s destination is "document", and request’s target browsing context has no parent browsing context.
  86. || (request.destination() == Fetch::Infrastructure::Request::Destination::Document && !request.client()->target_browsing_context->parent())) {
  87. return Fetch::Infrastructure::RequestOrResponseBlocking::Allowed;
  88. }
  89. // 2. Return blocked.
  90. dbgln("MixedContent: Blocked '{}' (response to request)", MUST(request.url().to_string()));
  91. return Fetch::Infrastructure::RequestOrResponseBlocking::Blocked;
  92. }
  93. }