-
Notifications
You must be signed in to change notification settings - Fork 0
fix: support MySQL usernames containing @ #27
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
OneWhoNests
wants to merge
7
commits into
main
Choose a base branch
from
fix/username-with-at-sign
base: main
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
0429d88
fix: support MySQL usernames containing @
OneWhoNests 34f90b7
fix: address review feedback on @-in-username PR
OneWhoNests bc8d0c9
fix: resolve staticcheck SA1019 deprecation warnings in user.go
OneWhoNests 76892be
refactor: build user resources via rs.NewUserResource
OneWhoNests 321d162
fix: allow empty username in SplitUserHost, wrap role/user split errors
OneWhoNests 8abb097
fix: allow empty username in escapeMySQLUserHost for anonymous accounts
OneWhoNests ea0c493
fix: CreateAccount UserType/empty-name guards, allow IPv6 and netmask…
OneWhoNests 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
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,118 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func Test_SplitUserHost(t *testing.T) { | ||
| type want struct { | ||
| user string | ||
| host string | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| in string | ||
| want want | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "simple user and host", | ||
| in: "someone@%", | ||
| want: want{user: "someone", host: "%"}, | ||
| }, | ||
| { | ||
| name: "username containing @", | ||
| in: "someone@orion.com@%", | ||
| want: want{user: "someone@orion.com", host: "%"}, | ||
| }, | ||
| { | ||
| name: "username containing multiple @", | ||
| in: "a@b@c@10.0.0.1", | ||
| want: want{user: "a@b@c", host: "10.0.0.1"}, | ||
| }, | ||
| { | ||
| name: "collapsed comma-separated hosts", | ||
| in: "someone@orion.com@localhost,%", | ||
| want: want{user: "someone@orion.com", host: "localhost,%"}, | ||
| }, | ||
| { | ||
| name: "ipv6 loopback host", | ||
| in: "root@::1", | ||
| want: want{user: "root", host: "::1"}, | ||
| }, | ||
| { | ||
| name: "username with @ and ipv6 host", | ||
| in: "someone@orion.com@::1", | ||
| want: want{user: "someone@orion.com", host: "::1"}, | ||
| }, | ||
| { | ||
| name: "netmask host", | ||
| in: "someone@198.51.100.0/255.255.255.0", | ||
| want: want{user: "someone", host: "198.51.100.0/255.255.255.0"}, | ||
| }, | ||
| { | ||
| name: "no @", | ||
| in: "someone", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty user (MySQL anonymous account)", | ||
| in: "@%", | ||
| want: want{user: "", host: "%"}, | ||
| }, | ||
| { | ||
| name: "empty host", | ||
| in: "someone@", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty string", | ||
| in: "", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| user, host, err := SplitUserHost(tt.in) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("SplitUserHost(%q) error = %v, wantErr %v", tt.in, err, tt.wantErr) | ||
| } | ||
| if tt.wantErr { | ||
| return | ||
| } | ||
| if user != tt.want.user || host != tt.want.host { | ||
| t.Errorf("SplitUserHost(%q) = (%q, %q), want (%q, %q)", tt.in, user, host, tt.want.user, tt.want.host) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_escapeMySQLUserHost(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in string | ||
| wantErr bool | ||
| }{ | ||
| {name: "empty (MySQL anonymous account username)", in: ""}, | ||
| {name: "plain username", in: "someone"}, | ||
| {name: "username with @", in: "someone@orion.com"}, | ||
| {name: "wildcard host", in: "%"}, | ||
| {name: "hostname", in: "%.example.com"}, | ||
| {name: "ipv4 host", in: "127.0.0.1"}, | ||
| {name: "ipv6 loopback host", in: "::1"}, | ||
| {name: "ipv6 full host", in: "2001:db8::8a2e:370:7334"}, | ||
| {name: "netmask host", in: "198.51.100.0/255.255.255.0"}, | ||
| {name: "wildcard octet host", in: "198.51.100.%"}, | ||
| {name: "quote injection attempt", in: "someone' OR '1'='1", wantErr: true}, | ||
| {name: "space", in: "some one", wantErr: true}, | ||
| {name: "trailing backslash", in: `someone\`, wantErr: true}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := escapeMySQLUserHost(tt.in) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("escapeMySQLUserHost(%q) error = %v, wantErr %v", tt.in, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
🟡 Suggestion: Widening the regex unblocks IPv6 at the escape layer, but the connector-side principal-ID parsing still splits on
:, so IPv6 hosts remain unreachable for grant/revoke. Foruser:root@::1,pkg/connector/server.go:77,database.go:85, andcolumn.go:80takeSplit(id, ":")[1]→"root@"(thenSplitUserHosterrors), whiletable.go:91,routine.go:106,role.go:96, andcolumn.go:105hit theirlen(parts) != 2guard and returninvalid principal ID. Netmask hosts are fine since/isn't a separator. Considerstrings.TrimPrefix(id, resourceType+":")at those sites, as already done ingrants.go:25anduser.go:179.