import {test, expect} from '../../fixtures/test' // Event handlers on arbitrary HTML elements (onclick on div, onmouseover on span, // onfocus on p) are XSS vectors even when the element itself is safe. // TinyMCE's attribute whitelist must strip ALL on* handlers from ALL elements. test.describe('event handler stripping on arbitrary elements', () => { test.beforeEach(async ({page, rcePage}) => { await page.goto('/scenarios/basic') await rcePage.waitForEditor() }) async function setAndGet(page: any, html: string): Promise { return page.evaluate((content: string) => { // @ts-expect-error -- TinyMCE global window.tinymce.activeEditor.setContent(content) // @ts-expect-error -- TinyMCE global return window.tinymce.activeEditor.getContent() }, html) } test('onclick on div is stripped', async ({page}) => { const content = await setAndGet(page, '
Click me
') expect(content).not.toContain('onclick') expect(content).not.toContain('alert(document.cookie)') expect(content).toContain('Click me') }) test('onmouseover on span is stripped', async ({page}) => { const content = await setAndGet( page, 'hover text', ) expect(content).not.toContain('onmouseover') expect(content).not.toContain('evil.example.com') expect(content).toContain('hover text') }) test('onfocus on paragraph is stripped', async ({page}) => { const content = await setAndGet( page, '

paragraph

', ) expect(content).not.toContain('onfocus') expect(content).toContain('paragraph') }) test('onload on body-like div is stripped', async ({page}) => { const content = await setAndGet(page, '
content
') expect(content).not.toContain('onload') expect(content).toContain('content') }) test('ondblclick on heading is stripped', async ({page}) => { const content = await setAndGet(page, '

Safe Heading

') expect(content).not.toContain('ondblclick') expect(content).toContain('Safe Heading') }) test('onkeypress on input-like element is stripped', async ({page}) => { const content = await setAndGet(page, 'key capture') expect(content).not.toContain('onkeypress') expect(content).not.toContain('exfiltrate') expect(content).toContain('key capture') }) test('multiple event handlers on same element are all stripped', async ({page}) => { const content = await setAndGet( page, '
multi-handler
', ) expect(content).not.toContain('onclick') expect(content).not.toContain('onmouseover') expect(content).not.toContain('onkeydown') expect(content).not.toContain('onfocus') expect(content).toContain('multi-handler') }) test('event handler on table element is stripped (not just on div/span)', async ({page}) => { const content = await setAndGet(page, '
data
') expect(content).not.toContain('onclick') expect(content).not.toContain('steal()') expect(content).toContain('data') }) test('event handler on list item is stripped', async ({page}) => { const content = await setAndGet(page, '') expect(content).not.toContain('onmouseenter') expect(content).not.toContain('track(') expect(content).toContain('List item') }) })