Skip to content

Commit

Permalink
fix: Iframe javascript URLs (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: Miguel Montalvo <miguel.montalvo@barrelny.com>
  • Loading branch information
montalvomiguelo and Miguel Montalvo authored Mar 27, 2022
1 parent 5d3a425 commit 0ae2ece
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ export const enum NodeName {
export const enum StateProp {
errorHandlers = 'error',
loadHandlers = 'load',
src = 0,
loadErrorStatus = 1,
cssRules = 2,
innerHTML = 3,
Expand Down
15 changes: 13 additions & 2 deletions src/lib/web-worker/worker-iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { getPartytownScript, resolveUrl } from './worker-exec';
import { getter, sendToMain, setter } from './worker-proxy';
import { HTMLSrcElementDescriptorMap } from './worker-src-element';
import { setInstanceStateValue } from './worker-state';
import { setInstanceStateValue, getInstanceStateValue } from './worker-state';
import {
StateProp,
WebWorkerEnvironment,
Expand All @@ -35,10 +35,21 @@ export const patchHTMLIFrameElement = (WorkerHTMLIFrameElement: any, env: WebWor

src: {
get() {
let src = getIframeEnv(this).$location$.href;
let src = getInstanceStateValue(this, StateProp.src);
if (src && src.startsWith('javascript:')) {
return src;
}
src = getIframeEnv(this).$location$.href;
return src.startsWith('about:') ? '' : src;
},
set(src: string) {
if (!src) {
return;
}
if (src.startsWith('javascript:')) {
setInstanceStateValue(this, StateProp.src, src);
return;
}
if (!src.startsWith('about:')) {
let xhr = new XMLHttpRequest();
let xhrStatus: number;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/web-worker/worker-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export const createNodeCstr = (
if (isIFrame) {
// an iframe element's instanceId is also
// the winId of its contentWindow
const src = getInstanceStateValue<string>(newNode, StateProp.src);
if (src && src.startsWith('javascript:')) {
const scriptContent = src.split('javascript:')[1];
runScriptContent(env, instanceId, scriptContent, winId, '');
}
insertIframe(instanceId, newNode);
}
if (isScript) {
Expand Down
4 changes: 4 additions & 0 deletions tests/platform/iframe/iframe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,8 @@ test('iframe', async ({ page }) => {
await page.waitForSelector('.testWindowFrames');
const testWindowFrames = page.locator('#testWindowFrames');
await expect(testWindowFrames).toHaveText('window-frames');

await page.waitForSelector('.testSetJavascriptSrc');
const testSetJavascriptSrc = page.locator('#testSetJavascriptSrc');
await expect(testSetJavascriptSrc).toHaveText('javascript:void(0);');
});
17 changes: 17 additions & 0 deletions tests/platform/iframe/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,23 @@ <h1>Iframe</h1>
})();
</script>
</li>

<li>
<strong>set javascript: source</strong>
<code id="testSetJavascriptSrc"></code>
<script type="text/partytown">
(function() {
const elm = document.getElementById('testSetJavascriptSrc');

const iframe = document.createElement('iframe');
iframe.src = 'javascript:void(0);';
elm.parentNode.appendChild(iframe);

elm.textContent = iframe.src;
elm.className = 'testSetJavascriptSrc';
})();
</script>
</li>
</ul>

<hr />
Expand Down

1 comment on commit 0ae2ece

@vercel
Copy link

@vercel vercel bot commented on 0ae2ece Mar 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.