Skip to content

Commit a5bc72d

Browse files
taint check fix for when document.currentScript is undefined
1 parent af16f46 commit a5bc72d

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html lang="en">
2+
<body>
3+
<h1>Check for stack tracing</h1>
4+
<script src="script.js"></script>
5+
</body>
6+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function init () {
2+
window.script1Ran = true
3+
const script = document.createElement('script')
4+
script.src = './script2.js'
5+
script.id = 'script2'
6+
script.setAttribute('magicalAttribute', 'yes')
7+
document.body.appendChild(script)
8+
}
9+
// Wait for setup
10+
window.addEventListener('initialize', (e) => {
11+
init()
12+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
window.script2Ran = true
2+
window.dispatchEvent(new CustomEvent('initializeFinished'))

integration-test/test-runtime-checks.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,60 @@ describe('Runtime checks: should allow element modification', () => {
385385
srcVal7: 'http://example4.com/'
386386
})
387387
})
388+
389+
it('Verify stack tracing', async () => {
390+
const port = server.address().port
391+
const page = await browser.newPage()
392+
await gotoAndWait(page, `http://localhost:${port}/runtimeChecks/index.html`, {
393+
site: {
394+
enabledFeatures: ['runtimeChecks']
395+
},
396+
featureSettings: {
397+
runtimeChecks: {
398+
taintCheck: 'enabled',
399+
matchAllDomains: 'enabled',
400+
matchAllStackDomains: 'disabled',
401+
stackDomains: [
402+
{
403+
domain: 'localhost'
404+
}
405+
],
406+
tagModifiers: {
407+
script: {
408+
filters: {
409+
// verify the runtime check did run for the stack traced script and filtered the attribute
410+
attribute: ['magicalattribute']
411+
}
412+
}
413+
}
414+
}
415+
}
416+
})
417+
// And now with a script that will execute
418+
const pageResults = await page.evaluate(
419+
async () => {
420+
window.dispatchEvent(new Event('initialize'))
421+
await new Promise(resolve => {
422+
window.addEventListener('initializeFinished', () => {
423+
resolve()
424+
})
425+
})
426+
const scripty = document.querySelector('script#script2')
427+
428+
return {
429+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
430+
script1: window.script1Ran,
431+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
432+
script2: window.script2Ran,
433+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
434+
magicalProperty: scripty.magicalProperty
435+
}
436+
}
437+
)
438+
expect(pageResults).toEqual({
439+
script1: true,
440+
script2: true
441+
// no magical property
442+
})
443+
})
388444
})

src/features/runtime-checks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function shouldFilterKey (tagName, filterName, key) {
2121

2222
let elementRemovalTimeout
2323
const featureName = 'runtimeChecks'
24-
const symbol = Symbol(featureName)
24+
const taintSymbol = Symbol(featureName)
2525
const supportedSinks = ['src']
2626

2727
class DDGRuntimeChecks extends HTMLElement {
@@ -99,7 +99,7 @@ class DDGRuntimeChecks extends HTMLElement {
9999

100100
if (taintCheck) {
101101
// Add a symbol to the element so we can identify it as a runtime checked element
102-
Object.defineProperty(el, symbol, { value: true, configurable: false, enumerable: false, writable: false })
102+
Object.defineProperty(el, taintSymbol, { value: true, configurable: false, enumerable: false, writable: false })
103103
}
104104

105105
// Reflect all attrs to the new element
@@ -319,7 +319,7 @@ function shouldInterrogate (tagName) {
319319
if (matchAllStackDomains) {
320320
return true
321321
}
322-
if (taintCheck && document.currentScript[symbol]) {
322+
if (taintCheck && document.currentScript?.[taintSymbol]) {
323323
return true
324324
}
325325
const stack = getStack()

0 commit comments

Comments
 (0)