Skip to content

Commit 35ac0b2

Browse files
committed
fix: companion when custom CORS *-Origin is set
Companion extension should be able to access RPC API even when custom Access-Control-Allow-Origin is set
1 parent c48b612 commit 35ac0b2

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

core/corehttp/commands.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ var defaultLocalhostOrigins = []string{
4444
"https://[::1]:<port>",
4545
"http://localhost:<port>",
4646
"https://localhost:<port>",
47+
}
48+
49+
var companionBrowserExtensionOrigins = []string{
4750
"chrome-extension://nibjojkomfdiaoajekhjakgkdhaomnch", // ipfs-companion
4851
"chrome-extension://hjoieblefckbooibpepigmacodalfndh", // ipfs-companion-beta
4952
}
@@ -86,10 +89,9 @@ func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
8689
}
8790

8891
func addCORSDefaults(c *cmdsHttp.ServerConfig) {
89-
// by default use localhost origins
90-
if len(c.AllowedOrigins()) == 0 {
91-
c.SetAllowedOrigins(defaultLocalhostOrigins...)
92-
}
92+
// always safelist certain origins
93+
c.AppendAllowedOrigins(defaultLocalhostOrigins...)
94+
c.AppendAllowedOrigins(companionBrowserExtensionOrigins...)
9395

9496
// by default, use GET, PUT, POST
9597
if len(c.AllowedMethods()) == 0 {

test/sharness/t0401-api-browser-security.sh

+11-3
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ test_expect_success "browser is able to access API if Origin is the API port on
3939
grep "HTTP/1.1 200 OK" curl_output && grep "$PEERID" curl_output
4040
'
4141

42-
test_expect_success "Companion extension is unable to access API with invalid Origin" '
42+
test_expect_success "Random browser extension is unable to access RPC API due to invalid Origin" '
4343
curl -sD - -X POST -A "Mozilla" -H "Origin: chrome-extension://invalidextensionid" "http://127.0.0.1:$API_PORT/api/v0/id" >curl_output &&
4444
grep "HTTP/1.1 403 Forbidden" curl_output
4545
'
4646

47-
test_expect_success "Companion extension is able to access API if Origin is the API port on localhost (ipv4)" '
47+
test_expect_success "Companion extension is able to access RPC API on localhost" '
4848
curl -sD - -X POST -A "Mozilla" -H "Origin: chrome-extension://nibjojkomfdiaoajekhjakgkdhaomnch" "http://127.0.0.1:$API_PORT/api/v0/id" >curl_output &&
49+
cat curl_output &&
4950
grep "HTTP/1.1 200 OK" curl_output && grep "$PEERID" curl_output
5051
'
5152

52-
test_expect_success "Companion beta extension is able to access API if Origin is the API port on localhost (ipv4)" '
53+
test_expect_success "Companion beta extension is able to access API on localhost" '
5354
curl -sD - -X POST -A "Mozilla" -H "Origin: chrome-extension://hjoieblefckbooibpepigmacodalfndh" "http://127.0.0.1:$API_PORT/api/v0/id" >curl_output &&
5455
grep "HTTP/1.1 200 OK" curl_output && grep "$PEERID" curl_output
5556
'
@@ -64,6 +65,13 @@ test_expect_success "setting CORS in API.HTTPHeaders works via CLI" "
6465

6566
test_launch_ipfs_daemon
6667

68+
test_expect_success "Companion extension is able to access RPC API even when custom Access-Control-Allow-Origin is set" '
69+
curl -sD - -X POST -A "Mozilla" -H "Origin: chrome-extension://nibjojkomfdiaoajekhjakgkdhaomnch" "http://127.0.0.1:$API_PORT/api/v0/id" >curl_output &&
70+
cat curl_output &&
71+
grep "HTTP/1.1 200 OK" curl_output &&
72+
grep "$PEERID" curl_output
73+
'
74+
6775
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
6876
test_expect_success "OPTIONS with preflight request to API with CORS allowlist succeeds" '
6977
curl -svX OPTIONS -A "Mozilla" -H "Origin: https://valid.example.com" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: origin, x-requested-with" "http://127.0.0.1:$API_PORT/api/v0/id" 2>curl_output &&

0 commit comments

Comments
 (0)