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