Skip to content

Commit 296c153

Browse files
committed
Limit uploaded avatar image-size to 4096x3072 by default
1 parent 69796dd commit 296c153

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

custom/conf/app.ini.sample

+4
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ SESSION_LIFE_TIME = 86400
402402

403403
[picture]
404404
AVATAR_UPLOAD_PATH = data/avatars
405+
; Max Width and Height of uploaded avatars. This is to limit the amount of RAM
406+
; used when resizing the image.
407+
AVATAR_MAX_WIDTH = 4096
408+
AVATAR_MAX_HEIGHT = 3072
405409
; Chinese users can choose "duoshuo"
406410
; or a custom avatar source, like: http://cn.gravatar.com/avatar/
407411
GRAVATAR_SOURCE = gravatar

models/user.go

+11
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ func (u *User) IsPasswordSet() bool {
433433
// UploadAvatar saves custom avatar for user.
434434
// FIXME: split uploads to different subdirs in case we have massive users.
435435
func (u *User) UploadAvatar(data []byte) error {
436+
imgCfg, _, err := image.DecodeConfig(bytes.NewReader(data))
437+
if err != nil {
438+
return fmt.Errorf("DecodeConfig: %v", err)
439+
}
440+
if imgCfg.Width > setting.AvatarMaxWidth {
441+
return fmt.Errorf("Image width is to large: %d > %d", imgCfg.Width, setting.AvatarMaxWidth)
442+
}
443+
if imgCfg.Height > setting.AvatarMaxHeight {
444+
return fmt.Errorf("Image height is to large: %d > %d", imgCfg.Height, setting.AvatarMaxHeight)
445+
}
446+
436447
img, _, err := image.Decode(bytes.NewReader(data))
437448
if err != nil {
438449
return fmt.Errorf("Decode: %v", err)

modules/setting/setting.go

+4
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ var (
341341

342342
// Picture settings
343343
AvatarUploadPath string
344+
AvatarMaxWidth int
345+
AvatarMaxHeight int
344346
GravatarSource string
345347
GravatarSourceURL *url.URL
346348
DisableGravatar bool
@@ -1024,6 +1026,8 @@ func NewContext() {
10241026
if !filepath.IsAbs(AvatarUploadPath) {
10251027
AvatarUploadPath = path.Join(AppWorkPath, AvatarUploadPath)
10261028
}
1029+
AvatarMaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
1030+
AvatarMaxHeight = sec.Key("AVATAR_MAX_HEIGHT").MustInt(3072)
10271031
switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
10281032
case "duoshuo":
10291033
GravatarSource = "http://gravatar.duoshuo.com/avatar/"

routers/repo/authorized_keys

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# gitea public key
2+
command="/tmp/go-build105149861/b434/repo.test serv key-1 --config=''",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment
3+
# gitea public key
4+
command="/tmp/go-build105149861/b434/repo.test serv key-2 --config=''",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment

0 commit comments

Comments
 (0)