Skip to content
Changes

Reference

serval · Nov 11, 2026

What this change means

Atlas added serval to its detector catalog in this update.

pkg/detectors/serval/serval.go+213−0View file
@@ -0,0 +1,213 @@
1added: package serval
2added:
3added: import (
4added: "cmp"
5added: "context"
6added: "fmt"
7added: "hash/crc32"
8added: "io"
9added: "net/http"
10added: "net/url"
11added: "strings"
12added:
13added: regexp "github.com/wasilibs/go-re2"
14added:
15added: "github.com/trufflesecurity/trufflehog/v3/pkg/common"
16added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
17added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
18added: )
19added:
20added: type Scanner struct {
21added: client *http.Client
22added: // endpoints overrides regionEndpoints in tests.
23added: endpoints []string
24added: }
25added:
26added: // Ensure the Scanner satisfies the interface at compile time.
27added: var _ detectors.Detector = (*Scanner)(nil)
28added:
29added: // regionEndpoints are the public API hosts of Serval's cloud regions. Regions
30added: // are isolated, so a key issued in one is unknown to the other and every
31added: // region must be tried before a key is called invalid.
32added: var regionEndpoints = []string{
33added: "https://public.api.serval.com",
34added: "https://public.eu1.serval.com",
35added: }
36added:
37added: var (
38added: defaultClient = common.SaneHttpClient()
39added:
40added: // Serval API key secrets are svl_secret_<tag>_<43 base62 random><6 base62
41added: // CRC32 checksum>. The tag names the key type: pk public API, wk
42added: // self-hosted worker, mk Serval-managed worker.
43added: secretPat = regexp.MustCompile(`\b(svl_secret_(pk|wk|mk)_([0-9A-Za-z]{43})([0-9A-Za-z]{6}))\b`)
44added:
45added: // The client ID that pairs with a secret is the API key's UUID.
46added: idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"serval", "client"}) +
47added: `\b([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\b`)
48added: )
49added:
50added: var keyTypes = map[string]string{
51added: "pk": "public_api",
52added: "wk": "worker",
53added: "mk": "managed_worker",
54added: }
55added:
56added: const base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
57added:
58added: // Keywords are used for efficiently pre-filtering chunks.
59added: // Use identifiers in the secret preferably, or the provider name.
60added: func (s Scanner) Keywords() []string {
61added: return []string{"svl_secret_"}
62added: }
63added:
64added: // FromData will find and optionally verify Serval API keys in a given set of bytes.
65added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
66added: dataStr := string(data)
67added:
68added: secrets := make(map[string]string)
69added: for _, match := range secretPat.FindAllStringSubmatch(dataStr, -1) {
70added: tag, random, checksum := match[2], match[3], match[4]
71added: // A failed checksum means the match is not a Serval-issued secret.
72added: if secretChecksum(tag, random) != checksum {
73added: continue
74added: }
75added: secrets[match[1]] = tag
76added: }
77added: if len(secrets) == 0 {
78added: return nil, nil
79added: }
80added:
81added: ids := make(map[string]struct{})
82added: for _, match := range idPat.FindAllStringSubmatch(dataStr, -1) {
83added: ids[strings.ToLower(match[1])] = struct{}{}
84added: }
85added:
86added: for secret, tag := range secrets {
87added: // Verification needs the client ID, so a secret found without one
88added: // is still reported, unverified.
89added: if len(ids) == 0 {
90added: results = append(results, detectors.Result{
91added: DetectorType: detector_typepb.DetectorType_Serval,
92added: Raw: []byte(secret),
93added: SecretParts: map[string]string{"client_secret": secret},
94added: ExtraData: map[string]string{"key_type": keyTypes[tag]},
95added: })
96added: continue
97added: }
98added:
99added: for id := range ids {
100added: r := detectors.Result{
101added: DetectorType: detector_typepb.DetectorType_Serval,
102added: Raw: []byte(secret),
103added: RawV2: []byte(id + ":" + secret),
104added: SecretParts: map[string]string{"client_id": id, "client_secret": secret},
105added: ExtraData: map[string]string{"key_type": keyTypes[tag]},
106added: }
107added:
108added: // Only public API keys can exchange credentials at the public
109added: // token endpoint. It rejects worker keys with the same 401 as a
110added: // wrong secret, so verifying them would report live keys as
111added: // invalid.
112added: if verify && tag == "pk" {
113added: client := cmp.Or(s.client, defaultClient)
114added: endpoints := s.endpoints
115added: if len(endpoints) == 0 {
116added: endpoints = regionEndpoints
117added: }
118added: isVerified, endpoint, vErr := verifyInRegions(ctx, client, endpoints, id, secret)
119added: r.Verified = isVerified
120added: if isVerified {
121added: r.ExtraData["endpoint"] = endpoint
122added: }
123added: r.SetVerificationError(vErr, secret)
124added: }
125added:
126added: results = append(results, r)
127added:
128added: // A secret belongs to exactly one client ID, so once a pair
129added: // verifies, trying the remaining IDs only spends rate limit.
130added: if r.Verified {
131added: break
132added: }
133added: }
134added: }
135added:
136added: return results, nil
137added: }
138added:
139added: // secretChecksum mirrors Serval's issuer: CRC32 (IEEE) of "<tag>_<random>",
140added: // written big-endian as six base62 digits.
141added: func secretChecksum(tag, random string) string {
142added: sum := crc32.ChecksumIEEE([]byte(tag + "_" + random))
143added: out := make([]byte, 6)
144added: for i := len(out) - 1; i >= 0; i-- {
145added: out[i] = base62Alphabet[sum%62]
146added: sum /= 62
147added: }
148added: return string(out)
149added: }
150added:
151added: // verifyInRegions tries each region until one verifies the key and returns
152added: // the endpoint that did. A 401 from one region does not override an
153added: // indeterminate error from another, since that region may own the key.
154added: func verifyInRegions(ctx context.Context, client *http.Client, endpoints []string, id, secret string) (bool, string, error) {
155added: var lastErr error
156added: for _, endpoint := range endpoints {
157added: isVerified, err := verifyKey(ctx, client, endpoint, id, secret)
158added: if isVerified {
159added: return true, endpoint, nil
160added: }
161added: if err != nil {
162added: lastErr = err
163added: }
164added: }
165added: return false, "", lastErr
166added: }
167added:
168added: // verifyKey exchanges the credentials for an access token with the OAuth2
169added: // client credentials grant. The token in a successful response is discarded.
170added: //
171added: // The error is non-nil only when verification is indeterminate. Serval
172added: // answers 401 for any unknown, revoked, expired or mismatched key; every
173added: // other status (429 rate limit, 5xx) says nothing about the key.
174added: func verifyKey(ctx context.Context, client *http.Client, baseURL, id, secret string) (bool, error) {
175added: endpoint, err := url.JoinPath(baseURL, "/v2/auth/token")
176added: if err != nil {
177added: return false, err
178added: }
179added:
180added: form := url.Values{"grant_type": {"client_credentials"}}
181added: req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(form.Encode()))
182added: if err != nil {
183added: return false, err
184added: }
185added: req.SetBasicAuth(id, secret)
186added: req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
187added:
188added: res, err := client.Do(req)
189added: if err != nil {
190added: return false, err
191added: }
192added: defer func() {
193added: _, _ = io.Copy(io.Discard, res.Body)
194added: _ = res.Body.Close()
195added: }()
196added:
197added: switch res.StatusCode {
198added: case http.StatusOK:
199added: return true, nil
200added: case http.StatusUnauthorized:
201added: return false, nil
202added: default:
203added: return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
204added: }
205added: }
206added:
207added: func (s Scanner) Type() detector_typepb.DetectorType {
208added: return detector_typepb.DetectorType_Serval
209added: }
210added:
211added: func (s Scanner) Description() string {
212added: return "Serval is a workflow automation platform for IT and security teams. API keys pair a client ID with a secret and can call the Serval public API or run self-hosted workers."
213added: }