Skip to content

Commit

Permalink
feat: whitelist version, add cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Nov 15, 2023
1 parent 767dc37 commit 7b97003
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/corehttp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ func convertAuthorizationsMap(authScopes map[string]*config.RPCAuthScope) (map[s

func withAuthSecrets(authorizations map[string]rpcAuthScopeWithUser, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v0/version" {
next.ServeHTTP(w, r)
return
}

authorizationHeader := r.Header.Get("Authorization")
auth, ok := authorizations[authorizationHeader]

Expand Down
24 changes: 23 additions & 1 deletion test/cli/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/test/cli/harness"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAuth(t *testing.T) {
Expand All @@ -26,7 +27,7 @@ func TestAuth(t *testing.T) {
return node
}

t.Run("Follows Allowed Paths", func(t *testing.T) {
t.Run("Follows Allowed Paths (HTTP)", func(t *testing.T) {
t.Parallel()

node := makeAndStartProtectedNode(t, map[string]*config.RPCAuthScope{
Expand All @@ -50,6 +51,27 @@ func TestAuth(t *testing.T) {
node.StopDaemon()
})

t.Run("Follows Allowed Paths (CLI)", func(t *testing.T) {
t.Parallel()

node := makeAndStartProtectedNode(t, map[string]*config.RPCAuthScope{
"userA": {
HTTPAuthSecret: "bearer:userAToken",
AllowedPaths: []string{"/api/v0/id"},
},
})

// Can access ID.
resp := node.RunIPFS("id", "--api-secret", "Bearer userAToken")
require.NoError(t, resp.Err)

// But not Ping.
resp = node.RunIPFS("ping", "--api-secret", "Bearer userAToken")
require.Error(t, resp.Err)

node.StopDaemon()
})

t.Run("Generic Allowed Path Gives Full Access", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 7b97003

Please sign in to comment.