tor-browser

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

browser_dbg-sourcemaps-credentials.js (3824B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 // Tests loading sourcemapped sources, setting breakpoints, and
      6 // stepping in them.
      7 
      8 "use strict";
      9 
     10 const EXPECTED_COOKIE = "credentials=true";
     11 
     12 // This test checks that the DevTools sourcemap request includes credentials
     13 // from the page. See Bug 1899389.
     14 add_task(async function () {
     15  const httpServer = setupTestServer();
     16  const port = httpServer.identity.primaryPort;
     17 
     18  const dbg = await initDebuggerWithAbsoluteURL(
     19    `http://localhost:${port}/index.html`
     20  );
     21 
     22  await waitForSources(dbg, "min.js");
     23  const minifiedSrc = findSource(dbg, "min.js");
     24  await selectSource(dbg, minifiedSrc);
     25 
     26  const footerButton = findElement(dbg, "sourceMapFooterButton");
     27  is(
     28    footerButton.textContent,
     29    "bundle file",
     30    "Sourcemap button mentions the bundle file"
     31  );
     32  ok(!footerButton.classList.contains("not-mapped"));
     33 
     34  info("Click on jump to original source link from editor's footer");
     35  const mappedSourceLink = findElement(dbg, "mappedSourceLink");
     36  is(
     37    mappedSourceLink.textContent,
     38    "From original.js",
     39    "The link to mapped source mentions the original file name"
     40  );
     41  mappedSourceLink.click();
     42 
     43  const originalSrc = findSource(dbg, "original.js");
     44  await waitForSelectedSource(dbg, originalSrc);
     45  info("The original source was successfully selected");
     46 });
     47 
     48 function setupTestServer() {
     49  // The test server will serve:
     50  // - index.html: page which loads a min.js file
     51  // - min.js: simple minified js file, linked to the map min.js.map
     52  // - min.js.map: the corresponding sourcemap
     53  // - original.js: the original file
     54  //
     55  // The sourcemap file will only be returned if the request contains a cookie
     56  // set in the page. Otherwise it will return a 404.
     57 
     58  const httpServer = createTestHTTPServer();
     59  httpServer.registerContentType("html", "text/html");
     60  httpServer.registerContentType("js", "application/javascript");
     61  httpServer.registerContentType("map", "text/plain");
     62 
     63  // Page: index.html
     64  httpServer.registerPathHandler("/index.html", function (request, response) {
     65    response.setStatusLine(request.httpVersion, 200, "OK");
     66    response.write(`<!DOCTYPE html>
     67    <html>
     68      <body>
     69      <h1>Sourcemap with credentials</h1>
     70      <script src="/min.js"></script>
     71      <script type=text/javascript>document.cookie="${EXPECTED_COOKIE}";</script>
     72      </body>
     73    `);
     74  });
     75 
     76  // Bundle: min.js
     77  httpServer.registerPathHandler("/min.js", function (request, response) {
     78    response.setStatusLine(request.httpVersion, 200, "OK");
     79    response.setHeader("Content-Type", "application/javascript", false);
     80    response.write(`function sum(n,u){return n+u}
     81 //# sourceMappingURL=min.js.map
     82 `);
     83  });
     84 
     85  // Sourcemap: min.js.map
     86  httpServer.registerPathHandler("/min.js.map", function (request, response) {
     87    if (
     88      request.hasHeader("Cookie") &&
     89      request.getHeader("Cookie") == EXPECTED_COOKIE
     90    ) {
     91      response.setStatusLine(request.httpVersion, 200, "OK");
     92      response.setHeader("Content-Type", "application/javascript", false);
     93      response.write(
     94        `{"version":3,"sources":["original.js"],"names":["sum","first","second"],"mappings":"AAAA,SAASA,IAAIC,EAAOC,GAClB,OAAOD,EAAQC"}`
     95      );
     96    } else {
     97      response.setStatusLine(request.httpVersion, 404, "Not found");
     98    }
     99  });
    100 
    101  // Original: original.js
    102  httpServer.registerPathHandler("/original.js", function (request, response) {
    103    response.setStatusLine(request.httpVersion, 200, "OK");
    104    response.setHeader("Content-Type", "application/javascript", false);
    105    response.write(`function sum(first, second) {
    106  return first + second;
    107 }`);
    108  });
    109 
    110  return httpServer;
    111 }