Skip to content

Commit 91f2afd

Browse files
authoredOct 16, 2020
Prevent panics with missing storage (#13164)
* The `.Use` of storageHandler before setting up the template renderer causes a panic if there is an error to log. * The error passed to `ctx.Error` in that case may contain sensitive information and should not be rendered to the end user. We should instead log the error and render a simple error message. * There is no handling of missing avatars and this needs a 404. Minio errors need to be mapped to standard golang errors such as os.ErrNotExist. * There is no logging when storage is set up. Related #13159 Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent cb171db commit 91f2afd

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed
 

‎modules/storage/local.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313

14+
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/util"
1516
)
1617

@@ -40,6 +41,7 @@ func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
4041
}
4142
config := configInterface.(LocalStorageConfig)
4243

44+
log.Info("Creating new Local Storage at %s", config.Path)
4345
if err := os.MkdirAll(config.Path, os.ModePerm); err != nil {
4446
return nil, err
4547
}

‎modules/storage/minio.go

+37-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
"time"
1515

16+
"code.gitea.io/gitea/modules/log"
17+
1618
"github.com/minio/minio-go/v7"
1719
"github.com/minio/minio-go/v7/pkg/credentials"
1820
)
@@ -58,20 +60,42 @@ type MinioStorage struct {
5860
basePath string
5961
}
6062

63+
func convertMinioErr(err error) error {
64+
if err == nil {
65+
return nil
66+
}
67+
errResp, ok := err.(minio.ErrorResponse)
68+
if !ok {
69+
return err
70+
}
71+
72+
// Convert two responses to standard analogues
73+
switch errResp.Code {
74+
case "NoSuchKey":
75+
return os.ErrNotExist
76+
case "AccessDenied":
77+
return os.ErrPermission
78+
}
79+
80+
return err
81+
}
82+
6183
// NewMinioStorage returns a minio storage
6284
func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error) {
6385
configInterface, err := toConfig(MinioStorageConfig{}, cfg)
6486
if err != nil {
65-
return nil, err
87+
return nil, convertMinioErr(err)
6688
}
6789
config := configInterface.(MinioStorageConfig)
6890

91+
log.Info("Creating Minio storage at %s:%s with base path %s", config.Endpoint, config.Bucket, config.BasePath)
92+
6993
minioClient, err := minio.New(config.Endpoint, &minio.Options{
7094
Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
7195
Secure: config.UseSSL,
7296
})
7397
if err != nil {
74-
return nil, err
98+
return nil, convertMinioErr(err)
7599
}
76100

77101
if err := minioClient.MakeBucket(ctx, config.Bucket, minio.MakeBucketOptions{
@@ -80,7 +104,7 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
80104
// Check to see if we already own this bucket (which happens if you run this twice)
81105
exists, errBucketExists := minioClient.BucketExists(ctx, config.Bucket)
82106
if !exists || errBucketExists != nil {
83-
return nil, err
107+
return nil, convertMinioErr(err)
84108
}
85109
}
86110

@@ -101,7 +125,7 @@ func (m *MinioStorage) Open(path string) (Object, error) {
101125
var opts = minio.GetObjectOptions{}
102126
object, err := m.client.GetObject(m.ctx, m.bucket, m.buildMinioPath(path), opts)
103127
if err != nil {
104-
return nil, err
128+
return nil, convertMinioErr(err)
105129
}
106130
return &minioObject{object}, nil
107131
}
@@ -117,7 +141,7 @@ func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
117141
minio.PutObjectOptions{ContentType: "application/octet-stream"},
118142
)
119143
if err != nil {
120-
return 0, err
144+
return 0, convertMinioErr(err)
121145
}
122146
return uploadInfo.Size, nil
123147
}
@@ -159,27 +183,25 @@ func (m *MinioStorage) Stat(path string) (os.FileInfo, error) {
159183
minio.StatObjectOptions{},
160184
)
161185
if err != nil {
162-
if errResp, ok := err.(minio.ErrorResponse); ok {
163-
if errResp.Code == "NoSuchKey" {
164-
return nil, os.ErrNotExist
165-
}
166-
}
167-
return nil, err
186+
return nil, convertMinioErr(err)
168187
}
169188
return &minioFileInfo{info}, nil
170189
}
171190

172191
// Delete delete a file
173192
func (m *MinioStorage) Delete(path string) error {
174-
return m.client.RemoveObject(m.ctx, m.bucket, m.buildMinioPath(path), minio.RemoveObjectOptions{})
193+
err := m.client.RemoveObject(m.ctx, m.bucket, m.buildMinioPath(path), minio.RemoveObjectOptions{})
194+
195+
return convertMinioErr(err)
175196
}
176197

