WindowProxy.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Optional.h>
  7. #include <LibJS/Heap/MarkedVector.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/PropertyDescriptor.h>
  11. #include <LibJS/Runtime/PropertyKey.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
  14. #include <LibWeb/HTML/CrossOrigin/Reporting.h>
  15. #include <LibWeb/HTML/Scripting/Environments.h>
  16. #include <LibWeb/HTML/Window.h>
  17. #include <LibWeb/HTML/WindowProxy.h>
  18. #include <LibWeb/WebIDL/DOMException.h>
  19. namespace Web::HTML {
  20. JS_DEFINE_ALLOCATOR(WindowProxy);
  21. // 7.4 The WindowProxy exotic object, https://html.spec.whatwg.org/multipage/window-object.html#the-windowproxy-exotic-object
  22. WindowProxy::WindowProxy(JS::Realm& realm)
  23. : JS::Object(realm, nullptr, MayInterfereWithIndexedPropertyAccess::Yes)
  24. {
  25. }
  26. // 7.4.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-getprototypeof
  27. JS::ThrowCompletionOr<JS::Object*> WindowProxy::internal_get_prototype_of() const
  28. {
  29. // 1. Let W be the value of the [[Window]] internal slot of this.
  30. // 2. If IsPlatformObjectSameOrigin(W) is true, then return ! OrdinaryGetPrototypeOf(W).
  31. if (is_platform_object_same_origin(*m_window))
  32. return MUST(m_window->internal_get_prototype_of());
  33. // 3. Return null.
  34. return nullptr;
  35. }
  36. // 7.4.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-setprototypeof
  37. JS::ThrowCompletionOr<bool> WindowProxy::internal_set_prototype_of(Object* prototype)
  38. {
  39. // 1. Return ! SetImmutablePrototype(this, V).
  40. return MUST(set_immutable_prototype(prototype));
  41. }
  42. // 7.4.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-isextensible
  43. JS::ThrowCompletionOr<bool> WindowProxy::internal_is_extensible() const
  44. {
  45. // 1. Return true.
  46. return true;
  47. }
  48. // 7.4.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-preventextensions
  49. JS::ThrowCompletionOr<bool> WindowProxy::internal_prevent_extensions()
  50. {
  51. // 1. Return false.
  52. return false;
  53. }
  54. // 7.4.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-getownproperty
  55. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> WindowProxy::internal_get_own_property(JS::PropertyKey const& property_key) const
  56. {
  57. auto& vm = this->vm();
  58. // 1. Let W be the value of the [[Window]] internal slot of this.
  59. // 2. If P is an array index property name, then:
  60. if (property_key.is_number()) {
  61. // 1. Let index be ! ToUint32(P).
  62. auto index = property_key.as_number();
  63. // 2. Let children be the document-tree child navigables of W's associated Document.
  64. auto children = m_window->associated_document().document_tree_child_navigables();
  65. // 3. Let value be undefined.
  66. Optional<JS::Value> value;
  67. // 4. If index is less than children's size, then:
  68. if (index < children.size()) {
  69. // 1. Sort children in ascending order, with navigableA being less than navigableB if navigableA's container was inserted into W's associated Document earlier than navigableB's container was.
  70. // NOTE: children are coming sorted in required order from document_tree_child_navigables()
  71. // 2. Set value to children[index]'s active WindowProxy.
  72. value = children[index]->active_window_proxy();
  73. }
  74. // 5. If value is undefined, then:
  75. if (!value.has_value()) {
  76. // 1. If IsPlatformObjectSameOrigin(W) is true, then return undefined.
  77. if (is_platform_object_same_origin(*m_window))
  78. return Optional<JS::PropertyDescriptor> {};
  79. // 2. Throw a "SecurityError" DOMException.
  80. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), MUST(String::formatted("Can't access property '{}' on cross-origin object", property_key))));
  81. }
  82. // 6. Return PropertyDescriptor{ [[Value]]: value, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: true }.
  83. return JS::PropertyDescriptor { .value = move(value), .writable = false, .enumerable = true, .configurable = true };
  84. }
  85. // 3. If IsPlatformObjectSameOrigin(W) is true, then return ! OrdinaryGetOwnProperty(W, P).
  86. // NOTE: This is a willful violation of the JavaScript specification's invariants of the essential internal methods to maintain compatibility with existing web content. See tc39/ecma262 issue #672 for more information.
  87. if (is_platform_object_same_origin(*m_window))
  88. return m_window->internal_get_own_property(property_key);
  89. // 4. Let property be CrossOriginGetOwnPropertyHelper(W, P).
  90. auto property = cross_origin_get_own_property_helper(const_cast<Window*>(m_window.ptr()), property_key);
  91. // 5. If property is not undefined, then return property.
  92. if (property.has_value())
  93. return property;
  94. // 6. If property is undefined and P is in W's document-tree child navigable target name property set, then:
  95. auto navigable_property_set = m_window->document_tree_child_navigable_target_name_property_set();
  96. if (auto navigable = navigable_property_set.get(property_key.to_string().view()); navigable.has_value()) {
  97. // 1. Let value be the active WindowProxy of the named object of W with the name P.
  98. auto value = navigable.value()->active_window_proxy();
  99. // 2. Return PropertyDescriptor{ [[Value]]: value, [[Enumerable]]: false, [[Writable]]: false, [[Configurable]]: true }.
  100. // NOTE: The reason the property descriptors are non-enumerable, despite this mismatching the same-origin behavior, is for compatibility with existing web content. See issue #3183 for details.
  101. return JS::PropertyDescriptor { .value = value, .writable = false, .enumerable = false, .configurable = true };
  102. }
  103. // 7. Return ? CrossOriginPropertyFallback(P).
  104. return TRY(cross_origin_property_fallback(vm, property_key));
  105. }
  106. // 7.4.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-defineownproperty
  107. JS::ThrowCompletionOr<bool> WindowProxy::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
  108. {
  109. // 1. Let W be the value of the [[Window]] internal slot of this.
  110. // 2. If IsPlatformObjectSameOrigin(W) is true, then:
  111. if (is_platform_object_same_origin(*m_window)) {
  112. // 1. If P is an array index property name, return false.
  113. if (property_key.is_number())
  114. return false;
  115. // 2. Return ? OrdinaryDefineOwnProperty(W, P, Desc).
  116. // NOTE: This is a willful violation of the JavaScript specification's invariants of the essential internal methods to maintain compatibility with existing web content. See tc39/ecma262 issue #672 for more information.
  117. return m_window->internal_define_own_property(property_key, descriptor);
  118. }
  119. // 3. Throw a "SecurityError" DOMException.
  120. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), MUST(String::formatted("Can't define property '{}' on cross-origin object", property_key))));
  121. }
  122. // 7.4.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-get
  123. JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata*) const
  124. {
  125. auto& vm = this->vm();
  126. // 1. Let W be the value of the [[Window]] internal slot of this.
  127. // 2. Check if an access between two browsing contexts should be reported, given the current global object's browsing context, W's browsing context, P, and the current settings object.
  128. check_if_access_between_two_browsing_contexts_should_be_reported(*verify_cast<Window>(current_global_object()).browsing_context(), m_window->browsing_context(), property_key, current_settings_object());
  129. // 3. If IsPlatformObjectSameOrigin(W) is true, then return ? OrdinaryGet(this, P, Receiver).
  130. // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
  131. if (is_platform_object_same_origin(*m_window))
  132. return JS::Object::internal_get(property_key, receiver);
  133. // 4. Return ? CrossOriginGet(this, P, Receiver).
  134. // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
  135. return cross_origin_get(vm, *this, property_key, receiver);
  136. }
  137. // 7.4.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-set
  138. JS::ThrowCompletionOr<bool> WindowProxy::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata*)
  139. {
  140. auto& vm = this->vm();
  141. // 1. Let W be the value of the [[Window]] internal slot of this.
  142. // 2. Check if an access between two browsing contexts should be reported, given the current global object's browsing context, W's browsing context, P, and the current settings object.
  143. check_if_access_between_two_browsing_contexts_should_be_reported(*verify_cast<Window>(current_global_object()).browsing_context(), m_window->browsing_context(), property_key, current_settings_object());
  144. // 3. If IsPlatformObjectSameOrigin(W) is true, then:
  145. if (is_platform_object_same_origin(*m_window)) {
  146. // 1. If P is an array index property name, then return false.
  147. if (property_key.is_number())
  148. return false;
  149. // 2. Return ? OrdinarySet(W, P, V, Receiver).
  150. return m_window->internal_set(property_key, value, receiver);
  151. }
  152. // 4. Return ? CrossOriginSet(this, P, V, Receiver).
  153. // NOTE: this is passed rather than W as CrossOriginSet will invoke the [[GetOwnProperty]] internal method.
  154. return cross_origin_set(vm, *this, property_key, value, receiver);
  155. }
  156. // 7.4.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-delete
  157. JS::ThrowCompletionOr<bool> WindowProxy::internal_delete(JS::PropertyKey const& property_key)
  158. {
  159. // 1. Let W be the value of the [[Window]] internal slot of this.
  160. // 2. If IsPlatformObjectSameOrigin(W) is true, then:
  161. if (is_platform_object_same_origin(*m_window)) {
  162. // 1. If P is an array index property name, then:
  163. if (property_key.is_number()) {
  164. // 2. Let desc be ! this.[[GetOwnProperty]](P).
  165. auto descriptor = MUST(internal_get_own_property(property_key));
  166. // 2. If desc is undefined, then return true.
  167. if (!descriptor.has_value())
  168. return true;
  169. // 3. Return false.
  170. return false;
  171. }
  172. // 2. Return ? OrdinaryDelete(W, P).
  173. return m_window->internal_delete(property_key);
  174. }
  175. // 3. Throw a "SecurityError" DOMException.
  176. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), MUST(String::formatted("Can't delete property '{}' on cross-origin object", property_key))));
  177. }
  178. // 7.4.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-ownpropertykeys
  179. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> WindowProxy::internal_own_property_keys() const
  180. {
  181. auto& event_loop = main_thread_event_loop();
  182. auto& vm = event_loop.vm();
  183. // 1. Let W be the value of the [[Window]] internal slot of this.
  184. // 2. Let keys be a new empty List.
  185. auto keys = JS::MarkedVector<JS::Value> { vm.heap() };
  186. // 3. Let maxProperties be W's associated Document's document-tree child navigables's size.
  187. auto max_properties = m_window->associated_document().document_tree_child_navigables().size();
  188. // 4. Let index be 0.
  189. // 5. Repeat while index < maxProperties,
  190. for (size_t i = 0; i < max_properties; ++i) {
  191. // 1. Add ! ToString(index) as the last element of keys.
  192. keys.append(JS::PrimitiveString::create(vm, ByteString::number(i)));
  193. // 2. Increment index by 1.
  194. }
  195. // 6. If IsPlatformObjectSameOrigin(W) is true, then return the concatenation of keys and OrdinaryOwnPropertyKeys(W).
  196. if (is_platform_object_same_origin(*m_window)) {
  197. keys.extend(MUST(m_window->internal_own_property_keys()));
  198. return keys;
  199. }
  200. // 7. Return the concatenation of keys and ! CrossOriginOwnPropertyKeys(W).
  201. keys.extend(cross_origin_own_property_keys(m_window.ptr()));
  202. return keys;
  203. }
  204. void WindowProxy::visit_edges(JS::Cell::Visitor& visitor)
  205. {
  206. Base::visit_edges(visitor);
  207. visitor.visit(m_window);
  208. }
  209. void WindowProxy::set_window(JS::NonnullGCPtr<Window> window)
  210. {
  211. m_window = move(window);
  212. }
  213. JS::NonnullGCPtr<BrowsingContext> WindowProxy::associated_browsing_context() const
  214. {
  215. return *m_window->associated_document().browsing_context();
  216. }
  217. }