WindowProxy.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. auto property_key_string = property_key.to_string();
  97. if (auto navigable = navigable_property_set.get(property_key_string.view()); navigable.has_value()) {
  98. // 1. Let value be the active WindowProxy of the named object of W with the name P.
  99. auto value = navigable.value()->active_window_proxy();
  100. // 2. Return PropertyDescriptor{ [[Value]]: value, [[Enumerable]]: false, [[Writable]]: false, [[Configurable]]: true }.
  101. // 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.
  102. return JS::PropertyDescriptor { .value = value, .writable = false, .enumerable = false, .configurable = true };
  103. }
  104. // 7. Return ? CrossOriginPropertyFallback(P).
  105. return TRY(cross_origin_property_fallback(vm, property_key));
  106. }
  107. // 7.4.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-defineownproperty
  108. JS::ThrowCompletionOr<bool> WindowProxy::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor, Optional<JS::PropertyDescriptor>*)
  109. {
  110. // 1. Let W be the value of the [[Window]] internal slot of this.
  111. // 2. If IsPlatformObjectSameOrigin(W) is true, then:
  112. if (is_platform_object_same_origin(*m_window)) {
  113. // 1. If P is an array index property name, return false.
  114. if (property_key.is_number())
  115. return false;
  116. // 2. Return ? OrdinaryDefineOwnProperty(W, P, Desc).
  117. // 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.
  118. return m_window->internal_define_own_property(property_key, descriptor);
  119. }
  120. // 3. Throw a "SecurityError" DOMException.
  121. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), MUST(String::formatted("Can't define property '{}' on cross-origin object", property_key))));
  122. }
  123. // 7.4.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/nav-history-apis.html#windowproxy-get
  124. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#windowproxy-get
  125. JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata*, PropertyLookupPhase) const
  126. {
  127. auto& vm = this->vm();
  128. // 1. Let W be the value of the [[Window]] internal slot of this.
  129. // 2. Check if an access between two browsing contexts should be reported, given the current principal global object's browsing context, W's browsing context, P, and the current principal settings object.
  130. check_if_access_between_two_browsing_contexts_should_be_reported(*verify_cast<Window>(current_principal_global_object()).browsing_context(), m_window->browsing_context(), property_key, current_principal_settings_object());
  131. // 3. If IsPlatformObjectSameOrigin(W) is true, then return ? OrdinaryGet(this, P, Receiver).
  132. // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
  133. if (is_platform_object_same_origin(*m_window))
  134. return JS::Object::internal_get(property_key, receiver);
  135. // 4. Return ? CrossOriginGet(this, P, Receiver).
  136. // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
  137. return cross_origin_get(vm, *this, property_key, receiver);
  138. }
  139. // 7.4.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/nav-history-apis.html#windowproxy-set
  140. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#windowproxy-set
  141. JS::ThrowCompletionOr<bool> WindowProxy::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata*)
  142. {
  143. auto& vm = this->vm();
  144. // 1. Let W be the value of the [[Window]] internal slot of this.
  145. // 2. Check if an access between two browsing contexts should be reported, given the current principal global object's browsing context, W's browsing context, P, and the current principal settings object.
  146. check_if_access_between_two_browsing_contexts_should_be_reported(*verify_cast<Window>(current_principal_global_object()).browsing_context(), m_window->browsing_context(), property_key, current_principal_settings_object());
  147. // 3. If IsPlatformObjectSameOrigin(W) is true, then:
  148. if (is_platform_object_same_origin(*m_window)) {
  149. // 1. If P is an array index property name, then return false.
  150. if (property_key.is_number())
  151. return false;
  152. // 2. Return ? OrdinarySet(W, P, V, Receiver).
  153. return m_window->internal_set(property_key, value, receiver);
  154. }
  155. // 4. Return ? CrossOriginSet(this, P, V, Receiver).
  156. // NOTE: this is passed rather than W as CrossOriginSet will invoke the [[GetOwnProperty]] internal method.
  157. return cross_origin_set(vm, *this, property_key, value, receiver);
  158. }
  159. // 7.4.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-delete
  160. JS::ThrowCompletionOr<bool> WindowProxy::internal_delete(JS::PropertyKey const& property_key)
  161. {
  162. // 1. Let W be the value of the [[Window]] internal slot of this.
  163. // 2. If IsPlatformObjectSameOrigin(W) is true, then:
  164. if (is_platform_object_same_origin(*m_window)) {
  165. // 1. If P is an array index property name, then:
  166. if (property_key.is_number()) {
  167. // 2. Let desc be ! this.[[GetOwnProperty]](P).
  168. auto descriptor = MUST(internal_get_own_property(property_key));
  169. // 2. If desc is undefined, then return true.
  170. if (!descriptor.has_value())
  171. return true;
  172. // 3. Return false.
  173. return false;
  174. }
  175. // 2. Return ? OrdinaryDelete(W, P).
  176. return m_window->internal_delete(property_key);
  177. }
  178. // 3. Throw a "SecurityError" DOMException.
  179. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), MUST(String::formatted("Can't delete property '{}' on cross-origin object", property_key))));
  180. }
  181. // 7.4.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-ownpropertykeys
  182. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> WindowProxy::internal_own_property_keys() const
  183. {
  184. auto& event_loop = main_thread_event_loop();
  185. auto& vm = event_loop.vm();
  186. // 1. Let W be the value of the [[Window]] internal slot of this.
  187. // 2. Let keys be a new empty List.
  188. auto keys = JS::MarkedVector<JS::Value> { vm.heap() };
  189. // 3. Let maxProperties be W's associated Document's document-tree child navigables's size.
  190. auto max_properties = m_window->associated_document().document_tree_child_navigables().size();
  191. // 4. Let index be 0.
  192. // 5. Repeat while index < maxProperties,
  193. for (size_t i = 0; i < max_properties; ++i) {
  194. // 1. Add ! ToString(index) as the last element of keys.
  195. keys.append(JS::PrimitiveString::create(vm, ByteString::number(i)));
  196. // 2. Increment index by 1.
  197. }
  198. // 6. If IsPlatformObjectSameOrigin(W) is true, then return the concatenation of keys and OrdinaryOwnPropertyKeys(W).
  199. if (is_platform_object_same_origin(*m_window)) {
  200. keys.extend(MUST(m_window->internal_own_property_keys()));
  201. return keys;
  202. }
  203. // 7. Return the concatenation of keys and ! CrossOriginOwnPropertyKeys(W).
  204. keys.extend(cross_origin_own_property_keys(m_window.ptr()));
  205. return keys;
  206. }
  207. void WindowProxy::visit_edges(JS::Cell::Visitor& visitor)
  208. {
  209. Base::visit_edges(visitor);
  210. visitor.visit(m_window);
  211. }
  212. void WindowProxy::set_window(JS::NonnullGCPtr<Window> window)
  213. {
  214. m_window = move(window);
  215. }
  216. JS::NonnullGCPtr<BrowsingContext> WindowProxy::associated_browsing_context() const
  217. {
  218. return *m_window->associated_document().browsing_context();
  219. }
  220. }