summaryrefslogtreecommitdiff
path: root/dom/base/test/referrerHelper.js
blob: 207bf5f15f64b877c20a645a96d9e6186ad647e6 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
 * Listen for notifications from the child.
 * These are sent in case of error, or when the loads we await have completed.
 */
window.addEventListener("message", function(event) {
    if (event.data == "childLoadComplete") {
      // all loads happen, continue the test.
      advance();
    } else if (event.data == "childOverload") {
      // too many loads happened in a test frame, abort.
      ok(false, "Too many load handlers called in test.");
      SimpleTest.finish();
    } else if (event.data.indexOf("fail-") == 0) {
      // something else failed in the test frame, abort.
      ok(false, "Child failed the test with error " + event.data.substr(5));
      SimpleTest.finish();
    }});


/**
 * helper to perform an XHR.
 */
function doXHR(url, onSuccess, onFail) {
  var xhr = new XMLHttpRequest();
  xhr.onload = function () {
    if (xhr.status == 200) {
      onSuccess(xhr);
    } else if (xhr.status == 418) {
      // Ignore HSTS priming responses
      return;
    } else {
      onFail(xhr);
    }
  };
  xhr.open('GET', url, true);
  xhr.send(null);
}



/**
 * This triggers state-resetting on the counter server.
 */
function resetCounter() {
  doXHR('/tests/dom/base/test/bug704320_counter.sjs?reset',
        advance,
        function(xhr) {
          ok(false, "Need to be able to reset the request counter");
          SimpleTest.finish();
        });
}

/**
 * Grabs the results via XHR and passes to checker.
 */
function checkIndividualResults(testname, expected) {
  doXHR('/tests/dom/base/test/bug704320_counter.sjs?results',
        function(xhr) {
          var results = JSON.parse(xhr.responseText);
          info(xhr.responseText);

          ok('img' in results,
              testname + " test: some image loads required in results object.");
          is(results['img'].count, 2,
            testname + " Test: Expected 2 loads for image requests.");

          expected.forEach(function (ref) {
            ok(results['img'].referrers.indexOf(ref) >= 0,
                testname + " Test: Expected " + ref + " referrer policy in test, results were " +
                JSON.stringify(results['img'].referrers) +".");
            });
          advance();
        },
        function(xhr) {
          ok(false, "Can't get results from the counter server.");
          SimpleTest.finish();
        });
}

/**
 * Grabs the results via XHR and checks them
 */
function checkExpectedGlobalResults() {
  var url = 'bug704320.sjs?action=get-test-results';
  doXHR(url,
	function(xhr) {
	      var response = JSON.parse(xhr.response);

	      for (type in response) {
		for (scheme in response[type]) {
		  for (policy in response[type][scheme]) {
		    var expectedResult = EXPECTED_RESULTS[type] === undefined ?
		      	EXPECTED_RESULTS['default'][scheme][policy] :
		      	EXPECTED_RESULTS[type][scheme][policy];
		    is(response[type][scheme][policy], expectedResult, type + ' ' + scheme + ' ' + policy);
		  }
		}
	      }
		advance();
	},
	function(xhr) {
          	ok(false, "Can't get results from the counter server.");
		SimpleTest.finish();
	});
}


