DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<!DOCTYPE HTML>
<html>
  <head>
    <title>Test mid-flight cross site redirects are blocked</title>
    <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    <script type="text/javascript" src="manifest.js"></script>
  </head>
  <body>
    <pre id='test'>
    <script class="testbody" type='application/javascript'>

      function testIfLoadsToMetadata(test, useCors) {
        return new Promise(function(resolve, reject) {
          var elemType = getMajorMimeType(test.type);
          var element = document.createElement(elemType);

          if (useCors) {
            element.crossOrigin = "anonymous";
          }

          // Log events for debugging.
          [
            "suspend", "play", "canplay", "canplaythrough", "loadstart",
            "loadedmetadata", "loadeddata", "playing", "ended", "error",
            "stalled", "emptied", "abort", "waiting", "pause"
          ].forEach((eventName) => {
            element.addEventListener(eventName, (event)=> {
              info(test.name + " " + event.type);
            });
          });

          element.addEventListener("loadedmetadata", ()=>{
            resolve(true);
            removeNodeAndSource(element);
          });

          element.addEventListener("error", ()=>{
            resolve(false);
            removeNodeAndSource(element);
          });

          // Note: request redirect before the end of metadata, otherwise we won't
          // error before metadata has loaded if mixed origins are allowed.
          element.src = "midflight-redirect.sjs?resource=" + test.name
                      + (useCors ? "&cors" : "")
                      + "&type=" + test.type
                      + "&redirectAt=200";
          element.preload = "metadata";
          document.body.appendChild(element);
          element.load()
        });
      }

      let v = document.createElement("video");
      const testCases = gSmallTests.filter(t => v.canPlayType(t.type));

      async function testMediaLoad(expectedToLoad, message, useCors) {
        for (let test of testCases) {
          let loaded = await testIfLoadsToMetadata(test, useCors);
          is(loaded, expectedToLoad, test.name + " " + message);
        }
      }

      async function runTest() {
        try {
          SimpleTest.info("Allowing midflight redirects...");
          await SpecialPowers.pushPrefEnv({'set': [["media.block-midflight-redirects", false]]});

          SimpleTest.info("Test that all media plays...");
          await testMediaLoad(true, "expected to load", false);

          SimpleTest.info("Blocking midflight redirects...");
          await SpecialPowers.pushPrefEnv({'set': [["media.block-midflight-redirects", true]]});

          SimpleTest.info("Test that all media no longer play...");
          await testMediaLoad(false, "expected to be blocked", false);

          SimpleTest.info("Test that all media play if CORS used...");
          await testMediaLoad(true, "expected to play with CORS", true);
        } catch (e) {
          info("Exception " + e.message);
          ok(false, "Threw exception " + e.message);
        }
        SimpleTest.finish();
      }

      SimpleTest.waitForExplicitFinish();

      addLoadEvent(runTest);

    </script>
    </pre>
  </body>
</html>