summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_referrer_policy.js
blob: 18b1cb3a1680e4cea36ee49a1257b05ac89bdd30 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";

const ReferrerInfo = Components.Constructor(
  "@mozilla.org/referrer-info;1",
  "nsIReferrerInfo",
  "init"
);

function test_policy(test) {
  info("Running test: " + test.toSource());

  var prefs = Services.prefs;
  if (test.defaultReferrerPolicyPref !== undefined) {
    prefs.setIntPref(
      "network.http.referer.defaultPolicy",
      test.defaultReferrerPolicyPref
    );
  } else {
    prefs.setIntPref("network.http.referer.defaultPolicy", 3);
  }

  if (test.disallowRelaxingDefault) {
    prefs.setBoolPref(
      "network.http.referer.disallowCrossSiteRelaxingDefault",
      test.disallowRelaxingDefault
    );
  } else {
    prefs.setBoolPref(
      "network.http.referer.disallowCrossSiteRelaxingDefault",
      false
    );
  }

  var uri = NetUtil.newURI(test.url);
  var chan = NetUtil.newChannel({
    uri,
    loadUsingSystemPrincipal: true,
  });

  var referrer = NetUtil.newURI(test.referrer);
  chan.QueryInterface(Ci.nsIHttpChannel);
  chan.referrerInfo = new ReferrerInfo(test.policy, true, referrer);

  if (test.expectedReferrerSpec === undefined) {
    try {
      chan.getRequestHeader("Referer");
      do_throw("Should not find a Referer header!");
    } catch (e) {}
  } else {
    var header = chan.getRequestHeader("Referer");
    Assert.equal(header, test.expectedReferrerSpec);
  }
}

const nsIReferrerInfo = Ci.nsIReferrerInfo;
// Assuming cross origin because we have no triggering principal available
var gTests = [
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 0,
    url: "https://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: undefined,
  },
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 1,
    url: "http://test.example/foo",
    referrer: "http://test1.example/referrer",
    expectedReferrerSpec: undefined,
  },
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 2,
    url: "https://sub1.\xe4lt.example/foo",
    referrer: "https://sub1.\xe4lt.example/referrer",
    expectedReferrerSpec: "https://sub1.xn--lt-uia.example/",
  },
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 2,
    url: "https://test.example/foo",
    referrer: "https://test1.example/referrer",
    expectedReferrerSpec: "https://test1.example/",
  },
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 3,
    url: "https://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: "https://test.example/referrer",
  },
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 3,
    url: "https://sub1.\xe4lt.example/foo",
    referrer: "https://sub1.\xe4lt.example/referrer",
    expectedReferrerSpec: "https://sub1.xn--lt-uia.example/referrer",
  },
  {
    policy: nsIReferrerInfo.EMPTY,
    defaultReferrerPolicyPref: 3,
    url: "http://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: undefined,
  },
  {
    policy: nsIReferrerInfo.NO_REFERRER,
    url: "https://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: undefined,
  },
  {
    policy: nsIReferrerInfo.ORIGIN,
    url: "https://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: "https://test.example/",
  },
  {
    policy: nsIReferrerInfo.ORIGIN,
    url: "https://sub1.\xe4lt.example/foo",
    referrer: "https://sub1.\xe4lt.example/referrer",
    expectedReferrerSpec: "https://sub1.xn--lt-uia.example/",
  },
  {
    policy: nsIReferrerInfo.UNSAFE_URL,
    url: "https://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: "https://test.example/referrer",
  },
  {
    policy: nsIReferrerInfo.UNSAFE_URL,
    url: "https://sub1.\xe4lt.example/foo",
    referrer: "https://sub1.\xe4lt.example/referrer",
    expectedReferrerSpec: "https://sub1.xn--lt-uia.example/referrer",
  },
  {
    policy: nsIReferrerInfo.UNSAFE_URL,
    url: "http://test.example/foo",
    referrer: "https://test.example/referrer",
    expectedReferrerSpec: "https://test.example/referrer",
  },
  {
    policy: nsIReferrerInfo.UNSAFE_URL,
    url: "http://sub1.\xe4lt.example/foo",
    referrer: "https://sub1.\xe4lt.example/referrer",
    expectedReferrerSpec: "https://sub1.xn--lt-uia.example/referrer",
  },
];

function run_test() {
  gTests.forEach(test => test_policy(test));
  Services.prefs.clearUserPref("network.http.referer.disallowRelaxingDefault");
}