Skip to content

Commit 68165af

Browse files
taint check fix for when document.currentScript is undefined (#307)
1 parent 27d2de9 commit 68165af

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
@@ -432,4 +432,60 @@ describe('Runtime checks: should allow element modification', () => {
432432
type: 'application/javascript'
433433
})
434434
})
435+
436+
it('Verify stack tracing', async () => {
437+
const port = server.address().port
438+
const page = await browser.newPage()
439+
await gotoAndWait(page, `http://localhost:${port}/runtimeChecks/index.html`, {
440+
site: {
441+
enabledFeatures: ['runtimeChecks']
442+
},
443+
featureSettings: {
444+
runtimeChecks: {
445+
taintCheck: 'enabled',
446+
matchAllDomains: 'enabled',
447+
matchAllStackDomains: 'disabled',
448+
stackDomains: [
449+
{
450+
domain: 'localhost'
451+
}
452+
],
453+
tagModifiers: {
454+
script: {
455+
filters: {
456+
// verify the runtime check did run for the stack traced script and filtered the attribute
457+
attribute: ['magicalattribute']
458+
}
459+
}
460+
}
461+
}
462+
}
463+
})
464+
// And now with a script that will execute
465+
const pageResults = await page.evaluate(
466+
async () => {
467+
window.dispatchEvent(new Event('initialize'))
468+
await new Promise(resolve => {
469+
window.addEventListener('initializeFinished', () => {
470+
resolve()
471+
})
472+
})
473+
const scripty = document.querySelector('script#script2')
474+
475+
return {
476+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
477+
script1: window.script1Ran,
478+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
479+
script2: window.script2Ran,
480+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
481+
magicalProperty: scripty.magicalProperty
482+
}
483+
}
484+
)
485+
expect(pageResults).toEqual({
486+
script1: true,
487+
script2: true
488+
// no magical property
489+
})
490+
})
435491
})

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
@@ -313,7 +313,7 @@ function shouldInterrogate (tagName) {
313313
if (matchAllStackDomains) {
314314
return true
315315
}
316-
if (taintCheck && document.currentScript[symbol]) {
316+
if (taintCheck && document.currentScript?.[taintSymbol]) {
317317
return true
318318
}
319319
const stack = getStack()

0 commit comments

Comments
 (0)