Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 056cd5e

Browse files
authoredDec 29, 2022
Merge pull request #535 from gotify/xss
Only serve image files on ./image
2 parents 022603d + 33d86e4 commit 056cd5e

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed
 

‎api/application.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,7 @@ func (a *ApplicationAPI) UploadApplicationImage(ctx *gin.Context) {
328328
}
329329

330330
ext := filepath.Ext(file.Filename)
331-
332-
switch ext {
333-
case ".gif", ".png", ".jpg", ".jpeg":
334-
// ok
335-
default:
331+
if !ValidApplicationImageExt(ext) {
336332
ctx.AbortWithError(400, errors.New("invalid file extension"))
337333
return
338334
}
@@ -391,3 +387,12 @@ func generateNonExistingImageName(imgDir string, gen func() string) string {
391387
}
392388
}
393389
}
390+
391+
func ValidApplicationImageExt(ext string) bool {
392+
switch ext {
393+
case ".gif", ".png", ".jpg", ".jpeg":
394+
return true
395+
default:
396+
return false
397+
}
398+
}

‎router/router.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package router
22

33
import (
44
"fmt"
5+
"net/http"
6+
"path/filepath"
57
"regexp"
68
"time"
79

@@ -14,7 +16,7 @@ import (
1416
"github.com/gotify/server/v2/config"
1517
"github.com/gotify/server/v2/database"
1618
"github.com/gotify/server/v2/docs"
17-
"github.com/gotify/server/v2/error"
19+
gerror "github.com/gotify/server/v2/error"
1820
"github.com/gotify/server/v2/model"
1921
"github.com/gotify/server/v2/plugin"
2022
"github.com/gotify/server/v2/ui"
@@ -24,8 +26,8 @@ import (
2426
func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Configuration) (*gin.Engine, func()) {
2527
g := gin.New()
2628

27-
g.Use(gin.LoggerWithFormatter(logFormatter), gin.Recovery(), error.Handler(), location.Default())
28-
g.NoRoute(error.NotFound())
29+
g.Use(gin.LoggerWithFormatter(logFormatter), gin.Recovery(), gerror.Handler(), location.Default())
30+
g.NoRoute(gerror.NotFound())
2931

3032
streamHandler := stream.New(time.Duration(conf.Server.Stream.PingPeriodSeconds)*time.Second, 15*time.Second, conf.Server.Stream.AllowedOrigins)
3133
authentication := auth.Auth{DB: db}
@@ -61,7 +63,8 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co
6163

6264
g.GET("/health", healthHandler.Health)
6365
g.GET("/swagger", docs.Serve)
64-
g.Static("/image", conf.UploadedImagesDir)
66+
g.StaticFS("/image", &onlyImageFS{inner: gin.Dir(conf.UploadedImagesDir, false)})
67+
6568
g.GET("/docs", docs.UI)
6669

6770
g.Use(func(ctx *gin.Context) {
@@ -194,3 +197,15 @@ func logFormatter(param gin.LogFormatterParams) string {
194197
param.ErrorMessage,
195198
)
196199
}
200+
201+
type onlyImageFS struct {
202+
inner http.FileSystem
203+
}
204+
205+
func (fs *onlyImageFS) Open(name string) (http.File, error) {
206+
ext := filepath.Ext(name)
207+
if !api.ValidApplicationImageExt(ext) {
208+
return nil, fmt.Errorf("invalid file")
209+
}
210+
return fs.inner.Open(name)
211+
}

0 commit comments

Comments
 (0)
Please sign in to comment.