Issue #2197 - Part 4: Expose structuredClone in Sandbox

Partially based on https://bugzilla.mozilla.org/show_bug.cgi?id=1734320
This commit is contained in:
FranklinDM 2023-04-07 21:45:15 +08:00 committed by roytam1
commit bbcfb62753
10 changed files with 129 additions and 67 deletions

View file

@ -326,6 +326,45 @@ SandboxCreateFetch(JSContext* cx, HandleObject obj)
dom::HeadersBinding::GetConstructorObject(cx);
}
static bool SandboxStructuredClone(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (!args.requireAtLeast(cx, "structuredClone", 1)) {
return false;
}
RootedDictionary<dom::StructuredSerializeOptions> options(cx);
if (!options.Init(cx, args.hasDefined(1) ? args[1] : JS::NullHandleValue,
"Argument 2", false)) {
return false;
}
nsIGlobalObject* global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(cx));
if (!global) {
JS_ReportErrorASCII(cx, "structuredClone: Missing global");
return false;
}
JS::Rooted<JS::Value> result(cx);
ErrorResult rv;
nsContentUtils::StructuredClone(cx, global, args[0], options, &result, rv);
if (rv.MaybeSetPendingException(cx)) {
return false;
}
MOZ_ASSERT_IF(result.isGCThing(),
!JS::GCThingIsMarkedGray(result.toGCCellPtr()));
args.rval().set(result);
return true;
}
static bool SandboxCreateStructuredClone(JSContext* cx, HandleObject obj) {
MOZ_ASSERT(JS_IsGlobalObject(obj));
return JS_DefineFunction(cx, obj, "structuredClone", SandboxStructuredClone,
1, 0);
}
static bool
SandboxIsProxy(JSContext* cx, unsigned argc, Value* vp)
{
@ -931,6 +970,8 @@ xpc::GlobalProperties::Parse(JSContext* cx, JS::HandleObject obj)
#endif
} else if (!strcmp(name.ptr(), "fetch")) {
fetch = true;
} else if (!strcmp(name.ptr(), "structuredClone")) {
structuredClone = true;
} else if (!strcmp(name.ptr(), "caches")) {
caches = true;
} else if (!strcmp(name.ptr(), "FileReader")) {
@ -1006,6 +1047,9 @@ xpc::GlobalProperties::Define(JSContext* cx, JS::HandleObject obj)
if (fetch && !SandboxCreateFetch(cx, obj))
return false;
if (structuredClone && !SandboxCreateStructuredClone(cx, obj))
return false;
if (caches && !dom::cache::CacheStorage::DefineCaches(cx, obj))
return false;

View file

@ -2879,6 +2879,7 @@ struct GlobalProperties {
bool crypto : 1;
bool rtcIdentityProvider : 1;
bool fetch : 1;
bool structuredClone : 1;
bool caches : 1;
bool fileReader: 1;
private: