@@ -43,27 +43,6 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
43
43
const isIgnored = ( id ) => ignoredRequests . get ( id ) !== undefined
44
44
const errorInFlight = new LRU ( { max : 3 , maxAge : 1000 } )
45
45
46
- const acrhHeaders = new LRU ( requestCacheCfg ) // webui cors fix in Chrome
47
- const originUrls = new LRU ( requestCacheCfg ) // request.originUrl workaround for Chrome
48
- const originUrl = ( request ) => {
49
- // Firefox and Chrome provide relevant value in different fields:
50
- // (Firefox) request object includes full URL of origin document, return as-is
51
- if ( request . originUrl ) return request . originUrl
52
- // (Chrome) is lacking: `request.initiator` is just the origin (protocol+hostname+port)
53
- // To reconstruct originUrl we read full URL from Referer header in onBeforeSendHeaders
54
- // and cache it for short time
55
- // TODO: when request.originUrl is available in Chrome the `originUrls` cache can be removed
56
- const cachedUrl = originUrls . get ( request . requestId )
57
- if ( cachedUrl ) return cachedUrl
58
- if ( request . requestHeaders ) {
59
- const referer = request . requestHeaders . find ( h => h . name === 'Referer' )
60
- if ( referer ) {
61
- originUrls . set ( request . requestId , referer . value )
62
- return referer . value
63
- }
64
- }
65
- }
66
-
67
46
// Returns a canonical hostname representing the site from url
68
47
// Main reason for this is unwrapping DNSLink from local subdomain
69
48
// <fqdn>.ipns.localhost → <fqdn>
@@ -208,59 +187,25 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
208
187
209
188
// Special handling of requests made to API
210
189
if ( sameGateway ( request . url , state . apiURL ) ) {
211
- // Requests made by 'blessed' Web UI
212
- // --------------------------------------------
213
- // Goal: Web UI works without setting CORS at go-ipfs
214
- // (Without this snippet go-ipfs will return HTTP 403 due to additional origin check on the backend)
215
- const origin = originUrl ( request )
216
- if ( origin && origin . startsWith ( state . webuiRootUrl ) ) {
217
- // console.log('onBeforeSendHeaders', request)
218
- // console.log('onBeforeSendHeaders.origin', origin)
219
- // Swap Origin to pass server-side check
220
- // (go-ipfs returns HTTP 403 on origin mismatch if there are no CORS headers)
221
- const swapOrigin = ( at ) => {
222
- request . requestHeaders [ at ] . value = request . requestHeaders [ at ] . value . replace ( state . gwURL . origin , state . apiURL . origin )
223
- }
224
- let foundAt = request . requestHeaders . findIndex ( h => h . name === 'Origin' )
225
- if ( foundAt > - 1 ) swapOrigin ( foundAt )
226
- foundAt = request . requestHeaders . findIndex ( h => h . name === 'Referer' )
227
- if ( foundAt > - 1 ) swapOrigin ( foundAt )
228
-
229
- // Save access-control-request-headers from preflight
230
- foundAt = request . requestHeaders . findIndex ( h => h . name && h . name . toLowerCase ( ) === 'access-control-request-headers' )
231
- if ( foundAt > - 1 ) {
232
- acrhHeaders . set ( request . requestId , request . requestHeaders [ foundAt ] . value )
233
- // console.log('onBeforeSendHeaders FOUND access-control-request-headers', acrhHeaders.get(request.requestId))
234
- }
235
- // console.log('onBeforeSendHeaders fixed headers', request.requestHeaders)
236
- }
237
-
238
190
// '403 - Forbidden' fix for Chrome and Firefox
239
191
// --------------------------------------------
240
- // We remove Origin header from requests made to API URL and WebUI
241
- // by js-ipfs-http-client running in WebExtension context to remove need
242
- // for manual CORS whitelisting via Access-Control-Allow-Origin at go-ipfs
192
+ // We remove "Origin: *-extension://" header from requests made to API
193
+ // by js-ipfs-http-client running in the background page of browser
194
+ // extension. Without this, some users would need to do manual CORS
195
+ // whitelisting by adding "..extension://<UUID>" to
196
+ // API.HTTPHeaders.Access-Control-Allow-Origin in go-ipfs config.
243
197
// More info:
244
198
// Firefox: https://github.com/ipfs-shipyard/ipfs-companion/issues/622
245
199
// Chromium 71: https://github.com/ipfs-shipyard/ipfs-companion/pull/616
246
200
// Chromium 72: https://github.com/ipfs-shipyard/ipfs-companion/issues/630
247
- const isWebExtensionOrigin = ( origin ) => {
248
- // console.log(`origin=${origin}, webExtensionOrigin=${webExtensionOrigin}`)
249
- // Chromium <= 71 returns opaque Origin as defined in
250
- // https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
251
- if ( origin == null || origin === 'null' ) {
252
- return true
253
- }
254
- // Firefox Nightly 65 sets moz-extension://{extension-installation-id}
255
- // Chromium Beta 72 sets chrome-extension://{uid}
256
- if ( origin &&
201
+
202
+ // Firefox Nightly 65 sets moz-extension://{extension-installation-id}
203
+ // Chromium Beta 72 sets chrome-extension://{uid}
204
+ const isWebExtensionOrigin = ( origin ) =>
205
+ origin &&
257
206
( origin . startsWith ( 'moz-extension://' ) ||
258
- origin . startsWith ( 'chrome-extension://' ) ) &&
259
- new URL ( origin ) . origin === webExtensionOrigin ) {
260
- return true
261
- }
262
- return false
263
- }
207
+ origin . startsWith ( 'chrome-extension://' ) ) &&
208
+ new URL ( origin ) . origin === webExtensionOrigin
264
209
265
210
// Remove Origin header matching webExtensionOrigin
266
211
const foundAt = request . requestHeaders . findIndex ( h => h . name === 'Origin' && isWebExtensionOrigin ( h . value ) )
@@ -317,41 +262,6 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
317
262
const state = getState ( )
318
263
if ( ! state . active ) return
319
264
320
- // Special handling of requests made to API
321
- if ( sameGateway ( request . url , state . apiURL ) ) {
322
- // Special handling of requests made by 'blessed' Web UI from local Gateway
323
- // Goal: Web UI works without setting CORS at go-ipfs
324
- // (This includes 'ignored' requests: CORS needs to be fixed even if no redirect is done)
325
- const origin = originUrl ( request )
326
- if ( origin && origin . startsWith ( state . webuiRootUrl ) && request . responseHeaders ) {
327
- // console.log('onHeadersReceived', request)
328
- const acaOriginHeader = { name : 'Access-Control-Allow-Origin' , value : state . gwURL . origin }
329
- const foundAt = findHeaderIndex ( acaOriginHeader . name , request . responseHeaders )
330
- if ( foundAt > - 1 ) {
331
- request . responseHeaders [ foundAt ] . value = acaOriginHeader . value
332
- } else {
333
- request . responseHeaders . push ( acaOriginHeader )
334
- }
335
-
336
- // Restore access-control-request-headers from preflight
337
- const acrhValue = acrhHeaders . get ( request . requestId )
338
- if ( acrhValue ) {
339
- const acahHeader = { name : 'Access-Control-Allow-Headers' , value : acrhValue }
340
- const foundAt = findHeaderIndex ( acahHeader . name , request . responseHeaders )
341
- if ( foundAt > - 1 ) {
342
- request . responseHeaders [ foundAt ] . value = acahHeader . value
343
- } else {
344
- request . responseHeaders . push ( acahHeader )
345
- }
346
- acrhHeaders . del ( request . requestId )
347
- // console.log('onHeadersReceived SET Access-Control-Allow-Headers', header)
348
- }
349
-
350
- // console.log('onHeadersReceived fixed headers', request.responseHeaders)
351
- return { responseHeaders : request . responseHeaders }
352
- }
353
- }
354
-
355
265
// Skip if request is marked as ignored
356
266
if ( isIgnored ( request . requestId ) ) {
357
267
return
@@ -651,10 +561,6 @@ function normalizedUnhandledIpfsProtocol (request, pubGwUrl) {
651
561
}
652
562
}
653
563
654
- function findHeaderIndex ( name , headers ) {
655
- return headers . findIndex ( x => x . name && x . name . toLowerCase ( ) === name . toLowerCase ( ) )
656
- }
657
-
658
564
// RECOVERY OF FAILED REQUESTS
659
565
// ===================================================================
660
566
0 commit comments