Skip to content

Commit

Permalink
Merge pull request #534 from gotify/fix-xss
Browse files Browse the repository at this point in the history
Fix file upload XSS
  • Loading branch information
jmattheis committed Dec 28, 2022
2 parents c8f78e8 + 925fb7e commit 022603d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/application.go
Expand Up @@ -329,6 +329,14 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {

ext := filepath.Ext(file.Filename)

switch ext {
case ".gif", ".png", ".jpg", ".jpeg":
// ok
default:
ctx.AbortWithError(400, errors.New("invalid file extension"))
return
}

name := generateNonExistingImageName(a.ImageDir, func() string {
return generateImageName() + ext
})
Expand Down
16 changes: 16 additions & 0 deletions api/application_test.go
Expand Up @@ -398,6 +398,22 @@ func (s *ApplicationSuite) Test_UploadAppImage_WithTextFile_expectBadRequest() {
assert.Equal(s.T(), s.ctx.Errors[0].Err, errors.New("file must be an image"))
}

func (s *ApplicationSuite) Test_UploadAppImage_WithHtmlFileHavingImageHeader() {
s.db.User(5).App(1)

cType, buffer, err := upload(map[string]*os.File{"file": mustOpen("../test/assets/image-header-with.html")})
assert.Nil(s.T(), err)
s.ctx.Request = httptest.NewRequest("POST", "/irrelevant", &buffer)
s.ctx.Request.Header.Set("Content-Type", cType)
test.WithUser(s.ctx, 5)
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}

s.a.UploadApplicationImage(s.ctx)

assert.Equal(s.T(), 400, s.recorder.Code)
assert.Equal(s.T(), s.ctx.Errors[0].Err, errors.New("invalid file extension"))
}

func (s *ApplicationSuite) Test_UploadAppImage_expectNotFound() {
s.db.User(5)

Expand Down
Binary file added test/assets/image-header-with.html
Binary file not shown.

0 comments on commit 022603d

Please sign in to comment.