Skip to content

taint check fix for when document.currentScript is undefined #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration-test/pages/runtimeChecks/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html lang="en">
<body>
<h1>Check for stack tracing</h1>
<script src="script.js"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions integration-test/pages/runtimeChecks/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function init () {
window.script1Ran = true
const script = document.createElement('script')
script.src = './script2.js'
script.id = 'script2'
script.setAttribute('magicalAttribute', 'yes')
document.body.appendChild(script)
}
// Wait for setup
window.addEventListener('initialize', (e) => {
init()
})
2 changes: 2 additions & 0 deletions integration-test/pages/runtimeChecks/script2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.script2Ran = true
window.dispatchEvent(new CustomEvent('initializeFinished'))
56 changes: 56 additions & 0 deletions integration-test/test-runtime-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,60 @@ describe('Runtime checks: should allow element modification', () => {
type: 'application/javascript'
})
})

it('Verify stack tracing', async () => {
const port = server.address().port
const page = await browser.newPage()
await gotoAndWait(page, `http://localhost:${port}/runtimeChecks/index.html`, {
site: {
enabledFeatures: ['runtimeChecks']
},
featureSettings: {
runtimeChecks: {
taintCheck: 'enabled',
matchAllDomains: 'enabled',
matchAllStackDomains: 'disabled',
stackDomains: [
{
domain: 'localhost'
}
],
tagModifiers: {
script: {
filters: {
// verify the runtime check did run for the stack traced script and filtered the attribute
attribute: ['magicalattribute']
}
}
}
}
}
})
// And now with a script that will execute
const pageResults = await page.evaluate(
async () => {
window.dispatchEvent(new Event('initialize'))
await new Promise(resolve => {
window.addEventListener('initializeFinished', () => {
resolve()
})
})
const scripty = document.querySelector('script#script2')

return {
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
script1: window.script1Ran,
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
script2: window.script2Ran,
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
magicalProperty: scripty.magicalProperty
}
}
)
expect(pageResults).toEqual({
script1: true,
script2: true
// no magical property
})
})
})
6 changes: 3 additions & 3 deletions src/features/runtime-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function shouldFilterKey (tagName, filterName, key) {

let elementRemovalTimeout
const featureName = 'runtimeChecks'
const symbol = Symbol(featureName)
const taintSymbol = Symbol(featureName)
const supportedSinks = ['src']

class DDGRuntimeChecks extends HTMLElement {
Expand Down Expand Up @@ -99,7 +99,7 @@ class DDGRuntimeChecks extends HTMLElement {

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

// Reflect all attrs to the new element
Expand Down Expand Up @@ -313,7 +313,7 @@ function shouldInterrogate (tagName) {
if (matchAllStackDomains) {
return true
}
if (taintCheck && document.currentScript[symbol]) {
if (taintCheck && document.currentScript?.[taintSymbol]) {
return true
}
const stack = getStack()
Expand Down