AbstractOperations.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/Window.h>
  7. #include <LibWeb/MixedContent/AbstractOperations.h>
  8. #include <LibWeb/SecureContexts/AbstractOperations.h>
  9. namespace Web::MixedContent {
  10. // https://w3c.github.io/webappsec-mixed-content/#categorize-settings-object
  11. ProhibitsMixedSecurityContexts does_settings_prohibit_mixed_security_contexts(JS::GCPtr<HTML::EnvironmentSettingsObject> settings)
  12. {
  13. // 1. If settings’ origin is a potentially trustworthy origin, then return "Prohibits Mixed Security Contexts".
  14. if (SecureContexts::is_origin_potentially_trustworthy(settings->origin()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  15. return ProhibitsMixedSecurityContexts::ProhibitsMixedSecurityContexts;
  16. // 2. If settings’ global object is a window, then:
  17. if (is<HTML::Window>(settings->global_object())) {
  18. // 1. Set document to settings’ global object's associated Document.
  19. auto document = verify_cast<HTML::Window>(settings->global_object()).document();
  20. // 2. For each navigable navigable in document’s ancestor navigables:
  21. for (auto const& navigable : document->ancestor_navigables()) {
  22. // 1. If navigable’s active document's origin is a potentially trustworthy origin, then return "Prohibits Mixed Security Contexts".
  23. if (SecureContexts::is_origin_potentially_trustworthy(navigable->active_document()->origin()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  24. return ProhibitsMixedSecurityContexts::ProhibitsMixedSecurityContexts;
  25. }
  26. }
  27. // 3. Return "Does Not Restrict Mixed Security Contexts".
  28. return ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts;
  29. }
  30. // https://w3c.github.io/webappsec-mixed-content/#should-block-fetch
  31. Fetch::Infrastructure::RequestOrResponseBlocking should_fetching_request_be_blocked_as_mixed_content(Fetch::Infrastructure::Request& request)
  32. {
  33. // 1. Return allowed if one or more of the following conditions are met:
  34. if (
  35. // 1. § 4.3 Does settings prohibit mixed security contexts? returns "Does Not Restrict Mixed Security Contexts" when applied to request’s client.
  36. does_settings_prohibit_mixed_security_contexts(request.client()) == ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts
  37. // 2. request’s URL is a potentially trustworthy URL.
  38. || SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy
  39. // FIXME: 3. The user agent has been instructed to allow mixed content, as described in § 7.2 User Controls).
  40. || false
  41. // 4. request’s destination is "document", and request’s target browsing context has no parent browsing context.
  42. || (request.destination() == Fetch::Infrastructure::Request::Destination::Document && !request.client()->target_browsing_context->parent())) {
  43. return Fetch::Infrastructure::RequestOrResponseBlocking::Allowed;
  44. }
  45. // 2. Return blocked.
  46. dbgln("MixedContent: Blocked '{}' (request)", MUST(request.url().to_string()));
  47. return Fetch::Infrastructure::RequestOrResponseBlocking::Blocked;
  48. }
  49. }