Skip to content

Commit 022603d

Browse files
authored
Merge pull request #534 from gotify/fix-xss
Fix file upload XSS
2 parents c8f78e8 + 925fb7e commit 022603d

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

api/application.go

+8
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {
329329

330330
ext := filepath.Ext(file.Filename)
331331

332+
switch ext {
333+
case ".gif", ".png", ".jpg", ".jpeg":
334+
// ok
335+
default:
336+
ctx.AbortWithError(400, errors.New("invalid file extension"))
337+
return
338+
}
339+
332340
name := generateNonExistingImageName(a.ImageDir, func() string {
333341
return generateImageName() + ext
334342
})

api/application_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,22 @@ func (s *ApplicationSuite) Test_UploadAppImage_WithTextFile_expectBadRequest() {
398398
assert.Equal(s.T(), s.ctx.Errors[0].Err, errors.New("file must be an image"))
399399
}
400400

401+
func (s *ApplicationSuite) Test_UploadAppImage_WithHtmlFileHavingImageHeader() {
402+
s.db.User(5).App(1)
403+
404+
cType, buffer, err := upload(map[string]*os.File{"file": mustOpen("../test/assets/image-header-with.html")})
405+
assert.Nil(s.T(), err)
406+
s.ctx.Request = httptest.NewRequest("POST", "/irrelevant", &buffer)
407+
s.ctx.Request.Header.Set("Content-Type", cType)
408+
test.WithUser(s.ctx, 5)
409+
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
410+
411+
s.a.UploadApplicationImage(s.ctx)
412+
413+
assert.Equal(s.T(), 400, s.recorder.Code)
414+
assert.Equal(s.T(), s.ctx.Errors[0].Err, errors.New("invalid file extension"))
415+
}
416+
401417
func (s *ApplicationSuite) Test_UploadAppImage_expectNotFound() {
402418
s.db.User(5)
403419

test/assets/image-header-with.html

154 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)