3 function isEvalAllowed(sandbox) {
5 Cu.evalInSandbox("eval('1234')", sandbox);
8 Assert.equal(e.message, "call to eval() blocked by CSP", "Eval error msg");
13 add_task(function test_empty_csp() {
14 let sand = Cu.Sandbox(["http://example.com/"], {
15 sandboxContentSecurityPolicy: "",
17 Assert.ok(isEvalAllowed(sand), "eval() not blocked with empty CSP string");
20 add_task(function test_undefined_csp() {
21 let sand = Cu.Sandbox(["http://example.com/"], {
22 sandboxContentSecurityPolicy: undefined,
24 Assert.ok(isEvalAllowed(sand), "eval() not blocked with undefined CSP");
27 add_task(function test_malformed_csp() {
28 let sand = Cu.Sandbox(["http://example.com/"], {
29 sandboxContentSecurityPolicy: "This is not a valid CSP value",
31 Assert.ok(isEvalAllowed(sand), "eval() not blocked with undefined CSP");
34 add_task(function test_allowed_by_sandboxContentSecurityPolicy() {
35 let sand = Cu.Sandbox(["http://example.com/"], {
36 sandboxContentSecurityPolicy: "script-src 'unsafe-eval';",
38 Assert.ok(isEvalAllowed(sand), "eval() allowed by 'unsafe-eval' CSP");
41 add_task(function test_blocked_by_sandboxContentSecurityPolicy() {
42 let sand = Cu.Sandbox(["http://example.com/"], {
43 sandboxContentSecurityPolicy: "script-src 'none';",
46 // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
47 Assert.ok(Cu.getObjectPrincipal(sand).isExpandedPrincipal, "Exp principal");
49 Assert.ok(!isEvalAllowed(sand), "eval() should be blocked by CSP");
50 // sandbox.eval is also blocked: callers should use Cu.evalInSandbox instead.
52 () => sand.eval("123"),
53 /EvalError: call to eval\(\) blocked by CSP/,
54 "sandbox.eval() is also blocked by CSP"
58 add_task(function test_sandboxContentSecurityPolicy_on_content_principal() {
61 Cu.Sandbox("http://example.com", {
62 sandboxContentSecurityPolicy: "script-src 'none';",
65 /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
66 // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
67 "sandboxContentSecurityPolicy does not work with content principal"
71 add_task(function test_sandboxContentSecurityPolicy_on_null_principal() {
74 Cu.Sandbox(null, { sandboxContentSecurityPolicy: "script-src 'none';" });
76 /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
77 // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
78 "sandboxContentSecurityPolicy does not work with content principal"
82 add_task(function test_sandboxContentSecurityPolicy_on_content_principal() {
85 Cu.Sandbox("http://example.com", {
86 sandboxContentSecurityPolicy: "script-src 'none';",
89 /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
90 // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
91 "sandboxContentSecurityPolicy does not work with content principal"
95 add_task(function test_sandboxContentSecurityPolicy_on_system_principal() {
96 const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
97 // Note: if we ever introduce support for CSP in non-Expanded principals,
98 // then the test should set security.allow_eval_with_system_principal=true
99 // to make sure that eval() is blocked because of CSP and not another reason.
102 Cu.Sandbox(systemPrincipal, {
103 sandboxContentSecurityPolicy: "script-src 'none';",
106 /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
107 // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
108 "sandboxContentSecurityPolicy does not work with system principal"