Unverified Commit d3a60807 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[test] Regression test for XSS-safe `GoogleTagManager#dataLayer` (#99464)

This is based on a vulnerability report we got without a reproduction.
The test shows `GoogleTagManager` is not vulnerable to XSS attacks.

`<GoogleTagManager dataLayer={...}>` serializes user-controlled values
into its inline init script via `JSON.stringify` without HTML escaping.
If such a value reaches an HTML document (or an HTML fragment parser in
a non-script context), a `</script>` sequence breaks out of the script
tag and the injected markup executes (XSS).

These assertions encode the safe behavior: the payload must never appear
unescaped in the served HTML, and it must never execute in the browser.

Closes https://linear.app/vercel/issue/NSEC-263
Closes https://github.com/vercel/next-js-mirror/pull/181
parent 5da8ef12
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { GoogleTagManager } from '@next/third-parties/google'
import { Suspense } from 'react'
async function Component({
searchParams,
}: {
searchParams: Promise<{ q?: string }>
}) {
const { q } = await searchParams
return (
<>
<p id="probe">q = {q ?? ''}</p>
<GoogleTagManager gtmId="GTM-XYZ" dataLayer={{ q: q ?? '' }} />
</>
)
}
export default function Page({
searchParams,
}: {
searchParams: Promise<{ q?: string }>
}) {
return (
<Suspense fallback="waiting for request">
<Component searchParams={searchParams} />
</Suspense>
)
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('gtm-datalayer-xss', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: {
'@next/third-parties': 'workspace:*',
},
})
const payload = '</script><script>window.__xss_gtm=1</script>'
describe.each([
['app router', '/'],
['pages router', '/pages-router'],
])('%s', (_, basePath) => {
const path = `${basePath}?q=${encodeURIComponent(payload)}`
it('does not serve the dataLayer value unescaped in the HTML document', async () => {
const res = await next.fetch(path)
const html = await res.text()
// The raw breakout sequence must not appear anywhere in the document.
expect(html).not.toContain(payload)
// In particular the GTM init script must not embed it unescaped.
const $ = await next.render$(path)
const initScript = $('script#_next-gtm-init').html() ?? ''
expect(initScript).not.toContain('</script>')
expect(initScript).not.toContain('window.__xss_gtm')
})
it('does not execute markup injected through the dataLayer value', async () => {
const browser = await next.browser(path)
// The init script is injected client-side, so wait until it ran:
// the dataLayer feature itself keeps working and the value arrives intact.
await retry(async () => {
const dataLayer = await browser.eval('window.dataLayer')
expect(JSON.stringify(dataLayer)).toContain(
'</script><script>window.__xss_gtm=1</script>'
)
})
// If the payload broke out of the inline script tag, this would be 1.
expect(await browser.eval('window.__xss_gtm')).toBeUndefined()
})
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import type { GetServerSideProps } from 'next'
import { GoogleTagManager } from '@next/third-parties/google'
export const getServerSideProps: GetServerSideProps = async ({ query }) => {
const q = typeof query.q === 'string' ? query.q : ''
return { props: { q } }
}
export default function Page({ q }: { q: string }) {
return (
<>
<p id="probe">q = {q}</p>
<GoogleTagManager gtmId="GTM-XYZ" dataLayer={{ q }} />
</>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment