|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); |
| 5 | + |
| 6 | +let chartId; |
| 7 | + |
| 8 | +test.describe( 'Custom CSS sanitization', () => { |
| 9 | + test.beforeAll( async ( { requestUtils } ) => { |
| 10 | + const chart = await requestUtils.rest( { |
| 11 | + method: 'POST', |
| 12 | + path: '/wp/v2/visualizer', |
| 13 | + data: { title: 'Custom CSS payload chart', status: 'publish' }, |
| 14 | + } ); |
| 15 | + chartId = chart.id; |
| 16 | + |
| 17 | + await requestUtils.rest( { |
| 18 | + method: 'POST', |
| 19 | + path: `/visualizer-e2e/v1/chart-settings/${ chartId }`, |
| 20 | + data: { |
| 21 | + settings: { |
| 22 | + customcss: { |
| 23 | + title: { |
| 24 | + color: 'red</style><script>window.vizXss=1</script><style>', |
| 25 | + 'font-size': '12px', |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + } ); |
| 31 | + } ); |
| 32 | + |
| 33 | + test.afterAll( async ( { requestUtils } ) => { |
| 34 | + if ( chartId ) { |
| 35 | + await requestUtils.rest( { method: 'DELETE', path: `/wp/v2/visualizer/${ chartId }`, params: { force: true } } ); |
| 36 | + } |
| 37 | + } ); |
| 38 | + |
| 39 | + test( 'strips tags from chart custom CSS on the library page', async ( { admin, page } ) => { |
| 40 | + await admin.visitAdminPage( 'admin.php?page=visualizer' ); |
| 41 | + |
| 42 | + const styleBlock = page.locator( `#customcss-visualizer-${ chartId }` ); |
| 43 | + await expect( styleBlock ).toHaveCount( 1 ); |
| 44 | + // Legitimate rules survive sanitization. <style> has no innerText, so read textContent. |
| 45 | + const css = await styleBlock.textContent(); |
| 46 | + expect( css ).toContain( 'font-size: 12px' ); |
| 47 | + expect( css ).not.toContain( '<script' ); |
| 48 | + // The injected script must not have executed. |
| 49 | + expect( await page.evaluate( () => window.vizXss ) ).toBeUndefined(); |
| 50 | + } ); |
| 51 | +} ); |
0 commit comments