Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / cross_origin / abstract_operations.lucb

#==============================================================================================
#
#   abstract_operations - HTML's cross-origin objects: the abstract operations of Location and
#   Window objects accessed across origins, and access reporting
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp (lines 1-263) and
#       Reporting.cpp (lines 1-93). The operations on JS property keys, property descriptors
#       and functions ([[GetOwnProperty]], Call, NativeFunction::create, well-known symbols)
#       need JavaScript objects, which exist with luce-js (phase 3); until then those bodies
#       trap and say so. CrossOriginProperties is plain data and is ported.
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: AbstractOperations.cpp ==================================================================

## One { [[Property]], [[NeedsGet]], [[NeedsSet]] } record of CrossOriginProperties.
func html_cross_origin_property(property: str, needs_get: bool?, needs_set: bool?) -> HtmlCrossOriginProperty:
    return HtmlCrossOriginProperty(property = ak.string_from_str(property), needs_get = needs_get, needs_set = needs_set)

## Ported from AbstractOperations.cpp:26 Web::HTML::cross_origin_properties.
# 7.2.3.1 CrossOriginProperties ( O ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginproperties-(-o-)
pub func html_cross_origin_properties(object: HtmlLocationOrHtmlWindow) -> ak.Vector[HtmlCrossOriginProperty]:
    # 1. Assert: O is a Location or Window object.

    var properties = ak.Vector[HtmlCrossOriginProperty]()
    match object:
        # 2. If O is a Location object, then return « { [[Property]]: "href", [[NeedsGet]]: false, [[NeedsSet]]: true }, { [[Property]]: "replace" } ».
        .html_html_location(_):
            ak.vector_append_t(&properties, html_cross_origin_property("href", false, true))
            ak.vector_append_t(&properties, html_cross_origin_property("replace", none, none))
        # 3. Return « { [[Property]]: "window", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "self", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "location", [[NeedsGet]]: true, [[NeedsSet]]: true }, { [[Property]]: "close" }, { [[Property]]: "closed", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "focus" }, { [[Property]]: "blur" }, { [[Property]]: "frames", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "length", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "top", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "opener", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "parent", [[NeedsGet]]: true, [[NeedsSet]]: false }, { [[Property]]: "postMessage" } ».
        .html_window(_):
            ak.vector_append_t(&properties, html_cross_origin_property("window", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("self", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("location", true, true))
            ak.vector_append_t(&properties, html_cross_origin_property("close", none, none))
            ak.vector_append_t(&properties, html_cross_origin_property("closed", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("focus", none, none))
            ak.vector_append_t(&properties, html_cross_origin_property("blur", none, none))
            ak.vector_append_t(&properties, html_cross_origin_property("frames", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("length", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("top", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("opener", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("parent", true, false))
            ak.vector_append_t(&properties, html_cross_origin_property("postMessage", none, none))
    return properties

## Ported from AbstractOperations.cpp:59 Web::HTML::is_cross_origin_accessible_window_property_name.
# https://html.spec.whatwg.org/multipage/browsers.html#cross-origin-accessible-window-property-name
pub func html_is_cross_origin_accessible_window_property_name(property_key: JsPropertyKey) -> bool:
    # A JavaScript property name P is a cross-origin accessible window property name if it is "window", "self", "location", "close", "closed", "focus", "blur", "frames", "length", "top", "opener", "parent", "postMessage", or an array index property name.
    # (JS::PropertyKey's string and number forms exist with luce-js.)
    trap("unported (P3): Web::HTML::is_cross_origin_accessible_window_property_name (a JS::PropertyKey)")

## Ported from AbstractOperations.cpp:69 Web::HTML::cross_origin_property_fallback.
# 7.2.3.2 CrossOriginPropertyFallback ( P ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertyfallback-(-p-)
pub func html_cross_origin_property_fallback(vm: Vm*, property_key: JsPropertyKey) -> JsPropertyDescriptor!:
    # 1. If P is "then", @@toStringTag, @@hasInstance, or @@isConcatSpreadable, then return PropertyDescriptor { [[Value]]: undefined, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.
    # 2. Throw a "SecurityError" DOMException.
    # (Property keys, well-known symbols and property descriptors exist with luce-js.)
    trap("unported (P3): Web::HTML::cross_origin_property_fallback (JS property keys and descriptors)")

## Ported from AbstractOperations.cpp:85 Web::HTML::is_platform_object_same_origin.
# 7.2.3.3 IsPlatformObjectSameOrigin ( O ), https://html.spec.whatwg.org/multipage/nav-history-apis.html#isplatformobjectsameorigin-(-o-)
pub func html_is_platform_object_same_origin(object: JsObject*) -> bool:
    # 1. Return true if the current settings object's origin is same origin-domain with O's relevant settings object's origin, and false otherwise.
    let current_origin = html_environment_settings_object_origin(html_current_settings_object())
    return web_url.origin_is_same_origin_domain(&current_origin, html_environment_settings_object_origin(html_relevant_settings_object_js_object(object)))

## Ported from AbstractOperations.cpp:92 Web::HTML::cross_origin_get_own_property_helper.
# 7.2.3.4 CrossOriginGetOwnPropertyHelper ( O, P ), https://html.spec.whatwg.org/multipage/nav-history-apis.html#crossorigingetownpropertyhelper-(-o,-p-)
pub func html_cross_origin_get_own_property_helper(object: HtmlLocationOrHtmlWindow, property_key: JsPropertyKey) -> JsPropertyDescriptor?:
    # 1. Let crossOriginKey be a tuple consisting of the current settings object, O's relevant settings object, and P.
    # 2. For each e of CrossOriginProperties(O): ... OrdinaryGetOwnProperty(O, P), anonymous built-in functions
    #    created in the current Realm Record ...
    # 3. Return undefined.
    # (Every step works on JS property keys, descriptors and functions, which exist with luce-js.)
    trap("unported (P3): Web::HTML::cross_origin_get_own_property_helper (JS property keys, descriptors and functions)")

## Ported from AbstractOperations.cpp:194 Web::HTML::cross_origin_get.
# 7.2.3.5 CrossOriginGet ( O, P, Receiver ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginget-(-o,-p,-receiver-)
pub func html_cross_origin_get(vm: Vm*, object: JsObject*, property_key: JsPropertyKey, receiver: JsValue) -> JsValue!:
    # 1. Let desc be ? O.[[GetOwnProperty]](P). ... 7. Return ? Call(getter, Receiver).
    # (O.[[GetOwnProperty]] and Call exist with luce-js.)
    trap("unported (P3): Web::HTML::cross_origin_get ([[GetOwnProperty]] and Call)")

## Ported from AbstractOperations.cpp:221 Web::HTML::cross_origin_set.
# 7.2.3.6 CrossOriginSet ( O, P, V, Receiver ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginset-(-o,-p,-v,-receiver-)
pub func html_cross_origin_set(vm: Vm*, object: JsObject*, property_key: JsPropertyKey, value: JsValue, receiver: JsValue) -> bool!:
    # 1. Let desc be ? O.[[GetOwnProperty]](P). ... 4. Throw a "SecurityError" DOMException.
    # (O.[[GetOwnProperty]] and Call exist with luce-js.)
    trap("unported (P3): Web::HTML::cross_origin_set ([[GetOwnProperty]] and Call)")

## Ported from AbstractOperations.cpp:243 Web::HTML::cross_origin_own_property_keys.
# 7.2.3.7 CrossOriginOwnPropertyKeys ( O ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginownpropertykeys-(-o-)
pub func html_cross_origin_own_property_keys(object: HtmlLocationOrHtmlWindow) -> ak.Vector[JsValue]:
    # 1. Let keys be a new empty List.
    # 2. For each e of CrossOriginProperties(O), append e.[[Property]] to keys.
    # 3. Return the concatenation of keys and « "then", @@toStringTag, @@hasInstance, @@isConcatSpreadable ».
    # (JS strings and well-known symbols as values exist with luce-js.)
    trap("unported (P3): Web::HTML::cross_origin_own_property_keys (JS strings and symbols)")

# mark: Reporting.cpp ===========================================================================

## The origins of the active documents of a browsing context's active document's inclusive
## ancestor navigables (steps 4 and 6 of check_if_access_between_two_browsing_contexts_should_be_reported).
func html_inclusive_ancestor_origins(browsing_context: HtmlBrowsingContext*) -> ak.Vector[web_url.Origin]:
    var origins = ak.Vector[web_url.Origin]()
    let active_document = html_browsing_context_active_document(browsing_context) else trap("VERIFY: an active document")
    var inclusive_ancestors = dom_document_ancestor_navigables(active_document)
    ak.vector_ensure_capacity(&origins, ak.vector_size(&inclusive_ancestors))
    for maybe_ancestor in ak.vector_span(&inclusive_ancestors):
        let ancestor = maybe_ancestor else trap("VERIFY: ancestor != nullptr")
        let ancestor_document = html_navigable_active_document(ancestor) else trap("VERIFY: ancestor->active_document() != nullptr")
        ak.vector_append_t(&origins, dom_document_origin(ancestor_document))
    return origins

## Ported from Reporting.cpp:20 Web::HTML::check_if_access_between_two_browsing_contexts_should_be_reported.
# https://html.spec.whatwg.org/multipage/origin.html#coop-check-access-report
pub func html_check_if_access_between_two_browsing_contexts_should_be_reported(accessor: HtmlBrowsingContext*?, accessed: HtmlBrowsingContext*?, property_key: JsPropertyKey, environment: HtmlEnvironmentSettingsObject*):
    # FIXME: Spec bug: https://github.com/whatwg/html/issues/10192
    let the_accessor = accessor else return
    let the_accessed = accessed else return

    # 1. If propertyKey is not a cross-origin accessible window property name, then return.
    if not html_is_cross_origin_accessible_window_property_name(property_key):
        return

    # 2. Assert: accessor's active document and accessed's active document are both fully active.
    assert(dom_document_is_fully_active(html_browsing_context_active_document(the_accessor) else trap("VERIFY: an active document")))
    assert(dom_document_is_fully_active(html_browsing_context_active_document(the_accessed) else trap("VERIFY: an active document")))

    # 3. Let accessorTopDocument be accessor's top-level browsing context's active document.
    let accessor_top_level = html_browsing_context_top_level_browsing_context(the_accessor) else trap("VERIFY: a top-level browsing context")
    let accessor_top_document = html_browsing_context_active_document(accessor_top_level) else trap("VERIFY: an active document")

    # 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.
    var accessor_inclusive_ancestor_origins = html_inclusive_ancestor_origins(the_accessor)

    # 5. Let accessedTopDocument be accessed's top-level browsing context's active document.
    let accessed_top_level = html_browsing_context_top_level_browsing_context(the_accessed) else trap("VERIFY: accessed->top_level_browsing_context() != nullptr")
    let accessed_top_document = html_browsing_context_active_document(accessed_top_level) else trap("VERIFY: an 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.
    var accessed_inclusive_ancestor_origins = html_inclusive_ancestor_origins(the_accessed)

    # 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 origin in ak.vector_span(&accessor_inclusive_ancestor_origins):
        if not web_url.origin_is_same_origin(&origin, dom_document_origin(accessor_top_document)):
            return
    for origin in ak.vector_span(&accessed_inclusive_ancestor_origins):
        if not web_url.origin_is_same_origin(&origin, dom_document_origin(accessed_top_document)):
            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.
    if html_browsing_context_virtual_browsing_context_group_id(accessor_top_level) == html_browsing_context_virtual_browsing_context_group_id(accessed_top_level):
        return

    # 9. Let accessorAccessedRelationship be a new accessor-accessed relationship with value none.
    var accessor_accessed_relationship = HtmlAccessorAccessedRelationship.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.
    if html_browsing_context_is_ancestor_of(the_accessor, html_browsing_context_opener_browsing_context(accessed_top_level) else trap("dereferencing a null opener browsing context")):
        accessor_accessed_relationship = HtmlAccessorAccessedRelationship.accessor_is_opener

    # 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.
    if html_browsing_context_is_ancestor_of(the_accessed, html_browsing_context_opener_browsing_context(accessor_top_level) else trap("dereferencing a null opener browsing context")):
        accessor_accessed_relationship = HtmlAccessorAccessedRelationship.accessor_is_opener

    # 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.
    _ = environment
    _ = accessor_accessed_relationship