1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/*
* Copyright (C) 2008-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JSWindowProxy.h"
#include "CommonVM.h"
#include "Frame.h"
#include "FrameLoaderTypes.h"
#include "GCController.h"
#include "JSDOMWindow.h"
#include "JSDOMWindowProperties.h"
#include "JSEventTarget.h"
#include "Location.h"
#include "Logging.h"
#include "ScriptController.h"
#include "WebCoreJSClientData.h"
#include <JavaScriptCore/Debugger.h>
#include <JavaScriptCore/JSObject.h>
#include <JavaScriptCore/StrongInlines.h>
#include <wtf/text/MakeString.h>
#if PLATFORM(COCOA)
#include <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
#endif
namespace WebCore {
using namespace JSC;
const ClassInfo JSWindowProxy::s_info = { "JSWindowProxy"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWindowProxy) };
inline JSWindowProxy::JSWindowProxy(VM& vm, Structure& structure, DOMWrapperWorld& world)
: Base(vm, &structure, nullptr)
, m_world(world)
{
}
JSWindowProxy::~JSWindowProxy() = default;
void JSWindowProxy::finishCreation(VM& vm, DOMWindow& window)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
setWindow(window);
}
JSWindowProxy& JSWindowProxy::create(VM& vm, DOMWindow& window, DOMWrapperWorld& world)
{
auto& structure = *Structure::create(vm, 0, jsNull(), TypeInfo(GlobalProxyType, StructureFlags), info());
auto& proxy = *new (NotNull, allocateCell<JSWindowProxy>(vm)) JSWindowProxy(vm, structure, world);
proxy.finishCreation(vm, window);
return proxy;
}
void JSWindowProxy::destroy(JSCell* cell)
{
static_cast<JSWindowProxy*>(cell)->JSWindowProxy::~JSWindowProxy();
}
DOMWrapperWorld& JSWindowProxy::world()
{
return m_world;
}
void JSWindowProxy::setWindow(VM& vm, JSDOMGlobalObject& window)
{
ASSERT(window.classInfo() == JSDOMWindow::info());
setTarget(vm, &window);
GCController::singleton().garbageCollectSoon();
}
void JSWindowProxy::setWindow(DOMWindow& domWindow)
{
// Replacing JSDOMWindow via telling JSWindowProxy to use the same LocalDOMWindow it already uses makes no sense,
// so we'd better never try to.
ASSERT(!window() || &domWindow != &wrapped());
auto* localWindow = dynamicDowncast<LocalDOMWindow>(domWindow);
VM& vm = commonVM();
auto& prototypeStructure = *JSDOMWindowPrototype::createStructure(vm, nullptr, jsNull());
// Explicitly protect the prototype so it isn't collected when we allocate the global object.
// (Once the global object is fully constructed, it will mark its own prototype.)
JSNonFinalObject* prototype = static_cast<JSNonFinalObject*>(JSDOMWindowPrototype::create(vm, nullptr, &prototypeStructure));
JSC::EnsureStillAliveScope protectedPrototype(prototype);
JSDOMGlobalObject* window = nullptr;
auto& windowStructure = *JSDOMWindow::createStructure(vm, nullptr, prototype);
window = JSDOMWindow::create(vm, &windowStructure, domWindow, this);
if (localWindow) {
bool linkedWithNewSDK = true;
#if PLATFORM(COCOA)
linkedWithNewSDK = linkedOnOrAfterSDKWithBehavior(SDKAlignedBehavior::DOMWindowReuseRestriction);
#endif
if (!localWindow->document()->haveInitializedSecurityOrigin() && linkedWithNewSDK)
localWindow->setAsWrappedWithoutInitializedSecurityOrigin();
}
prototype->structure()->setGlobalObject(vm, window);
auto& propertiesStructure = *JSDOMWindowProperties::createStructure(vm, window, JSEventTarget::prototype(vm, *window));
auto& properties = *JSDOMWindowProperties::create(&propertiesStructure, *window);
properties.didBecomePrototype(vm);
prototype->structure()->setPrototypeWithoutTransition(vm, &properties);
setWindow(vm, *window);
ASSERT(window->globalObject() == window);
ASSERT(prototype->globalObject() == window);
}
WindowProxy* JSWindowProxy::windowProxy() const
{
auto& window = wrapped();
return window.frame() ? &window.frame()->windowProxy() : nullptr;
}
void JSWindowProxy::attachDebugger(JSC::Debugger* debugger)
{
auto* globalObject = window();
JSLockHolder lock(globalObject->vm());
if (debugger)
debugger->attach(globalObject);
else if (auto* currentDebugger = globalObject->debugger())
currentDebugger->detach(globalObject, JSC::Debugger::TerminatingDebuggingSession);
}
DOMWindow& JSWindowProxy::wrapped() const
{
auto* window = this->window();
return jsCast<JSDOMWindowBase*>(window)->wrapped();
}
Ref<DOMWindow> JSWindowProxy::protectedWrapped() const
{
return wrapped();
}
JSValue toJS(JSGlobalObject* lexicalGlobalObject, WindowProxy& windowProxy)
{
auto* jsWindowProxy = windowProxy.jsWindowProxy(currentWorld(*lexicalGlobalObject));
return jsWindowProxy ? JSValue(jsWindowProxy) : jsNull();
}
JSWindowProxy* toJSWindowProxy(WindowProxy& windowProxy, DOMWrapperWorld& world)
{
return windowProxy.jsWindowProxy(world);
}
WindowProxy* JSWindowProxy::toWrapped(VM&, JSValue value)
{
if (!value.isObject())
return nullptr;
JSObject* object = asObject(value);
if (object->inherits<JSWindowProxy>())
return jsCast<JSWindowProxy*>(object)->windowProxy();
return nullptr;
}
JSC::GCClient::IsoSubspace* JSWindowProxy::subspaceForImpl(JSC::VM& vm)
{
return &downcast<JSVMClientData>(vm.clientData)->windowProxySpace();
}
Ref<DOMWrapperWorld> JSWindowProxy::protectedWorld()
{
return m_world;
}
#if ENABLE(WINDOW_PROXY_PROPERTY_ACCESS_NOTIFICATION)
struct FrameInfo {
Ref<Frame> frame;
Ref<Frame> mainFrame;
};
static std::optional<FrameInfo> frameInfo(JSGlobalObject* globalObject)
{
auto* domGlobalObject = jsDynamicCast<JSDOMGlobalObject*>(globalObject);
if (!domGlobalObject)
return std::nullopt;
RefPtr document { dynamicDowncast<Document>(domGlobalObject->scriptExecutionContext()) };
if (!document)
return std::nullopt;
RefPtr frame { document->frame() };
if (!frame)
return std::nullopt;
Ref mainFrame { frame->mainFrame() };
return FrameInfo { frame.releaseNonNull(), WTFMove(mainFrame) };
}
static bool hasSameMainFrame(const Frame* a, const FrameInfo& b)
{
return a && (&a->mainFrame() == b.mainFrame.ptr());
}
static void logCrossTabPropertyAccess(Frame& childFrame, const std::variant<PropertyName, unsigned>& propertyName)
{
#if LOG_DISABLED
UNUSED_PARAM(childFrame);
UNUSED_PARAM(propertyName);
#else
if (!childFrame.opener())
return;
RefPtr parentWindow { childFrame.opener()->window() };
RefPtr childWindow { childFrame.window() };
if (!parentWindow || !childWindow)
return;
String propertyNameDescription;
if (std::holds_alternative<PropertyName>(propertyName))
propertyNameDescription = std::get<PropertyName>(propertyName).uid();
else
propertyNameDescription = makeString(std::get<unsigned>(propertyName));
LOG(Loading, "Detected cross-tab WindowProxy property access of %s between parent window (origin = %s) and child window (origin = %s)", propertyNameDescription.utf8().data(), parentWindow->location().origin().utf8().data(), childWindow->location().origin().utf8().data());
#endif // #if LOG_DISABLED
}
static void checkCrossTabWindowProxyUsage(JSWindowProxy* proxy, JSGlobalObject* lexicalGlobalObject, const std::variant<PropertyName, unsigned>& propertyName)
{
if (!proxy || !lexicalGlobalObject)
return;
auto target = proxy->target();
if (!target)
return;
// If the caller is just trying to access their own window, we don't need to log anything.
if (target == lexicalGlobalObject)
return;
auto lexicalInfo = frameInfo(lexicalGlobalObject);
auto targetInfo = frameInfo(target);
if (!lexicalInfo || !targetInfo)
return;
// If the caller is trying to access a window within the same tab, then we don't need to log anything.
if (lexicalInfo->mainFrame.ptr() == targetInfo->mainFrame.ptr())
return;
WindowProxyProperty property = WindowProxyProperty::Other;
auto& builtinNames = WebCore::builtinNames(lexicalGlobalObject->vm());
if (std::holds_alternative<PropertyName>(propertyName)) {
auto name = std::get<PropertyName>(propertyName);
if (name == builtinNames.closedPublicName())
property = WindowProxyProperty::Closed;
else if (name == builtinNames.postMessagePublicName())
property = WindowProxyProperty::PostMessage;
}
// For the following scenarios, assume window A calls window.open to create window B.
// We'll call A the "parent" and B the "child".
//
// Scenario 1: some script in the parent tab tries to access the child window via a WindowProxy object.
// In this case, the child is the target of the WindowProxy object, and we check that the child's opener
// points to some tab in the parent.
{
auto& parent = *lexicalInfo;
auto& child = *targetInfo;
if (hasSameMainFrame(child.frame->opener(), parent)) {
logCrossTabPropertyAccess(child.frame, propertyName);
if (auto childFrame = dynamicDowncast<LocalFrame>(child.frame))
childFrame->didAccessWindowProxyPropertyViaOpener(property);
}
}
// Scenario 2: some script in the child's tab tries to access the parent window via a WindowProxy object.
// In this case, the parent is the target of the WindowProxy object, and we check that the child's main
// frame's opener points to some tab in the parent.
{
auto& parent = *targetInfo;
auto& child = *lexicalInfo;
if (hasSameMainFrame(child.mainFrame->opener(), parent)) {
logCrossTabPropertyAccess(child.mainFrame, propertyName);
if (auto childMainFrame = dynamicDowncast<LocalFrame>(child.mainFrame))
childMainFrame->didAccessWindowProxyPropertyViaOpener(property);
}
}
}
bool JSWindowProxy::getOwnPropertySlot(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(object), globalObject, propertyName);
return Base::getOwnPropertySlot(object, globalObject, propertyName, slot);
}
bool JSWindowProxy::getOwnPropertySlotByIndex(JSObject* object, JSGlobalObject* globalObject, unsigned propertyName, PropertySlot& slot)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(object), globalObject, propertyName);
return Base::getOwnPropertySlotByIndex(object, globalObject, propertyName, slot);
}
bool JSWindowProxy::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(cell), globalObject, propertyName);
return Base::put(cell, globalObject, propertyName, value, slot);
}
bool JSWindowProxy::putByIndex(JSCell* cell, JSGlobalObject* globalObject, unsigned propertyName, JSValue value, bool shouldThrow)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(cell), globalObject, propertyName);
return Base::putByIndex(cell, globalObject, propertyName, value, shouldThrow);
}
bool JSWindowProxy::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, DeletePropertySlot& slot)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(cell), globalObject, propertyName);
return Base::deleteProperty(cell, globalObject, propertyName, slot);
}
bool JSWindowProxy::deletePropertyByIndex(JSCell* cell, JSGlobalObject* globalObject, unsigned propertyName)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(cell), globalObject, propertyName);
return Base::deletePropertyByIndex(cell, globalObject, propertyName);
}
bool JSWindowProxy::defineOwnProperty(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
{
checkCrossTabWindowProxyUsage(jsCast<JSWindowProxy*>(object), globalObject, propertyName);
return Base::defineOwnProperty(object, globalObject, propertyName, descriptor, shouldThrow);
}
#endif // #if ENABLE(WINDOW_PROXY_PROPERTY_ACCESS_NOTIFICATION)
} // namespace WebCore
|