summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/test_xhr_forbidden_headers.html
blob: d393544b796f382d3b97a574045e32230d008838 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=308484
-->
<head>
  <title>Test for Bug 308484</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=308484">Mozilla Bug 308484</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 308484 **/

var headers = [
  "aCCept-chaRset",
  "acCePt-eNcoDing",
  "aCcEsS-cOnTrOl-ReQuEsT-mEtHoD",
  "aCcEsS-cOnTrOl-ReQuEsT-hEaDeRs",
  "coNnEctIon",
  "coNtEnt-LEngth",
  "CoOKIe",
  "cOOkiE2",
  "DATE",
  "dNT",
  "exPeCt",
  "hOSt",
  "keep-alive",
  "oRiGiN",
  "reFERer",
  "te",
  "trAiLer",
  "trANsfEr-eNcoDiNg",
  "uPGraDe",
  "viA",
  "pRoxy-",
  "sEc-",
  "proxy-fOobar",
  "sec-bAZbOx"
];
var i, request;

function  startTest() {
  // Try setting headers in unprivileged context
  request = new XMLHttpRequest();
  request.open("GET", window.location.href);
  for (i = 0; i < headers.length; i++)
    request.setRequestHeader(headers[i], "test" + i);
  request.send(); // headers aren't set on the channel until send()

  // Read out headers
  channel = SpecialPowers.wrap(request).channel.QueryInterface(SpecialPowers.Ci.nsIHttpChannel);
  for (i = 0; i < headers.length; i++) {
    // Retrieving Content-Length will throw an exception
    value = null;
    try {
      value = channel.getRequestHeader(headers[i]);
    }
    catch(e) {}

    isnot(value, "test" + i, "Setting " + headers[i] + " header in unprivileged context");
  }

  // Try setting headers in privileged context
  request = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  request.open("GET", window.location.href);
  for (i = 0; i < headers.length; i++)
    request.setRequestHeader(headers[i], `http://test${i}/`);
  request.send(); // headers aren't set on the channel until send()

  // Read out headers
  var channel = SpecialPowers.wrap(request).channel.QueryInterface(SpecialPowers.Ci.nsIHttpChannel);
  for (i = 0; i < headers.length; i++) {
    var value = channel.getRequestHeader(headers[i]);
    is(value, `http://test${i}/`, "Setting " + headers[i] + " header in privileged context");
  }

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();

addLoadEvent(function() {
   SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], startTest);
});
</script>
</pre>
</body>
</html>