-
Notifications
You must be signed in to change notification settings - Fork 107
Masked the sensitive credential data in the connection string (DSN,data source name) from error messages for security reasons. #1973
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
+196
−27
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b8ab367
Excluded the sensitive credential data in the connection string (DSN,…
asmyasnikov 0c6f07e
Apply suggestions from code review
asmyasnikov c1128f5
fixes
asmyasnikov 2e44267
fixes
asmyasnikov 75e40e2
fix
asmyasnikov 837ef17
CHANGELOG
asmyasnikov aae1637
fixes
asmyasnikov f3a9cfb
fix
asmyasnikov 962b353
fix
asmyasnikov 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
Some comments aren't visible on the classic Files Changed page.
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
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,62 @@ | ||
| package secret | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/ydb-platform/ydb-go-sdk/v3/pkg/xstring" | ||
| ) | ||
|
|
||
| func DSN(dsn string) string { | ||
| u, err := url.Parse(dsn) | ||
| if err != nil { | ||
| return "<invalid DSN>" | ||
| } | ||
|
|
||
| buffer := xstring.Buffer() | ||
| defer buffer.Free() | ||
|
|
||
| if u.Scheme != "" { | ||
| buffer.WriteString(u.Scheme + "://") | ||
| } | ||
|
|
||
| if u.User != nil { | ||
| buffer.WriteString(u.User.Username()) | ||
| if password, has := u.User.Password(); has { | ||
| buffer.WriteString(":" + Password(password)) | ||
| } | ||
| buffer.WriteString("@") | ||
| } | ||
|
|
||
| buffer.WriteString(u.Host) | ||
| buffer.WriteString(u.Path) | ||
|
|
||
| if len(u.RawQuery) > 0 { | ||
| buffer.WriteString("?") | ||
|
|
||
| params := strings.Split(u.RawQuery, "&") | ||
|
|
||
| for i, param := range params { | ||
| if i > 0 { | ||
| buffer.WriteString("&") | ||
| } | ||
| paramValue := strings.SplitN(param, "=", 2) | ||
| buffer.WriteString(paramValue[0]) | ||
| if len(paramValue) > 1 { | ||
| buffer.WriteString("=") | ||
| switch paramValue[0] { | ||
| case "token", "password": | ||
| buffer.WriteString(Mask(paramValue[1])) | ||
| default: | ||
| buffer.WriteString(paramValue[1]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
asmyasnikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if u.Fragment != "" { | ||
| buffer.WriteString("#" + u.Fragment) | ||
| } | ||
|
|
||
| return buffer.String() | ||
| } | ||
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,55 @@ | ||
| package secret | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDSN(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| dsn string | ||
| exp string | ||
| }{ | ||
| { | ||
asmyasnikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dsn: "grpc://192.168.0.%31:2136/", | ||
| exp: "<invalid DSN>", | ||
| }, | ||
| { | ||
| dsn: "grpc://debuguser:debugpassword@localhost:2136/local1", | ||
| exp: "grpc://debuguser:d***********d@localhost:2136/local1", | ||
| }, | ||
| { | ||
| dsn: "grpc://host/db?password=pass%3Dword", | ||
| exp: "grpc://host/db?password=p*********d", | ||
| }, | ||
| { | ||
| dsn: "grpc://host/db?password=pass%26word", | ||
| exp: "grpc://host/db?password=p*********d", | ||
| }, | ||
| { | ||
| dsn: "grpc://localhost:2136/local1?user=debuguser&password=debugpassword", | ||
| exp: "grpc://localhost:2136/local1?user=debuguser&password=d***********d", | ||
| }, | ||
| { | ||
| dsn: "grpc://localhost:2136/local1?login=debuguser&password=debugpassword", | ||
| exp: "grpc://localhost:2136/local1?login=debuguser&password=d***********d", | ||
| }, | ||
| { | ||
| dsn: "grpc://localhost:2136/local1?param1=value1&login=debuguser¶m2=value2&password=debugpassword¶m2=value3", | ||
| exp: "grpc://localhost:2136/local1?param1=value1&login=debuguser¶m2=value2&password=d***********d¶m2=value3", | ||
| }, | ||
| { | ||
| dsn: "grpc://localhost:2136/local1?param1&login=debuguser¶m2=value2&password=debugpassword¶m2=value3", | ||
| exp: "grpc://localhost:2136/local1?param1&login=debuguser¶m2=value2&password=d***********d¶m2=value3", | ||
| }, | ||
| { | ||
| dsn: "grpc://localhost:2136/local1?token=secrettoken123", | ||
| exp: "grpc://localhost:2136/local1?token=s************3", | ||
| }, | ||
asmyasnikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } { | ||
| t.Run(tt.dsn, func(t *testing.T) { | ||
| require.Equal(t, tt.exp, DSN(tt.dsn)) | ||
| }) | ||
| } | ||
| } | ||
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,20 @@ | ||
| package secret | ||
|
|
||
| func Mask(s string) string { | ||
| var ( | ||
| runes = []rune(s) | ||
| startPosition = 1 | ||
| endPosition = len(runes) - 1 | ||
| ) | ||
|
|
||
| if len(runes) < 5 { | ||
| startPosition = 0 | ||
| endPosition = len(runes) | ||
| } | ||
|
|
||
| for i := startPosition; i < endPosition; i++ { | ||
| runes[i] = '*' | ||
| } | ||
|
|
||
| return string(runes) | ||
| } |
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,35 @@ | ||
| package secret | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMask(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| s string | ||
| exp string | ||
| }{ | ||
| { | ||
| s: "test", | ||
| exp: "****", | ||
| }, | ||
| { | ||
| s: "test-long-password", | ||
| exp: "t****************d", | ||
| }, | ||
| { | ||
| s: "пароль", | ||
| exp: "п****ь", | ||
| }, | ||
| { | ||
| s: "пар", | ||
| exp: "***", | ||
| }, | ||
| } { | ||
| t.Run("", func(t *testing.T) { | ||
| require.Equal(t, tt.exp, Mask(tt.s)) | ||
| }) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,24 +1,5 @@ | ||
| package secret | ||
|
|
||
| import ( | ||
| "github.com/ydb-platform/ydb-go-sdk/v3/pkg/xstring" | ||
| ) | ||
|
|
||
| func Password(password string) string { | ||
| var ( | ||
| bytes = []byte(password) | ||
| startPosition = 3 | ||
| endPosition = len(bytes) - 2 | ||
| ) | ||
| if startPosition > endPosition { | ||
| for i := range bytes { | ||
| bytes[i] = '*' | ||
| } | ||
| } else { | ||
| for i := startPosition; i < endPosition; i++ { | ||
| bytes[i] = '*' | ||
| } | ||
| } | ||
|
|
||
| return xstring.FromBytes(bytes) | ||
| return Mask(password) | ||
| } |
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.
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.