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