2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2014-03-07 23:50:41 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* JSPrincipals and related interfaces. */
|
|
|
|
|
|
|
|
#ifndef js_Principals_h
|
|
|
|
#define js_Principals_h
|
|
|
|
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "jspubtd.h"
|
|
|
|
|
2018-07-17 15:08:55 +03:00
|
|
|
struct JSStructuredCloneReader;
|
|
|
|
struct JSStructuredCloneWriter;
|
2015-10-02 17:44:00 +03:00
|
|
|
|
2014-03-07 23:50:41 +04:00
|
|
|
struct JSPrincipals {
|
|
|
|
/* Don't call "destroy"; use reference counting macros below. */
|
2018-07-21 17:24:16 +03:00
|
|
|
mozilla::Atomic<int32_t, mozilla::SequentiallyConsistent,
|
|
|
|
mozilla::recordreplay::Behavior::DontPreserve>
|
|
|
|
refcount;
|
2014-03-07 23:50:41 +04:00
|
|
|
|
|
|
|
#ifdef JS_DEBUG
|
|
|
|
/* A helper to facilitate principals debugging. */
|
|
|
|
uint32_t debugToken;
|
|
|
|
#endif
|
|
|
|
|
2018-04-12 22:15:26 +03:00
|
|
|
JSPrincipals() : refcount(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-03-07 23:50:41 +04:00
|
|
|
void setDebugToken(uint32_t token) {
|
|
|
|
#ifdef JS_DEBUG
|
|
|
|
debugToken = token;
|
|
|
|
#endif
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-02 17:44:00 +03:00
|
|
|
/*
|
|
|
|
* Write the principals with the given |writer|. Return false on failure,
|
|
|
|
* true on success.
|
|
|
|
*/
|
|
|
|
virtual bool write(JSContext* cx, JSStructuredCloneWriter* writer) = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-10-04 20:37:36 +03:00
|
|
|
/*
|
|
|
|
* Whether the principal corresponds to a System or AddOn Principal.
|
|
|
|
* Technically this also checks for an ExpandedAddonPrincipal.
|
|
|
|
*/
|
|
|
|
virtual bool isSystemOrAddonPrincipal() = 0;
|
|
|
|
|
2014-03-07 23:50:41 +04:00
|
|
|
/*
|
|
|
|
* This is not defined by the JS engine but should be provided by the
|
|
|
|
* embedding.
|
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
JS_PUBLIC_API void dump();
|
2014-03-07 23:50:41 +04:00
|
|
|
};
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
extern JS_PUBLIC_API void JS_HoldPrincipals(JSPrincipals* principals);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
2016-07-05 17:49:41 +03:00
|
|
|
extern JS_PUBLIC_API void JS_DropPrincipals(JSContext* cx,
|
|
|
|
JSPrincipals* principals);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
|
|
|
// Return whether the first principal subsumes the second. The exact meaning of
|
|
|
|
// 'subsumes' is left up to the browser. Subsumption is checked inside the JS
|
|
|
|
// engine when determining, e.g., which stack frames to display in a backtrace.
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef bool (*JSSubsumesOp)(JSPrincipals* first, JSPrincipals* second);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Used to check if a CSP instance wants to disable eval() and friends.
|
2018-07-16 18:58:04 +03:00
|
|
|
* See GlobalObject::isRuntimeCodeGenEnabled() in vm/GlobalObject.cpp.
|
2014-03-07 23:50:41 +04:00
|
|
|
*/
|
2018-07-16 18:58:04 +03:00
|
|
|
typedef bool (*JSCSPEvalChecker)(JSContext* cx, JS::HandleValue value);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
|
|
|
struct JSSecurityCallbacks {
|
|
|
|
JSCSPEvalChecker contentSecurityPolicyAllows;
|
|
|
|
JSSubsumesOp subsumes;
|
|
|
|
};
|
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_PUBLIC_API void JS_SetSecurityCallbacks(
|
2016-07-05 17:49:44 +03:00
|
|
|
JSContext* cx, const JSSecurityCallbacks* callbacks);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_PUBLIC_API const JSSecurityCallbacks* JS_GetSecurityCallbacks(
|
2016-07-05 17:49:44 +03:00
|
|
|
JSContext* cx);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Code running with "trusted" principals will be given a deeper stack
|
|
|
|
* allocation than ordinary scripts. This allows trusted script to run after
|
|
|
|
* untrusted script has exhausted the stack. This function sets the
|
|
|
|
* runtime-wide trusted principal.
|
|
|
|
*
|
2016-07-05 17:49:46 +03:00
|
|
|
* This principals is not held (via JS_HoldPrincipals/JS_DropPrincipals).
|
|
|
|
* Instead, the caller must ensure that the given principals stays valid for as
|
|
|
|
* long as 'cx' may point to it. If the principals would be destroyed before
|
|
|
|
* 'cx', JS_SetTrustedPrincipals must be called again, passing nullptr for
|
|
|
|
* 'prin'.
|
2014-03-07 23:50:41 +04:00
|
|
|
*/
|
2016-07-05 17:49:46 +03:00
|
|
|
extern JS_PUBLIC_API void JS_SetTrustedPrincipals(JSContext* cx,
|
|
|
|
JSPrincipals* prin);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef void (*JSDestroyPrincipalsOp)(JSPrincipals* principals);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the callback that is called to destroy JSPrincipals instance
|
|
|
|
* when its reference counter drops to zero. The initialization can be done
|
|
|
|
* only once per JS runtime.
|
|
|
|
*/
|
2016-07-05 17:49:46 +03:00
|
|
|
extern JS_PUBLIC_API void JS_InitDestroyPrincipalsCallback(
|
|
|
|
JSContext* cx, JSDestroyPrincipalsOp destroyPrincipals);
|
2014-03-07 23:50:41 +04:00
|
|
|
|
2015-10-02 17:44:00 +03:00
|
|
|
/*
|
|
|
|
* Read a JSPrincipals instance from the given |reader| and initialize the out
|
|
|
|
* paratemer |outPrincipals| to the JSPrincipals instance read.
|
|
|
|
*
|
|
|
|
* Return false on failure, true on success. The |outPrincipals| parameter
|
|
|
|
* should not be modified if false is returned.
|
|
|
|
*
|
|
|
|
* The caller is not responsible for calling JS_HoldPrincipals on the resulting
|
|
|
|
* JSPrincipals instance, the JSReadPrincipalsOp must increment the refcount of
|
|
|
|
* the resulting JSPrincipals on behalf of the caller.
|
|
|
|
*/
|
|
|
|
using JSReadPrincipalsOp = bool (*)(JSContext* cx,
|
|
|
|
JSStructuredCloneReader* reader,
|
|
|
|
JSPrincipals** outPrincipals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the callback that is called to read JSPrincipals instances from a
|
|
|
|
* buffer. The initialization can be done only once per JS runtime.
|
|
|
|
*/
|
2016-07-05 17:49:46 +03:00
|
|
|
extern JS_PUBLIC_API void JS_InitReadPrincipalsCallback(
|
|
|
|
JSContext* cx, JSReadPrincipalsOp read);
|
2015-10-02 17:44:00 +03:00
|
|
|
|
2019-06-11 13:11:21 +03:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class MOZ_RAII AutoHoldPrincipals {
|
|
|
|
JSContext* cx_;
|
|
|
|
JSPrincipals* principals_ = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoHoldPrincipals(JSContext* cx, JSPrincipals* principals = nullptr)
|
|
|
|
: cx_(cx) {
|
|
|
|
reset(principals);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoHoldPrincipals() { reset(nullptr); }
|
|
|
|
|
|
|
|
void reset(JSPrincipals* principals) {
|
|
|
|
if (principals) {
|
|
|
|
JS_HoldPrincipals(principals);
|
|
|
|
}
|
|
|
|
if (principals_) {
|
|
|
|
JS_DropPrincipals(cx_, principals_);
|
|
|
|
}
|
|
|
|
principals_ = principals;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSPrincipals* get() const { return principals_; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
2014-03-07 23:50:41 +04:00
|
|
|
#endif /* js_Principals_h */
|