content/base/test/csp/file_bug836922_npolicies_ro_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_KEY = "bug836922_ro_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   });
    18   if ('results' in query) {
    19     // if asked for the received data, send it.
    20     response.setHeader("Content-Type", "text/javascript", false);
    21     if (getState(STATE_KEY)) {
    22       response.write(getState(STATE_KEY));
    23     } else {
    24       // no state has been recorded.
    25       response.write(JSON.stringify({}));
    26     }
    27   } else if ('reset' in query) {
    28     //clear state
    29     setState(STATE_KEY, JSON.stringify(null));
    30   } else {
    31     // ... otherwise, just respond "ok".
    32     response.write("null");
    34     var bodystream = new BinaryInputStream(request.bodyInputStream);
    35     var avail;
    36     var bytes = [];
    37     while ((avail = bodystream.available()) > 0)
    38       Array.prototype.push.apply(bytes, bodystream.readByteArray(avail));
    40     var data = String.fromCharCode.apply(null, bytes);
    42     // figure out which test was violating a policy
    43     var testpat = new RegExp("testid=([a-z0-9_]+)");
    44     var testid = testpat.exec(data)[1];
    46     // store the violation in the persistent state
    47     var s = JSON.parse(getState(STATE_KEY) || "{}");
    48     s[testid] ? s[testid]++ : s[testid] = 1;
    49     setState(STATE_KEY, JSON.stringify(s));
    50   }
    51 }

mercurial