tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

capture-first-frame-with-principals.js (2619B)


      1 // Create two different globals whose compartments have two different
      2 // principals. Test getting the first frame on the stack with some given
      3 // principals in various configurations of JS stack and of wanting self-hosted
      4 // frames or not.
      5 
      6 const g1 = newGlobal({
      7  principal: 0xffff
      8 });
      9 
     10 const g2 = newGlobal({
     11  principal: 0xff
     12 });
     13 
     14 // Introduce everyone to themselves and each other.
     15 g1.g2 = g2.g2 = g2;
     16 g1.g1 = g2.g1 = g1;
     17 
     18 g1.g2obj = g2.eval("new Object");
     19 
     20 g1.evaluate(`
     21  const global = this;
     22 
     23  // Capture the stack back to the first frame in the g2 global.
     24  function capture(shouldIgnoreSelfHosted = true) {
     25    return captureFirstSubsumedFrame(global.g2obj, shouldIgnoreSelfHosted);
     26  }
     27 `, {
     28  fileName: "script1.js"
     29 });
     30 
     31 g2.evaluate(`
     32  const capture = g1.capture;
     33 
     34  function getOldestFrame(stack) {
     35    while (stack.parent) {
     36      stack = stack.parent;
     37    }
     38    return stack;
     39  }
     40 
     41  function dumpStack(name, stack) {
     42    print("Stack " + name + " =");
     43    while (stack) {
     44      print("    " + stack.functionDisplayName + " @ " + stack.source);
     45      stack = stack.parent;
     46    }
     47    print();
     48  }
     49 
     50  // When the youngest frame is not self-hosted, it doesn't matter whether or not
     51  // we specify that we should ignore self hosted frames when capturing the first
     52  // frame with the given principals.
     53  //
     54  // Stack: iife1 (g2) <- capture (g1)
     55 
     56  (function iife1() {
     57    const captureTrueStack = capture(true);
     58    dumpStack("captureTrueStack", captureTrueStack);
     59    assertEq(getOldestFrame(captureTrueStack).functionDisplayName, "iife1");
     60    assertEq(getOldestFrame(captureTrueStack).source, "script2.js");
     61 
     62    const captureFalseStack = capture(false);
     63    dumpStack("captureFalseStack", captureFalseStack);
     64    assertEq(getOldestFrame(captureFalseStack).functionDisplayName, "iife1");
     65    assertEq(getOldestFrame(captureFalseStack).source, "script2.js");
     66  }());
     67 
     68  // When the youngest frame is a self hosted frame, we get two different
     69  // captured stacks depending on whether or not we ignore self-hosted frames.
     70  //
     71  // Stack: iife2 (g2) <- Array.prototype.map <- capture (g1)
     72 
     73  (function iife2() {
     74    const trueStack = [true].map(capture)[0];
     75    dumpStack("trueStack", trueStack);
     76    assertEq(getOldestFrame(trueStack).functionDisplayName, "iife2");
     77    assertEq(getOldestFrame(trueStack).source, "script2.js");
     78 
     79    const falseStack = [false].map(capture)[0];
     80    dumpStack("falseStack", falseStack);
     81    assertEq(getOldestFrame(falseStack).functionDisplayName !== "iife2", true);
     82    assertEq(getOldestFrame(falseStack).source, "self-hosted");
     83  }());
     84 `, {
     85  fileName: "script2.js"
     86 });