WindowProxy.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/HTML/CrossOrigin/AbstractOperations.h>
  13. #include <LibWeb/HTML/CrossOrigin/Reporting.h>
  14. #include <LibWeb/HTML/Scripting/Environments.h>
  15. #include <LibWeb/HTML/Window.h>
  16. #include <LibWeb/HTML/WindowProxy.h>
  17. #include <LibWeb/WebIDL/DOMException.h>
  18. namespace Web::HTML {
  19. // 7.4 The WindowProxy exotic object, https://html.spec.whatwg.org/multipage/window-object.html#the-windowproxy-exotic-object
  20. WindowProxy::WindowProxy(JS::Realm& realm, Window& window)
  21. : JS::Object(realm, nullptr)
  22. , m_window(window)
  23. {
  24. }
  25. // 7.4.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-getprototypeof
  26. JS::ThrowCompletionOr<JS::Object*> WindowProxy::internal_get_prototype_of() const
  27. {
  28. // 1. Let W be the value of the [[Window]] internal slot of this.
  29. // 2. If IsPlatformObjectSameOrigin(W) is true, then return ! OrdinaryGetPrototypeOf(W).
  30. if (is_platform_object_same_origin(*m_window))
  31. return MUST(m_window->internal_get_prototype_of());
  32. // 3. Return null.
  33. return nullptr;
  34. }
  35. // 7.4.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-setprototypeof
  36. JS::ThrowCompletionOr<bool> WindowProxy::internal_set_prototype_of(Object* prototype)
  37. {
  38. // 1. Return ! SetImmutablePrototype(this, V).
  39. return MUST(set_immutable_prototype(prototype));
  40. }
  41. // 7.4.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-isextensible
  42. JS::ThrowCompletionOr<bool> WindowProxy::internal_is_extensible() const
  43. {
  44. // 1. Return true.
  45. return true;
  46. }
  47. // 7.4.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-preventextensions
  48. JS::ThrowCompletionOr<bool> WindowProxy::internal_prevent_extensions()
  49. {
  50. // 1. Return false.
  51. return false;
  52. }
  53. // 7.4.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-getownproperty
  54. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> WindowProxy::internal_get_own_property(JS::PropertyKey const& property_key) const
  55. {
  56. auto& vm = this->vm();
  57. // 1. Let W be the value of the [[Window]] internal slot of this.
  58. // 2. If P is an array index property name, then:
  59. if (property_key.is_number()) {
  60. // 1. Let index be ! ToUint32(P).
  61. auto index = property_key.as_number();
  62. // 2. Let maxProperties be the number of document-tree child browsing contexts of W.
  63. auto max_properties = TRY(m_window->document_tree_child_browsing_context_count());
  64. // 3. Let value be undefined.
  65. Optional<JS::Value> value;
  66. // 4. If maxProperties is greater than 0 and index is less than maxProperties, then set value to the WindowProxy object of the indexth document-tree child browsing context of W's browsing context, sorted in the order that their browsing context container elements were most recently inserted into W's associated Document, the WindowProxy object of the most recently inserted browsing context container's nested browsing context being last.
  67. if (max_properties > 0 && index < max_properties) {
  68. // FIXME: Implement this.
  69. }
  70. // 5. If value is undefined, then:
  71. if (!value.has_value()) {
  72. // 1. If IsPlatformObjectSameOrigin(W) is true, then return undefined.
  73. if (is_platform_object_same_origin(*m_window))
  74. return Optional<JS::PropertyDescriptor> {};
  75. // 2. Throw a "SecurityError" DOMException.
  76. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), String::formatted("Can't access property '{}' on cross-origin object", property_key)));
  77. }
  78. // 6. Return PropertyDescriptor{ [[Value]]: value, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: true }.
  79. return JS::PropertyDescriptor { .value = move(value), .writable = false, .enumerable = true, .configurable = true };
  80. }
  81. // 3. If IsPlatformObjectSameOrigin(W) is true, then return ! OrdinaryGetOwnProperty(W, P).
  82. // 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.
  83. if (is_platform_object_same_origin(*m_window))
  84. return m_window->internal_get_own_property(property_key);
  85. // 4. Let property be CrossOriginGetOwnPropertyHelper(W, P).
  86. auto property = cross_origin_get_own_property_helper(const_cast<Window*>(m_window.ptr()), property_key);
  87. // 5. If property is not undefined, then return property.
  88. if (property.has_value())
  89. return property;
  90. // FIXME: 6. If property is undefined and P is in W's document-tree child browsing context name property set, then:
  91. if (false) {
  92. // FIXME: 1. Let value be the WindowProxy object of the named object of W with the name P.
  93. auto value = JS::js_undefined();
  94. // 2. Return PropertyDescriptor{ [[Value]]: value, [[Enumerable]]: false, [[Writable]]: false, [[Configurable]]: true }.
  95. // 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.
  96. return JS::PropertyDescriptor { .value = value, .writable = false, .enumerable = false, .configurable = true };
  97. }
  98. // 7. Return ? CrossOriginPropertyFallback(P).
  99. return TRY(cross_origin_property_fallback(vm, property_key));
  100. }
  101. // 7.4.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-defineownproperty
  102. JS::ThrowCompletionOr<bool> WindowProxy::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
  103. {
  104. // 1. Let W be the value of the [[Window]] internal slot of this.
  105. // 2. If IsPlatformObjectSameOrigin(W) is true, then:
  106. if (is_platform_object_same_origin(*m_window)) {
  107. // 1. If P is an array index property name, return false.
  108. if (property_key.is_number())
  109. return false;
  110. // 2. Return ? OrdinaryDefineOwnProperty(W, P, Desc).
  111. // 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.
  112. return m_window->internal_define_own_property(property_key, descriptor);
  113. }
  114. // 3. Throw a "SecurityError" DOMException.
  115. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), String::formatted("Can't define property '{}' on cross-origin object", property_key)));
  116. }
  117. // 7.4.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-get
  118. JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
  119. {
  120. auto& vm = this->vm();
  121. // 1. Let W be the value of the [[Window]] internal slot of this.
  122. // 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.
  123. 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());
  124. // 3. If IsPlatformObjectSameOrigin(W) is true, then return ? OrdinaryGet(this, P, Receiver).
  125. // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
  126. if (is_platform_object_same_origin(*m_window))
  127. return JS::Object::internal_get(property_key, receiver);
  128. // 4. Return ? CrossOriginGet(this, P, Receiver).
  129. // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
  130. return cross_origin_get(vm, *this, property_key, receiver);
  131. }
  132. // 7.4.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-set
  133. JS::ThrowCompletionOr<bool> WindowProxy::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver)
  134. {
  135. auto& vm = this->vm();
  136. // 1. Let W be the value of the [[Window]] internal slot of this.
  137. // 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.
  138. 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());
  139. // 3. If IsPlatformObjectSameOrigin(W) is true, then:
  140. if (is_platform_object_same_origin(*m_window)) {
  141. // 1. If P is an array index property name, then return false.
  142. if (property_key.is_number())
  143. return false;
  144. // 2. Return ? OrdinarySet(W, P, V, Receiver).
  145. return m_window->internal_set(property_key, value, receiver);
  146. }
  147. // 4. Return ? CrossOriginSet(this, P, V, Receiver).
  148. // NOTE: this is passed rather than W as CrossOriginSet will invoke the [[GetOwnProperty]] internal method.
  149. return cross_origin_set(vm, *this, property_key, value, receiver);
  150. }
  151. // 7.4.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-delete
  152. JS::ThrowCompletionOr<bool> WindowProxy::internal_delete(JS::PropertyKey const& property_key)
  153. {
  154. // 1. Let W be the value of the [[Window]] internal slot of this.
  155. // 2. If IsPlatformObjectSameOrigin(W) is true, then:
  156. if (is_platform_object_same_origin(*m_window)) {
  157. // 1. If P is an array index property name, then:
  158. if (property_key.is_number()) {
  159. // 2. Let desc be ! this.[[GetOwnProperty]](P).
  160. auto descriptor = MUST(internal_get_own_property(property_key));
  161. // 2. If desc is undefined, then return true.
  162. if (!descriptor.has_value())
  163. return true;
  164. // 3. Return false.
  165. return false;
  166. }
  167. // 2. Return ? OrdinaryDelete(W, P).
  168. return m_window->internal_delete(property_key);
  169. }
  170. // 3. Throw a "SecurityError" DOMException.
  171. return throw_completion(WebIDL::SecurityError::create(m_window->realm(), String::formatted("Can't delete property '{}' on cross-origin object", property_key)));
  172. }
  173. // 7.4.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-ownpropertykeys
  174. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> WindowProxy::internal_own_property_keys() const
  175. {
  176. auto& event_loop = main_thread_event_loop();
  177. auto& vm = event_loop.vm();
  178. // 1. Let W be the value of the [[Window]] internal slot of this.
  179. // 2. Let keys be a new empty List.
  180. auto keys = JS::MarkedVector<JS::Value> { vm.heap() };
  181. // 3. Let maxProperties be the number of document-tree child browsing contexts of W.
  182. auto max_properties = TRY(m_window->document_tree_child_browsing_context_count());
  183. // 4. Let index be 0.
  184. // 5. Repeat while index < maxProperties,
  185. for (size_t i = 0; i < max_properties; ++i) {
  186. // 1. Add ! ToString(index) as the last element of keys.
  187. keys.append(JS::js_string(vm, String::number(i)));
  188. // 2. Increment index by 1.
  189. }
  190. // 6. If IsPlatformObjectSameOrigin(W) is true, then return the concatenation of keys and OrdinaryOwnPropertyKeys(W).
  191. if (is_platform_object_same_origin(*m_window)) {
  192. keys.extend(MUST(m_window->internal_own_property_keys()));
  193. return keys;
  194. }
  195. // 7. Return the concatenation of keys and ! CrossOriginOwnPropertyKeys(W).
  196. keys.extend(cross_origin_own_property_keys(m_window.ptr()));
  197. return keys;
  198. }
  199. void WindowProxy::visit_edges(JS::Cell::Visitor& visitor)
  200. {
  201. Base::visit_edges(visitor);
  202. visitor.visit(m_window.ptr());
  203. }
  204. }