test_components.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const Cu = Components.utils;
  2. function run_test() {
  3. var sb1 = Cu.Sandbox("http://www.blah.com");
  4. var sb2 = Cu.Sandbox("http://www.blah.com");
  5. var sb3 = Cu.Sandbox(this);
  6. var sb4 = Cu.Sandbox("http://www.other.com");
  7. var rv;
  8. // Components is normally hidden from content on the XBL scope chain, but we
  9. // expose it to content here to make sure that the security wrappers work
  10. // regardless.
  11. [sb1, sb2, sb4].forEach(function(x) { x.Components = Cu.getComponentsForScope(x); });
  12. // non-chrome accessing chrome Components
  13. sb1.C = Components;
  14. checkThrows("C.utils", sb1);
  15. checkThrows("C.classes", sb1);
  16. // non-chrome accessing own Components
  17. do_check_eq(Cu.evalInSandbox("typeof Components.interfaces", sb1), 'object');
  18. do_check_eq(Cu.evalInSandbox("typeof Components.utils", sb1), 'undefined');
  19. do_check_eq(Cu.evalInSandbox("typeof Components.classes", sb1), 'undefined');
  20. // Make sure an unprivileged Components is benign.
  21. var C2 = Cu.evalInSandbox("Components", sb2);
  22. var whitelist = ['interfaces', 'interfacesByID', 'results', 'isSuccessCode', 'QueryInterface'];
  23. for (var prop in Components) {
  24. do_print("Checking " + prop);
  25. do_check_eq((prop in C2), whitelist.indexOf(prop) != -1);
  26. }
  27. // non-chrome same origin
  28. sb1.C2 = C2;
  29. do_check_eq(Cu.evalInSandbox("typeof C2.interfaces", sb1), 'object');
  30. do_check_eq(Cu.evalInSandbox("typeof C2.utils", sb1), 'undefined');
  31. do_check_eq(Cu.evalInSandbox("typeof C2.classes", sb1), 'undefined');
  32. // chrome accessing chrome
  33. sb3.C = Components;
  34. rv = Cu.evalInSandbox("C.utils", sb3);
  35. do_check_eq(rv, Cu);
  36. // non-chrome cross origin
  37. sb4.C2 = C2;
  38. checkThrows("C2.interfaces", sb4);
  39. checkThrows("C2.utils", sb4);
  40. checkThrows("C2.classes", sb4);
  41. }
  42. function checkThrows(expression, sb) {
  43. var result = Cu.evalInSandbox('(function() { try { ' + expression + '; return "allowed"; } catch (e) { return e.toString(); }})();', sb);
  44. do_check_true(!!/denied/.exec(result));
  45. }