OLD | NEW |
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 let httpProtocol = null; |
7 | 7 |
8 module("Content policy", { | 8 module("Content policy", { |
9 setup: function() | 9 setup: function() |
10 { | 10 { |
11 prepareFilterComponents.call(this); | 11 prepareFilterComponents.call(this); |
12 preparePrefs.call(this); | 12 preparePrefs.call(this); |
13 | 13 |
14 server = new nsHttpServer(); | 14 server = new nsHttpServer(); |
15 server.start(1234); | 15 server.start(1234); |
16 | 16 |
17 frame = document.createElement("iframe"); | 17 frame = document.createElementNS("http://www.mozilla.org/keymaster/gatekee
per/there.is.only.xul", "iframe"); |
| 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(window, onPolicyHit); | 22 requestNotifier = new RequestNotifier(frame.contentWindow, onPolicyHit); |
22 | 23 |
23 httpProtocol = Utils.httpProtocol; | 24 httpProtocol = Utils.httpProtocol; |
24 Utils.httpProtocol = {userAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64;
rv:30.0) Gecko/20100101 Firefox/30.0"}; | 25 Utils.httpProtocol = {userAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64;
rv:30.0) Gecko/20100101 Firefox/30.0"}; |
25 }, | 26 }, |
26 teardown: function() | 27 teardown: function() |
27 { | 28 { |
28 restoreFilterComponents.call(this); | 29 restoreFilterComponents.call(this); |
29 restorePrefs.call(this); | 30 restorePrefs.call(this); |
30 | 31 |
31 stop(); | 32 stop(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 '<iframe src="data:text/html,%3Cinput%20type%3D%22image%22%20src%3D%22http
%3A%2F%2F127.0.0.1:1234%2Ftest.gif%22%3E"></iframe>', | 86 '<iframe src="data:text/html,%3Cinput%20type%3D%22image%22%20src%3D%22http
%3A%2F%2F127.0.0.1:1234%2Ftest.gif%22%3E"></iframe>', |
86 "http://127.0.0.1:1234/test.gif", "image", false, false | 87 "http://127.0.0.1:1234/test.gif", "image", false, false |
87 ], | 88 ], |
88 [ | 89 [ |
89 "HTML image button inside a nested frame", | 90 "HTML image button inside a nested frame", |
90 '<iframe src="data:text/html,%3Ciframe%20src%3D%22data%3Atext%2Fhtml%2C%25
3Cinput%2520type%253D%2522image%2522%2520src%253D%2522http%253A%252F%252F127.0.0
.1%3A1234%252Ftest.gif%2522%253E%22%3E%3C%2Fiframe%3E"></iframe>', | 91 '<iframe src="data:text/html,%3Ciframe%20src%3D%22data%3Atext%2Fhtml%2C%25
3Cinput%2520type%253D%2522image%2522%2520src%253D%2522http%253A%252F%252F127.0.0
.1%3A1234%252Ftest.gif%2522%253E%22%3E%3C%2Fiframe%3E"></iframe>', |
91 "http://127.0.0.1:1234/test.gif", "image", false, false | 92 "http://127.0.0.1:1234/test.gif", "image", false, false |
92 ], | 93 ], |
93 [ | 94 [ |
94 "Dynamically inserted image button", | 95 "Dynamically inserted image button", |
95 '<div id="insert"></div><script>window.addEventListener("DOMContentLoaded"
, function() { var div = document.getElementById("insert"); div.innerHTML = \'<i
nput type="image" id="image" src="test.gif">\'; var image = document.getElementB
yId("image"); image.onload = image.onerror = function () { parent.postMessage("l
oaded", "*"); }; }, false);<' + '/script>', | 96 '<div id="insert"></div>' + |
| 97 '<script>' + |
| 98 'window.addEventListener("DOMContentLoaded", function()' + |
| 99 '{' + |
| 100 'var div = document.getElementById("insert");' + |
| 101 'div.innerHTML = \'<input type="image" id="image" src="test.gif">\';'
+ |
| 102 'var image = document.getElementById("image");' + |
| 103 'image.onload = image.onerror = function ()' + |
| 104 '{' + |
| 105 'document.dispatchEvent(new CustomEvent("abp:frameready", {bubbles:
true}));' + |
| 106 '};' + |
| 107 '}, false);' + |
| 108 '</script>', |
96 "http://127.0.0.1:1234/test.gif", "image", false, true | 109 "http://127.0.0.1:1234/test.gif", "image", false, true |
97 ], | 110 ], |
98 [ | 111 [ |
99 "CSS background-image", | 112 "CSS background-image", |
100 '<div style="background-image: url(test.gif)"></div>', | 113 '<div style="background-image: url(test.gif)"></div>', |
101 "http://127.0.0.1:1234/test.gif", "image", false, false | 114 "http://127.0.0.1:1234/test.gif", "image", false, false |
102 ], | 115 ], |
103 [ | 116 [ |
104 "CSS cursor", | 117 "CSS cursor", |
105 '<div style="cursor: url(test.gif), pointer"></div>', | 118 '<div style="cursor: url(test.gif), pointer"></div>', |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 '<img src="redirect2.gif">', | 183 '<img src="redirect2.gif">', |
171 "http://127.0.0.1:1234/test.gif", "image", false, false | 184 "http://127.0.0.1:1234/test.gif", "image", false, false |
172 ], | 185 ], |
173 [ | 186 [ |
174 "CSS fonts", | 187 "CSS fonts", |
175 '<style type="text/css">@font-face { font-family: Test; src: url("test.otf
"); } html { font-family: Test; }</style>', | 188 '<style type="text/css">@font-face { font-family: Test; src: url("test.otf
"); } html { font-family: Test; }</style>', |
176 "http://127.0.0.1:1234/test.otf", "font", false, false | 189 "http://127.0.0.1:1234/test.otf", "font", false, false |
177 ], | 190 ], |
178 [ | 191 [ |
179 "XMLHttpRequest loading", | 192 "XMLHttpRequest loading", |
180 '<script>var request = new XMLHttpRequest();request.open("GET", "test.xml"
, false);request.send(null);</script>', | 193 '<script>' + |
| 194 'try' + |
| 195 '{' + |
| 196 'var request = new XMLHttpRequest();' + |
| 197 'request.open("GET", "test.xml", false);' + |
| 198 'request.send(null);' + |
| 199 '}' + |
| 200 'catch(e){}' + |
| 201 '</script>', |
181 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false | 202 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false |
182 ], | 203 ], |
183 [ | 204 [ |
184 "XML document loading", | 205 "XML document loading", |
185 '<script>var xmlDoc = document.implementation.createDocument(null, "root",
null);xmlDoc.async = false;xmlDoc.load("test.xml")</script>', | 206 '<script>' + |
| 207 'try' + |
| 208 '{' + |
| 209 'var xmlDoc = document.implementation.createDocument(null, "root", nul
l);' + |
| 210 'xmlDoc.async = false;' + |
| 211 'xmlDoc.load("test.xml");' + |
| 212 '}' + |
| 213 'catch(e){}' + |
| 214 '</script>', |
186 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false | 215 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false |
187 ], | 216 ], |
188 [ | 217 [ |
189 "Web worker", | 218 "Web worker", |
190 '<script>try { var worker = new Worker("test.js"); worker.onerror = functi
on() { parent.postMessage("loaded", "*"); }; } catch (e) { parent.postMessage("e
rror", "*"); }</script>', | 219 '<script>' + |
| 220 'var e = new CustomEvent(\'abp:frameready\', {bubbles: true});' + |
| 221 'try' + |
| 222 '{' + |
| 223 'var worker = new Worker("test.js");' + |
| 224 'worker.onerror = function(event)' + |
| 225 '{' + |
| 226 'event.preventDefault();' + |
| 227 'document.dispatchEvent(e);' + |
| 228 '};' + |
| 229 '}' + |
| 230 'catch (x)' + |
| 231 '{' + |
| 232 'document.dispatchEvent(e);' + |
| 233 '}' + |
| 234 '</script>', |
191 "http://127.0.0.1:1234/test.js", "script", false, true | 235 "http://127.0.0.1:1234/test.js", "script", false, true |
192 ], | 236 ], |
193 ]; | 237 ]; |
194 | 238 |
195 if (window.navigator.mimeTypes["application/x-shockwave-flash"] && window.navi
gator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) | 239 if (window.navigator.mimeTypes["application/x-shockwave-flash"] && window.navi
gator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) |
196 { | 240 { |
197 tests.push([ | 241 tests.push([ |
198 "HTML embed (Flash)", | 242 "HTML embed (Flash)", |
199 '<embed type="application/x-shockwave-flash" src="test.swf"></embed>' + | 243 '<embed type="application/x-shockwave-flash" src="test.swf"></embed>' + |
200 '<script>var r = new XMLHttpRequest();r.open("GET", "", false);r.send(nu
ll);</script>', | 244 '<script>var r = new XMLHttpRequest();r.open("GET", "", false);r.send(nu
ll);</script>', |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 if (policyHits.length > 0) | 285 if (policyHits.length > 0) |
242 { | 286 { |
243 // Ignore duplicate policy calls (possible due to prefetching) | 287 // Ignore duplicate policy calls (possible due to prefetching) |
244 let [prevWnd, prevNode, prevItem] = policyHits[policyHits.length - 1]; | 288 let [prevWnd, prevNode, prevItem] = policyHits[policyHits.length - 1]; |
245 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) |
246 policyHits.pop(); | 290 policyHits.pop(); |
247 } | 291 } |
248 policyHits.push([wnd, node, item]); | 292 policyHits.push([wnd, node, item]); |
249 } | 293 } |
250 | 294 |
251 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, w
aitForMessage], stage) | 295 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, e
xplicitEvent], stage) |
252 { | 296 { |
253 defaultMatcher.clear(); | 297 defaultMatcher.clear(); |
254 | 298 |
255 if (stage > 1) | 299 if (stage > 1) |
256 defaultMatcher.add(Filter.fromText(expectedURL)); | 300 defaultMatcher.add(Filter.fromText(expectedURL)); |
257 if (stage == 3) | 301 if (stage == 3) |
258 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document")); | 302 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document")); |
259 if (stage == 4) | 303 if (stage == 4) |
260 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document")); | 304 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document")); |
261 if (stage == 5) | 305 if (stage == 5) |
262 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document,site
key=" + publickey)); | 306 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document,site
key=" + publickey)); |
263 | 307 |
| 308 if (!explicitEvent) |
| 309 { |
| 310 if (body.indexOf("2000/svg") >= 0) |
| 311 { |
| 312 // SVG image: add an onload attribute to the document element |
| 313 body = body.replace(/(<svg\b)/, '$1 onload="this.dispatchEvent(new Custo
mEvent(\'abp:frameready\', {bubbles: true}));"'); |
| 314 } |
| 315 else |
| 316 { |
| 317 // HTML data: wrap it into a <body> tag |
| 318 body = '<body onload="this.dispatchEvent(new CustomEvent(\'abp:frameread
y\', {bubbles: true}));">' + body + '</body>'; |
| 319 } |
| 320 } |
| 321 |
264 let serverHit = false; | 322 let serverHit = false; |
265 server.registerPathHandler("/test", function(metadata, response) | 323 server.registerPathHandler("/test", function(metadata, response) |
266 { | 324 { |
267 response.setStatusLine("1.1", "200", "OK"); | 325 response.setStatusLine("1.1", "200", "OK"); |
268 | 326 |
269 let contentType = "text/html"; | 327 let contentType = "text/html"; |
270 if (body.indexOf("2000/svg") >= 0) | 328 if (body.indexOf("2000/svg") >= 0) |
271 { | 329 { |
272 contentType = "image/svg+xml"; | 330 contentType = "image/svg+xml"; |
273 body = body.replace(/^<svg/, "<svg data-adblockkey='" + adblockkey + "'"
); | 331 body = body.replace(/^<svg/, "<svg data-adblockkey='" + adblockkey + "'"
); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 379 |
322 equal(actualStatus, expectedStatus, "Request blocked"); | 380 equal(actualStatus, expectedStatus, "Request blocked"); |
323 equal(item.typeDescr.toLowerCase(), expectedType, "Request type"); | 381 equal(item.typeDescr.toLowerCase(), expectedType, "Request type"); |
324 equal(item.thirdParty, expectedThirdParty, "Third-party flag"); | 382 equal(item.thirdParty, expectedThirdParty, "Third-party flag"); |
325 equal(item.docDomain, "127.0.0.1", "Document domain"); | 383 equal(item.docDomain, "127.0.0.1", "Document domain"); |
326 } | 384 } |
327 } | 385 } |
328 server.registerPathHandler(expectedURL.replace(/http:\/\/[^\/]+/, ""), nul
l); | 386 server.registerPathHandler(expectedURL.replace(/http:\/\/[^\/]+/, ""), nul
l); |
329 equal(serverHit, expectedStatus == "allowed", "Request received by server"
); | 387 equal(serverHit, expectedStatus == "allowed", "Request received by server"
); |
330 | 388 |
331 window.removeEventListener("message", callback, false); | 389 frame.removeEventListener("abp:frameready", callback, false); |
332 frame.removeEventListener("load", callback, false); | |
333 | 390 |
334 start(); | 391 start(); |
335 }; | 392 }; |
336 frame.contentWindow.location.href = "http://127.0.0.1:1234/test"; | 393 frame.addEventListener("abp:frameready", callback, false, true); |
337 | 394 frame.setAttribute("src", "http://127.0.0.1:1234/test"); |
338 if (waitForMessage) | |
339 window.addEventListener("message", callback, false, true); | |
340 else | |
341 frame.addEventListener("load", callback, false); | |
342 } | 395 } |
343 | 396 |
344 let stageDescriptions = { | 397 let stageDescriptions = { |
345 1: "running without filters", | 398 1: "running without filters", |
346 2: "running with filter %S", | 399 2: "running with filter %S", |
347 3: "running with filter %S and site exception", | 400 3: "running with filter %S and site exception", |
348 4: "running with filter %S and exception not applicable to sites", | 401 4: "running with filter %S and exception not applicable to sites", |
349 5: "running with filter %S and sitekey exception" | 402 5: "running with filter %S and sitekey exception" |
350 }; | 403 }; |
351 | 404 |
352 for (let test = 0; test < tests.length; test++) | 405 for (let test = 0; test < tests.length; test++) |
353 { | 406 { |
354 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar
ty] = tests[test]; | 407 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar
ty] = tests[test]; |
355 for (let stage = 1; stage in stageDescriptions; stage++) | 408 for (let stage = 1; stage in stageDescriptions; stage++) |
356 { | 409 { |
357 let stageDescription = stageDescriptions[stage]; | 410 let stageDescription = stageDescriptions[stage]; |
358 if (stageDescription.indexOf("%S") >= 0) | 411 if (stageDescription.indexOf("%S") >= 0) |
359 stageDescription = stageDescription.replace("%S", expectedURL); | 412 stageDescription = stageDescription.replace("%S", expectedURL); |
360 | 413 |
361 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t
est], stage)); | 414 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t
est], stage)); |
362 } | 415 } |
363 } | 416 } |
364 })(); | 417 })(); |
OLD | NEW |