content/base/test/csp/file_bug836922_npolicies_violation.sjs

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 // SJS file that receives violation reports and then responds with nothing.
     3 const CC = Components.Constructor;
     4 const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
     5                               "nsIBinaryInputStream",
     6                               "setInputStream");
     8 const STATE = "bug836922_violations";
    10 function handleRequest(request, response)
    11 {
    12   var query = {};
    13   request.queryString.split('&').forEach(function (val) {
    14     var [name, value] = val.split('=');
    15     query[name] = unescape(value);
    16   });
    19   if ('results' in query) {
    20     // if asked for the received data, send it.
    21     response.setHeader("Content-Type", "text/javascript", false);
    22     if (getState(STATE)) {
    23       response.write(getState(STATE));
    24     } else {
    25       // no state has been recorded.
    26       response.write(JSON.stringify({}));
    27     }
    28   } else if ('reset' in query) {
    29     //clear state
    30     setState(STATE, JSON.stringify(null));
    31   } else {
    32     // ... otherwise, just respond "ok".
    33     response.write("null");
    35     var bodystream = new BinaryInputStream(request.bodyInputStream);
    36     var avail;
    37     var bytes = [];
    38     while ((avail = bodystream.available()) > 0)
    39       Array.prototype.push.apply(bytes, bodystream.readByteArray(avail));
    41     var data = String.fromCharCode.apply(null, bytes);
    43     // figure out which test was violating a policy
    44     var testpat = new RegExp("testid=([a-z0-9_]+)");
    45     var testid = testpat.exec(data)[1];
    47     // store the violation in the persistent state
    48     var s = getState(STATE);
    49     if (!s) s = "{}";
    50     s = JSON.parse(s);
    51     if (!s) s = {};
    53     if (!s[testid]) s[testid] = 0;
    54     s[testid]++;
    55     setState(STATE, JSON.stringify(s));
    56   }
    57 }

mercurial