diff --git a/siteimprove/admin/js/siteimprove.js b/siteimprove/admin/js/siteimprove.js index d0dc29b..5ef7351 100644 --- a/siteimprove/admin/js/siteimprove.js +++ b/siteimprove/admin/js/siteimprove.js @@ -36,7 +36,15 @@ document.body.appendChild(iframeContainer); const iframe = document.createElement("iframe"); iframe.setAttribute("id", "domIframe"); - iframe.setAttribute("style", "height:100vh; width:100%"); + // No allow-top-navigation: stops frame-busting scripts replacing the editor's page. + iframe.setAttribute("sandbox", "allow-same-origin allow-scripts"); + iframe.setAttribute( + "style", + "position:fixed; top:0; left:0; width:100%; height:100vh; border:0; opacity:0; pointer-events:none;" + ); + iframe.setAttribute("aria-hidden", "true"); + iframe.setAttribute("tabindex", "-1"); + iframe.setAttribute("title", "Siteimprove Prepublish"); iframe.src = buildDomFetchUrl(nonce); iframeContainer.appendChild(iframe); const promise = new Promise(function (resolve, reject) { diff --git a/tests/browser/prepublish.spec.js b/tests/browser/prepublish.spec.js index 2fe8dfb..9435c17 100644 --- a/tests/browser/prepublish.spec.js +++ b/tests/browser/prepublish.spec.js @@ -59,8 +59,11 @@ async function setup(page, options = {}) { const preview = url.searchParams.get('preview') === 'true'; const toolbar = '
Prepublish
'; + const frameBuster = options.frameBuster + ? '' + : ''; response.writeHead(200, headers); - response.end(`Fixture + response.end(`Fixture${frameBuster} ${options.noAdminBar && isFrame ? '' : toolbar}
STYLE CAPTURE MARKER
${preview ? 'DRAFT ONLY CONTENT' : 'PUBLISHED CONTENT'}
${isFrame && url.searchParams.get('si_preview_nonce') !== NONCE ? '
Invalid or missing nonce
' : ''} @@ -185,6 +188,34 @@ for (const entry of ['toolbar', 'overlay']) { await test.step('And it removes the temporary frame and loading indicator', () => expectCleanup(page)); }); + test(`${entry}: a page that reloads itself out of frames cannot take over the browser tab`, async ({ page }) => { + const state = await test.step('Given a preview whose scripts navigate the top window when framed', () => + setup(page, { frameBuster: true })); + const editorUrl = page.url(); + await test.step(startDescription, () => start(page, entry)); + await test.step('Then the plugin still hands over the draft content', async () => { + expect(await readResult(page, entry)).toMatchObject({ content: 'DRAFT ONLY CONTENT', invalidNonce: false }); + }); + await test.step('And the editor stays on the preview page it started from', async () => { + expect(page.url()).toBe(editorUrl); + expect(state.frames).toHaveLength(1); + }); + await test.step('And it removes the temporary frame and loading indicator', () => expectCleanup(page)); + }); + + test(`${entry}: the temporary frame is hidden from the editor while it loads`, async ({ page }) => { + await test.step('Given a preview that is slow to load', () => setup(page, { failure: 'timeout' })); + await test.step(startDescription, () => start(page, entry)); + await test.step('Then the frame cannot be seen, clicked or reached with the keyboard', async () => { + const frame = page.locator('#domIframe'); + await expect(frame).toHaveCount(1); + await expect(frame).toHaveCSS('opacity', '0'); + await expect(frame).toHaveCSS('pointer-events', 'none'); + await expect(frame).toHaveAttribute('aria-hidden', 'true'); + await expect(frame).toHaveAttribute('tabindex', '-1'); + }); + }); + const failures = { xfo: 'the server blocks framing with X-Frame-Options', csp: 'the server blocks framing with Content Security Policy',