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}
|
||||
|
||||
@@ -31,3 +31,67 @@ func TestValidateMultipartDirectNormalizesImageField(t *testing.T) {
|
||||
require.Equal(t, []string{"https://example.com/first.png"}, storedReq.Images)
|
||||
require.Equal(t, constant.TaskActionGenerate, info.Action)
|
||||
}
|
||||
|
||||
// TestTaskDurationBounds guards the billing invariant that user-supplied
|
||||
// video duration (a quota multiplier via OtherRatio "seconds") is bounded, so
|
||||
// it can never overflow quota calculation into a negative charge.
|
||||
func TestTaskDurationBounds(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
newContext := func(t *testing.T, body string) (*gin.Context, *RelayInfo) {
|
||||
request := httptest.NewRequest(http.MethodPost, "/v1/video/generations", strings.NewReader(body))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
context, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
context.Request = request
|
||||
return context, &RelayInfo{TaskRelayInfo: &TaskRelayInfo{}}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "huge duration is rejected",
|
||||
body: `{"model":"sora-2","prompt":"a cat","duration":9999999999}`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "huge seconds string is rejected",
|
||||
body: `{"model":"sora-2","prompt":"a cat","seconds":"9999999999"}`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative duration is rejected",
|
||||
body: `{"model":"sora-2","prompt":"a cat","duration":-8}`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "normal duration is accepted",
|
||||
body: `{"model":"sora-2","prompt":"a cat","seconds":"8"}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name+" (multipart direct)", func(t *testing.T) {
|
||||
context, info := newContext(t, tt.body)
|
||||
taskErr := ValidateMultipartDirect(context, info)
|
||||
if tt.wantErr {
|
||||
require.NotNil(t, taskErr)
|
||||
require.Equal(t, "invalid_seconds", taskErr.Code)
|
||||
} else {
|
||||
require.Nil(t, taskErr)
|
||||
}
|
||||
})
|
||||
t.Run(tt.name+" (basic task request)", func(t *testing.T) {
|
||||
context, info := newContext(t, tt.body)
|
||||
taskErr := ValidateBasicTaskRequest(context, info, constant.TaskActionGenerate)
|
||||
if tt.wantErr {
|
||||
require.NotNil(t, taskErr)
|
||||
require.Equal(t, "invalid_seconds", taskErr.Code)
|
||||
} else {
|
||||
require.Nil(t, taskErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user