|
| 1 | +package sentrytoken |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + |
| 11 | + regexp "github.com/wasilibs/go-re2" |
| 12 | + |
| 13 | + "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
| 14 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 15 | + "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
| 16 | +) |
| 17 | + |
| 18 | +type Scanner struct { |
| 19 | + client *http.Client |
| 20 | +} |
| 21 | + |
| 22 | +type Organization struct { |
| 23 | + ID string `json:"id"` |
| 24 | + Name string `json:"name"` |
| 25 | +} |
| 26 | + |
| 27 | +// Ensure the Scanner satisfies the interface at compile time. |
| 28 | +var _ detectors.Detector = (*Scanner)(nil) |
| 29 | +var _ detectors.Versioner = (*Scanner)(nil) |
| 30 | + |
| 31 | +var ( |
| 32 | + // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. |
| 33 | + keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"sentry"}) + `\b([a-f0-9]{64})\b`) |
| 34 | + |
| 35 | + forbiddenError = "You do not have permission to perform this action." |
| 36 | +) |
| 37 | + |
| 38 | +func (s Scanner) Version() int { |
| 39 | + return 1 |
| 40 | +} |
| 41 | + |
| 42 | +// Keywords are used for efficiently pre-filtering chunks. |
| 43 | +// Use identifiers in the secret preferably, or the provider name. |
| 44 | +func (s Scanner) Keywords() []string { |
| 45 | + return []string{"sentry"} |
| 46 | +} |
| 47 | + |
| 48 | +// FromData will find and optionally verify SentryToken secrets in a given set of bytes. |
| 49 | +func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { |
| 50 | + dataStr := string(data) |
| 51 | + |
| 52 | + // find all unique auth tokens |
| 53 | + var uniqueAuthTokens = make(map[string]struct{}) |
| 54 | + |
| 55 | + for _, authToken := range keyPat.FindAllStringSubmatch(dataStr, -1) { |
| 56 | + uniqueAuthTokens[authToken[1]] = struct{}{} |
| 57 | + } |
| 58 | + |
| 59 | + for authToken := range uniqueAuthTokens { |
| 60 | + s1 := detectors.Result{ |
| 61 | + DetectorType: detectorspb.DetectorType_SentryToken, |
| 62 | + Raw: []byte(authToken), |
| 63 | + } |
| 64 | + |
| 65 | + if verify { |
| 66 | + if s.client == nil { |
| 67 | + s.client = common.SaneHttpClient() |
| 68 | + } |
| 69 | + extraData, isVerified, verificationErr := VerifyToken(ctx, s.client, authToken) |
| 70 | + s1.Verified = isVerified |
| 71 | + s1.SetVerificationError(verificationErr, authToken) |
| 72 | + s1.ExtraData = extraData |
| 73 | + } |
| 74 | + |
| 75 | + results = append(results, s1) |
| 76 | + } |
| 77 | + |
| 78 | + return results, nil |
| 79 | +} |
| 80 | + |
| 81 | +func VerifyToken(ctx context.Context, client *http.Client, token string) (map[string]string, bool, error) { |
| 82 | + // api docs: https://docs.sentry.io/api/organizations/ |
| 83 | + // this api will return 200 for user auth tokens with scope of org:<> |
| 84 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://sentry.io/api/0/organizations/", nil) |
| 85 | + if err != nil { |
| 86 | + return nil, false, err |
| 87 | + } |
| 88 | + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 89 | + |
| 90 | + resp, err := client.Do(req) |
| 91 | + if err != nil { |
| 92 | + return nil, false, err |
| 93 | + } |
| 94 | + defer func() { |
| 95 | + _, _ = io.Copy(io.Discard, resp.Body) |
| 96 | + _ = resp.Body.Close() |
| 97 | + }() |
| 98 | + |
| 99 | + switch resp.StatusCode { |
| 100 | + case http.StatusOK: |
| 101 | + var organizations []Organization |
| 102 | + if err = json.NewDecoder(resp.Body).Decode(&organizations); err != nil { |
| 103 | + return nil, false, err |
| 104 | + } |
| 105 | + |
| 106 | + var extraData = make(map[string]string) |
| 107 | + for _, org := range organizations { |
| 108 | + extraData[fmt.Sprintf("orginzation_%s", org.ID)] = org.Name |
| 109 | + } |
| 110 | + |
| 111 | + return extraData, true, nil |
| 112 | + case http.StatusForbidden: |
| 113 | + var APIResp interface{} |
| 114 | + if err = json.NewDecoder(resp.Body).Decode(&APIResp); err != nil { |
| 115 | + return nil, false, err |
| 116 | + } |
| 117 | + |
| 118 | + // if response contain the forbiddenError message it means the token is active but does not have the right scope for this API call |
| 119 | + if strings.Contains(fmt.Sprintf("%v", APIResp), forbiddenError) { |
| 120 | + return nil, true, nil |
| 121 | + } |
| 122 | + |
| 123 | + return nil, false, nil |
| 124 | + case http.StatusUnauthorized: |
| 125 | + return nil, false, nil |
| 126 | + default: |
| 127 | + return nil, false, fmt.Errorf("unexpected HTTP response status %d", resp.StatusCode) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (s Scanner) Type() detectorspb.DetectorType { |
| 132 | + return detectorspb.DetectorType_SentryToken |
| 133 | +} |
| 134 | + |
| 135 | +func (s Scanner) Description() string { |
| 136 | + return "Sentry is an error tracking service that helps developers monitor and fix crashes in real time. Sentry tokens can be used to access and manage projects and organizations within Sentry." |
| 137 | +} |
0 commit comments