JSDOMBindingSecurity.cpp [plain text]
#include "config.h"
#include "JSDOMBindingSecurity.h"
#include "DOMWindow.h"
#include "Document.h"
#include "Frame.h"
#include "HTTPParsers.h"
#include "JSDOMExceptionHandling.h"
#include "JSDOMWindowBase.h"
#include "SecurityOrigin.h"
#include <wtf/text/WTFString.h>
namespace WebCore {
using namespace JSC;
void printErrorMessageForFrame(Frame* frame, const String& message)
{
if (!frame)
return;
frame->document()->domWindow()->printErrorMessage(message);
}
static inline bool canAccessDocument(JSC::JSGlobalObject* lexicalGlobalObject, Document* targetDocument, SecurityReportingOption reportingOption)
{
if (!targetDocument)
return false;
if (auto* templateHost = targetDocument->templateDocumentHost())
targetDocument = templateHost;
DOMWindow& active = activeDOMWindow(*lexicalGlobalObject);
if (active.document()->securityOrigin().canAccess(targetDocument->securityOrigin()))
return true;
switch (reportingOption) {
case ThrowSecurityError: {
VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
throwSecurityError(*lexicalGlobalObject, scope, targetDocument->domWindow()->crossDomainAccessErrorMessage(active, IncludeTargetOrigin::No));
break;
}
case LogSecurityError:
printErrorMessageForFrame(targetDocument->frame(), targetDocument->domWindow()->crossDomainAccessErrorMessage(active, IncludeTargetOrigin::Yes));
break;
case DoNotReportSecurityError:
break;
}
return false;
}
bool BindingSecurity::shouldAllowAccessToFrame(JSGlobalObject& lexicalGlobalObject, Frame& frame, String& message)
{
if (BindingSecurity::shouldAllowAccessToFrame(&lexicalGlobalObject, &frame, DoNotReportSecurityError))
return true;
message = frame.document()->domWindow()->crossDomainAccessErrorMessage(activeDOMWindow(lexicalGlobalObject), IncludeTargetOrigin::No);
return false;
}
bool BindingSecurity::shouldAllowAccessToDOMWindow(JSGlobalObject& lexicalGlobalObject, DOMWindow* globalObject, String& message)
{
return globalObject && shouldAllowAccessToDOMWindow(lexicalGlobalObject, *globalObject, message);
}
bool BindingSecurity::shouldAllowAccessToDOMWindow(JSGlobalObject& lexicalGlobalObject, DOMWindow& globalObject, String& message)
{
VM& vm = lexicalGlobalObject.vm();
auto scope = DECLARE_CATCH_SCOPE(vm);
bool shouldAllowAccess = BindingSecurity::shouldAllowAccessToDOMWindow(&lexicalGlobalObject, globalObject, DoNotReportSecurityError);
EXCEPTION_ASSERT_UNUSED(scope, !scope.exception());
if (shouldAllowAccess)
return true;
message = globalObject.crossDomainAccessErrorMessage(activeDOMWindow(lexicalGlobalObject), IncludeTargetOrigin::No);
return false;
}
bool BindingSecurity::shouldAllowAccessToDOMWindow(JSC::JSGlobalObject* lexicalGlobalObject, DOMWindow& target, SecurityReportingOption reportingOption)
{
return canAccessDocument(lexicalGlobalObject, target.document(), reportingOption);
}
bool BindingSecurity::shouldAllowAccessToDOMWindow(JSC::JSGlobalObject* lexicalGlobalObject, DOMWindow* target, SecurityReportingOption reportingOption)
{
return target && shouldAllowAccessToDOMWindow(lexicalGlobalObject, *target, reportingOption);
}
bool BindingSecurity::shouldAllowAccessToFrame(JSC::JSGlobalObject* lexicalGlobalObject, Frame* target, SecurityReportingOption reportingOption)
{
return target && canAccessDocument(lexicalGlobalObject, target->document(), reportingOption);
}
bool BindingSecurity::shouldAllowAccessToNode(JSC::JSGlobalObject& lexicalGlobalObject, Node* target)
{
return !target || canAccessDocument(&lexicalGlobalObject, &target->document(), LogSecurityError);
}
}