From 8001eab9db52e0ec76846ab60e69f4dd19465749 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Wed, 29 Apr 2020 21:15:13 +0300 Subject: [PATCH] Add rough sentry reporting to catch panic in the mojang textures decoder --- Gopkg.lock | 6 ++--- api/mojang/mojang.go | 13 +++++++---- api/mojang/mojang_test.go | 6 +++-- http/skinsystem.go | 6 ++--- mojangtextures/in_memory_textures_storage.go | 22 ++++++++++++++++--- .../in_memory_textures_storage_test.go | 2 +- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 1dd3e1d..418c689 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -265,7 +265,7 @@ version = "v0.1.1" [[projects]] - digest = "1:381bcbeb112a51493d9d998bbba207a529c73dbb49b3fd789e48c63fac1f192c" + digest = "1:cc4eb6813da8d08694e557fcafae8fcc24f47f61a0717f952da130ca9a486dfc" name = "github.com/stretchr/testify" packages = [ "assert", @@ -274,8 +274,8 @@ "suite", ] pruneopts = "" - revision = "ffdc059bfe9ce6a4e144ba849dbedead332c6053" - version = "v1.3.0" + revision = "3ebf1ddaeb260c4b1ae502a01c7844fa8c1fa0e9" + version = "v1.5.1" [[projects]] branch = "master" diff --git a/api/mojang/mojang.go b/api/mojang/mojang.go index a50a015..6f74b72 100644 --- a/api/mojang/mojang.go +++ b/api/mojang/mojang.go @@ -3,6 +3,7 @@ package mojang import ( "bytes" "encoding/json" + "errors" "fmt" "io/ioutil" "net/http" @@ -24,7 +25,7 @@ type SignedTexturesResponse struct { decodedTextures *TexturesProp } -func (t *SignedTexturesResponse) DecodeTextures() *TexturesProp { +func (t *SignedTexturesResponse) DecodeTextures() (*TexturesProp, error) { if t.decodedTextures == nil { var texturesProp string for _, prop := range t.Props { @@ -35,14 +36,18 @@ func (t *SignedTexturesResponse) DecodeTextures() *TexturesProp { } if texturesProp == "" { - return nil + return nil, errors.New("unable to find the textures property") + } + + decodedTextures, err := DecodeTextures(texturesProp) + if err != nil { + return nil, err } - decodedTextures, _ := DecodeTextures(texturesProp) t.decodedTextures = decodedTextures } - return t.decodedTextures + return t.decodedTextures, nil } type Property struct { diff --git a/api/mojang/mojang_test.go b/api/mojang/mojang_test.go index b2756ca..b5f2001 100644 --- a/api/mojang/mojang_test.go +++ b/api/mojang/mojang_test.go @@ -20,7 +20,8 @@ func TestSignedTexturesResponse(t *testing.T) { }, }, } - textures := obj.DecodeTextures() + textures, err := obj.DecodeTextures() + testify.Nil(t, err) testify.Equal(t, "3e3ee6c35afa48abb61e8cd8c42fc0d9", textures.ProfileID) }) @@ -30,7 +31,8 @@ func TestSignedTexturesResponse(t *testing.T) { Name: "mock", Props: []*Property{}, } - textures := obj.DecodeTextures() + textures, err := obj.DecodeTextures() + testify.Errorf(t, err, "unable to find the textures property") testify.Nil(t, textures) }) } diff --git a/http/skinsystem.go b/http/skinsystem.go index 5ab0d27..ed03b80 100644 --- a/http/skinsystem.go +++ b/http/skinsystem.go @@ -66,7 +66,7 @@ func (ctx *Skinsystem) skinHandler(response http.ResponseWriter, request *http.R return } - texturesProp := mojangTextures.DecodeTextures() + texturesProp, _ := mojangTextures.DecodeTextures() skin := texturesProp.Textures.Skin if skin == nil { response.WriteHeader(http.StatusNotFound) @@ -104,7 +104,7 @@ func (ctx *Skinsystem) capeHandler(response http.ResponseWriter, request *http.R return } - texturesProp := mojangTextures.DecodeTextures() + texturesProp, _ := mojangTextures.DecodeTextures() cape := texturesProp.Textures.Cape if cape == nil { response.WriteHeader(http.StatusNotFound) @@ -162,7 +162,7 @@ func (ctx *Skinsystem) texturesHandler(response http.ResponseWriter, request *ht return } - texturesProp := mojangTextures.DecodeTextures() + texturesProp, _ := mojangTextures.DecodeTextures() if texturesProp == nil { ctx.Emit("skinsystem:error", errors.New("unable to find textures property")) apiServerError(response) diff --git a/mojangtextures/in_memory_textures_storage.go b/mojangtextures/in_memory_textures_storage.go index 14a33a0..d5b7957 100644 --- a/mojangtextures/in_memory_textures_storage.go +++ b/mojangtextures/in_memory_textures_storage.go @@ -1,9 +1,12 @@ package mojangtextures import ( + "fmt" "sync" "time" + "github.com/getsentry/raven-go" + "github.com/elyby/chrly/api/mojang" "github.com/tevino/abool" @@ -75,9 +78,22 @@ func (s *InMemoryTexturesStorage) GetTextures(uuid string) (*mojang.SignedTextur func (s *InMemoryTexturesStorage) StoreTextures(uuid string, textures *mojang.SignedTexturesResponse) { var timestamp int64 if textures != nil { - decoded := textures.DecodeTextures() - if decoded == nil { - panic("unable to decode textures") + decoded, err := textures.DecodeTextures() + if err != nil { + tags := map[string]string{ + "textures.id": textures.Id, + "textures.name": textures.Name, + } + + for i, prop := range textures.Props { + tags[fmt.Sprintf("textures.props[%d].name", i)] = prop.Name + tags[fmt.Sprintf("textures.props[%d].value", i)] = prop.Value + tags[fmt.Sprintf("textures.props[%d].signature", i)] = prop.Signature + } + + raven.CaptureErrorAndWait(err, tags) + + panic(err) } timestamp = decoded.Timestamp diff --git a/mojangtextures/in_memory_textures_storage_test.go b/mojangtextures/in_memory_textures_storage_test.go index 8a178c8..ef0f831 100644 --- a/mojangtextures/in_memory_textures_storage_test.go +++ b/mojangtextures/in_memory_textures_storage_test.go @@ -116,7 +116,7 @@ func TestInMemoryTexturesStorage_StoreTextures(t *testing.T) { Props: []*mojang.Property{}, } - assert.PanicsWithValue(t, "unable to decode textures", func() { + assert.PanicsWithError(t, "unable to find the textures property", func() { storage := NewInMemoryTexturesStorage() storage.StoreTextures("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", toStore) })