Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/js/xpconnect/tests/unit/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 3 kB image not shown  

Quelle  test_sandbox_csp.js   Sprache: JAVA

 
"use strict";

function isEvalAllowed(sandbox) {
  try {
    Cu.evalInSandbox("eval('1234')", sandbox);
    return true;
  } catch (e) {
    Assert.equal(e.message, "call to eval() blocked by CSP""Eval error msg");
    return false;
  }
}

add_task(function test_empty_csp() {
  let sand = Cu.Sandbox(["http://example.com/"], {
    sandboxContentSecurityPolicy: "",
  });
  Assert.ok(isEvalAllowed(sand), "eval() not blocked with empty CSP string");
});

add_task(function test_undefined_csp() {
  let sand = Cu.Sandbox(["http://example.com/"], {
    sandboxContentSecurityPolicy: undefined,
  });
  Assert.ok(isEvalAllowed(sand), "eval() not blocked with undefined CSP");
});

add_task(function test_malformed_csp() {
  let sand = Cu.Sandbox(["http://example.com/"], {
    sandboxContentSecurityPolicy: "This is not a valid CSP value",
  });
  Assert.ok(isEvalAllowed(sand), "eval() not blocked with undefined CSP");
});

add_task(function test_allowed_by_sandboxContentSecurityPolicy() {
  let sand = Cu.Sandbox(["http://example.com/"], {
    sandboxContentSecurityPolicy: "script-src 'unsafe-eval';",
  });
  Assert.ok(isEvalAllowed(sand), "eval() allowed by 'unsafe-eval' CSP");
});

add_task(function test_blocked_by_sandboxContentSecurityPolicy() {
  let sand = Cu.Sandbox(["http://example.com/"], {
    sandboxContentSecurityPolicy: "script-src 'none';",
  });

  // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
  Assert.ok(Cu.getObjectPrincipal(sand).isExpandedPrincipal, "Exp principal");

  Assert.ok(!isEvalAllowed(sand), "eval() should be blocked by CSP");
  // sandbox.eval is also blocked: callers should use Cu.evalInSandbox instead.
  Assert.throws(
    () => sand.eval("123"),
    /EvalError: call to eval\(\) blocked by CSP/,
    "sandbox.eval() is also blocked by CSP"
  );
});

add_task(function test_sandboxContentSecurityPolicy_on_content_principal() {
  Assert.throws(
    () => {
      Cu.Sandbox("http://example.com", {
        sandboxContentSecurityPolicy: "script-src 'none';",
      });
    },
    /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
    // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
    "sandboxContentSecurityPolicy does not work with content principal"
  );
});

add_task(function test_sandboxContentSecurityPolicy_on_null_principal() {
  Assert.throws(
    () => {
      Cu.Sandbox(null, { sandboxContentSecurityPolicy: "script-src 'none';" });
    },
    /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
    // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
    "sandboxContentSecurityPolicy does not work with content principal"
  );
});

add_task(function test_sandboxContentSecurityPolicy_on_content_principal() {
  Assert.throws(
    () => {
      Cu.Sandbox("http://example.com", {
        sandboxContentSecurityPolicy: "script-src 'none';",
      });
    },
    /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
    // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
    "sandboxContentSecurityPolicy does not work with content principal"
  );
});

add_task(function test_sandboxContentSecurityPolicy_on_system_principal() {
  const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
  // Note: if we ever introduce support for CSP in non-Expanded principals,
  // then the test should set security.allow_eval_with_system_principal=true
  // to make sure that eval() is blocked because of CSP and not another reason.
  Assert.throws(
    () => {
      Cu.Sandbox(systemPrincipal, {
        sandboxContentSecurityPolicy: "script-src 'none';",
      });
    },
    /Error: sandboxContentSecurityPolicy is currently only supported with ExpandedPrincipals/,
    // Until bug 1548468 is fixed, CSP only works with an ExpandedPrincipal.
    "sandboxContentSecurityPolicy does not work with system principal"
  );
});

Messung V0.5
C=93 H=88 G=90

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.