Skip to content

Commit bb36028

Browse files
authored
fix: allow requests from electron renderer (#201)
* fix: allow requests from electron renderer The Electron Renderer process runs in an embedded browser window so has access to browser-native `fetch`. If this is used by `ipfs-http-client` to make requests against the HTTP API, the `User-Agent` header is set to a value that looks similar to a browser but no `Origin` or `Referer` headers are sent. The change here is to relax the user agent check to allow through requests from clients with `'Electron'` in their user agents.
1 parent df39339 commit bb36028

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

http/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,12 @@ func allowUserAgent(r *http.Request, cfg *ServerConfig) bool {
176176
// This means the request probably came from a browser and thus, it
177177
// should have included Origin or referer headers.
178178
ua := r.Header.Get("User-agent")
179+
180+
// The fetch API in the Electron Renderer process sends no referer or
181+
// origin but should be allowed
182+
if strings.Contains(ua, "Electron") {
183+
return true
184+
}
185+
179186
return !strings.HasPrefix(ua, "Mozilla")
180187
}

http/errors_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ func TestDisallowedUserAgents(t *testing.T) {
203203
"User-Agent": "Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Build/KLP) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30",
204204
},
205205
},
206+
{
207+
// Do not block the Electron Renderer process
208+
Method: "POST",
209+
AllowGet: false,
210+
Code: http.StatusOK,
211+
ReqHeaders: map[string]string{
212+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.104 Electron/9.0.4 Safari/537.36",
213+
},
214+
},
206215
}
207216

208217
for _, tc := range tcs {

0 commit comments

Comments
 (0)