| 1 |
package basicauth |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 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 |
Name = "basic_auth" |
| 13 |
secretType = "HTTP Basic Authentication" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
basicAuthRegex = `(?i)(?:\"?authorization\"? *[:=] *)?\"?basic(?-i) +[a-zA-Z0-9+\/,_\-]{2,}={0,2}\"?` |
| 19 |
) |
| 20 |
|
| 21 |
func init() { |
| 22 |
secrets.GetDetectorFactory().Register(Name, NewDetector) |
| 23 |
} |
| 24 |
|
| 25 |
type detector struct { |
| 26 |
secrets.Detector |
| 27 |
} |
| 28 |
|
| 29 |
func NewDetector(config ...string) secrets.Detector { |
| 30 |
return &detector{ |
| 31 |
Detector: helpers.NewRegexDetectorBuilder(secretType, basicAuthRegex).WithVerifier(isParameterValidBase64).WithKeyValueRegexWithoutNewLine().Build(), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func isParameterValidBase64(_, s string) bool { |
| 36 |
words := strings.Split(s, " ") |
| 37 |
if len(words) == 0 { |
| 38 |
return false |
| 39 |
} |
| 40 |
parameter := words[len(words)-1] |
| 41 |
if parameter[len(parameter)-1] == '"' { |
| 42 |
parameter = parameter[:len(parameter)-1] |
| 43 |
} |
| 44 |
|
| 45 |
encodedValue, err := base64.StdEncoding.DecodeString(parameter) |
| 46 |
if err != nil { |
| 47 |
return false |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
for _, char := range encodedValue[1 : len(encodedValue)-1] { |
| 52 |
if char == ':' { |
| 53 |
return true |
| 54 |
} |
| 55 |
} |
| 56 |
return false |
| 57 |
} |