Skip to content

Commit

Permalink
Merge pull request #191 from aircraft-cerier/master
Browse files Browse the repository at this point in the history
go-mysql password sanitizing support
  • Loading branch information
bhautikpip authored Feb 19, 2020
2 parents 16d917b + dbd138f commit 664b447
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions xray/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ func stripPasswords(dsn string) string {
inBraces = false
buf.UnreadByte()
}
case '@':
if strings.Contains(res.String(), ":") {
resLen := res.Len()
if resLen > 0 && res.Bytes()[resLen-1] == ':' {
res.Truncate(resLen - 1)
}
isPassword = true
flush()
res.WriteByte(c)
}
}
}
inBraces = false
Expand Down
43 changes: 43 additions & 0 deletions xray/sql_go111_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.

// +build go1.11

package xray

func (s *sqlTestSuite) TestMySQLPasswordConnectionString() {
s.mockDB("username:password@protocol(address:1234)/dbname?param=value")
s.mockMySQL(nil)
s.connect()

s.Require().NoError(s.mock.ExpectationsWereMet())
if s.db.connectionString != "" {
s.Equal("username@protocol(address:1234)/dbname?param=value", s.db.connectionString)
s.Equal("", s.db.url)
}
if s.db.url != "" {
s.Equal("username@protocol(address:1234)/dbname?param=value", s.db.url)
s.Equal("", s.db.connectionString)
}
}

func (s *sqlTestSuite) TestMySQLPasswordlessConnectionString() {
s.mockDB("username@protocol(address:1234)/dbname?param=value")
s.mockMySQL(nil)
s.connect()

s.Require().NoError(s.mock.ExpectationsWereMet())
if s.db.connectionString != "" {
s.Equal("username@protocol(address:1234)/dbname?param=value", s.db.connectionString)
s.Equal("", s.db.url)
}
if s.db.url != "" {
s.Equal("username@protocol(address:1234)/dbname?param=value", s.db.url)
s.Equal("", s.db.connectionString)
}
}
31 changes: 31 additions & 0 deletions xray/sql_prego111_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.

// +build !go1.11

package xray

func (s *sqlTestSuite) TestMySQLPasswordConnectionStringPreGo111() {
s.mockDB("username:password@protocol(address:1234)/dbname?param=value")
s.mockMySQL(nil)
s.connect()

s.Require().NoError(s.mock.ExpectationsWereMet())
s.Equal("", s.db.connectionString)
s.Equal("username@protocol(address:1234)/dbname?param=value", s.db.url)
}

func (s *sqlTestSuite) TestMySQLPasswordlessConnectionStringPreGo111() {
s.mockDB("username@protocol(address:1234)/dbname?param=value")
s.mockMySQL(nil)
s.connect()

s.Require().NoError(s.mock.ExpectationsWereMet())
s.Equal("", s.db.connectionString)
s.Equal("username@protocol(address:1234)/dbname?param=value", s.db.url)
}

0 comments on commit 664b447

Please sign in to comment.