var EXPECTED_RESULTS = {
  // From docshell/base/nsDocShell.cpp:
  //   "If the document containing the hyperlink being audited was not retrieved
  //    over an encrypted connection and its address does not have the same
  //    origin as "ping URL", send a referrer."
  'link-ping': {
    // Same-origin
    'http-to-http': {
      'no-referrer': '',
      'unsafe-url': '',
      'origin': '',
      'origin-when-cross-origin': '',
      'no-referrer-when-downgrade': '',
      'same-origin': '',
      'strict-origin': '',
      'strict-origin-when-cross-origin':''
    },
    'http-to-https': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=https&policy=unsafe-url',
      'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=https&policy=no-referrer-when-downgrade',
      'same-origin': '',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/'
    },
    // Encrypted and not same-origin
    'https-to-http': {
      'no-referrer': '',
      'unsafe-url': '',
      'origin': '',
      'origin-when-cross-origin': '',
      'no-referrer-when-downgrade': '',
      'same-origin': '',
      'strict-origin': '',
      'strict-origin-when-cross-origin':''
    },
    // Encrypted
    'https-to-https': {
      'no-referrer': '',
      'unsafe-url': '',
      'origin': '',
      'origin-when-cross-origin': '',
      'no-referrer-when-downgrade': '',
      'same-origin': '',
      'strict-origin': '',
      'strict-origin-when-cross-origin':''
    }
  },
  // form is tested in a 2nd level iframe.
  'form': {
    'http-to-http': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=unsafe-url&type=form',
      'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=origin-when-cross-origin&type=form',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=no-referrer-when-downgrade&type=form',
      'same-origin': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=same-origin&type=form',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=strict-origin-when-cross-origin&type=form'
    },
    'http-to-https': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=https&policy=unsafe-url&type=form',
      'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=https&policy=no-referrer-when-downgrade&type=form',
      'same-origin': '',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/'
    },
    'https-to-http': {
      'no-referrer': '',
      'unsafe-url': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=http&policy=unsafe-url&type=form',
      'origin': 'https://example.com/',
      'origin-when-cross-origin': 'https://example.com/',
      'no-referrer-when-downgrade': '',
      'same-origin': '',
      'strict-origin': '',
      'strict-origin-when-cross-origin':''
    },
    'https-to-https': {
      'no-referrer': '',
      'unsafe-url': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=unsafe-url&type=form',
      'origin': 'https://example.com/',
     'origin-when-cross-origin': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=origin-when-cross-origin&type=form',
      'no-referrer-when-downgrade': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=no-referrer-when-downgrade&type=form',
      'same-origin': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=same-origin&type=form',
      'strict-origin': 'https://example.com/',
      'strict-origin-when-cross-origin':'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=strict-origin-when-cross-origin&type=form'
    }
  },
  // window.location is tested in a 2nd level iframe.
  'window.location': {
    'http-to-http': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=unsafe-url&type=window.location',
     'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=origin-when-cross-origin&type=window.location',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=no-referrer-when-downgrade&type=window.location',
      'same-origin': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=same-origin&type=window.location',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=http&policy=strict-origin-when-cross-origin&type=window.location'
    },
    'http-to-https': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=https&policy=unsafe-url&type=window.location',
      'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=http&scheme-to=https&policy=no-referrer-when-downgrade&type=window.location',
      'same-origin': '',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/'
    },
    'https-to-http': {
      'no-referrer': '',
      'unsafe-url': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=http&policy=unsafe-url&type=window.location',
      'origin': 'https://example.com/',
      'origin-when-cross-origin': 'https://example.com/',
      'no-referrer-when-downgrade': '',
      'same-origin': '',
      'strict-origin': '',
      'strict-origin-when-cross-origin':''
    },
    'https-to-https': {
      'no-referrer': '',
      'unsafe-url': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=unsafe-url&type=window.location',
      'origin': 'https://example.com/',
      'origin-when-cross-origin': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=origin-when-cross-origin&type=window.location',
      'no-referrer-when-downgrade': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=no-referrer-when-downgrade&type=window.location',
      'same-origin': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=same-origin&type=window.location',
      'strict-origin': 'https://example.com/',
      'strict-origin-when-cross-origin':'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-2nd-level-iframe&scheme-from=https&scheme-to=https&policy=strict-origin-when-cross-origin&type=window.location'
    }
  },
  'default': {
    'http-to-http': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=http&policy=unsafe-url',
      'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=http&policy=origin-when-cross-origin',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=http&policy=no-referrer-when-downgrade',
      'same-origin': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=http&policy=same-origin',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=http&policy=strict-origin-when-cross-origin'
    },
    'http-to-https': {
      'no-referrer': '',
      'unsafe-url': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=https&policy=unsafe-url',
      'origin': 'http://example.com/',
      'origin-when-cross-origin': 'http://example.com/',
      'no-referrer-when-downgrade': 'http://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=http&scheme-to=https&policy=no-referrer-when-downgrade',
      'same-origin': '',
      'strict-origin': 'http://example.com/',
      'strict-origin-when-cross-origin':'http://example.com/'
    },
    'https-to-http': {
      'no-referrer': '',
      'unsafe-url': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=https&scheme-to=http&policy=unsafe-url',
      'origin': 'https://example.com/',
      'origin-when-cross-origin': 'https://example.com/',
      'no-referrer-when-downgrade': '',
      'same-origin': '',
      'strict-origin': '',
      'strict-origin-when-cross-origin':''
    },
    'https-to-https': {
      'no-referrer': '',
      'unsafe-url': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=https&scheme-to=https&policy=unsafe-url',
      'origin': 'https://example.com/',
      'origin-when-cross-origin': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=https&scheme-to=https&policy=origin-when-cross-origin',
      'no-referrer-when-downgrade': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=https&scheme-to=https&policy=no-referrer-when-downgrade',
      'same-origin': 'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=https&scheme-to=https&policy=same-origin',
      'strict-origin': 'https://example.com/',
      'strict-origin-when-cross-origin':'https://example.com/tests/dom/base/test/bug704320.sjs?action=create-1st-level-iframe&scheme-from=https&scheme-to=https&policy=strict-origin-when-cross-origin'
    }
  }
};