-
Notifications
You must be signed in to change notification settings - Fork 4.7k
resolver: add ValidateTargetURI for target URI validation #9247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hugehoo
wants to merge
7
commits into
grpc:master
Choose a base branch
from
hugehoo:feat/validate-target-uri-8747
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49b1b50
grpcutil: add ValidateTargetURI
hugehoo 5e0f408
grpcutil: add ValidateTargetURI tests
hugehoo c4d1f70
resolver: move ValidateTargetURI from grpcutil to avoid new leaf-pack…
hugehoo 0eb2b13
resolver: apply default-scheme fallback for opaque targets in Validat…
hugehoo a2b8b5e
rls: validate lookup_service with ValidateTargetURI
hugehoo 863f4e7
xds/bootstrap: validate server_uri with ValidateTargetURI
hugehoo 83f750a
resolver: add tests for uncovered ValidateTargetURI branches
hugehoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package resolver | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| "google.golang.org/grpc/internal" | ||
| "google.golang.org/grpc/resolver" | ||
| ) | ||
|
|
||
| // ValidateTargetURI reports whether target is a valid gRPC dial target. It is | ||
| // intended for validating targets received via configuration (e.g. xDS | ||
| // bootstrap server_uri, RLS lookup_service) before dial time. | ||
| // | ||
| // A target is valid if: | ||
| // - it parses as an RFC 3986 authority-form URI (via net/url.Parse) whose | ||
| // scheme has a resolver builder registered in the global registry | ||
| // (resolver.Get), or | ||
| // - it does not parse as an authority-form URI (e.g. a "host:port" string | ||
| // such as "trafficdirector.googleapis.com:443", which parses as an opaque | ||
| // URI, or a string that does not parse at all), but is accepted after | ||
| // applying the default scheme, mirroring grpc.NewClient's fallback | ||
| // behavior for schemeless targets. | ||
| // | ||
| // Unlike grpc.NewClient, an authority-form URI ("scheme://...") with a scheme | ||
| // that has no registered resolver is rejected instead of falling back to the | ||
| // default scheme, so that scheme typos in configuration surface as errors. | ||
| // | ||
| // Per-channel resolvers registered via grpc.WithResolvers are not visible to | ||
| // this function. | ||
| func ValidateTargetURI(target string) error { | ||
| if target == "" { | ||
| return fmt.Errorf("resolver: target URI cannot be empty") | ||
| } | ||
| // Mirror grpc.NewClient's choice of default scheme: "dns", unless the | ||
| // user overrode it via resolver.SetDefaultScheme. | ||
| defScheme := "dns" | ||
| if internal.UserSetDefaultScheme { | ||
| defScheme = resolver.GetDefaultScheme() | ||
| } | ||
| u, err := url.Parse(target) | ||
| if err != nil || u.Opaque != "" { | ||
| // Not an authority-form URI: treat it as a host:port shorthand and | ||
| // apply the default scheme, as grpc.NewClient does. | ||
| if u, err = url.Parse(defScheme + ":///" + target); err != nil { | ||
| return fmt.Errorf("resolver: invalid target URI %q: %v", target, err) | ||
| } | ||
| } | ||
| if u.Scheme == "" { | ||
| u.Scheme = defScheme | ||
| } | ||
| if resolver.Get(u.Scheme) == nil { | ||
| return fmt.Errorf("resolver: target URI %q uses scheme %q which has no registered resolver", target, u.Scheme) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package resolver | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "google.golang.org/grpc/internal" | ||
| "google.golang.org/grpc/resolver" | ||
|
|
||
| _ "google.golang.org/grpc/internal/resolver/dns" // Register the default (dns) resolver for fallback tests. | ||
| ) | ||
|
|
||
| // testResolverBuilder is a minimal resolver.Builder used only to register | ||
| // schemes for ValidateTargetURI tests. | ||
| type testResolverBuilder struct{ scheme string } | ||
|
|
||
| func (b *testResolverBuilder) Build(resolver.Target, resolver.ClientConn, resolver.BuildOptions) (resolver.Resolver, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (b *testResolverBuilder) Scheme() string { return b.scheme } | ||
|
|
||
| func init() { | ||
| resolver.Register(&testResolverBuilder{scheme: "iresolver-test"}) | ||
| } | ||
|
|
||
| func TestValidateTargetURI(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| target string | ||
| wantErr bool | ||
| }{ | ||
| {name: "registered scheme with authority and endpoint", target: "iresolver-test:///endpoint", wantErr: false}, | ||
| // url.Parse canonicalizes the scheme to lowercase (RFC 3986 3.1), | ||
| // so an uppercase scheme still matches the registered lowercase one. | ||
| {name: "registered scheme uppercase input", target: "IRESOLVER-TEST:///endpoint", wantErr: false}, | ||
| // Opaque (host:port) forms fall back to the default scheme, as | ||
| // grpc.NewClient does. | ||
| {name: "host:port without scheme", target: "my-service:50051", wantErr: false}, | ||
| {name: "host:port with dotted host", target: "trafficdirector.googleapis.com:443", wantErr: false}, | ||
| {name: "ip:port without scheme", target: "127.0.0.1:443", wantErr: false}, | ||
| {name: "registered scheme opaque form", target: "iresolver-test:endpoint", wantErr: false}, | ||
| // A string that does not parse as a URI is accepted if it parses | ||
| // after the default-scheme fallback, matching grpc.NewClient. | ||
| {name: "unparseable URI accepted via fallback", target: "://bad", wantErr: false}, | ||
| // Parses with an empty scheme (not opaque), so the default scheme is | ||
| // applied directly. | ||
| {name: "absolute path without scheme", target: "/var/run/foo.sock", wantErr: false}, | ||
| // An invalid percent-escape fails to parse both as-is and after the | ||
| // default-scheme fallback. | ||
| {name: "invalid percent-escape", target: "%zz", wantErr: true}, | ||
| {name: "empty target", target: "", wantErr: true}, | ||
| // Authority-form URIs with an unregistered scheme are rejected, so | ||
| // that scheme typos in configuration surface as errors. | ||
| {name: "unregistered scheme", target: "no-such-scheme:///endpoint", wantErr: true}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := ValidateTargetURI(tc.target) | ||
| if (err != nil) != tc.wantErr { | ||
| t.Fatalf("ValidateTargetURI(%q) = %v, wantErr %v", tc.target, err, tc.wantErr) | ||
| } | ||
| if err != nil && !strings.Contains(err.Error(), tc.target) && tc.target != "" { | ||
| t.Errorf("ValidateTargetURI(%q) error %q does not mention target", tc.target, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateTargetURI_UserSetDefaultScheme(t *testing.T) { | ||
| resolver.SetDefaultScheme("iresolver-test") | ||
| defer func() { | ||
| // Reset the default scheme as though it was never set by the user. | ||
| resolver.SetDefaultScheme("passthrough") | ||
| internal.UserSetDefaultScheme = false | ||
| }() | ||
| if err := ValidateTargetURI("my-service:50051"); err != nil { | ||
| t.Fatalf("ValidateTargetURI(%q) with user-set default scheme = %v, want nil", "my-service:50051", err) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ func newFilterChainManagerForTesting(t *testing.T, lis *v3listenerpb.Listener) * | |
| bc, err := bootstrap.NewConfigFromContents([]byte(`{ | ||
| "xds_servers": [ | ||
| { | ||
| "server_uri": "ipv4:///127.0.0.1:443", | ||
| "server_uri": "passthrough:///127.0.0.1:443", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated scheme to |
||
| "channel_creds": [ | ||
| { | ||
| "type": "insecure" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a
descfield to the test struct and add these decriptions in that instead of as a comment here.Also can you please move the fields of each struct to a new line instead of it all being in one line to make sure it is readable?