summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/referrer.sjs
blob: 8e80eb7fa328d7afb3ba59eb83608fb4263dcb8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// A 1x1 PNG image.
// Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
const IMAGE = atob(
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
    "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII="
);

const IFRAME =
  "<!DOCTYPE html>\n" +
  "<script>\n" +
  "onmessage = event => {\n" +
  "parent.postMessage(document.referrer, '*');\n" +
  "};\n" +
  "</script>";

function handleRequest(aRequest, aResponse) {
  aResponse.setStatusLine(aRequest.httpVersion, 200);

  let key;
  if (aRequest.queryString.includes("what=script")) {
    key = "script";
  } else if (aRequest.queryString.includes("what=image")) {
    key = "image";
  } else {
    key = "iframe";
  }

  if (aRequest.queryString.includes("result")) {
    aResponse.write(getState(key));
    setState(key, "");
    return;
  }

  if (aRequest.hasHeader("Referer")) {
    let referrer = aRequest.getHeader("Referer");
    setState(key, referrer);
  }

  if (key == "script") {
    aResponse.setHeader("Content-Type", "text/javascript", false);
    aResponse.write("42;");
  } else if (key == "image") {
    aResponse.setHeader("Content-Type", "image/png", false);
    aResponse.write(IMAGE);
  } else {
    aResponse.setHeader("Content-Type", "text/html", false);
    aResponse.write(IFRAME);
  }
}