@@ -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,63 +187,34 @@ 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
-
190
+ const { requestHeaders } = request
238
191
// '403 - Forbidden' fix for Chrome and Firefox
239
192
// --------------------------------------------
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
193
+ // We update "Origin: *-extension://" HTTP headers in requests made to API
194
+ // by js-ipfs-http-client running in the background page of browser
195
+ // extension. Without this, some users would need to do manual CORS
196
+ // whitelisting by adding "..extension://<UUID>" to
197
+ // API.HTTPHeaders.Access-Control-Allow-Origin in go-ipfs config.
198
+ // With this, API calls made by browser extension look like ones made
199
+ // by webui loaded from the API port.
243
200
// More info:
244
201
// Firefox: https://github.com/ipfs-shipyard/ipfs-companion/issues/622
245
202
// Chromium 71: https://github.com/ipfs-shipyard/ipfs-companion/pull/616
246
203
// 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 &&
204
+
205
+ // Firefox Nightly 65 sets moz-extension://{extension-installation-id}
206
+ // Chromium Beta 72 sets chrome-extension://{uid}
207
+ const isWebExtensionOrigin = ( origin ) =>
208
+ origin &&
257
209
( origin . startsWith ( 'moz-extension://' ) ||
258
- origin . startsWith ( 'chrome-extension://' ) ) &&
259
- new URL ( origin ) . origin === webExtensionOrigin ) {
260
- return true
261
- }
262
- return false
263
- }
210
+ origin . startsWith ( 'chrome-extension://' ) ) &&
211
+ new URL ( origin ) . origin === webExtensionOrigin
264
212
265
- // Remove Origin header matching webExtensionOrigin
266
- const foundAt = request . requestHeaders . findIndex ( h => h . name === 'Origin' && isWebExtensionOrigin ( h . value ) )
267
- if ( foundAt > - 1 ) request . requestHeaders . splice ( foundAt , 1 )
213
+ // Replace Origin header matching webExtensionOrigin with API one
214
+ const foundAt = requestHeaders . findIndex ( h => h . name === 'Origin' && isWebExtensionOrigin ( h . value ) )
215
+ if ( foundAt > - 1 ) {
216
+ requestHeaders [ foundAt ] . value = state . apiURL . origin
217
+ }
268
218
269
219
// Fix "http: invalid Read on closed Body"
270
220
// ----------------------------------
@@ -277,7 +227,7 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
277
227
let addExpectHeader = true
278
228
const expectHeader = { name : 'Expect' , value : '100-continue' }
279
229
const warningMsg = 'Executing "Expect: 100-continue" workaround for ipfs.add due to https://github.com/ipfs/go-ipfs/issues/5168'
280
- for ( const header of request . requestHeaders ) {
230
+ for ( const header of requestHeaders ) {
281
231
// Workaround A: https://github.com/ipfs/go-ipfs/issues/5168#issuecomment-401417420
282
232
// (works in Firefox, but Chromium does not expose Connection header)
283
233
/* (disabled so we use the workaround B in all browsers)
@@ -301,12 +251,10 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
301
251
}
302
252
if ( addExpectHeader ) {
303
253
log ( warningMsg )
304
- request . requestHeaders . push ( expectHeader )
254
+ requestHeaders . push ( expectHeader )
305
255
}
306
256
}
307
- }
308
- return {
309
- requestHeaders : request . requestHeaders
257
+ return { requestHeaders }
310
258
}
311
259
} ,
312
260
@@ -317,41 +265,6 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
317
265
const state = getState ( )
318
266
if ( ! state . active ) return
319
267
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
268
// Skip if request is marked as ignored
356
269
if ( isIgnored ( request . requestId ) ) {
357
270
return
@@ -651,10 +564,6 @@ function normalizedUnhandledIpfsProtocol (request, pubGwUrl) {
651
564
}
652
565
}
653
566
654
- function findHeaderIndex ( name , headers ) {
655
- return headers . findIndex ( x => x . name && x . name . toLowerCase ( ) === name . toLowerCase ( ) )
656
- }
657
-
658
567
// RECOVERY OF FAILED REQUESTS
659
568
// ===================================================================
660
569
0 commit comments