-
Notifications
You must be signed in to change notification settings - Fork 0
scram support fails #1
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // SCRAM mechanisms advertised and accepted by the proxy alongside PLAIN. | ||
| // kroxy implements these in pass-through "relay" mode: the SaslAuthenticate | ||
| // payloads are forwarded verbatim between client and the upstream broker, | ||
| // which is the sole authentication authority. kroxy peeks only at the first | ||
| // client message in order to extract the SASLname (== tenant ID) for | ||
| // routing. | ||
| const ( | ||
| MechanismSCRAMSHA256 = "SCRAM-SHA-256" | ||
| MechanismSCRAMSHA512 = "SCRAM-SHA-512" | ||
| ) | ||
|
|
||
| // IsSCRAMMechanism reports whether mech is one of the SCRAM mechanisms | ||
| // supported by the proxy. | ||
| func IsSCRAMMechanism(mech string) bool { | ||
| return mech == MechanismSCRAMSHA256 || mech == MechanismSCRAMSHA512 | ||
| } | ||
|
|
||
| // ParseSCRAMClientFirstUsername extracts the SASLname (== tenant ID) from a | ||
| // SCRAM client-first-message as defined by RFC 5802 §7. The grammar we | ||
| // accept is: | ||
| // | ||
| // gs2-cbind-flag "," [ authzid ] "," "n=" saslname "," "r=" c-nonce ... | ||
| // gs2-cbind-flag = "n" | "y" | "p=..." | ||
| // | ||
| // kroxy does NOT support SASL channel binding, so only the "n" flag is | ||
| // accepted; any "y" or "p=..." flag is rejected. authzid (if present) is | ||
| // ignored. SASLname escapes "=2C" / "=3D" are decoded. | ||
| func ParseSCRAMClientFirstUsername(payload []byte) (string, error) { | ||
| s := string(payload) | ||
|
|
||
| // gs2-cbind-flag. | ||
| cb, rest, ok := cutByte(s, ',') | ||
| if !ok { | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: missing gs2 cbind-flag separator") | ||
| } | ||
| switch { | ||
| case cb == "n": | ||
| // no channel binding, ok. | ||
| case cb == "y" || strings.HasPrefix(cb, "p="): | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: channel binding not supported") | ||
| default: | ||
| return "", errors.Errorf("ParseSCRAMClientFirstUsername: invalid gs2 cbind-flag %q", cb) | ||
| } | ||
|
|
||
| // optional authzid then "," then client-first-message-bare. | ||
| _, bare, ok := cutByte(rest, ',') | ||
| if !ok { | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: missing authzid separator") | ||
| } | ||
|
|
||
| // client-first-message-bare = [reserved-mext ","] username "," nonce ["," extensions] | ||
| // Skip any leading m=... reserved-mext attribute. | ||
| if strings.HasPrefix(bare, "m=") { | ||
| _, after, ok := cutByte(bare, ',') | ||
| if !ok { | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: malformed reserved-mext") | ||
| } | ||
| bare = after | ||
| } | ||
|
|
||
| if !strings.HasPrefix(bare, "n=") { | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: missing n= attribute") | ||
| } | ||
| rest = bare[2:] | ||
| rawName, _, ok := cutByte(rest, ',') | ||
| if !ok { | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: missing nonce separator") | ||
| } | ||
| if rawName == "" { | ||
| return "", errors.New("ParseSCRAMClientFirstUsername: empty username") | ||
| } | ||
| name, err := decodeSASLname(rawName) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "ParseSCRAMClientFirstUsername") | ||
| } | ||
| return name, nil | ||
| } | ||
|
|
||
| // cutByte splits s at the first occurrence of sep. It is a tiny helper to | ||
| // avoid pulling in strings.Cut's allocation pattern repeatedly. | ||
| func cutByte(s string, sep byte) (before, after string, found bool) { | ||
| if i := strings.IndexByte(s, sep); i >= 0 { | ||
| return s[:i], s[i+1:], true | ||
| } | ||
| return s, "", false | ||
| } | ||
|
|
||
| // decodeSASLname reverses the "=2C" / "=3D" escapes used by SCRAM SASLnames | ||
| // (RFC 5802 §5.1). Any other "=XX" sequence, or a stray '=' or ',' in the | ||
| // raw name, is rejected. | ||
| func decodeSASLname(raw string) (string, error) { | ||
| if !strings.ContainsRune(raw, '=') { | ||
| // fast path: no escapes. | ||
| if strings.ContainsRune(raw, ',') { | ||
| return "", errors.New("decodeSASLname: unescaped comma") | ||
| } | ||
| return raw, nil | ||
| } | ||
| var b strings.Builder | ||
| b.Grow(len(raw)) | ||
| for i := 0; i < len(raw); i++ { | ||
| c := raw[i] | ||
| switch c { | ||
| case ',': | ||
| return "", errors.New("decodeSASLname: unescaped comma") | ||
| case '=': | ||
| if i+2 >= len(raw) { | ||
| return "", errors.New("decodeSASLname: truncated escape") | ||
| } | ||
| esc := raw[i+1 : i+3] | ||
| switch esc { | ||
| case "2C": | ||
| b.WriteByte(',') | ||
| case "3D": | ||
| b.WriteByte('=') | ||
| default: | ||
| return "", errors.Errorf("decodeSASLname: invalid escape =%s", esc) | ||
| } | ||
| i += 2 | ||
| default: | ||
| b.WriteByte(c) | ||
| } | ||
| } | ||
| return b.String(), 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,119 @@ | ||
| package auth_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/bubunyo/kroxy/auth" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIsSCRAMMechanism(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| assert.True(t, auth.IsSCRAMMechanism(auth.MechanismSCRAMSHA256)) | ||
| assert.True(t, auth.IsSCRAMMechanism(auth.MechanismSCRAMSHA512)) | ||
| assert.False(t, auth.IsSCRAMMechanism(auth.MechanismPlain)) | ||
| assert.False(t, auth.IsSCRAMMechanism("")) | ||
| assert.False(t, auth.IsSCRAMMechanism("scram-sha-256")) | ||
| } | ||
|
|
||
| func TestParseSCRAMClientFirstUsername(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| in string | ||
| want string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "no channel binding, no authzid", | ||
| in: "n,,n=alice,r=fyko+d2lbbFgONRv9qkxdawL", | ||
| want: "alice", | ||
| }, | ||
| { | ||
| name: "no channel binding, with authzid (ignored)", | ||
| in: "n,a=admin,n=alice,r=abc", | ||
| want: "alice", | ||
| }, | ||
| { | ||
| name: "escaped comma in name", | ||
| in: "n,,n=al=2Cice,r=abc", | ||
| want: "al,ice", | ||
| }, | ||
| { | ||
| name: "escaped equals in name", | ||
| in: "n,,n=al=3Dice,r=abc", | ||
| want: "al=ice", | ||
| }, | ||
| { | ||
| name: "with extensions after nonce", | ||
| in: "n,,n=tenantA,r=abc,m=foo", | ||
| want: "tenantA", | ||
| }, | ||
| { | ||
| name: "leading reserved-mext skipped", | ||
| in: "n,,m=ignored,n=tenantA,r=abc", | ||
| want: "tenantA", | ||
| }, | ||
| { | ||
| name: "channel binding y rejected", | ||
| in: "y,,n=alice,r=abc", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "channel binding p= rejected", | ||
| in: "p=tls-unique,,n=alice,r=abc", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "missing gs2 cbind separator", | ||
| in: "n", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "missing authzid separator", | ||
| in: "n,", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "missing n= attribute", | ||
| in: "n,,r=abc,n=alice", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "missing nonce", | ||
| in: "n,,n=alice", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty username", | ||
| in: "n,,n=,r=abc", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "invalid escape", | ||
| in: "n,,n=al=FFice,r=abc", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "truncated escape", | ||
| in: "n,,n=alice=2,r=abc", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := auth.ParseSCRAMClientFirstUsername([]byte(tt.in)) | ||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.