* added braintrust detector * made keyword more specific * Enhance Braintrust detector description * regen protos and updated test * add feature flag gating
136 lines
2.9 KiB
Go
136 lines
2.9 KiB
Go
package braintrust
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
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/detector_typepb"
|
|
)
|
|
|
|
type Scanner struct {
|
|
client *http.Client
|
|
}
|
|
|
|
// Compile-time interface check
|
|
var _ detectors.Detector = (*Scanner)(nil)
|
|
|
|
var (
|
|
defaultClient = common.SaneHttpClient()
|
|
|
|
// Braintrust API keys:
|
|
// Format: sk- + 48 alphanumeric characters (observed)
|
|
braintrustTokenPat = regexp.MustCompile(
|
|
`\b(sk-[A-Za-z0-9]{48})\b`,
|
|
)
|
|
)
|
|
|
|
// Keywords used for fast pre-filtering
|
|
func (s Scanner) Keywords() []string {
|
|
return []string{"braintrust"}
|
|
}
|
|
|
|
func (s Scanner) getClient() *http.Client {
|
|
if s.client != nil {
|
|
return s.client
|
|
}
|
|
return defaultClient
|
|
}
|
|
|
|
// FromData scans for Braintrust API tokens and optionally verifies them
|
|
func (s Scanner) FromData(
|
|
ctx context.Context,
|
|
verify bool,
|
|
data []byte,
|
|
) (results []detectors.Result, err error) {
|
|
|
|
dataStr := string(data)
|
|
|
|
uniqueTokens := make(map[string]struct{})
|
|
for _, match := range braintrustTokenPat.FindAllStringSubmatch(dataStr, -1) {
|
|
uniqueTokens[match[1]] = struct{}{}
|
|
}
|
|
|
|
for token := range uniqueTokens {
|
|
result := detectors.Result{
|
|
DetectorType: detector_typepb.DetectorType_BrainTrustApiKey,
|
|
Raw: []byte(token),
|
|
Redacted: token[:8] + "...",
|
|
SecretParts: map[string]string{
|
|
"token": token,
|
|
},
|
|
}
|
|
|
|
if verify {
|
|
verified, verificationErr := verifyBraintrustToken(
|
|
ctx,
|
|
s.getClient(),
|
|
token,
|
|
)
|
|
result.SetVerificationError(verificationErr, token)
|
|
result.Verified = verified
|
|
}
|
|
|
|
results = append(results, result)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func verifyBraintrustToken(
|
|
ctx context.Context,
|
|
client *http.Client,
|
|
token string,
|
|
) (bool, error) {
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
ctx,
|
|
http.MethodGet,
|
|
"https://api.braintrust.dev/v1/project?limit=1",
|
|
http.NoBody,
|
|
)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer func() {
|
|
_, _ = io.Copy(io.Discard, res.Body)
|
|
_ = res.Body.Close()
|
|
}()
|
|
|
|
switch res.StatusCode {
|
|
case http.StatusOK:
|
|
return true, nil
|
|
case http.StatusUnauthorized:
|
|
// Invalid or revoked token
|
|
return false, nil
|
|
case http.StatusForbidden:
|
|
// Valid token but lacks permission (still valid)
|
|
return true, nil
|
|
default:
|
|
return false, fmt.Errorf(
|
|
"unexpected HTTP response status %d",
|
|
res.StatusCode,
|
|
)
|
|
}
|
|
}
|
|
|
|
func (s Scanner) Type() detector_typepb.DetectorType {
|
|
return detector_typepb.DetectorType_BrainTrustApiKey
|
|
}
|
|
|
|
func (s Scanner) Description() string {
|
|
return "Braintrust is an AI evaluation and observability platform. Braintrust API keys can be used to access evaluation, observability, and AI development resources within the Braintrust platform."
|
|
}
|