Skip to content

Commit

Permalink
Update sqlserver redaction, deduplication, and URI redaction (truffle…
Browse files Browse the repository at this point in the history
…security#1369)

* Update sqlserver redaction, deduplication, and URI redaction

* don't use pointer
  • Loading branch information
dustin-decker authored Jun 9, 2023
1 parent c28c70b commit ca19472
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pkg/detectors/detectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package detectors

import (
"context"
"net/url"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -153,3 +154,8 @@ func MustGetBenchmarkData() map[string][]byte {
"big": big,
}
}

func RedactURL(u url.URL) string {
u.User = url.UserPassword(u.User.Username(), "********")
return strings.TrimSpace(strings.Replace(u.String(), "%2A", "*", -1))
}
10 changes: 6 additions & 4 deletions pkg/detectors/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ func (s Scanner) Keywords() []string {
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
matches := pattern.FindAllStringSubmatch(string(data), -1)
for _, match := range matches {
params, _, err := msdsn.Parse(match[1])
paramsUnsafe, _, err := msdsn.Parse(match[1])
if err != nil {
continue
}

if params.Password == "" {
if paramsUnsafe.Password == "" {
continue
}

detected := detectors.Result{
DetectorType: detectorspb.DetectorType_SQLServer,
Raw: []byte(params.Password),
Raw: []byte(paramsUnsafe.Password),
RawV2: []byte(paramsUnsafe.URL().String()),
Redacted: detectors.RedactURL(*paramsUnsafe.URL()),
}

if verify {
verified, err := ping(params)
verified, err := ping(paramsUnsafe)
if err != nil {
} else {
detected.Verified = verified
Expand Down
3 changes: 3 additions & 0 deletions pkg/detectors/sqlserver/sqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestSQLServer_FromChunk(t *testing.T) {
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_SQLServer,
Redacted: "sqlserver://sa:********@localhost?database=Demo&disableRetry=false",
Verified: true,
},
},
Expand All @@ -66,6 +67,7 @@ func TestSQLServer_FromChunk(t *testing.T) {
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_SQLServer,
Redacted: "sqlserver://sa:********@localhost?disableRetry=false",
Verified: false,
},
},
Expand Down Expand Up @@ -103,6 +105,7 @@ func TestSQLServer_FromChunk(t *testing.T) {
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_SQLServer,
Redacted: "sqlserver://username:********@server_name?database=testdb&disableRetry=false",
Verified: true,
},
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/detectors/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
rawURLStr := rawURL.String()
// Removing the path causes possible deduplication issues if some paths have basic auth and some do not.
rawURL.Path = ""
redact := strings.TrimSpace(strings.Replace(rawURL.String(), password, "********", -1))

s := detectors.Result{
DetectorType: detectorspb.DetectorType_URI,
Raw: []byte(rawURL.String()),
RawV2: []byte(rawURLStr),
Redacted: redact,
Redacted: detectors.RedactURL(*rawURL),
}

if verify {
Expand Down

0 comments on commit ca19472

Please sign in to comment.