GODT-980: placeholder for user agent

This commit is contained in:
James Houlahan
2021-01-28 12:57:37 +01:00
committed by Jakub
parent 4e531d4524
commit 1f25aeab31
35 changed files with 287 additions and 278 deletions

View File

@ -78,13 +78,6 @@ type ClientConfig struct {
// The client application name and version.
AppVersion string
// The client application user agent in format `client name/client version (os)`, e.g.:
// (Intel Mac OS X 10_15_3)
// Mac OS X Mail/13.0 (3608.60.0.2.5) (Intel Mac OS X 10_15_3)
// Thunderbird/1.5.0 (Ubuntu 18.04.4 LTS)
// MSOffice 12 (Windows 10 (10.0))
UserAgent string
// The client ID.
ClientID string
@ -236,7 +229,7 @@ func (c *client) Do(req *http.Request, retryUnauthorized bool) (res *http.Respon
func (c *client) doBuffered(req *http.Request, bodyBuffer []byte, retryUnauthorized bool) (res *http.Response, err error) { // nolint[funlen]
isAuthReq := strings.Contains(req.URL.Path, "/auth")
req.Header.Set("User-Agent", c.cm.config.UserAgent)
req.Header.Set("User-Agent", c.cm.userAgent.String())
req.Header.Set("x-pm-appversion", c.cm.config.AppVersion)
if c.uid != "" {

View File

@ -24,6 +24,7 @@ import (
"sync"
"time"
"github.com/ProtonMail/proton-bridge/internal/config/useragent"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@ -37,6 +38,7 @@ type ClientManager struct { //nolint[maligned]
newClient func(userID string) Client
config *ClientConfig
userAgent *useragent.UserAgent
roundTripper http.RoundTripper
clients map[string]Client
@ -86,9 +88,10 @@ type tokenExpiration struct {
}
// NewClientManager creates a new ClientMan which manages clients configured with the given client config.
func NewClientManager(config *ClientConfig) (cm *ClientManager) {
func NewClientManager(config *ClientConfig, userAgent *useragent.UserAgent) (cm *ClientManager) {
cm = &ClientManager{
config: config,
userAgent: userAgent,
roundTripper: http.DefaultTransport,
clients: make(map[string]Client),
@ -118,7 +121,6 @@ func NewClientManager(config *ClientConfig) (cm *ClientManager) {
cm.newClient = func(userID string) Client {
return newClient(cm, userID)
}
cm.SetUserAgent("", "", "") // Set default user agent.
go cm.watchTokenExpirations()
@ -169,16 +171,12 @@ func (cm *ClientManager) SetRoundTripper(rt http.RoundTripper) {
cm.roundTripper = rt
}
func (cm *ClientManager) GetClientConfig() *ClientConfig {
return cm.config
}
func (cm *ClientManager) SetUserAgent(clientName, clientVersion, os string) {
cm.config.UserAgent = formatUserAgent(clientName, clientVersion, os)
func (cm *ClientManager) GetAppVersion() string {
return cm.config.AppVersion
}
func (cm *ClientManager) GetUserAgent() string {
return cm.config.UserAgent
return cm.userAgent.String()
}
// GetClient returns a client for the given userID.

View File

@ -17,8 +17,10 @@
package pmapi
import "github.com/ProtonMail/proton-bridge/internal/config/useragent"
func newTestClientManager(cfg *ClientConfig) *ClientManager {
cm := NewClientManager(cfg)
cm := NewClientManager(cfg, useragent.New())
go func() {
for range cm.authUpdates {

View File

@ -75,17 +75,18 @@ func certFingerprint(cert *x509.Certificate) string {
return fmt.Sprintf(`pin-sha256=%q`, base64.StdEncoding.EncodeToString(hash[:]))
}
type clientConfigProvider interface {
GetClientConfig() *ClientConfig
type clientInfoProvider interface {
GetAppVersion() string
GetUserAgent() string
}
type tlsReporter struct {
cm clientConfigProvider
cm clientInfoProvider
p *pinChecker
sentReports []sentReport
}
func newTLSReporter(p *pinChecker, cm clientConfigProvider) *tlsReporter {
func newTLSReporter(p *pinChecker, cm clientInfoProvider) *tlsReporter {
return &tlsReporter{
cm: cm,
p: p,
@ -102,13 +103,14 @@ func (r *tlsReporter) reportCertIssue(remoteURI, host, port string, connState tl
certChain = marshalCert7468(connState.PeerCertificates)
}
cfg := r.cm.GetClientConfig()
appVersion := r.cm.GetAppVersion()
userAgent := r.cm.GetUserAgent()
report := newTLSReport(host, port, connState.ServerName, certChain, r.p.trustedPins, cfg.AppVersion)
report := newTLSReport(host, port, connState.ServerName, certChain, r.p.trustedPins, appVersion)
if !r.hasRecentlySentReport(report) {
r.recordReport(report)
go report.sendReport(remoteURI, cfg.UserAgent)
go report.sendReport(remoteURI, userAgent)
}
}

View File

@ -27,12 +27,16 @@ import (
"github.com/stretchr/testify/assert"
)
type fakeClientConfigProvider struct {
type fakeClientInfoProvider struct {
version, useragent string
}
func (c *fakeClientConfigProvider) GetClientConfig() *ClientConfig {
return &ClientConfig{AppVersion: c.version, UserAgent: c.useragent}
func (c *fakeClientInfoProvider) GetAppVersion() string {
return c.version
}
func (c *fakeClientInfoProvider) GetUserAgent() string {
return c.useragent
}
func TestPinCheckerDoubleReport(t *testing.T) {
@ -42,7 +46,7 @@ func TestPinCheckerDoubleReport(t *testing.T) {
reportCounter++
}))
r := newTLSReporter(newPinChecker(TrustedAPIPins), &fakeClientConfigProvider{version: "3", useragent: "useragent"})
r := newTLSReporter(newPinChecker(TrustedAPIPins), &fakeClientInfoProvider{version: "3", useragent: "useragent"})
// Report the same issue many times.
for i := 0; i < 10; i++ {

View File

@ -1,52 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package pmapi
import (
"fmt"
"runtime"
"strings"
)
// removeBrackets handle unwanted brackets in client identification string and join with given joinBy parameter.
// Mac OS X Mail/13.0 (3601.0.4) -> Mac OS X Mail/13.0-3601.0.4 (joinBy = "-")
func removeBrackets(s string, joinBy string) (r string) {
r = strings.ReplaceAll(s, " (", joinBy)
r = strings.ReplaceAll(r, "(", joinBy) // Should be faster than regex.
r = strings.ReplaceAll(r, ")", "")
return
}
func formatUserAgent(clientName, clientVersion, os string) string {
client := ""
if clientName != "" {
client = removeBrackets(clientName, "-")
if clientVersion != "" {
client += "/" + removeBrackets(clientVersion, "-")
}
}
if os == "" {
os = runtime.GOOS
}
os = removeBrackets(os, " ")
return fmt.Sprintf("%s (%s)", client, os)
}

View File

@ -1,55 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package pmapi
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpdateCurrentUserAgentGOOS(t *testing.T) {
userAgent := formatUserAgent("", "", "")
assert.Equal(t, " ("+runtime.GOOS+")", userAgent)
}
func TestUpdateCurrentUserAgentOS(t *testing.T) {
userAgent := formatUserAgent("", "", "os")
assert.Equal(t, " (os)", userAgent)
}
func TestUpdateCurrentUserAgentClientVer(t *testing.T) {
userAgent := formatUserAgent("", "ver", "os")
assert.Equal(t, " (os)", userAgent)
}
func TestUpdateCurrentUserAgentClientName(t *testing.T) {
userAgent := formatUserAgent("mail", "", "os")
assert.Equal(t, "mail (os)", userAgent)
}
func TestUpdateCurrentUserAgentClientNameAndVersion(t *testing.T) {
userAgent := formatUserAgent("mail", "ver", "os")
assert.Equal(t, "mail/ver (os)", userAgent)
}
func TestRemoveBrackets(t *testing.T) {
userAgent := formatUserAgent("mail (submail)", "ver (subver)", "os (subos)")
assert.Equal(t, "mail-submail/ver-subver (os subos)", userAgent)
}

View File

@ -1,177 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package sentry
import (
"errors"
"fmt"
"os"
"runtime"
"time"
"github.com/ProtonMail/proton-bridge/internal/constants"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
)
var skippedFunctions = []string{} //nolint[gochecknoglobals]
func init() { // nolint[noinit]
if err := sentry.Init(sentry.ClientOptions{
Dsn: constants.DSNSentry,
Release: constants.Revision,
BeforeSend: EnhanceSentryEvent,
}); err != nil {
logrus.WithError(err).Error("Failed to initialize sentry options")
}
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetFingerprint([]string{"{{ default }}"})
})
}
type userAgentProvider interface {
GetUserAgent() string
}
type Reporter struct {
appName string
appVersion string
uap userAgentProvider
}
// NewReporter creates new sentry reporter with appName and appVersion to report.
func NewReporter(appName, appVersion string) *Reporter {
return &Reporter{
appName: appName,
appVersion: appVersion,
}
}
func (r *Reporter) SetUserAgentProvider(uap userAgentProvider) {
r.uap = uap
}
func (r *Reporter) ReportException(i interface{}) error {
err := fmt.Errorf("recover: %v", i)
return r.scopedReport(func() {
if eventID := sentry.CaptureException(err); eventID != nil {
logrus.WithError(err).
WithField("reportID", *eventID).
Warn("Captured exception")
}
})
}
func (r *Reporter) ReportMessage(msg string) error {
return r.scopedReport(func() {
if eventID := sentry.CaptureMessage(msg); eventID != nil {
logrus.WithField("message", msg).
WithField("reportID", *eventID).
Warn("Captured message")
}
})
}
// Report reports a sentry crash with stacktrace from all goroutines.
func (r *Reporter) scopedReport(doReport func()) error {
SkipDuringUnwind()
if os.Getenv("PROTONMAIL_ENV") == "dev" {
return nil
}
// In case clientManager is not yet created we can get at least OS string.
var userAgent string
if r.uap != nil {
userAgent = r.uap.GetUserAgent()
} else {
userAgent = runtime.GOOS
}
tags := map[string]string{
"OS": runtime.GOOS,
"Client": r.appName,
"Version": r.appVersion,
"UserAgent": userAgent,
"UserID": "",
}
sentry.WithScope(func(scope *sentry.Scope) {
SkipDuringUnwind()
scope.SetTags(tags)
doReport()
})
if !sentry.Flush(time.Second * 10) {
return errors.New("failed to report sentry error")
}
return nil
}
// SkipDuringUnwind removes caller from the traceback.
func SkipDuringUnwind() {
pcs := make([]uintptr, 2)
n := runtime.Callers(2, pcs)
if n == 0 {
return
}
frames := runtime.CallersFrames(pcs)
frame, _ := frames.Next()
if isFunctionFilteredOut(frame.Function) {
return
}
skippedFunctions = append(skippedFunctions, frame.Function)
}
// EnhanceSentryEvent swaps type with value and removes panic handlers from the stacktrace.
func EnhanceSentryEvent(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
for idx, exception := range event.Exception {
exception.Type, exception.Value = exception.Value, exception.Type
if exception.Stacktrace != nil {
exception.Stacktrace.Frames = filterOutPanicHandlers(exception.Stacktrace.Frames)
}
event.Exception[idx] = exception
}
return event
}
func filterOutPanicHandlers(frames []sentry.Frame) []sentry.Frame {
newFrames := []sentry.Frame{}
for _, frame := range frames {
// Sentry splits runtime.Frame.Function into Module and Function.
function := frame.Module + "." + frame.Function
if !isFunctionFilteredOut(function) {
newFrames = append(newFrames, frame)
}
}
return newFrames
}
func isFunctionFilteredOut(function string) bool {
for _, skipFunction := range skippedFunctions {
if function == skipFunction {
return true
}
}
return false
}

View File

@ -1,66 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package sentry
import (
"testing"
r "github.com/stretchr/testify/require"
"github.com/getsentry/sentry-go"
)
func TestSkipDuringUnwind(t *testing.T) {
// More calls in one function adds it only once.
SkipDuringUnwind()
SkipDuringUnwind()
func() {
SkipDuringUnwind()
SkipDuringUnwind()
}()
wantSkippedFunctions := []string{
"github.com/ProtonMail/proton-bridge/pkg/sentry.TestSkipDuringUnwind",
"github.com/ProtonMail/proton-bridge/pkg/sentry.TestSkipDuringUnwind.func1",
}
r.Equal(t, wantSkippedFunctions, skippedFunctions)
}
func TestFilterOutPanicHandlers(t *testing.T) {
skippedFunctions = []string{
"github.com/ProtonMail/proton-bridge/pkg/config.(*PanicHandler).HandlePanic",
"github.com/ProtonMail/proton-bridge/pkg/config.HandlePanic",
"github.com/ProtonMail/proton-bridge/pkg/sentry.ReportSentryCrash",
"github.com/ProtonMail/proton-bridge/pkg/sentry.ReportSentryCrash.func1",
}
frames := []sentry.Frame{
{Module: "github.com/ProtonMail/proton-bridge/internal/cmd", Function: "main"},
{Module: "github.com/urfave/cli", Function: "(*App).Run"},
{Module: "github.com/ProtonMail/proton-bridge/internal/cmd", Function: "RegisterHandlePanic"},
{Module: "github.com/ProtonMail/pkg", Function: "HandlePanic"},
{Module: "main", Function: "run"},
{Module: "github.com/ProtonMail/proton-bridge/pkg/config", Function: "(*PanicHandler).HandlePanic"},
{Module: "github.com/ProtonMail/proton-bridge/pkg/config", Function: "HandlePanic"},
{Module: "github.com/ProtonMail/proton-bridge/pkg/sentry", Function: "ReportSentryCrash"},
{Module: "github.com/ProtonMail/proton-bridge/pkg/sentry", Function: "ReportSentryCrash.func1"},
}
gotFrames := filterOutPanicHandlers(frames)
r.Equal(t, frames[:5], gotFrames)
}

View File

@ -1,53 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package useragent
import (
"os/exec"
"runtime"
"strings"
"github.com/Masterminds/semver/v3"
)
// IsCatalinaOrNewer checks that host is MacOS Catalina 10.15.x or higher.
func IsCatalinaOrNewer() bool {
if runtime.GOOS != "darwin" {
return false
}
return isVersionCatalinaOrNewer(getMacVersion())
}
func getMacVersion() string {
out, err := exec.Command("sw_vers", "-productVersion").Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
func isVersionCatalinaOrNewer(version string) bool {
v, err := semver.NewVersion(version)
if err != nil {
return false
}
catalina := semver.MustParse("10.15.0")
return v.GreaterThan(catalina) || v.Equal(catalina)
}

View File

@ -1,44 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package useragent
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsVersionCatalinaOrNewer(t *testing.T) {
testData := map[struct{ version string }]bool{
{""}: false,
{"9.0.0"}: false,
{"9.15.0"}: false,
{"10.13.0"}: false,
{"10.14.0"}: false,
{"10.14.99"}: false,
{"10.15.0"}: true,
{"10.16.0"}: true,
{"11.0.0"}: true,
{"11.1"}: true,
}
for args, exp := range testData {
got := isVersionCatalinaOrNewer(args.version)
assert.Equal(t, exp, got, "version %v", args.version)
}
}