Skip to content

Commit fcb8bdf

Browse files
authored
xds/google-c2p: validate url for no authorities (#5756)
1 parent 040b795 commit fcb8bdf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

xds/googledirectpath/googlec2p.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ type c2pResolverBuilder struct {
9292
}
9393

9494
func (c2pResolverBuilder) Build(t resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
95+
if t.URL.Host != "" {
96+
return nil, fmt.Errorf("google-c2p URI scheme does not support authorities")
97+
}
98+
9599
if !runDirectPath() {
96100
// If not xDS, fallback to DNS.
97101
t.Scheme = dnsName

xds/googledirectpath/googlec2p_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ package googledirectpath
2020

2121
import (
2222
"strconv"
23+
"strings"
2324
"testing"
2425
"time"
2526

2627
"github.com/google/go-cmp/cmp"
2728
"github.com/google/go-cmp/cmp/cmpopts"
2829
"google.golang.org/grpc"
30+
"google.golang.org/grpc/credentials/insecure"
2931
"google.golang.org/grpc/internal/envconfig"
3032
"google.golang.org/grpc/resolver"
3133
"google.golang.org/grpc/xds/internal/xdsclient"
@@ -251,3 +253,20 @@ func TestBuildXDS(t *testing.T) {
251253
})
252254
}
253255
}
256+
257+
// TestDialFailsWhenTargetContainsAuthority attempts to Dial a target URI of
258+
// google-c2p scheme with a non-empty authority and verifies that it fails with
259+
// an expected error.
260+
func TestBuildFailsWhenCalledWithAuthority(t *testing.T) {
261+
uri := "google-c2p://an-authority/resource"
262+
cc, err := grpc.Dial(uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
263+
defer func() {
264+
if cc != nil {
265+
cc.Close()
266+
}
267+
}()
268+
wantErr := "google-c2p URI scheme does not support authorities"
269+
if err == nil || !strings.Contains(err.Error(), wantErr) {
270+
t.Fatalf("grpc.Dial(%s) returned error: %v, want: %v", uri, err, wantErr)
271+
}
272+
}

0 commit comments

Comments
 (0)