LibWeb: Implement the CrossOriginPropertyFallback AO

This commit is contained in:
Linus Groh 2022-03-05 20:43:45 +01:00
parent 78938d933a
commit 34fed7bfcc
Notes: sideshowbarker 2024-07-17 17:51:51 +09:00
2 changed files with 26 additions and 0 deletions

View file

@ -6,10 +6,17 @@
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/HTML/Scripting/Environments.h>
namespace Web::Bindings {
@ -46,4 +53,22 @@ Vector<CrossOriginProperty> cross_origin_properties(Variant<LocationObject const
});
}
// 7.2.3.2 CrossOriginPropertyFallback ( P ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertyfallback-(-p-)
JS::ThrowCompletionOr<JS::PropertyDescriptor> cross_origin_property_fallback(JS::GlobalObject& global_object, JS::PropertyKey const& property_key)
{
auto& vm = global_object.vm();
// 1. If P is "then", @@toStringTag, @@hasInstance, or @@isConcatSpreadable, then return PropertyDescriptor{ [[Value]]: undefined, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.
auto property_key_is_then = property_key.is_string() && property_key.as_string() == vm.names.then.as_string();
auto property_key_is_allowed_symbol = property_key.is_symbol()
&& (property_key.as_symbol() == vm.well_known_symbol_to_string_tag()
|| property_key.as_symbol() == vm.well_known_symbol_has_instance()
|| property_key.as_symbol() == vm.well_known_symbol_is_concat_spreadable());
if (property_key_is_then || property_key_is_allowed_symbol)
return JS::PropertyDescriptor { .value = JS::js_undefined(), .writable = false, .enumerable = false, .configurable = true };
// 2. Throw a "SecurityError" DOMException.
return vm.throw_completion<DOMExceptionWrapper>(global_object, DOM::SecurityError::create(String::formatted("Can't access property '{}' on cross-origin object", property_key)));
}
}

View file

@ -28,6 +28,7 @@ struct CrossOriginKey {
using CrossOriginPropertyDescriptorMap = HashMap<CrossOriginKey, JS::PropertyDescriptor>;
Vector<CrossOriginProperty> cross_origin_properties(Variant<LocationObject const*, WindowObject const*> const&);
JS::ThrowCompletionOr<JS::PropertyDescriptor> cross_origin_property_fallback(JS::GlobalObject&, JS::PropertyKey const&);
}