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
1 change: 0 additions & 1 deletion packages/insomnia/src/main/window-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export function createWindow(): ElectronBrowserWindow {
zoomFactor: getZoomFactor(),
nodeIntegration: true,
nodeIntegrationInWorker: false, // must remain false to ensure the nunjucks web worker sandbox does not have access to Node.js APIs
webviewTag: true,
// TODO: enable context isolation
contextIsolation: false,
disableBlinkFeatures: 'Auxclick',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export const ResponseViewer = ({
body={getBodyAsString()}
key={disableHtmlPreviewJs ? 'no-js' : 'yes-js'}
url={url}
webpreferences={`disableDialogs=true, javascript=${disableHtmlPreviewJs ? 'no' : 'yes'}`}
disableHtmlPreviewJs={disableHtmlPreviewJs}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';

import { getResponsePreviewHtml, getResponsePreviewSandbox } from './response-web-view';

describe('response-web-view', () => {
it('injects a base tag into the document head', () => {
expect(getResponsePreviewHtml('<html><head></head><body>Hello</body></html>', 'https://example.com/path/')).toBe(
'<html><head><base href="https://example.com/path/"></head><body>Hello</body></html>',
);
});

it('keeps scripts disabled when HTML preview JS is turned off', () => {
expect(getResponsePreviewSandbox(true)).toBe('');
});

it('allows scripts when HTML preview JS is enabled', () => {
expect(getResponsePreviewSandbox(false)).toBe('allow-scripts');
});
});
40 changes: 17 additions & 23 deletions packages/insomnia/src/ui/components/viewers/response-web-view.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import React, { type FC, useEffect, useRef } from 'react';
import React, { type FC } from 'react';

interface Props {
body: string;
url: string;
webpreferences: string;
disableHtmlPreviewJs: boolean;
}
export const ResponseWebView: FC<Props> = ({ webpreferences, body, url }) => {
const webviewRef = useRef<Electron.WebviewTag>(null);

useEffect(() => {
const webview = webviewRef.current;
const handleDOMReady = () => {
if (webview) {
webview.removeEventListener('dom-ready', handleDOMReady);
const bodyWithBase = body.replace('<head>', `<head><base href="${url}">`);
webview.loadURL(`data:text/html; charset=utf-8,${encodeURIComponent(bodyWithBase)}`);
}
};
if (webview) {
webview.addEventListener('dom-ready', handleDOMReady);
}
return () => {
if (webview) {
webview.removeEventListener('dom-ready', handleDOMReady);
}
};
}, [body, url]);
return <webview data-testid="ResponseWebView" ref={webviewRef} src="about:blank" webpreferences={webpreferences} />;
export const getResponsePreviewHtml = (body: string, url: string) => body.replace('<head>', `<head><base href="${url}">`);

Comment on lines +9 to +10
export const getResponsePreviewSandbox = (disableHtmlPreviewJs: boolean) =>
disableHtmlPreviewJs ? '' : 'allow-scripts';
Comment on lines +11 to +12

export const ResponseWebView: FC<Props> = ({ body, disableHtmlPreviewJs, url }) => {
return (
<iframe
className="h-full w-full border-0"
data-testid="ResponseWebView"
sandbox={getResponsePreviewSandbox(disableHtmlPreviewJs)}
srcDoc={getResponsePreviewHtml(body, url)}
title="HTML response preview"
/>
);
};
Loading