Skip to content

Commit 6961f2b

Browse files
authored
OpenAI Admin Key Detector (#4689)
* restrict openai detector regex to skip admin key * add openai admin key detector * fix: add missing hyphen in openai detector regex * incorporated feedback added tests to check exclusivity tightened regexes * remove redundant comment * uniformity in openai regex
1 parent 4158734 commit 6961f2b

File tree

9 files changed

+497
-9
lines changed

9 files changed

+497
-9
lines changed

pkg/detectors/openai/openai.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ var (
2828
defaultClient = common.SaneHttpClient()
2929

3030
// The magic string T3BlbkFJ is the base64-encoded string: OpenAI
31-
keyPat = regexp.MustCompile(`\b(sk-[[:alnum:]_-]+T3BlbkFJ[[:alnum:]_-]+)\b`)
31+
// Matches: legacy keys (sk-{alnum}T3BlbkFJ...), project keys (sk-proj-...),
32+
// service account keys (sk-svcacct-... or sk-service-...)
33+
// Does NOT match: admin keys (sk-admin-...)
34+
keyPat = regexp.MustCompile(`\b(sk-(?:(?:proj|svcacct|service)-[A-Za-z0-9_-]+|[a-zA-Z0-9]+)T3BlbkFJ[A-Za-z0-9_-]+)\b`)
3235
)
3336

3437
// Keywords are used for efficiently pre-filtering chunks.

pkg/detectors/openai/openai_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func TestOpenAI_FromChunk(t *testing.T) {
20-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
20+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
2121
defer cancel()
2222
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
2323
if err != nil {

pkg/detectors/openai/openai_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ import (
88
"testing"
99
)
1010

11+
func TestOpenAI_DoesNotMatchAdminKeys(t *testing.T) {
12+
d := Scanner{}
13+
adminKey := `OPENAI_ADMIN_KEY = "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"`
14+
15+
results, err := d.FromData(context.Background(), false, []byte(adminKey))
16+
if err != nil {
17+
t.Fatalf("unexpected error: %v", err)
18+
}
19+
20+
if len(results) != 0 {
21+
t.Errorf("openai detector should not match admin keys, but got %d results", len(results))
22+
}
23+
}
24+
1125
func TestOpenAI_Pattern(t *testing.T) {
1226
d := Scanner{}
1327
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package openaiadmin
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
9+
regexp "github.com/wasilibs/go-re2"
10+
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
12+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
13+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
14+
)
15+
16+
type Scanner struct {
17+
client *http.Client
18+
}
19+
20+
// Ensure the Scanner satisfies the interface at compile time.
21+
var _ detectors.Detector = (*Scanner)(nil)
22+
23+
var (
24+
defaultClient = common.SaneHttpClient()
25+
26+
// Admin keys follow the format: sk-admin-{58 chars}T3BlbkFJ{58 chars}
27+
// Total length: 133 chars (9 char prefix + 124 chars for key)
28+
// where T3BlbkFJ is the base64-encoded string: OpenAI
29+
keyPat = regexp.MustCompile(`\b(sk-admin-[A-Za-z0-9_-]{58}T3BlbkFJ[A-Za-z0-9_-]{58})\b`)
30+
)
31+
32+
// Keywords are used for efficiently pre-filtering chunks.
33+
func (s Scanner) Keywords() []string {
34+
return []string{"sk-admin-"}
35+
}
36+
37+
// FromData will find and optionally verify Openaiadmin secrets in a given set of bytes.
38+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
39+
dataStr := string(data)
40+
41+
uniqueMatches := make(map[string]struct{})
42+
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
43+
uniqueMatches[match[1]] = struct{}{}
44+
}
45+
46+
for token := range uniqueMatches {
47+
s1 := detectors.Result{
48+
DetectorType: detectorspb.DetectorType_OpenAIAdmin,
49+
Redacted: token[:11] + "..." + token[len(token)-4:],
50+
Raw: []byte(token),
51+
}
52+
53+
if verify {
54+
client := s.client
55+
if client == nil {
56+
client = defaultClient
57+
}
58+
59+
isVerified, verificationErr := verifyMatch(ctx, client, token)
60+
s1.Verified = isVerified
61+
s1.SetVerificationError(verificationErr, token)
62+
s1.AnalysisInfo = map[string]string{
63+
"key": token,
64+
}
65+
}
66+
67+
results = append(results, s1)
68+
}
69+
70+
return
71+
}
72+
73+
func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) {
74+
// Use the Admin API Keys list endpoint to verify the admin key
75+
// https://platform.openai.com/docs/api-reference/admin-api-keys/list
76+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.openai.com/v1/organization/admin_api_keys", http.NoBody)
77+
if err != nil {
78+
return false, err
79+
}
80+
81+
req.Header.Set("Content-Type", "application/json")
82+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
83+
84+
res, err := client.Do(req)
85+
if err != nil {
86+
return false, err
87+
}
88+
defer func() {
89+
_, _ = io.Copy(io.Discard, res.Body)
90+
_ = res.Body.Close()
91+
}()
92+
93+
switch res.StatusCode {
94+
case http.StatusOK:
95+
return true, nil
96+
case http.StatusUnauthorized:
97+
// Invalid admin key - determinate failure
98+
return false, nil
99+
default:
100+
// Unexpected response - indeterminate failure
101+
return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
102+
}
103+
}
104+
105+
func (s Scanner) Type() detectorspb.DetectorType {
106+
return detectorspb.DetectorType_OpenAIAdmin
107+
}
108+
109+
func (s Scanner) Description() string {
110+
return "OpenAI Admin API keys provide administrative access to OpenAI organization resources. These keys can be used to manage API keys, audit logs, and other organization-level settings."
111+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package openaiadmin
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"testing"
10+
"time"
11+
12+
"github.com/google/go-cmp/cmp"
13+
"github.com/google/go-cmp/cmp/cmpopts"
14+
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
17+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
18+
)
19+
20+
func TestOpenAIAdmin_FromChunk(t *testing.T) {
21+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
22+
defer cancel()
23+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors6")
24+
if err != nil {
25+
t.Fatalf("could not get test secrets from GCP: %s", err)
26+
}
27+
secret := testSecrets.MustGetField("OPENAI_ADMIN")
28+
inactiveSecret := testSecrets.MustGetField("OPENAI_ADMIN_INACTIVE")
29+
30+
type args struct {
31+
ctx context.Context
32+
data []byte
33+
verify bool
34+
}
35+
tests := []struct {
36+
name string
37+
s Scanner
38+
args args
39+
want []detectors.Result
40+
wantErr bool
41+
wantVerificationErr bool
42+
}{
43+
{
44+
name: "found, verified",
45+
s: Scanner{},
46+
args: args{
47+
ctx: context.Background(),
48+
data: []byte(fmt.Sprintf("You can find an OpenAI admin secret %s within", secret)),
49+
verify: true,
50+
},
51+
want: []detectors.Result{
52+
{
53+
DetectorType: detectorspb.DetectorType_OpenAIAdmin,
54+
Verified: true,
55+
},
56+
},
57+
wantErr: false,
58+
wantVerificationErr: false,
59+
},
60+
{
61+
name: "found, unverified",
62+
s: Scanner{},
63+
args: args{
64+
ctx: context.Background(),
65+
data: []byte(fmt.Sprintf("You can find an OpenAI admin secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
66+
verify: true,
67+
},
68+
want: []detectors.Result{
69+
{
70+
DetectorType: detectorspb.DetectorType_OpenAIAdmin,
71+
Verified: false,
72+
},
73+
},
74+
wantErr: false,
75+
wantVerificationErr: false,
76+
},
77+
{
78+
name: "not found",
79+
s: Scanner{},
80+
args: args{
81+
ctx: context.Background(),
82+
data: []byte("You cannot find the secret within"),
83+
verify: true,
84+
},
85+
want: nil,
86+
wantErr: false,
87+
wantVerificationErr: false,
88+
},
89+
{
90+
name: "found, would be verified if not for timeout",
91+
s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},
92+
args: args{
93+
ctx: context.Background(),
94+
data: []byte(fmt.Sprintf("You can find an OpenAI admin secret %s within", secret)),
95+
verify: true,
96+
},
97+
want: []detectors.Result{
98+
{
99+
DetectorType: detectorspb.DetectorType_OpenAIAdmin,
100+
Verified: false,
101+
},
102+
},
103+
wantErr: false,
104+
wantVerificationErr: true,
105+
},
106+
{
107+
name: "found, verified but unexpected api surface",
108+
s: Scanner{client: common.ConstantResponseHttpClient(404, "")},
109+
args: args{
110+
ctx: context.Background(),
111+
data: []byte(fmt.Sprintf("You can find an OpenAI admin secret %s within", secret)),
112+
verify: true,
113+
},
114+
want: []detectors.Result{
115+
{
116+
DetectorType: detectorspb.DetectorType_OpenAIAdmin,
117+
Verified: false,
118+
},
119+
},
120+
wantErr: false,
121+
wantVerificationErr: true,
122+
},
123+
}
124+
for _, tt := range tests {
125+
t.Run(tt.name, func(t *testing.T) {
126+
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
127+
if (err != nil) != tt.wantErr {
128+
t.Errorf("OpenAIAdmin.FromData() error = %v, wantErr %v", err, tt.wantErr)
129+
return
130+
}
131+
for i := range got {
132+
if len(got[i].Raw) == 0 {
133+
t.Fatalf("no raw secret present: \n %+v", got[i])
134+
}
135+
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
136+
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
137+
}
138+
}
139+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "AnalysisInfo", "Redacted")
140+
ignoreUnexported := cmpopts.IgnoreUnexported(detectors.Result{})
141+
if diff := cmp.Diff(got, tt.want, ignoreOpts, ignoreUnexported); diff != "" {
142+
t.Errorf("OpenAIAdmin.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
143+
}
144+
})
145+
}
146+
}
147+
148+
func BenchmarkFromData(benchmark *testing.B) {
149+
ctx := context.Background()
150+
s := Scanner{}
151+
for name, data := range detectors.MustGetBenchmarkData() {
152+
benchmark.Run(name, func(b *testing.B) {
153+
b.ResetTimer()
154+
for n := 0; n < b.N; n++ {
155+
_, err := s.FromData(ctx, false, data)
156+
if err != nil {
157+
b.Fatal(err)
158+
}
159+
}
160+
})
161+
}
162+
}

0 commit comments

Comments
 (0)