Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

fix: reject uploads if too large

Smaug67391617153c0e67
diff --git a/backend/controllers/ressources.controller.go b/backend/controllers/ressources.controller.go
index 6e16cf38..7582eaf7 100644
--- a/backend/controllers/ressources.controller.go
+++ b/backend/controllers/ressources.controller.go
@@ -38,9 +38,16 @@ func (ctr *Controller) GetBackup(c *gin.Context) (int, any) {
}
func (ctr *Controller) UploadFile(c *gin.Context) (int, any) {
+
+ // Limit request body size
+ c.Request.Body = http.MaxBytesReader(
+ c.Writer,
+ c.Request.Body,
+ int64(ctr.app.Config.Cdn.MaxSize),
+ )
file, header, err := c.Request.FormFile("file")
if err != nil {
- return http.StatusBadRequest, errors.New("failed to get file")
+ return http.StatusBadRequest, errors.New("file too large or failed to get file")
}
defer file.Close()
@@ -61,7 +68,6 @@ func (ctr *Controller) UploadFile(c *gin.Context) (int, any) {
fileContent,
mimeType,
userId,
- ctr.app.Config.Cdn.MaxSize,
ctr.app.Config.Cdn.MaxUploadsSize,
ctr.app.Config.Cdn.SupportedTypes,
ctr.app.MinioClient,
@@ -74,9 +80,16 @@ func (ctr *Controller) UploadFile(c *gin.Context) (int, any) {
}
func (ctr *Controller) UploadAvatar(c *gin.Context) (int, any) {
+
+ // Limit request body size
+ c.Request.Body = http.MaxBytesReader(
+ c.Writer,
+ c.Request.Body,
+ int64(ctr.app.Config.Cdn.MaxSize),
+ )
file, header, err := c.Request.FormFile("file")
if err != nil {
- return http.StatusBadRequest, errors.New("failed to get file")
+ return http.StatusBadRequest, errors.New("file too large or failed to get file")
}
defer file.Close()
@@ -97,7 +110,6 @@ func (ctr *Controller) UploadAvatar(c *gin.Context) (int, any) {
fileContent,
mimeType,
userId,
- ctr.app.Config.Cdn.MaxSize,
ctr.app.Config.Cdn.SupportedTypesImages,
ctr.app.MinioClient,
)
diff --git a/backend/services/ressource.service.go b/backend/services/ressource.service.go
index 7ab74aae..855de5ba 100644
--- a/backend/services/ressource.service.go
+++ b/backend/services/ressource.service.go
@@ -20,8 +20,8 @@ import (
type RessourceService interface {
CreateBackup(userId types.Snowflake, minioClient *minio.Client) (string, error)
- UploadFile(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxSize, maxUploadsSize float64, supportedTypes []string, minioClient *minio.Client) (*models.Node, error)
- UploadAvatar(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxSize float64, supportedTypes []string, minioClient *minio.Client) error
+ UploadFile(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxUploadsSize float64, supportedTypes []string, minioClient *minio.Client) (*models.Node, error)
+ UploadAvatar(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, supportedTypes []string, minioClient *minio.Client) error
}
type ressourceService struct {
@@ -63,15 +63,11 @@ func (s *ressourceService) CreateBackup(userId types.Snowflake, minioClient *min
return objectName, nil
}
-func (s *ressourceService) UploadFile(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxSize, maxUploadsSize float64, supportedTypes []string, minioClient *minio.Client) (*models.Node, error) {
+func (s *ressourceService) UploadFile(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxUploadsSize float64, supportedTypes []string, minioClient *minio.Client) (*models.Node, error) {
if minioClient == nil {
return nil, errors.New("minio client not initialized")
}
- if fileSize > int64(maxSize) {
- return nil, errors.New("file size exceeds the limit")
- }
-
if !slices.Contains(supportedTypes, mimeType) {
return nil, errors.New("file type not supported")
}
@@ -128,15 +124,11 @@ func (s *ressourceService) UploadFile(filename string, fileSize int64, fileConte
return node, nil
}
-func (s *ressourceService) UploadAvatar(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxSize float64, supportedTypes []string, minioClient *minio.Client) error {
+func (s *ressourceService) UploadAvatar(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, supportedTypes []string, minioClient *minio.Client) error {
if minioClient == nil {
return errors.New("minio client not initialized")
}
- if fileSize > int64(maxSize) {
- return errors.New("file size exceeds the limit")
- }
-
if !slices.Contains(supportedTypes, mimeType) {
return errors.New("file type not supported")
}