Reporting.cpp 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Vector.h>
  8. #include <LibJS/Runtime/PropertyKey.h>
  9. #include <LibURL/Origin.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/BrowsingContext.h>
  12. #include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
  13. #include <LibWeb/HTML/CrossOrigin/Reporting.h>
  14. namespace Web::HTML {
  15. // https://html.spec.whatwg.org/multipage/origin.html#coop-check-access-report
  16. void check_if_access_between_two_browsing_contexts_should_be_reported(
  17. BrowsingContext const& accessor,
  18. BrowsingContext const* accessed,
  19. JS::PropertyKey const& property_key,
  20. EnvironmentSettingsObject const& environment)
  21. {
  22. // FIXME: Spec bug: https://github.com/whatwg/html/issues/10192
  23. if (!accessed)
  24. return;
  25. // 1. If propertyKey is not a cross-origin accessible window property name, then return.
  26. if (!is_cross_origin_accessible_window_property_name(property_key))
  27. return;
  28. // 2. Assert: accessor's active document and accessed's active document are both fully active.
  29. VERIFY(accessor.active_document()->is_fully_active());
  30. VERIFY(accessed->active_document()->is_fully_active());
  31. // 3. Let accessorTopDocument be accessor's top-level browsing context's active document.
  32. auto* accessor_top_document = accessor.top_level_browsing_context()->active_document();
  33. // 4. Let accessorInclusiveAncestorOrigins be the list obtained by taking the origin of the active document of each of accessor's active document's inclusive ancestor navigables.
  34. Vector<URL::Origin> accessor_inclusive_ancestor_origins = {};
  35. auto accessor_inclusive_ancestors = accessor.active_document()->ancestor_navigables();
  36. accessor_inclusive_ancestor_origins.ensure_capacity(accessor_inclusive_ancestors.size());
  37. for (auto const& ancestor : accessor_inclusive_ancestors) {
  38. VERIFY(ancestor != nullptr);
  39. VERIFY(ancestor->active_document() != nullptr);
  40. accessor_inclusive_ancestor_origins.append(ancestor->active_document()->origin());
  41. }
  42. // 5. Let accessedTopDocument be accessed's top-level browsing context's active document.
  43. VERIFY(accessed->top_level_browsing_context() != nullptr);
  44. auto* accessed_top_document = accessed->top_level_browsing_context()->active_document();
  45. // 6. Let accessedInclusiveAncestorOrigins be the list obtained by taking the origin of the active document of each of accessed's active document's inclusive ancestor navigables.
  46. Vector<URL::Origin> accessed_inclusive_ancestor_origins = {};
  47. auto accessed_inclusive_ancestors = accessed->active_document()->ancestor_navigables();
  48. accessed_inclusive_ancestor_origins.ensure_capacity(accessed_inclusive_ancestors.size());
  49. for (auto const& ancestor : accessed_inclusive_ancestors) {
  50. VERIFY(ancestor != nullptr);
  51. VERIFY(ancestor->active_document() != nullptr);
  52. accessed_inclusive_ancestor_origins.append(ancestor->active_document()->origin());
  53. }
  54. // 7. If any of accessorInclusiveAncestorOrigins are not same origin with accessorTopDocument's origin, or if any of accessedInclusiveAncestorOrigins are not same origin with accessedTopDocument's origin, then return.
  55. for (auto const& origin : accessor_inclusive_ancestor_origins)
  56. if (!origin.is_same_origin(accessor_top_document->origin()))
  57. return;
  58. for (auto const& origin : accessed_inclusive_ancestor_origins)
  59. if (!origin.is_same_origin(accessed_top_document->origin()))
  60. return;
  61. // 8. If accessor's top-level browsing context's virtual browsing context group ID is accessed's top-level browsing context's virtual browsing context group ID, then return.
  62. if (accessor.top_level_browsing_context()->virtual_browsing_context_group_id() == accessed->top_level_browsing_context()->virtual_browsing_context_group_id())
  63. return;
  64. // 9. Let accessorAccessedRelationship be a new accessor-accessed relationship with value none.
  65. auto accessor_accessed_relationship = AccessorAccessedRelationship::None;
  66. // 10. If accessed's top-level browsing context's opener browsing context is accessor or is an ancestor of accessor, then set accessorAccessedRelationship to accessor is opener.
  67. if (accessor.is_ancestor_of(*accessed->top_level_browsing_context()->opener_browsing_context()))
  68. accessor_accessed_relationship = AccessorAccessedRelationship::AccessorIsOpener;
  69. // 11. If accessor's top-level browsing context's opener browsing context is accessed or is an ancestor of accessed, then set accessorAccessedRelationship to accessor is openee.
  70. if (accessed->is_ancestor_of(*accessor.top_level_browsing_context()->opener_browsing_context()))
  71. accessor_accessed_relationship = AccessorAccessedRelationship::AccessorIsOpener;
  72. // 12. Queue violation reports for accesses, given accessorAccessedRelationship, accessorTopDocument's opener policy, accessedTopDocument's opener policy, accessor's active document's URL, accessed's active document's URL, accessor's top-level browsing context's initial URL, accessed's top-level browsing context's initial URL, accessor's active document's origin, accessed's active document's origin, accessor's top-level browsing context's opener origin at creation, accessed's top-level browsing context's opener origin at creation, accessorTopDocument's referrer, accessedTopDocument's referrer, propertyKey, and environment.
  73. (void)environment;
  74. (void)accessor_accessed_relationship;
  75. }
  76. }