content/base/test/csp/file_bug836922_npolicies_violation.sjs

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial