Skip to content
Open
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
10 changes: 9 additions & 1 deletion siteimprove/admin/js/siteimprove.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 32 additions & 1 deletion tests/browser/prepublish.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ async function setup(page, options = {}) {

const preview = url.searchParams.get('preview') === 'true';
const toolbar = '<div id="wpadminbar"><div class="siteimprove-trigger-contentcheck"><a href="#">Prepublish</a></div></div>';
const frameBuster = options.frameBuster
? '<script>if (window.top !== window.self) { window.top.location.href = window.location.href; }</script>'
: '';
response.writeHead(200, headers);
response.end(`<!doctype html><html><head><title>Fixture</title><link id="fixture-stylesheet" rel="stylesheet" href="/assets/prepublish.css"><style id="fixture-inline-style">#style-fixture { background-color: rgb(240, 230, 220); }</style></head><body>
response.end(`<!doctype html><html><head><title>Fixture</title>${frameBuster}<link id="fixture-stylesheet" rel="stylesheet" href="/assets/prepublish.css"><style id="fixture-inline-style">#style-fixture { background-color: rgb(240, 230, 220); }</style></head><body>
${options.noAdminBar && isFrame ? '' : toolbar}
<div id="style-fixture" style="border-top: 3px solid rgb(70, 80, 90)">STYLE CAPTURE MARKER</div><main id="content">${preview ? 'DRAFT ONLY CONTENT' : 'PUBLISHED CONTENT'}</main>
${isFrame && url.searchParams.get('si_preview_nonce') !== NONCE ? '<div id="plugin-would-load">Invalid or missing nonce</div>' : ''}
Expand Down Expand Up @@ -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',
Expand Down
Loading