LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /* global assert */ | 20 /* global assert */ |
21 | 21 |
22 const library = require("../../lib/content/snippets.js"); | 22 const library = require("../../lib/content/snippets.js"); |
| 23 const {timeout} = require("./_utils"); |
23 | 24 |
24 describe("Snippets", () => | 25 describe("Snippets", () => |
25 { | 26 { |
26 before(() => | 27 before(() => |
27 { | 28 { |
28 // We need this stub for the injector. | 29 // We need this stub for the injector. |
29 window.browser = | 30 window.browser = { |
30 { | 31 runtime: { |
31 runtime: | |
32 { | |
33 getURL: () => "" | 32 getURL: () => "" |
34 } | 33 } |
35 }; | 34 }; |
36 }); | 35 }); |
37 | 36 |
38 let testDocument = null; | |
39 | |
40 beforeEach(() => | |
41 { | |
42 let iframe = document.createElement("iframe"); | |
43 document.body.appendChild(iframe); | |
44 testDocument = iframe.contentDocument; | |
45 }); | |
46 | |
47 afterEach(() => | |
48 { | |
49 let iframe = testDocument.defaultView.frameElement; | |
50 iframe.parentNode.removeChild(iframe); | |
51 testDocument = null; | |
52 }); | |
53 | |
54 function timeout(delay) | |
55 { | |
56 return new Promise((resolve, reject) => | |
57 { | |
58 window.setTimeout(resolve, delay); | |
59 }); | |
60 } | |
61 | |
62 async function runSnippet(snippetName, ...args) | 37 async function runSnippet(snippetName, ...args) |
63 { | 38 { |
64 let snippet = library[snippetName]; | 39 let snippet = library[snippetName]; |
65 | 40 |
66 assert.ok(snippet); | 41 assert.ok(snippet); |
67 | 42 |
68 snippet(...args); | 43 snippet(...args); |
| 44 |
| 45 // For snippets that run in the context of the document via a <script> |
| 46 // element (i.e. snippets that use makeInjector()), we need to wait for |
| 47 // execution to be complete. |
69 await timeout(100); | 48 await timeout(100); |
70 } | 49 } |
71 | 50 |
72 async function testProperty(property, result = true) | 51 function testProperty(property, result = true, errorName = "ReferenceError") |
73 { | 52 { |
74 let properties = property.split("."); | 53 let path = property.split("."); |
75 assert.ok(properties.length >= 1); | |
76 | 54 |
77 let exceptionCaught = false; | 55 let exceptionCaught = false; |
78 let value = 1; | 56 let value = 1; |
| 57 |
79 try | 58 try |
80 { | 59 { |
81 let obj = window; | 60 let obj = window; |
82 while (properties.length > 1) | 61 while (path.length > 1) |
83 obj = obj[properties.shift()]; | 62 obj = obj[path.shift()]; |
84 value = obj[properties.shift()]; | 63 value = obj[path.shift()]; |
85 } | 64 } |
86 catch (e) | 65 catch (e) |
87 { | 66 { |
88 if (properties.length == 0) | 67 assert.equal(e.name, errorName); |
89 exceptionCaught = true; | 68 exceptionCaught = true; |
90 } | 69 } |
91 assert.equal(exceptionCaught, result, `The property "${property}" didn't tri
gger and exception.`); | 70 |
92 assert.equal(value, result ? 1 : undefined, `The value for "${property}" sho
uldn't have been read.`); | 71 assert.equal( |
| 72 exceptionCaught, |
| 73 result, |
| 74 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an
exception.` |
| 75 ); |
| 76 assert.equal( |
| 77 value, |
| 78 result ? 1 : undefined, |
| 79 `The value for "${property}" ${result ? "shouldn't" : "should"} have been
read.` |
| 80 ); |
93 } | 81 } |
94 | 82 |
95 it("Test abort property read", async() => | 83 it("abort-property-read", async() => |
96 { | 84 { |
97 window.abpTest = "foo"; | 85 window.abpTest = "fortytwo"; |
98 | |
99 await runSnippet("abort-on-property-read", "abpTest"); | 86 await runSnippet("abort-on-property-read", "abpTest"); |
100 await testProperty("abpTest"); | 87 testProperty("abpTest"); |
101 | 88 |
102 window.abpTest2 = {prop1: "foo"}; | 89 window.abpTest2 = {prop1: "fortytwo"}; |
103 | |
104 await runSnippet("abort-on-property-read", "abpTest2.prop1"); | 90 await runSnippet("abort-on-property-read", "abpTest2.prop1"); |
105 await testProperty("abpTest2.prop1"); | 91 testProperty("abpTest2.prop1"); |
106 | 92 |
107 // Test that we try to catch a property that don't exist yet. | 93 // Test that we try to catch a property that doesn't exist yet. |
108 await runSnippet("abort-on-property-read", "abpTest3.prop1"); | 94 await runSnippet("abort-on-property-read", "abpTest3.prop1"); |
109 window.abpTest3 = {prop1: "foo"}; | 95 window.abpTest3 = {prop1: "fortytwo"}; |
110 await testProperty("abpTest3.prop1"); | 96 testProperty("abpTest3.prop1"); |
111 | 97 |
112 // Test that other properties don't trigger. | 98 // Test that other properties don't trigger. |
113 await testProperty("abpTest3.prop2", false); | 99 testProperty("abpTest3.prop2", false); |
114 | 100 |
115 // Test overwriting the object with another object | 101 // Test overwriting the object with another object. |
116 window.foo = {bar: {}}; | 102 window.abpTest4 = {prop3: {}}; |
117 await runSnippet("abort-on-property-read", "foo.bar.lambda"); | 103 await runSnippet("abort-on-property-read", "abpTest4.prop3.foo"); |
118 await testProperty("foo.bar.lambda"); | 104 testProperty("abpTest4.prop3.foo"); |
119 window.foo.bar = {}; | 105 window.abpTest4.prop3 = {}; |
120 await testProperty("foo.bar.lambda"); | 106 testProperty("abpTest4.prop3.foo"); |
121 | 107 |
122 // Test if we start with a non-object | 108 // Test if we start with a non-object. |
123 window.foo2 = 5; | 109 window.abpTest5 = 42; |
124 await runSnippet("abort-on-property-read", "foo2.bar2.lambda"); | 110 await runSnippet("abort-on-property-read", "abpTest5.prop4.bar"); |
125 await testProperty("foo2.bar2.lambda"); | 111 |
126 window.foo2 = {}; | 112 testProperty("abpTest5.prop4.bar", true, "TypeError"); |
127 await testProperty("foo2.bar2.lambda"); | 113 |
| 114 window.abpTest5 = {prop4: 42}; |
| 115 testProperty("abpTest5.prop4.bar", false); |
| 116 window.abpTest5 = {prop4: {}}; |
| 117 testProperty("abpTest5.prop4.bar"); |
| 118 |
| 119 // Check that it works on properties that are functions. |
| 120 // https://issues.adblockplus.org/ticket/7419 |
| 121 |
| 122 // Existing function (from the API). |
| 123 await runSnippet("abort-on-property-read", "Object.keys"); |
| 124 testProperty("Object.keys"); |
| 125 |
| 126 // Function properties. |
| 127 window.abpTest6 = function() {}; |
| 128 window.abpTest6.prop1 = function() {}; |
| 129 await runSnippet("abort-on-property-read", "abpTest6.prop1"); |
| 130 testProperty("abpTest6.prop1"); |
| 131 |
| 132 // Function properties, with sub-property set afterwards. |
| 133 window.abpTest7 = function() {}; |
| 134 await runSnippet("abort-on-property-read", "abpTest7.prop1"); |
| 135 window.abpTest7.prop1 = function() {}; |
| 136 testProperty("abpTest7.prop1"); |
| 137 |
| 138 // Function properties, with base property as function set afterwards. |
| 139 await runSnippet("abort-on-property-read", "abpTest8.prop1"); |
| 140 window.abpTest8 = function() {}; |
| 141 window.abpTest8.prop1 = function() {}; |
| 142 testProperty("abpTest8.prop1"); |
| 143 |
| 144 // Arrow function properties. |
| 145 window.abpTest9 = () => {}; |
| 146 await runSnippet("abort-on-property-read", "abpTest9"); |
| 147 testProperty("abpTest9"); |
| 148 |
| 149 // Class function properties. |
| 150 window.abpTest10 = class {}; |
| 151 await runSnippet("abort-on-property-read", "abpTest10"); |
| 152 testProperty("abpTest10"); |
| 153 |
| 154 // Class function properties with prototype function properties. |
| 155 window.abpTest11 = class {}; |
| 156 window.abpTest11.prototype.prop1 = function() {}; |
| 157 await runSnippet("abort-on-property-read", "abpTest11.prototype.prop1"); |
| 158 testProperty("abpTest11.prototype.prop1"); |
| 159 |
| 160 // Class function properties with prototype function properties, with |
| 161 // prototype property set afterwards. |
| 162 window.abpTest12 = class {}; |
| 163 await runSnippet("abort-on-property-read", "abpTest12.prototype.prop1"); |
| 164 window.abpTest12.prototype.prop1 = function() {}; |
| 165 testProperty("abpTest12.prototype.prop1"); |
| 166 }); |
| 167 |
| 168 it("abort-curent-inline-script", async() => |
| 169 { |
| 170 function injectInlineScript(doc, script) |
| 171 { |
| 172 let scriptElement = doc.createElement("script"); |
| 173 scriptElement.type = "application/javascript"; |
| 174 scriptElement.async = false; |
| 175 scriptElement.textContent = script; |
| 176 doc.body.appendChild(scriptElement); |
| 177 } |
| 178 |
| 179 await runSnippet( |
| 180 "abort-current-inline-script", "document.write", "atob" |
| 181 ); |
| 182 await runSnippet( |
| 183 "abort-current-inline-script", "document.write", "btoa" |
| 184 ); |
| 185 |
| 186 document.body.innerHTML = "<p id=\"result1\"></p><p id=\"message1\"></p><p i
d=\"result2\"></p><p id=\"message2\"></p>"; |
| 187 |
| 188 let script = ` |
| 189 try |
| 190 { |
| 191 let element = document.getElementById("result1"); |
| 192 document.write("<p>atob: " + atob("dGhpcyBpcyBhIGJ1Zw==") + "</p>"); |
| 193 element.textContent = atob("dGhpcyBpcyBhIGJ1Zw=="); |
| 194 } |
| 195 catch (e) |
| 196 { |
| 197 let msg = document.getElementById("message1"); |
| 198 msg.textContent = e.name; |
| 199 }`; |
| 200 |
| 201 injectInlineScript(document, script); |
| 202 |
| 203 let element = document.getElementById("result1"); |
| 204 assert.ok(element, "Element 'result1' was not found"); |
| 205 |
| 206 let msg = document.getElementById("message1"); |
| 207 assert.ok(msg, "Element 'message1' was not found"); |
| 208 |
| 209 if (element && msg) |
| 210 { |
| 211 assert.equals(element.textContent, "", "Result element should be empty"); |
| 212 assert.equals(msg.textContent, "ReferenceError", |
| 213 "There should have been an error"); |
| 214 } |
| 215 |
| 216 script = ` |
| 217 try |
| 218 { |
| 219 let element = document.getElementById("result2"); |
| 220 document.write("<p>btoa: " + btoa("this is a bug") + "</p>"); |
| 221 element.textContent = btoa("this is a bug"); |
| 222 } |
| 223 catch (e) |
| 224 { |
| 225 let msg = document.getElementById("message2"); |
| 226 msg.textContent = e.name; |
| 227 }`; |
| 228 |
| 229 injectInlineScript(document, script); |
| 230 |
| 231 element = document.getElementById("result2"); |
| 232 assert.ok(element, "Element 'result2' was not found"); |
| 233 |
| 234 msg = document.getElementById("message2"); |
| 235 assert.ok(msg, "Element 'message2' was not found"); |
| 236 |
| 237 if (element && msg) |
| 238 { |
| 239 assert.equals(element.textContent, "", "Result element should be empty"); |
| 240 assert.equals(msg.textContent, "ReferenceError", |
| 241 "There should have been an error"); |
| 242 } |
128 }); | 243 }); |
129 }); | 244 }); |
LEFT | RIGHT |