|
137 | 137 | ];
|
138 | 138 | });
|
139 | 139 |
|
| 140 | + test('Prevent src overloading', async () => { |
| 141 | + window.scripty2Ran = false; |
| 142 | + const scriptElement = document.createElement('script'); |
| 143 | + scriptElement.id = 'scripty2'; |
| 144 | + scriptElement.setAttribute('type', 'application/javascript'); |
| 145 | + scriptElement.src = 'test://url' |
| 146 | + |
| 147 | + let setCounter = 0 |
| 148 | + // Pretend to be page overloading the src attribute |
| 149 | + Object.defineProperty(scriptElement, 'src', { |
| 150 | + get: () => 'invalid', |
| 151 | + set: () => { |
| 152 | + setCounter++ |
| 153 | + } |
| 154 | + }) |
| 155 | + |
| 156 | + const getAttribute = scriptElement.getAttribute('src'); |
| 157 | + // Should increment setCounter |
| 158 | + scriptElement.src = 'test://other' |
| 159 | + // Should NOT increment setCounter |
| 160 | + scriptElement.setAttribute('src', 'bloop'); |
| 161 | + |
| 162 | + document.body.appendChild(scriptElement); |
| 163 | + const hadInspectorNode = scriptElement === document.querySelector('ddg-runtime-checks:last-of-type'); |
| 164 | + // Continue to modify the script element after it has been added to the DOM |
| 165 | + scriptElement.madeUpProp = 'val'; |
| 166 | + const instanceofResult = scriptElement instanceof HTMLScriptElement; |
| 167 | + const scripty = document.querySelector('#scripty2'); |
| 168 | + |
| 169 | + return [ |
| 170 | + { name: 'hadInspectorNode', result: hadInspectorNode, expected: true }, |
| 171 | + { name: 'expect script to match', result: scripty, expected: scriptElement }, |
| 172 | + { name: 'scripty.getAttribute', result: getAttribute, expected: 'test://url' }, |
| 173 | + { name: 'setAttribute does not loop', result: setCounter, expected: 1 }, |
| 174 | + { name: 'scripty.type', result: scripty.type, expected: 'application/javascript' }, |
| 175 | + { name: 'scripty.id', result: scripty.id, expected: 'scripty2' } |
| 176 | + ]; |
| 177 | + }); |
| 178 | + |
140 | 179 | // eslint-disable-next-line no-undef
|
141 | 180 | renderResults();
|
142 | 181 | </script>
|
|
0 commit comments