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