mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-11 06:30:21 +00:00
fix(billing): validate quantity parameters and harden quota calculations
Bound user-supplied count/duration parameters at request validation, route ratio multipliers through guarded setters, and use saturating int conversions in all quota math paths.
This commit is contained in:
@@ -78,6 +78,22 @@ func validatePrompt(prompt string) *dto.TaskError {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxTaskDurationSeconds caps user-supplied video duration. Duration is used
|
||||
// as a billing multiplier (OtherRatio "seconds"); an unbounded value could
|
||||
// overflow quota calculation into a negative charge.
|
||||
const MaxTaskDurationSeconds = 3600
|
||||
|
||||
func validateTaskDurationBounds(req TaskSubmitReq) *dto.TaskError {
|
||||
seconds := req.Duration
|
||||
if seconds == 0 && req.Seconds != "" {
|
||||
seconds, _ = strconv.Atoi(req.Seconds)
|
||||
}
|
||||
if seconds < 0 || seconds > MaxTaskDurationSeconds {
|
||||
return createTaskError(fmt.Errorf("seconds must be between 1 and %d", MaxTaskDurationSeconds), "invalid_seconds", http.StatusBadRequest, true)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMultipartTaskRequest(c *gin.Context, info *RelayInfo, action string) (TaskSubmitReq, error) {
|
||||
var req TaskSubmitReq
|
||||
if _, err := c.MultipartForm(); err != nil {
|
||||
@@ -156,6 +172,10 @@ func ValidateMultipartDirect(c *gin.Context, info *RelayInfo) *dto.TaskError {
|
||||
return taskErr
|
||||
}
|
||||
|
||||
if taskErr := validateTaskDurationBounds(req); taskErr != nil {
|
||||
return taskErr
|
||||
}
|
||||
|
||||
action := constant.TaskActionTextGenerate
|
||||
if hasInputReference {
|
||||
action = constant.TaskActionGenerate
|
||||
@@ -217,6 +237,10 @@ func ValidateBasicTaskRequest(c *gin.Context, info *RelayInfo, action string) *d
|
||||
return taskErr
|
||||
}
|
||||
|
||||
if taskErr := validateTaskDurationBounds(req); taskErr != nil {
|
||||
return taskErr
|
||||
}
|
||||
|
||||
if len(req.Images) == 0 && strings.TrimSpace(req.Image) != "" {
|
||||
// 兼容单图上传
|
||||
req.Images = []string{req.Image}
|
||||
|
||||
Reference in New Issue
Block a user