From 250d4b1ff839c2bbb4dc34b275f3a37a2b076b78 Mon Sep 17 00:00:00 2001 From: Christoph Kerschbaumer Date: Tue, 11 Jul 2017 08:48:37 +0200 Subject: [PATCH] Bug 1377426 - Set CSP on freshly created nullprincipal when iframe is sandboxed. r=dveditz --- dom/base/nsDocument.cpp | 12 ++++++++---- dom/security/test/csp/test_sandbox.html | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index dac984702faa..fca9020a9c30 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -2792,10 +2792,14 @@ nsDocument::InitCSP(nsIChannel* aChannel) mSandboxFlags |= cspSandboxFlags; - if (cspSandboxFlags & SANDBOXED_ORIGIN) { - // If the new CSP sandbox flags do not have the allow-same-origin flag - // reset the document principal to a null principal - principal = NullPrincipal::Create(); + // Probably the iframe sandbox attribute already caused the creation of a + // new NullPrincipal. Only create a new NullPrincipal if CSP requires so + // and no one has been created yet. + bool needNewNullPrincipal = + (cspSandboxFlags & SANDBOXED_ORIGIN) && !(mSandboxFlags & SANDBOXED_ORIGIN); + if (needNewNullPrincipal) { + principal = NullPrincipal::CreateWithInheritedAttributes(principal); + principal->SetCsp(csp); SetPrincipal(principal); } diff --git a/dom/security/test/csp/test_sandbox.html b/dom/security/test/csp/test_sandbox.html index b6c9c6a72fc8..b36043a04549 100644 --- a/dom/security/test/csp/test_sandbox.html +++ b/dom/security/test/csp/test_sandbox.html @@ -106,6 +106,15 @@ var testCases = [ results: { img12_bad: -1, script12_bad: -1 }, nrOKmessages: 4 // sends 4 ok message }, + { + // Test 13: same as Test 5 and Test 11, but: + // * using sandbox flag 'allow-scripts' in CSP and not as iframe attribute + // * not using allow-same-origin in CSP (so a new NullPrincipal is created). + csp: "default-src 'none'; script-src 'unsafe-inline'; sandbox allow-scripts", + file: "file_sandbox_5.html", + results: { img13_bad: -1, img13a_bad: -1, script13_bad: -1, script13a_bad: -1 }, + nrOKmessages: 2 // sends 2 ok message + }, ]; // a postMessage handler that is used by sandboxed iframes without