Bug 1032592 - Allow principals to be sent with the message manager (r=bent)

--HG--
rename : content/base/test/test_messagemanager_principal.html => content/base/test/test_messagemanager_send_principal.html
This commit is contained in:
Bill McCloskey 2014-08-05 21:46:01 -07:00
parent b9f927293c
commit 2200e6f512
4 changed files with 190 additions and 0 deletions

View File

@ -3,4 +3,5 @@
[test_bug357450.js]
[test_copypaste.xul]
[test_messagemanager_principal.html]
[test_messagemanager_send_principal.html]
skip-if = buildapp == 'mulet'

View File

@ -0,0 +1,118 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Principal in MessageManager</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>
<script type="application/javascript;version=1.7">
"use strict";
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;
var permManager = Cc["@mozilla.org/permissionmanager;1"]
.getService(Ci.nsIPermissionManager);
SimpleTest.waitForExplicitFinish();
const childFrameURL =
"data:text/html,<!DOCTYPE HTML><html><body></body></html>";
function childFrameScript() {
"use strict";
const secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
addMessageListener("test:content", function(message) {
sendAsyncMessage("test:result", "is nsIPrincipal: " +
(message.data instanceof Ci.nsIPrincipal ? "OK" : "KO"));
sendAsyncMessage("test:result", "principal.appId: " +
("appId" in message.data ? "OK" : "KO"));
sendAsyncMessage("test:result", "principal.origin: " +
("origin" in message.data ? "OK" : "KO"));
sendAsyncMessage("test:result", "principal.isInBrowserElement: " +
("isInBrowserElement" in message.data ? "OK" : "KO"));
});
addMessageListener("test:system", function(message) {
sendAsyncMessage("test:result", "isSystemPrincipal: " +
(secMan.isSystemPrincipal(message.data) ? "OK" : "KO"));
});
addMessageListener("test:null", function(message) {
sendAsyncMessage("test:result", "is nsIPrincipal: " +
(message.data instanceof Ci.nsIPrincipal ? "OK" : "KO"));
sendAsyncMessage("test:result", "isNullPrincipal: " +
(message.data.isNullPrincipal ? "OK" : "KO"));
sendAsyncMessage("test:result", "DONE");
});
}
function runTests() {
ok("Browser prefs set.");
let iframe = document.createElement("iframe");
SpecialPowers.wrap(iframe).mozbrowser = true;
iframe.id = "iframe";
iframe.src = childFrameURL;
iframe.addEventListener("mozbrowserloadend", function() {
ok(true, "Got iframe load event.");
let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
mm.addMessageListener("test:result", function(message) {
// We need to wrap to access message.json, and unwrap to do the
// identity check.
var msg = SpecialPowers.unwrap(SpecialPowers.wrap(message).data);
if (/OK$/.exec(msg)) {
ok(true, msg);
} else if(/KO$/.exec(msg)) {
ok(true, false);
} else if (/DONE/.exec(msg)) {
permManager.removeFromPrincipal(window.document.nodePrincipal, "browser",
Ci.nsIPermissionManager.ALLOW_ACTION);
SimpleTest.finish();
}
});
mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")();",
false);
mm.sendAsyncMessage("test:content", window.document.nodePrincipal);
let system = Cc["@mozilla.org/systemprincipal;1"].
createInstance(Ci.nsIPrincipal);
mm.sendAsyncMessage("test:system", system);
let nullP = Cc["@mozilla.org/nullprincipal;1"].
createInstance(Ci.nsIPrincipal);
mm.sendAsyncMessage("test:null", nullP);
});
document.body.appendChild(iframe);
}
addEventListener("load", function() {
info("Got load event.");
permManager.addFromPrincipal(window.document.nodePrincipal, "browser",
Ci.nsIPermissionManager.ALLOW_ACTION);
SpecialPowers.pushPrefEnv({
"set": [
["dom.mozBrowserFramesEnabled", true],
["browser.pagethumbnails.capturing_disabled", true]
]
}, runTests);
});
</script>
</body>
</html>

