Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed sentry auth token detector #3827

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

kashifkhan0771
Copy link
Contributor

Description:

This Pull request fixes github issue #3575
Screenshot from 2025-01-01 14-05-59

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@kashifkhan0771 kashifkhan0771 requested a review from a team as a code owner January 1, 2025 09:42
@kashifkhan0771 kashifkhan0771 linked an issue Jan 1, 2025 that may be closed by this pull request
func verifyToken(ctx context.Context, client *http.Client, token string) (map[string]string, bool, error) {
// api docs: https://docs.sentry.io/api/organizations/
// this api will return 200 for user auth tokens with scope of org:<>
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://sentry.io/api/0/organizations/", nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/projects API require project:<> scope and /organizations API require org:<> scope. I updated to organization API because we capture the organizations from response for extraData.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How common is it for a token to have org: scope versus project: scope?


return isVerified, err
// if response contain the forbiddenError message it means the token is active but does not have the right scope for this API call
if strings.Contains(fmt.Sprintf("%v", responseBody), forbiddenError) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the token is Active and does not have the org:<> scope the API returns 403 with a specific error message. In case token is removed the API return 401

@@ -20,7 +20,7 @@ import (
func TestSentryToken_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detectors3 vault limit is full

wantVerificationErr: true,
},
{
name: "found, good key but wrong scope",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed some tests which are not necessary.

@kashifkhan0771 kashifkhan0771 requested a review from a team as a code owner January 1, 2025 10:13
// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()

// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"sentry"}) + `\b([a-f0-9]{64})\b`)
keyPat = regexp.MustCompile(`\b(sntryu_[a-f0-9]{64})\b`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still possible to find tokens with the old format. You should create a new detector version, even if it calls the same code from v1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to legacy API keys? According to the documentation, the legacy authentication method using API keys is still supported. However, it states that these must be passed as Basic Auth. Does this mean the functionality was broken? 😕

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Either way, we should still detect old keys without the sntryu_ prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Either way, we should still detect old keys without the sntryu_ prefix.

Done Added new changes to version 2 ✅

pkg/detectors/sentrytoken/sentrytoken.go Outdated Show resolved Hide resolved
pkg/detectors/sentrytoken/sentrytoken.go Outdated Show resolved Hide resolved
func verifyToken(ctx context.Context, client *http.Client, token string) (map[string]string, bool, error) {
// api docs: https://docs.sentry.io/api/organizations/
// this api will return 200 for user auth tokens with scope of org:<>
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://sentry.io/api/0/organizations/", nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How common is it for a token to have org: scope versus project: scope?

Comment on lines 108 to 110
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, false, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary, you can read the body directly with json.NewDecoder(res.Body).Decode(...) instead of json.Unmarshal(...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How common is it for a token to have org: scope versus project: scope?

It depends on the task the user is creating the token for. When generating a token, users have the option to add any scope they need, with at least one scope being mandatory. The reason for using the organization API in this case is that we're trying to retrieve some organization-level data for extraData.

@kashifkhan0771 kashifkhan0771 requested a review from rgmz January 2, 2025 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Sentry Auth Token not detected
2 participants