Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Commit

Permalink
Restrict dapp access to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
axelchalon committed Jul 9, 2018
1 parent 68fda6c commit c5ab168
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ function createWindow () {
let baseUrl;
let appId;

// Keep track of the first URL of the webview (index.html of the dapp).
// This defines what files the webview is allowed to navigate to within
// the same frame. For example, my-dapp/index.html can navigate to
// my-dapp/some/folder/hi.html and then back to my-dapp/index.html
// Derive the dapp baseUrl (.../my-dapp/) from the first URL of the webview
// (.../my-dapp/index.html). The baseUrl defines what files the webview is
// allowed to navigate to within the same frame.
// For example, my-dapp/index.html can navigate to my-dapp/some/dir/hi.html
// and then back to my-dapp/index.html
webContents.once('did-navigate', (e, initialUrl) => {
const initialURL = new URL(initialUrl);

appId = initialURL.searchParams.get('appId');

initialURL.hash = '';
initialURL.search = '';

baseUrl = initialURL.href.substr(0, initialURL.href.lastIndexOf('/') + 1);
});

Expand All @@ -144,7 +144,7 @@ function createWindow () {
e.preventDefault();

if (targetUrl.startsWith(baseUrl)) {
// The target URL is located inside the dapp folder: allow in-frame
// The target resource is located inside the dapp folder: allow in-frame
// navigation but enforce appId query parameter for inject.js

const newURL = new URL(targetUrl);
Expand All @@ -159,6 +159,18 @@ function createWindow () {
electron.shell.openExternal(targetUrl);
}
});

// Block in-page requests to resources outside the dapp folder
webContents.session.webRequest.onBeforeRequest({ urls: ['file://*'] }, (details, callback) => {
if (baseUrl && !details.url.startsWith(baseUrl)) {
const sanitizedUrl = details.url.replace(/'/, '');

webContents.executeJavaScript(`console.warn('Parity UI blocked a request to access ${sanitizedUrl}')`);
callback({ cancel: true });
} else {
callback({ cancel: false });
}
});
});

mainWindow.on('closed', () => {
Expand Down

0 comments on commit c5ab168

Please sign in to comment.