2022-03-08 19:32:35 +01:00
/*
* Copyright ( c ) 2022 , Linus Groh < linusg @ serenityos . org >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2024-08-10 15:26:40 +03:00
# include <AK/Assertions.h>
# include <AK/Vector.h>
2022-03-08 19:32:35 +01:00
# include <LibJS/Runtime/PropertyKey.h>
2024-10-05 15:33:34 +13:00
# include <LibURL/Origin.h>
2024-08-10 15:26:40 +03:00
# include <LibWeb/DOM/Document.h>
2022-03-08 19:32:35 +01:00
# include <LibWeb/HTML/BrowsingContext.h>
2022-09-24 14:02:47 +01:00
# include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
2022-03-08 19:32:35 +01:00
# include <LibWeb/HTML/CrossOrigin/Reporting.h>
namespace Web : : HTML {
// https://html.spec.whatwg.org/multipage/origin.html#coop-check-access-report
2024-03-11 09:35:35 +01:00
void check_if_access_between_two_browsing_contexts_should_be_reported (
2025-05-30 00:34:50 +02:00
GC : : Ptr < BrowsingContext const > accessor ,
GC : : Ptr < BrowsingContext const > accessed ,
2024-03-11 09:35:35 +01:00
JS : : PropertyKey const & property_key ,
2025-05-30 00:34:50 +02:00
GC : : Ref < EnvironmentSettingsObject const > environment )
2022-03-08 19:32:35 +01:00
{
2024-03-11 09:35:35 +01:00
// FIXME: Spec bug: https://github.com/whatwg/html/issues/10192
2025-05-30 00:34:50 +02:00
if ( ! accessor | | ! accessed )
2024-03-11 09:35:35 +01:00
return ;
2024-08-10 15:26:40 +03:00
// 1. If propertyKey is not a cross-origin accessible window property name, then return.
2022-09-24 14:02:47 +01:00
if ( ! is_cross_origin_accessible_window_property_name ( property_key ) )
2022-03-08 19:32:35 +01:00
return ;
2024-08-10 15:26:40 +03:00
// 2. Assert: accessor's active document and accessed's active document are both fully active.
2025-05-30 00:34:50 +02:00
VERIFY ( accessor - > active_document ( ) - > is_fully_active ( ) ) ;
2024-08-10 15:26:40 +03:00
VERIFY ( accessed - > active_document ( ) - > is_fully_active ( ) ) ;
2022-03-08 19:32:35 +01:00
2024-08-10 15:26:40 +03:00
// 3. Let accessorTopDocument be accessor's top-level browsing context's active document.
2025-05-30 00:34:50 +02:00
auto * accessor_top_document = accessor - > top_level_browsing_context ( ) - > active_document ( ) ;
2022-03-08 19:32:35 +01:00
2024-08-10 15:26:40 +03:00
// 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.
2024-10-05 15:33:34 +13:00
Vector < URL : : Origin > accessor_inclusive_ancestor_origins = { } ;
2025-05-30 00:34:50 +02:00
auto accessor_inclusive_ancestors = accessor - > active_document ( ) - > ancestor_navigables ( ) ;
2024-08-10 15:26:40 +03:00
accessor_inclusive_ancestor_origins . ensure_capacity ( accessor_inclusive_ancestors . size ( ) ) ;
for ( auto const & ancestor : accessor_inclusive_ancestors ) {
VERIFY ( ancestor ! = nullptr ) ;
VERIFY ( ancestor - > active_document ( ) ! = nullptr ) ;
accessor_inclusive_ancestor_origins . append ( ancestor - > active_document ( ) - > origin ( ) ) ;
2024-03-30 08:13:03 +01:00
}
2022-03-08 19:32:35 +01:00
2024-08-10 15:26:40 +03:00
// 5. Let accessedTopDocument be accessed's top-level browsing context's active document.
VERIFY ( accessed - > top_level_browsing_context ( ) ! = nullptr ) ;
auto * accessed_top_document = accessed - > top_level_browsing_context ( ) - > active_document ( ) ;
// 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.
2024-10-05 15:33:34 +13:00
Vector < URL : : Origin > accessed_inclusive_ancestor_origins = { } ;
2024-08-10 15:26:40 +03:00
auto accessed_inclusive_ancestors = accessed - > active_document ( ) - > ancestor_navigables ( ) ;
accessed_inclusive_ancestor_origins . ensure_capacity ( accessed_inclusive_ancestors . size ( ) ) ;
for ( auto const & ancestor : accessed_inclusive_ancestors ) {
VERIFY ( ancestor ! = nullptr ) ;
VERIFY ( ancestor - > active_document ( ) ! = nullptr ) ;
accessed_inclusive_ancestor_origins . append ( ancestor - > active_document ( ) - > origin ( ) ) ;
2024-03-30 08:13:03 +01:00
}
2022-03-08 19:32:35 +01:00
2024-08-10 15:26:40 +03:00
// 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.
for ( auto const & origin : accessor_inclusive_ancestor_origins )
if ( ! origin . is_same_origin ( accessor_top_document - > origin ( ) ) )
return ;
for ( auto const & origin : accessed_inclusive_ancestor_origins )
if ( ! origin . is_same_origin ( accessed_top_document - > origin ( ) ) )
return ;
// 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.
2025-05-30 00:34:50 +02:00
if ( accessor - > top_level_browsing_context ( ) - > virtual_browsing_context_group_id ( ) = = accessed - > top_level_browsing_context ( ) - > virtual_browsing_context_group_id ( ) )
2024-08-10 15:26:40 +03:00
return ;
// 9. Let accessorAccessedRelationship be a new accessor-accessed relationship with value none.
auto accessor_accessed_relationship = AccessorAccessedRelationship : : None ;
// 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.
2025-05-30 00:34:50 +02:00
if ( accessor - > is_ancestor_of ( * accessed - > top_level_browsing_context ( ) - > opener_browsing_context ( ) ) )
2024-08-10 15:26:40 +03:00
accessor_accessed_relationship = AccessorAccessedRelationship : : AccessorIsOpener ;
// 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.
2025-05-30 00:34:50 +02:00
if ( accessed - > is_ancestor_of ( * accessor - > top_level_browsing_context ( ) - > opener_browsing_context ( ) ) )
2024-08-10 15:26:40 +03:00
accessor_accessed_relationship = AccessorAccessedRelationship : : AccessorIsOpener ;
2024-09-18 19:48:22 +02:00
// 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.
2022-03-08 19:32:35 +01:00
( void ) environment ;
( void ) accessor_accessed_relationship ;
}
}