View File

@ -35,6 +35,10 @@ enum StructuredCloneTags {
// This tag is for WebCrypto keys
SCTAG_DOM_WEBCRYPTO_KEY,
SCTAG_DOM_NULL_PRINCIPAL,
SCTAG_DOM_SYSTEM_PRINCIPAL,
SCTAG_DOM_CONTENT_PRINCIPAL,
SCTAG_DOM_MAX
};

View File

@ -60,6 +60,8 @@
#include "mozilla/dom/ImageData.h"
#include "mozilla/dom/StructuredClone.h"
#include "mozilla/dom/SubtleCryptoBinding.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "nsAXPCNativeCallContext.h"
#include "mozilla/CycleCollectedJSRuntime.h"
@ -2830,6 +2832,46 @@ NS_DOMReadStructuredClone(JSContext* cx,
}
}
return result;
} else if (tag == SCTAG_DOM_NULL_PRINCIPAL ||
tag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
tag == SCTAG_DOM_CONTENT_PRINCIPAL) {
mozilla::ipc::PrincipalInfo info;
if (tag == SCTAG_DOM_SYSTEM_PRINCIPAL) {
info = mozilla::ipc::SystemPrincipalInfo();
} else if (tag == SCTAG_DOM_NULL_PRINCIPAL) {
info = mozilla::ipc::NullPrincipalInfo();
} else {
uint32_t appId = data;
uint32_t isInBrowserElement, specLength;
if (!JS_ReadUint32Pair(reader, &isInBrowserElement, &specLength)) {
return nullptr;
}
nsAutoCString spec;
spec.SetLength(specLength);
if (!JS_ReadBytes(reader, spec.BeginWriting(), specLength)) {
return nullptr;
}
info = mozilla::ipc::ContentPrincipalInfo(appId, isInBrowserElement, spec);
}
nsresult rv;
nsCOMPtr<nsIPrincipal> principal = PrincipalInfoToPrincipal(info, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR);
return nullptr;
}
JS::RootedValue result(cx);
rv = nsContentUtils::WrapNative(cx, principal, &NS_GET_IID(nsIPrincipal), &result);
if (NS_FAILED(rv)) {
xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR);
return nullptr;
}
return result.toObjectOrNull();
}
// Don't know what this is. Bail.
@ -2856,6 +2898,31 @@ NS_DOMWriteStructuredClone(JSContext* cx,
key->WriteStructuredClone(writer);
}
if (xpc::IsReflector(obj)) {
nsCOMPtr<nsISupports> base = xpc::UnwrapReflectorToISupports(obj);
nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(base);
if (principal) {
mozilla::ipc::PrincipalInfo info;
if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(principal, &info)))) {
xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR);
return false;
}
if (info.type() == mozilla::ipc::PrincipalInfo::TNullPrincipalInfo) {
return JS_WriteUint32Pair(writer, SCTAG_DOM_NULL_PRINCIPAL, 0);
}
if (info.type() == mozilla::ipc::PrincipalInfo::TSystemPrincipalInfo) {
return JS_WriteUint32Pair(writer, SCTAG_DOM_SYSTEM_PRINCIPAL, 0);
}
MOZ_ASSERT(info.type() == mozilla::ipc::PrincipalInfo::TContentPrincipalInfo);
const mozilla::ipc::ContentPrincipalInfo& cInfo = info;
return JS_WriteUint32Pair(writer, SCTAG_DOM_CONTENT_PRINCIPAL, cInfo.appId()) &&
JS_WriteUint32Pair(writer, cInfo.isInBrowserElement(), cInfo.spec().Length()) &&
JS_WriteBytes(writer, cInfo.spec().get(), cInfo.spec().Length());
}
}
// Don't know what this is
xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR);
return false;