feat(users): add account limits and API keys
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m43s
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m43s
This commit is contained in:
@@ -29,6 +29,10 @@ func (app *App) requireGuestUploads(ctx *gin.Context) bool {
|
||||
}
|
||||
|
||||
func (app *App) validateCreateBoxRequest(request *models.CreateBoxRequest) error {
|
||||
return app.validateCreateBoxRequestForActor(request, nil)
|
||||
}
|
||||
|
||||
func (app *App) validateCreateBoxRequestForActor(request *models.CreateBoxRequest, actor *requestActor) error {
|
||||
if request == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -45,19 +49,23 @@ func (app *App) validateCreateBoxRequest(request *models.CreateBoxRequest) error
|
||||
|
||||
totalSize := int64(0)
|
||||
for _, file := range request.Files {
|
||||
if err := app.validateFileSize(file.Size); err != nil {
|
||||
if err := app.validateFileSizeForActor(file.Size, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
totalSize += file.Size
|
||||
}
|
||||
return app.validateBoxSize(totalSize)
|
||||
return app.validateBoxSizeForActor(totalSize, actor)
|
||||
}
|
||||
|
||||
func (app *App) validateIncomingFile(boxID string, size int64) error {
|
||||
if err := app.validateFileSize(size); err != nil {
|
||||
return app.validateIncomingFileForActor(boxID, size, nil)
|
||||
}
|
||||
|
||||
func (app *App) validateIncomingFileForActor(boxID string, size int64, actor *requestActor) error {
|
||||
if err := app.validateFileSizeForActor(size, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
if app.config.GlobalMaxBoxSizeBytes <= 0 {
|
||||
if app.effectiveMaxBoxBytes(actor) <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -69,23 +77,27 @@ func (app *App) validateIncomingFile(boxID string, size int64) error {
|
||||
for _, file := range files {
|
||||
totalSize += file.Size
|
||||
}
|
||||
return app.validateBoxSize(totalSize)
|
||||
return app.validateBoxSizeForActor(totalSize, actor)
|
||||
}
|
||||
|
||||
func (app *App) validateManifestFileUpload(boxID string, fileID string, size int64) error {
|
||||
if err := app.validateFileSize(size); err != nil {
|
||||
return app.validateManifestFileUploadForActor(boxID, fileID, size, nil)
|
||||
}
|
||||
|
||||
func (app *App) validateManifestFileUploadForActor(boxID string, fileID string, size int64, actor *requestActor) error {
|
||||
if err := app.validateFileSizeForActor(size, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
manifest, err := boxstore.ReadManifest(boxID)
|
||||
if err != nil {
|
||||
return app.validateIncomingFile(boxID, size)
|
||||
return app.validateIncomingFileForActor(boxID, size, actor)
|
||||
}
|
||||
if boxstore.IsExpired(manifest) {
|
||||
_ = boxstore.DeleteBox(boxID)
|
||||
return fmt.Errorf("Box expired")
|
||||
}
|
||||
if app.config.GlobalMaxBoxSizeBytes <= 0 {
|
||||
if app.effectiveMaxBoxBytes(actor) <= 0 {
|
||||
return nil
|
||||
}
|
||||
totalSize := int64(0)
|
||||
@@ -101,24 +113,54 @@ func (app *App) validateManifestFileUpload(boxID string, fileID string, size int
|
||||
if !found {
|
||||
totalSize += size
|
||||
}
|
||||
return app.validateBoxSize(totalSize)
|
||||
return app.validateBoxSizeForActor(totalSize, actor)
|
||||
}
|
||||
|
||||
func (app *App) validateFileSize(size int64) error {
|
||||
return app.validateFileSizeForActor(size, nil)
|
||||
}
|
||||
|
||||
func (app *App) effectiveMaxFileBytes(actor *requestActor) int64 {
|
||||
if actor == nil {
|
||||
return app.config.GlobalMaxFileSizeBytes
|
||||
}
|
||||
return actor.User.Limits.MaxFileSizeBytes
|
||||
}
|
||||
|
||||
func (app *App) effectiveMaxBoxBytes(actor *requestActor) int64 {
|
||||
if actor == nil {
|
||||
return app.config.GlobalMaxBoxSizeBytes
|
||||
}
|
||||
return actor.User.Limits.MaxBoxSizeBytes
|
||||
}
|
||||
|
||||
func (app *App) validateFileSizeForActor(size int64, actor *requestActor) error {
|
||||
if size < 0 {
|
||||
return fmt.Errorf("File size cannot be negative")
|
||||
}
|
||||
if app.config.GlobalMaxFileSizeBytes > 0 && size > app.config.GlobalMaxFileSizeBytes {
|
||||
limit := app.effectiveMaxFileBytes(actor)
|
||||
if limit > 0 && size > limit {
|
||||
if actor != nil {
|
||||
return fmt.Errorf("File exceeds this account's max file size")
|
||||
}
|
||||
return fmt.Errorf("File exceeds the global max file size")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *App) validateBoxSize(size int64) error {
|
||||
return app.validateBoxSizeForActor(size, nil)
|
||||
}
|
||||
|
||||
func (app *App) validateBoxSizeForActor(size int64, actor *requestActor) error {
|
||||
if size < 0 {
|
||||
return fmt.Errorf("Box size cannot be negative")
|
||||
}
|
||||
if app.config.GlobalMaxBoxSizeBytes > 0 && size > app.config.GlobalMaxBoxSizeBytes {
|
||||
limit := app.effectiveMaxBoxBytes(actor)
|
||||
if limit > 0 && size > limit {
|
||||
if actor != nil {
|
||||
return fmt.Errorf("Box exceeds this account's max box size")
|
||||
}
|
||||
return fmt.Errorf("Box exceeds the global max box size")
|
||||
}
|
||||
return nil
|
||||
@@ -137,7 +179,11 @@ func (app *App) rejectExpiredManifestBox(boxID string) error {
|
||||
}
|
||||
|
||||
func (app *App) limitRequestBody(ctx *gin.Context) {
|
||||
limit := app.maxRequestBodyBytes()
|
||||
app.limitRequestBodyForActor(ctx, nil)
|
||||
}
|
||||
|
||||
func (app *App) limitRequestBodyForActor(ctx *gin.Context, actor *requestActor) {
|
||||
limit := app.maxRequestBodyBytesForActor(actor)
|
||||
if limit <= 0 {
|
||||
return
|
||||
}
|
||||
@@ -145,9 +191,14 @@ func (app *App) limitRequestBody(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
func (app *App) maxRequestBodyBytes() int64 {
|
||||
limit := app.config.GlobalMaxBoxSizeBytes
|
||||
if limit <= 0 || app.config.GlobalMaxFileSizeBytes > limit {
|
||||
limit = app.config.GlobalMaxFileSizeBytes
|
||||
return app.maxRequestBodyBytesForActor(nil)
|
||||
}
|
||||
|
||||
func (app *App) maxRequestBodyBytesForActor(actor *requestActor) int64 {
|
||||
limit := app.effectiveMaxBoxBytes(actor)
|
||||
fileLimit := app.effectiveMaxFileBytes(actor)
|
||||
if limit <= 0 || fileLimit > limit {
|
||||
limit = fileLimit
|
||||
}
|
||||
if limit <= 0 {
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user