@@ -111,18 +111,18 @@ function createWindow () {
111
111
let baseUrl ;
112
112
let appId ;
113
113
114
- // Keep track of the first URL of the webview (index.html of the dapp).
115
- // This defines what files the webview is allowed to navigate to within
116
- // the same frame. For example, my-dapp/index.html can navigate to
117
- // my-dapp/some/folder/hi.html and then back to my-dapp/index.html
114
+ // Derive the dapp baseUrl (.../my-dapp/) from the first URL of the webview
115
+ // (.../my-dapp/index.html). The baseUrl defines what files the webview is
116
+ // allowed to navigate to within the same frame.
117
+ // For example, my-dapp/index.html can navigate to my-dapp/some/dir/hi.html
118
+ // and then back to my-dapp/index.html
118
119
webContents . once ( 'did-navigate' , ( e , initialUrl ) => {
119
120
const initialURL = new URL ( initialUrl ) ;
120
121
121
122
appId = initialURL . searchParams . get ( 'appId' ) ;
122
123
123
124
initialURL . hash = '' ;
124
125
initialURL . search = '' ;
125
-
126
126
baseUrl = initialURL . href . substr ( 0 , initialURL . href . lastIndexOf ( '/' ) + 1 ) ;
127
127
} ) ;
128
128
@@ -132,7 +132,7 @@ function createWindow () {
132
132
e . preventDefault ( ) ;
133
133
134
134
if ( targetUrl . startsWith ( baseUrl ) ) {
135
- // The target URL is located inside the dapp folder: allow in-frame
135
+ // The target resource is located inside the dapp folder: allow in-frame
136
136
// navigation but enforce appId query parameter for inject.js
137
137
138
138
const newURL = new URL ( targetUrl ) ;
@@ -147,6 +147,18 @@ function createWindow () {
147
147
electron . shell . openExternal ( targetUrl ) ;
148
148
}
149
149
} ) ;
150
+
151
+ // Block in-page requests to resources outside the dapp folder
152
+ webContents . session . webRequest . onBeforeRequest ( { urls : [ 'file://*' ] } , ( details , callback ) => {
153
+ if ( baseUrl && ! details . url . startsWith ( baseUrl ) ) {
154
+ const sanitizedUrl = details . url . replace ( / ' / , '' ) ;
155
+
156
+ webContents . executeJavaScript ( `console.warn('Parity UI blocked a request to access ${ sanitizedUrl } ')` ) ;
157
+ callback ( { cancel : true } ) ;
158
+ } else {
159
+ callback ( { cancel : false } ) ;
160
+ }
161
+ } ) ;
150
162
} ) ;
151
163
152
164
mainWindow . on ( 'closed' , ( ) => {
0 commit comments