177198
// URL gets the redirect URL to a file. The presigned link is valid for 5 minutes.
178199
func (m *MinioStorage) URL(path, name string) (*url.URL, error) {
179200
reqParams := make(url.Values)
180201
// TODO it may be good to embed images with 'inline' like ServeData does, but we don't want to have to read the file, do we?
181202
reqParams.Set("response-content-disposition", "attachment; filename=\""+quoteEscaper.Replace(name)+"\"")
182-
return m.client.PresignedGetObject(m.ctx, m.bucket, m.buildMinioPath(path), 5*time.Minute, reqParams)
203+
u, err := m.client.PresignedGetObject(m.ctx, m.bucket, m.buildMinioPath(path), 5*time.Minute, reqParams)
204+
return u, convertMinioErr(err)
183205
}
184206

185207
// IterateObjects iterates across the objects in the miniostorage
@@ -193,13 +215,13 @@ func (m *MinioStorage) IterateObjects(fn func(path string, obj Object) error) er
193215
}) {
194216
object, err := m.client.GetObject(lobjectCtx, m.bucket, mObjInfo.Key, opts)
195217
if err != nil {
196-
return err
218+
return convertMinioErr(err)
197219
}
198220
if err := func(object *minio.Object, fn func(path string, obj Object) error) error {
199221
defer object.Close()
200222
return fn(strings.TrimPrefix(m.basePath, mObjInfo.Key), &minioObject{object})
201223
}(object, fn); err != nil {
202-
return err
224+
return convertMinioErr(err)
203225
}
204226
}
205227
return nil

‎modules/storage/storage.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"os"
1414

15+
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/setting"
1617
)
1718

@@ -141,21 +142,25 @@ func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) {
141142
}
142143

143144
func initAvatars() (err error) {
145+
log.Info("Initialising Avatar storage with type: %s", setting.Avatar.Storage.Type)
144146
Avatars, err = NewStorage(setting.Avatar.Storage.Type, setting.Avatar.Storage)
145147
return
146148
}
147149

148150
func initAttachments() (err error) {
151+
log.Info("Initialising Attachment storage with type: %s", setting.Attachment.Storage.Type)
149152
Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage)
150153
return
151154
}
152155

153156
func initLFS() (err error) {
157+
log.Info("Initialising LFS storage with type: %s", setting.LFS.Storage.Type)
154158
LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage)
155159
return
156160
}
157161

158162
func initRepoAvatars() (err error) {
163+
log.Info("Initialising Repository Avatar storage with type: %s", setting.RepoAvatar.Storage.Type)
159164
RepoAvatars, err = NewStorage(setting.RepoAvatar.Storage.Type, setting.RepoAvatar.Storage)
160165
return
161166
}

‎routers/routes/routes.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ package routes
77
import (
88
"bytes"
99
"encoding/gob"
10+
"fmt"
1011
"io"
1112
"net/http"
13+
"os"
1214
"path"
1315
"strings"
1416
"text/template"
@@ -125,7 +127,13 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
125127
rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix)
126128
u, err := objStore.URL(rPath, path.Base(rPath))
127129
if err != nil {
128-
ctx.Error(500, err.Error())
130+
if err == os.ErrNotExist {
131+
log.Warn("Unable to find %s %s", prefix, rPath)
132+
ctx.Error(404, "file not found")
133+
return
134+
}
135+
log.Error("Error whilst getting URL for %s %s. Error: %v", prefix, rPath, err)
136+
ctx.Error(500, fmt.Sprintf("Error whilst getting URL for %s %s", prefix, rPath))
129137
return
130138
}
131139
http.Redirect(
@@ -152,14 +160,21 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
152160
//If we have matched and access to release or issue
153161
fr, err := objStore.Open(rPath)
154162
if err != nil {
155-
ctx.Error(500, err.Error())
163+
if err == os.ErrNotExist {
164+
log.Warn("Unable to find %s %s", prefix, rPath)
165+
ctx.Error(404, "file not found")
166+
return
167+
}
168+
log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
169+
ctx.Error(500, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath))
156170
return
157171
}
158172
defer fr.Close()
159173

160174
_, err = io.Copy(ctx.Resp, fr)
161175
if err != nil {
162-
ctx.Error(500, err.Error())
176+
log.Error("Error whilst rendering %s %s. Error: %v", prefix, rPath, err)
177+
ctx.Error(500, fmt.Sprintf("Error whilst rendering %s %s", prefix, rPath))
163178
return
164179
}
165180
}
@@ -208,10 +223,11 @@ func NewMacaron() *macaron.Macaron {
208223
},
209224
))
210225

226+
m.Use(templates.HTMLRenderer())
227+
211228
m.Use(storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
212229
m.Use(storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
213230

214-
m.Use(templates.HTMLRenderer())
215231
mailer.InitMailRender(templates.Mailer())
216232

217233
localeNames, err := options.Dir("locale")

0 commit comments

Comments
 (0)
Please sign in to comment.