Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion arrow/flight/basic_auth_flight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package flight_test

import (
"context"
"encoding/base64"
"errors"
"io"
"testing"
Expand All @@ -32,7 +33,7 @@ import (

const (
validUsername = "flight_username"
validPassword = "flight_password"
validPassword = "flight_password:with_colon"
invalidUsername = "invalid_flight_username"
invalidPassword = "invalid_flight_password"
validBearer = "CAREBARESTARE"
Expand Down Expand Up @@ -249,3 +250,30 @@ func TestBasicAuthMissingCredential(t *testing.T) {
t.Fatalf("unexpected code: got %v, want %v", got, want)
}
}

func TestBasicAuthMalformedCredentials(t *testing.T) {
s := flight.NewServerWithMiddleware([]flight.ServerMiddleware{flight.CreateServerBasicAuthMiddleware(&validator{})})
s.Init("localhost:0")
s.RegisterFlightService(&HeaderAuthTestFlight{})
go s.Serve()
defer s.Shutdown()

client, err := flight.NewFlightClient(s.Addr().String(), nil, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}

for _, credentials := range []string{"", "username-only", ":password", "username:"} {
t.Run(credentials, func(t *testing.T) {
encoded := base64.StdEncoding.EncodeToString([]byte(credentials))
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", "Basic "+encoded))
stream, err := client.Handshake(ctx)
if err == nil {
_, err = stream.Recv()
}
if status.Code(err) != codes.Unauthenticated {
t.Fatalf("expected unauthenticated error, got %v", err)
}
})
}
}
7 changes: 5 additions & 2 deletions arrow/flight/server_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ func createServerBearerTokenStreamInterceptor(validator BasicAuthValidator) grpc
}
}

creds := strings.SplitN(string(val), ":", 2)
token, err := validator.Validate(creds[0], creds[1])
username, password, found := strings.Cut(string(val), ":")
if !found {
return status.Error(codes.Unauthenticated, "invalid basic auth credentials")
}
token, err := validator.Validate(username, password)
if err != nil {
return err
}
Expand Down