Skip to content

Commit

Permalink
Improve domain / url handling in detectors (#3221)
Browse files Browse the repository at this point in the history
* Strip path and params and use new client

* update clients

* additional client updates

* revert client
  • Loading branch information
dustin-decker authored Aug 15, 2024
1 parent e8a297f commit fe5624c
Show file tree
Hide file tree
Showing 49 changed files with 385 additions and 124 deletions.
3 changes: 1 addition & 2 deletions pkg/detectors/aha/aha.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -22,7 +21,7 @@ var (
// Ensure the Scanner satisfies the interface at compile time.
_ detectors.Detector = (*Scanner)(nil)

defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"aha"}) + `\b([0-9a-f]{64})\b`)
Expand Down
6 changes: 3 additions & 3 deletions pkg/detectors/apiflash/apiflash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package apiflash
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -20,7 +20,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"apiflash"}) + `\b([a-z0-9]{32})\b`)
Expand Down
3 changes: 1 addition & 2 deletions pkg/detectors/artifactory/artifactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -22,7 +21,7 @@ var (
// Ensure the Scanner satisfies the interface at compile time.
_ detectors.Detector = (*Scanner)(nil)

defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(`\b([a-zA-Z0-9]{73}|\b[a-zA-Z0-9]{64})`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -22,7 +21,7 @@ var _ detectors.Detector = (*Scanner)(nil)
var _ detectors.MaxSecretSizeProvider = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithLocalAddresses

// long jwt token but note this is default 8640000 seconds = 24 hours but could be set to maximum 2592000 seconds = 720 hours = 30 days
// at https://manage.auth0.com/dashboard/us/dev-63memjo3/apis/management/explorer
Expand Down
8 changes: 4 additions & 4 deletions pkg/detectors/auth0oauth/auth0oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package auth0oauth

import (
"context"
regexp "github.com/wasilibs/go-re2"
"io"
"net/http"
"net/url"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -21,7 +21,7 @@ type Scanner struct{
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithLocalAddresses

clientIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"auth0"}) + `\b([a-zA-Z0-9_-]{32,60})\b`)
clientSecretPat = regexp.MustCompile(`\b([a-zA-Z0-9_-]{64,})\b`)
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result

if !strings.Contains(body, "access_denied") {
s1.Verified = true
}
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/detectors/azurebatch/azurebatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -27,7 +26,7 @@ var _ detectors.Detector = (*Scanner)(nil)
var _ detectors.CustomFalsePositiveChecker = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
urlPat = regexp.MustCompile(`https://(.{1,50})\.(.{1,50})\.batch\.azure\.com`)
secretPat = regexp.MustCompile(`[A-Za-z0-9+/=]{88}`)
Expand Down
3 changes: 1 addition & 2 deletions pkg/detectors/azurefunctionkey/azurefunctionkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -22,7 +21,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"azure"}) + `\b([a-zA-Z0-9_-]{20,56})\b={0,2}`)
azureUrlPat = regexp.MustCompile(`\bhttps:\/\/([a-zA-Z0-9-]{2,30})\.azurewebsites\.net\/api\/([a-zA-Z0-9-]{2,30})\b`)
Expand Down
3 changes: 1 addition & 2 deletions pkg/detectors/azuresearchquerykey/azuresearchquerykey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -22,7 +21,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"azure"}) + `\b([0-9a-zA-Z]{52})\b`)
urlPat = regexp.MustCompile(detectors.PrefixRegex([]string{"azure"}) + `https:\/\/([0-9a-z]{5,40})\.search\.windows\.net\/indexes\/([0-9a-z]{5,40})\b`)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detectors/azurestorage/azurestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = http.DefaultClient
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses
keyPat = regexp.MustCompile(`DefaultEndpointsProtocol=https;AccountName=(?P<account_name>[^;]+);AccountKey=(?P<account_key>[^;]+);EndpointSuffix=core\.windows\.net`)
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/detectors/caspio/caspio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package caspio
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -20,7 +20,7 @@ type Scanner struct{
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"caspio"}) + `\b([a-z0-9]{50})\b`)
Expand Down
6 changes: 3 additions & 3 deletions pkg/detectors/databrickstoken/databrickstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package databrickstoken
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -21,7 +21,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
domain = regexp.MustCompile(`\b([a-z0-9-]+(?:\.[a-z0-9-]+)*\.(cloud\.databricks\.com|gcp\.databricks\.com|azurewebsites\.net))\b`)
Expand Down
6 changes: 3 additions & 3 deletions pkg/detectors/deputy/deputy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package deputy
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -20,7 +20,7 @@ type Scanner struct{
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"deputy"}) + `\b([0-9a-z]{32})\b`)
Expand Down
10 changes: 10 additions & 0 deletions pkg/detectors/detectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,13 @@ func RedactURL(u url.URL) string {
u.User = url.UserPassword(u.User.Username(), "********")
return strings.TrimSpace(strings.Replace(u.String(), "%2A", "*", -1))
}

func ParseURLAndStripPathAndParams(u string) (*url.URL, error) {
parsedURL, err := url.Parse(u)
if err != nil {
return nil, err
}
parsedURL.Path = ""
parsedURL.RawQuery = ""
return parsedURL, nil
}
3 changes: 1 addition & 2 deletions pkg/detectors/discordwebhook/discordwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -18,7 +17,7 @@ type Scanner struct{}
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(`(https:\/\/discord\.com\/api\/webhooks\/[0-9]{18}\/[0-9a-zA-Z-]{68})`)
Expand Down
6 changes: 3 additions & 3 deletions pkg/detectors/fibery/fibery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package fibery
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"
"time"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -21,7 +21,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()
client = detectors.DetectorHttpClientWithNoLocalAddresses

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"fibery"}) + `\b([0-9a-f]{8}.[0-9a-f]{35})\b`)
Expand Down
8 changes: 4 additions & 4 deletions pkg/detectors/freshbooks/freshbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package freshbooks
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"io"
"net/http"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -21,10 +21,10 @@ type Scanner struct{
var _ detectors.Detector = (*Scanner)(nil)

var (
client = common.SaneHttpClient()

client = detectors.DetectorHttpClientWithNoLocalAddresses
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"freshbooks"}) + `\b([0-9a-z]{64})\b`)
// TODO: this domain pattern is too restrictive
uriPat = regexp.MustCompile(detectors.PrefixRegex([]string{"freshbooks"}) + `\b(https://www.[0-9A-Za-z_-]{1,}.com)\b`)
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/detectors/grafanaserviceaccount/grafanaserviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package grafanaserviceaccount
import (
"context"
"fmt"
regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
Expand All @@ -21,7 +21,7 @@ type Scanner struct {
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()
defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(`\b(glsa_[0-9a-zA-Z_]{41})\b`)
domainPat = regexp.MustCompile(`\b([a-zA-Z0-9-]+\.grafana\.net)\b`)
Expand Down
Loading

0 comments on commit fe5624c

Please sign in to comment.