Debian Codegraph @

pkg/detectors/generic/url_password_detector.go

raw symbols SCIP
1 package generic
2
3 import (
4 "net/url"
5 "strings"
6
7 "github.com/DefangLabs/secret-detector/pkg/detectors/helpers"
8 "github.com/DefangLabs/secret-detector/pkg/secrets"
9 )
10
11 const (
12 URLPasswordDetectorName = "url_password"
13 urlPasswordDetectorSecretType = "URL with password"
14
15 // urlPasswordRegex represents a regex that matches urls with user & password.
16 // e.g. scheme://user:[email protected]/
17 urlPasswordRegex = `[a-z][a-z0-9+.-]+://[^:\s]*:[^@:\s]+@[^\s'"\];]+`
18 )
19
20 func init() {
21 secrets.GetDetectorFactory().Register(URLPasswordDetectorName, NewURLPasswordDetector)
22 }
23
24 type urlPasswordDetector struct {
25 secrets.Detector
26 }
27
28 func NewURLPasswordDetector(config ...string) secrets.Detector {
29 d := &urlPasswordDetector{}
30 d.Detector = helpers.NewRegexDetectorBuilder(urlPasswordDetectorSecretType, urlPasswordRegex).WithVerifier(isUrlWithPassword).Build()
31 return d
32 }
33
34 func isUrlWithPassword(_, s string) bool {
35 u, _ := url.Parse(s)
36 if u == nil || u.User == nil {
37 return false
38 }
39
40 pwd, _ := u.User.Password()
41 return pwd != "" && !strings.HasPrefix(pwd, "$")
42 }