Skip to content

Commit

Permalink
Attach the X-Sentry-Auth header to /envelope requests (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Sep 4, 2023
1 parent cd0b874 commit bee7c46
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 0 additions & 1 deletion sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const SDKIdentifier = "sentry.go"

// apiVersion is the minimum version of the Sentry API compatible with the
// sentry-go SDK.
// Deprecated: To be removed in 0.25.0.
const apiVersion = "7"

// Init initializes the SDK with options. The returned error is non-nil if
Expand Down
12 changes: 12 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ func getRequestFromEvent(event *Event, dsn *Dsn) (r *http.Request, err error) {
if r != nil {
r.Header.Set("User-Agent", fmt.Sprintf("%s/%s", event.Sdk.Name, event.Sdk.Version))
r.Header.Set("Content-Type", "application/x-sentry-envelope")

auth := fmt.Sprintf("Sentry sentry_version=%s, "+
"sentry_client=%s/%s, sentry_key=%s", apiVersion, event.Sdk.Name, event.Sdk.Version, dsn.publicKey)

// The key sentry_secret is effectively deprecated and no longer needs to be set.
// However, since it was required in older self-hosted versions,
// it should still passed through to Sentry if set.
if dsn.secretKey != "" {
auth = fmt.Sprintf("%s, sentry_secret=%s", auth, dsn.secretKey)
}

r.Header.Set("X-Sentry-Auth", auth)
}
}()
body := getRequestBodyFromEvent(event)
Expand Down
21 changes: 13 additions & 8 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,13 @@ func TestGetRequestFromEvent(t *testing.T) {
}{
{
testName: "Sample Event",
event: NewEvent(),
event: newTestEvent(eventType),
apiURL: "https://host/path/api/42/envelope/",
},
{
testName: "Transaction",
event: func() *Event {
event := NewEvent()
event.Type = transactionType

return event
}(),
apiURL: "https://host/path/api/42/envelope/",
event: newTestEvent(transactionType),
apiURL: "https://host/path/api/42/envelope/",
},
}

Expand Down Expand Up @@ -361,6 +356,16 @@ func TestGetRequestFromEvent(t *testing.T) {
if ua := req.UserAgent(); ua != userAgent {
t.Errorf("got User-Agent = %q, want %q", ua, userAgent)
}

contentType := "application/x-sentry-envelope"
if ct := req.Header.Get("Content-Type"); ct != contentType {
t.Errorf("got Content-Type = %q, want %q", ct, contentType)
}

xSentryAuth := "Sentry sentry_version=7, sentry_client=sentry.go/0.0.1, sentry_key=key"
if auth := req.Header.Get("X-Sentry-Auth"); !strings.HasPrefix(auth, xSentryAuth) {
t.Errorf("got X-Sentry-Auth = %q, want %q", auth, xSentryAuth)
}
})
}
}
Expand Down

0 comments on commit bee7c46

Please sign in to comment.