LEFT | RIGHT |
1 (function() | 1 (function() |
2 { | 2 { |
3 let server = null; | 3 let server = null; |
4 let frame = null; | 4 let frame = null; |
5 let requestNotifier = null; | 5 let requestNotifier = null; |
| 6 let httpProtocol = null; |
6 | 7 |
7 module("Content policy", { | 8 module("Content policy", { |
8 setup: function() | 9 setup: function() |
9 { | 10 { |
10 prepareFilterComponents.call(this); | 11 prepareFilterComponents.call(this); |
11 preparePrefs.call(this); | 12 preparePrefs.call(this); |
12 | 13 |
13 server = new nsHttpServer(); | 14 server = new nsHttpServer(); |
14 server.start(1234); | 15 server.start(1234); |
15 | 16 |
16 frame = document.createElementNS("http://www.mozilla.org/keymaster/gatekee
per/there.is.only.xul", "iframe"); | 17 frame = document.createElementNS("http://www.mozilla.org/keymaster/gatekee
per/there.is.only.xul", "iframe"); |
17 frame.setAttribute("type", "content"); | 18 frame.setAttribute("type", "content"); |
18 frame.style.visibility = "collapse"; | 19 frame.style.visibility = "collapse"; |
19 document.body.appendChild(frame); | 20 document.body.appendChild(frame); |
20 | 21 |
21 requestNotifier = new RequestNotifier(frame.contentWindow, onPolicyHit); | 22 requestNotifier = new RequestNotifier(frame.contentWindow, onPolicyHit); |
| 23 |
| 24 httpProtocol = Utils.httpProtocol; |
| 25 Utils.httpProtocol = {userAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64;
rv:30.0) Gecko/20100101 Firefox/30.0"}; |
22 }, | 26 }, |
23 teardown: function() | 27 teardown: function() |
24 { | 28 { |
25 restoreFilterComponents.call(this); | 29 restoreFilterComponents.call(this); |
26 restorePrefs.call(this); | 30 restorePrefs.call(this); |
27 | 31 |
28 stop(); | 32 stop(); |
29 server.stop(function() | 33 server.stop(function() |
30 { | 34 { |
31 frame.parentElement.removeChild(frame); | 35 frame.parentElement.removeChild(frame); |
32 requestNotifier.shutdown(); | 36 requestNotifier.shutdown(); |
33 | 37 |
34 server = null; | 38 server = null; |
35 frame = null; | 39 frame = null; |
36 requestNotifier = null; | 40 requestNotifier = null; |
37 | 41 |
38 start(); | 42 start(); |
39 }); | 43 }); |
| 44 |
| 45 Utils.httpProtocol = httpProtocol; |
40 } | 46 } |
41 }); | 47 }); |
| 48 |
| 49 /* |
| 50 -----BEGIN RSA PRIVATE KEY----- |
| 51 MIIBOQIBAAJBALZc50pEXnz9TSRozwM04rryuaXl/wgUFqV9FHq8HDlkdKvRU0hX |
| 52 hb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQJBALRxYs5irhgAz2b6afOj |
| 53 TcFr0PRtipckwW/IPw5euZKyvswEJt/tWDv4OdmDnRe8FSy6FG2Got3zxvaxYdc3 |
| 54 AXkCIQDfFGcytIVq3sbdF3lmhzcXf29R4Hrxg/eoByAKabxknwIhANFGSNMOGPt6 |
| 55 JRajfB9XmsltQJzbkr2sfHgjMN2FLM49AiAH6tt2yz1o+5snQawHXYkxBk7XIxZ5 |
| 56 9+sURZx3giUzlQIfXF+pxX9zh41i0ZtYLn181WxkGNjS7OY2CtF9wEoIfQIgcHuf |
| 57 shh1qrvuKiXnD9b72PF676laKdzxzX5rX6cZZLA= |
| 58 -----END RSA PRIVATE KEY----- |
| 59 */ |
| 60 let publickey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl/
wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ"; |
| 61 |
| 62 /** |
| 63 * Content: |
| 64 * /test\0127.0.0.1:1234\0Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gec
ko/20100101 Firefox/30.0 |
| 65 */ |
| 66 let adblockkey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl
/wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ==_gM4C/j8KkD2
byPeP+THXk1GbLTUm5y+5jbdhcMtnzPMgImIfge0dGCtfU9cxLpe8BnqnEGNhTxpuu4pZxjOHYQ=="; |
42 | 67 |
43 let tests = [ | 68 let tests = [ |
44 [ | 69 [ |
45 "HTML image with relative URL", | 70 "HTML image with relative URL", |
46 '<img src="test.gif">', | 71 '<img src="test.gif">', |
47 "http://127.0.0.1:1234/test.gif", "image", false, false | 72 "http://127.0.0.1:1234/test.gif", "image", false, false |
48 ], | 73 ], |
49 [ | 74 [ |
50 "HTML image with absolute URL", | 75 "HTML image with absolute URL", |
51 '<img src="http://localhost:1234/test.gif">', | 76 '<img src="http://localhost:1234/test.gif">', |
(...skipping 18 matching lines...) Expand all Loading... |
70 "Dynamically inserted image button", | 95 "Dynamically inserted image button", |
71 '<div id="insert"></div>' + | 96 '<div id="insert"></div>' + |
72 '<script>' + | 97 '<script>' + |
73 'window.addEventListener("DOMContentLoaded", function()' + | 98 'window.addEventListener("DOMContentLoaded", function()' + |
74 '{' + | 99 '{' + |
75 'var div = document.getElementById("insert");' + | 100 'var div = document.getElementById("insert");' + |
76 'div.innerHTML = \'<input type="image" id="image" src="test.gif">\';'
+ | 101 'div.innerHTML = \'<input type="image" id="image" src="test.gif">\';'
+ |
77 'var image = document.getElementById("image");' + | 102 'var image = document.getElementById("image");' + |
78 'image.onload = image.onerror = function ()' + | 103 'image.onload = image.onerror = function ()' + |
79 '{' + | 104 '{' + |
80 'document.dispatchEvent(new CustomEvent("frameready", {bubbles: true
}));' + | 105 'document.dispatchEvent(new CustomEvent("abp:frameready", {bubbles:
true}));' + |
81 '};' + | 106 '};' + |
82 '}, false);' + | 107 '}, false);' + |
83 '</script>', | 108 '</script>', |
84 "http://127.0.0.1:1234/test.gif", "image", false, true | 109 "http://127.0.0.1:1234/test.gif", "image", false, true |
85 ], | 110 ], |
86 [ | 111 [ |
87 "CSS background-image", | 112 "CSS background-image", |
88 '<div style="background-image: url(test.gif)"></div>', | 113 '<div style="background-image: url(test.gif)"></div>', |
89 "http://127.0.0.1:1234/test.gif", "image", false, false | 114 "http://127.0.0.1:1234/test.gif", "image", false, false |
90 ], | 115 ], |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 'xmlDoc.async = false;' + | 210 'xmlDoc.async = false;' + |
186 'xmlDoc.load("test.xml");' + | 211 'xmlDoc.load("test.xml");' + |
187 '}' + | 212 '}' + |
188 'catch(e){}' + | 213 'catch(e){}' + |
189 '</script>', | 214 '</script>', |
190 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false | 215 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false |
191 ], | 216 ], |
192 [ | 217 [ |
193 "Web worker", | 218 "Web worker", |
194 '<script>' + | 219 '<script>' + |
195 'var e = new CustomEvent(\'frameready\', {bubbles: true});' + | 220 'var e = new CustomEvent(\'abp:frameready\', {bubbles: true});' + |
196 'try' + | 221 'try' + |
197 '{' + | 222 '{' + |
198 'var worker = new Worker("test.js");' + | 223 'var worker = new Worker("test.js");' + |
199 'worker.onerror = function(event)' + | 224 'worker.onerror = function(event)' + |
200 '{' + | 225 '{' + |
201 'event.preventDefault();' + | 226 'event.preventDefault();' + |
202 'document.dispatchEvent(e);' + | 227 'document.dispatchEvent(e);' + |
203 '};' + | 228 '};' + |
204 '}' + | 229 '}' + |
205 'catch (x)' + | 230 'catch (x)' + |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 { | 279 { |
255 return; | 280 return; |
256 } | 281 } |
257 if (item.filter instanceof WhitelistFilter) | 282 if (item.filter instanceof WhitelistFilter) |
258 return; | 283 return; |
259 | 284 |
260 if (policyHits.length > 0) | 285 if (policyHits.length > 0) |
261 { | 286 { |
262 // Ignore duplicate policy calls (possible due to prefetching) | 287 // Ignore duplicate policy calls (possible due to prefetching) |
263 let [prevWnd, prevNode, prevItem] = policyHits[policyHits.length - 1]; | 288 let [prevWnd, prevNode, prevItem] = policyHits[policyHits.length - 1]; |
264 if (prevWnd == wnd && prevItem.location == item.location && prevItem.type
== item.type && prevItem.docDomain == item.docDomain) | 289 if (prevWnd == wnd && prevItem.location == item.location && prevItem.type
== item.type && prevItem.docDomain == item.docDomain) |
265 policyHits.pop(); | 290 policyHits.pop(); |
266 } | 291 } |
267 policyHits.push([wnd, node, item]); | 292 policyHits.push([wnd, node, item]); |
268 } | 293 } |
269 | 294 |
270 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, e
xplicitEvent], stage) | 295 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, e
xplicitEvent], stage) |
271 { | 296 { |
272 defaultMatcher.clear(); | 297 defaultMatcher.clear(); |
273 | 298 |
274 if (stage > 1) | 299 if (stage > 1) |
275 defaultMatcher.add(Filter.fromText(expectedURL)); | 300 defaultMatcher.add(Filter.fromText(expectedURL)); |
276 if (stage == 3) | 301 if (stage == 3) |
277 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document")); | 302 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document")); |
278 if (stage == 4) | 303 if (stage == 4) |
279 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document")); | 304 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document")); |
| 305 if (stage == 5) |
| 306 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document,site
key=" + publickey)); |
280 | 307 |
281 if (!explicitEvent) | 308 if (!explicitEvent) |
282 { | 309 { |
283 if (body.indexOf("2000/svg") >= 0) | 310 if (body.indexOf("2000/svg") >= 0) |
284 { | 311 { |
285 // SVG image: add an onload attribute to the document element | 312 // SVG image: add an onload attribute to the document element |
286 body = body.replace(/(<svg\b)/, '$1 onload="this.dispatchEvent(new Custo
mEvent(\'frameready\', {bubbles: true}));"'); | 313 body = body.replace(/(<svg\b)/, '$1 onload="this.dispatchEvent(new Custo
mEvent(\'abp:frameready\', {bubbles: true}));"'); |
287 } | 314 } |
288 else | 315 else |
289 { | 316 { |
290 // HTML data: wrap it into a <body> tag | 317 // HTML data: wrap it into a <body> tag |
291 body = '<body onload="this.dispatchEvent(new CustomEvent(\'frameready\',
{bubbles: true}));">' + body + '</body>'; | 318 body = '<body onload="this.dispatchEvent(new CustomEvent(\'abp:frameread
y\', {bubbles: true}));">' + body + '</body>'; |
292 } | 319 } |
293 } | 320 } |
294 | 321 |
295 let serverHit = false; | 322 let serverHit = false; |
296 server.registerPathHandler("/test", function(metadata, response) | 323 server.registerPathHandler("/test", function(metadata, response) |
297 { | 324 { |
298 response.setStatusLine("1.1", "200", "OK"); | 325 response.setStatusLine("1.1", "200", "OK"); |
299 | 326 |
300 let contentType = "text/html"; | 327 let contentType = "text/html"; |
301 if (body.indexOf("2000/svg") >= 0) | 328 if (body.indexOf("2000/svg") >= 0) |
| 329 { |
302 contentType = "image/svg+xml"; | 330 contentType = "image/svg+xml"; |
| 331 body = body.replace(/^<svg/, "<svg data-adblockkey='" + adblockkey + "'"
); |
| 332 } |
| 333 else |
| 334 body = "<html data-adblockkey='" + adblockkey + "'>" + body + "</html>"; |
303 response.setHeader("Content-Type", contentType + "; charset=utf-8"); | 335 response.setHeader("Content-Type", contentType + "; charset=utf-8"); |
304 | 336 |
305 response.bodyOutputStream.write(body, body.length); | 337 response.bodyOutputStream.write(body, body.length); |
306 }); | 338 }); |
307 server.registerPathHandler("/redirect.gif", function(metadata, response) | 339 server.registerPathHandler("/redirect.gif", function(metadata, response) |
308 { | 340 { |
309 response.setStatusLine("1.1", "302", "Moved Temporarily"); | 341 response.setStatusLine("1.1", "302", "Moved Temporarily"); |
310 response.setHeader("Location", "http://127.0.0.1:1234/test.gif"); | 342 response.setHeader("Location", "http://127.0.0.1:1234/test.gif"); |
311 }); | 343 }); |
312 server.registerPathHandler("/redirect2.gif", function(metadata, response) | 344 server.registerPathHandler("/redirect2.gif", function(metadata, response) |
(...skipping 11 matching lines...) Expand all Loading... |
324 var error = "<b>Not found...<b>"; | 356 var error = "<b>Not found...<b>"; |
325 response.bodyOutputStream.write(error, error.length); | 357 response.bodyOutputStream.write(error, error.length); |
326 }); | 358 }); |
327 | 359 |
328 policyHits = []; | 360 policyHits = []; |
329 var callback = function() | 361 var callback = function() |
330 { | 362 { |
331 let expectedStatus = "allowed"; | 363 let expectedStatus = "allowed"; |
332 if (stage == 3) | 364 if (stage == 3) |
333 equal(policyHits.length, 0, "Number of policy hits"); | 365 equal(policyHits.length, 0, "Number of policy hits"); |
334 else | 366 // We cannot rely on the correctness of policy hits for sitekey filters du
e to blocking |
| 367 // filter hits being counted even if the resource doesn't end up getting b
locked |
| 368 else if (stage != 5) |
335 { | 369 { |
336 equal(policyHits.length, 1, "Number of policy hits"); | 370 equal(policyHits.length, 1, "Number of policy hits"); |
337 if (policyHits.length == 1) | 371 if (policyHits.length == 1) |
338 { | 372 { |
339 let [wnd, node, item] = policyHits[0]; | 373 let [wnd, node, item] = policyHits[0]; |
340 | 374 |
341 equal(item.location, expectedURL, "Request URL"); | 375 equal(item.location, expectedURL, "Request URL"); |
342 | 376 |
343 expectedStatus = (stage == 1 ? "allowed" : "blocked"); | 377 expectedStatus = (stage == 1 ? "allowed" : "blocked"); |
344 let actualStatus = (item.filter ? "blocked" : "allowed"); | 378 let actualStatus = (item.filter ? "blocked" : "allowed"); |
345 | 379 |
346 equal(actualStatus, expectedStatus, "Request blocked"); | 380 equal(actualStatus, expectedStatus, "Request blocked"); |
347 equal(item.typeDescr.toLowerCase(), expectedType, "Request type"); | 381 equal(item.typeDescr.toLowerCase(), expectedType, "Request type"); |
348 equal(item.thirdParty, expectedThirdParty, "Third-party flag"); | 382 equal(item.thirdParty, expectedThirdParty, "Third-party flag"); |
349 equal(item.docDomain, "127.0.0.1", "Document domain"); | 383 equal(item.docDomain, "127.0.0.1", "Document domain"); |
350 } | 384 } |
351 } | 385 } |
352 server.registerPathHandler(expectedURL.replace(/http:\/\/[^\/]+/, ""), nul
l); | 386 server.registerPathHandler(expectedURL.replace(/http:\/\/[^\/]+/, ""), nul
l); |
353 equal(serverHit, expectedStatus == "allowed", "Request received by server"
); | 387 equal(serverHit, expectedStatus == "allowed", "Request received by server"
); |
354 | 388 |
355 frame.removeEventListener("frameready", callback, false); | 389 frame.removeEventListener("abp:frameready", callback, false); |
356 | 390 |
357 start(); | 391 start(); |
358 }; | 392 }; |
359 frame.addEventListener("frameready", callback, false, true); | 393 frame.addEventListener("abp:frameready", callback, false, true); |
360 frame.setAttribute("src", "http://127.0.0.1:1234/test"); | 394 frame.setAttribute("src", "http://127.0.0.1:1234/test"); |
361 } | 395 } |
362 | 396 |
363 let stageDescriptions = { | 397 let stageDescriptions = { |
364 1: "running without filters", | 398 1: "running without filters", |
365 2: "running with filter %S", | 399 2: "running with filter %S", |
366 3: "running with filter %S and site exception", | 400 3: "running with filter %S and site exception", |
367 4: "running with filter %S and exception not applicable to sites", | 401 4: "running with filter %S and exception not applicable to sites", |
| 402 5: "running with filter %S and sitekey exception" |
368 }; | 403 }; |
369 | 404 |
370 for (let test = 0; test < tests.length; test++) | 405 for (let test = 0; test < tests.length; test++) |
371 { | 406 { |
372 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar
ty] = tests[test]; | 407 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar
ty] = tests[test]; |
373 for (let stage = 1; stage in stageDescriptions; stage++) | 408 for (let stage = 1; stage in stageDescriptions; stage++) |
374 { | 409 { |
375 let stageDescription = stageDescriptions[stage]; | 410 let stageDescription = stageDescriptions[stage]; |
376 if (stageDescription.indexOf("%S") >= 0) | 411 if (stageDescription.indexOf("%S") >= 0) |
377 stageDescription = stageDescription.replace("%S", expectedURL); | 412 stageDescription = stageDescription.replace("%S", expectedURL); |
378 | 413 |
379 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t
est], stage)); | 414 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t
est], stage)); |
380 } | 415 } |
381 } | 416 } |
382 })(); | 417 })(); |
LEFT | RIGHT |