test_csp.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var tests = 3;
  6. SimpleTest.waitForExplicitFinish();
  7. testDone = function(event) {
  8. if (!--tests) SimpleTest.finish();
  9. }
  10. // Workers don't inherit CSP
  11. worker = new Worker("csp_worker.js");
  12. worker.postMessage({ do: "eval" });
  13. worker.onmessage = function(event) {
  14. is(event.data, 42, "Eval succeeded!");
  15. testDone();
  16. }
  17. // blob: workers *do* inherit CSP
  18. xhr = new XMLHttpRequest;
  19. xhr.open("GET", "csp_worker.js");
  20. xhr.responseType = "blob";
  21. xhr.send();
  22. xhr.onload = (e) => {
  23. uri = URL.createObjectURL(e.target.response);
  24. worker = new Worker(uri);
  25. worker.postMessage({ do: "eval" })
  26. worker.onmessage = function(event) {
  27. is(event.data, "Error: call to eval() blocked by CSP", "Eval threw");
  28. testDone();
  29. }
  30. }
  31. xhr = new XMLHttpRequest;
  32. xhr.open("GET", "csp_worker.js");
  33. xhr.responseType = "blob";
  34. xhr.send();
  35. xhr.onload = (e) => {
  36. uri = URL.createObjectURL(e.target.response);
  37. worker = new Worker(uri);
  38. worker.postMessage({ do: "nest", uri: uri, level: 3 })
  39. worker.onmessage = function(event) {
  40. is(event.data, "Error: call to eval() blocked by CSP", "Eval threw in nested worker");
  41. testDone();
  42. }
  43. }