tor-browser

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

file_form_submission.sjs (2989B)


      1 const CC = Components.Constructor;
      2 const BinaryInputStream = CC(
      3   "@mozilla.org/binaryinputstream;1",
      4   "nsIBinaryInputStream",
      5   "setInputStream"
      6 );
      7 
      8 function makeResponse(success) {
      9   let res = `
     10   <html>
     11     <body>
     12       send message, downgraded
     13     <script type="application/javascript">
     14       let scheme = document.location.protocol;
     15       const loc = document.location.href;
     16       window.opener.postMessage({location: loc, scheme: scheme, form:"test=${
     17         success ? "success" : "failure"
     18       }" }, '*');
     19     </script>
     20     </body>
     21   </html>`;
     22   return res;
     23 }
     24 
     25 function makeForm(method, testID) {
     26   return `
     27   <html>
     28       <body>
     29           <form action="http://example.com/tests/dom/security/test/https-first/file_form_submission.sjs" method="${method}" id="testform">
     30               <div>
     31               <label id="submit">Submit</label>
     32               <input name="test" id="form" value="${testID}">
     33               <input name="result" id="form" value="success">
     34               </div>
     35           </form>
     36           <script class="testbody" type="text/javascript">
     37               document.getElementById("testform").submit();
     38           </script>
     39       </body>
     40   </html>
     41   `;
     42 }
     43 
     44 function handleRequest(request, response) {
     45   // avoid confusing cache behaviors
     46   response.setHeader("Cache-Control", "no-cache", false);
     47   let queryString = request.queryString;
     48   // Endpoints to return a form
     49   if (
     50     (request.scheme === "https" && queryString === "test=1") ||
     51     (request.scheme === "http" && queryString === "test=2")
     52   ) {
     53     response.write(makeForm("GET", queryString.substr(-1, 1)));
     54     return;
     55   }
     56   if (queryString === "test=3" || queryString === "test=4") {
     57     response.write(makeForm("POST", queryString.substr(-1, 1)));
     58     return;
     59   }
     60   // Endpoints to trigger downgrades because of timeouts
     61   if (
     62     request.scheme === "https" &&
     63     (queryString === "test=2" || queryString === "test=4")
     64   ) {
     65     response.processAsync();
     66     return;
     67   }
     68   // Endpoints for receiving the form data
     69   if (
     70     request.method == "GET" &&
     71     ((queryString.includes("test=1") && request.scheme === "https") ||
     72       queryString.includes("test=2")) &&
     73     queryString.includes("result=success")
     74   ) {
     75     response.write(makeResponse(true));
     76     return;
     77   }
     78   if (request.method == "POST" && request.scheme === "http") {
     79     // extract form parameters
     80     let body = new BinaryInputStream(request.bodyInputStream);
     81     let avail;
     82     let bytes = [];
     83     while ((avail = body.available()) > 0) {
     84       Array.prototype.push.apply(bytes, body.readByteArray(avail));
     85     }
     86     let requestBodyContents = String.fromCharCode.apply(null, bytes);
     87 
     88     response.write(
     89       makeResponse(
     90         (requestBodyContents.includes("test=3") ||
     91           requestBodyContents.includes("test=4")) &&
     92           requestBodyContents.includes("result=success")
     93       )
     94     );
     95     return;
     96   }
     97   // we should never get here; just in case, return something unexpected
     98   response.write(makeResponse(false));
     99 }