Repository navigation
fix(billing): 修复 quota 整数溢出导致扣费变充值的漏洞 - #7
Open
Saltyfish-123 wants to merge 1 commit into
Open
Saltyfish-123 wants to merge 1 commit into
Saltyfish-123 wants to merge 1 commit into
Conversation
移植上游 v1.0.0-rc.19 的饱和钳位思路到当前计费架构。 用户传入超大的 n(图片张数)、duration(视频秒数)、max_tokens 等乘性参数时,quota 计算的中间结果会超出有符号整数范围并回绕成 负数;而计费代码的退款分支把非正 quota 当作退款处理 (IncreaseUserQuota(-quota)),导致扣费操作变成给用户增加余额。 修复要点: 1. 新增 common/quota_math.go,集中所有 quota→int 转换,钳位到 int32 范围(MaxQuota/MinQuota),超界饱和到边界并记 SysError, 不再回绕成负数。 2. 将预扣费、后结算、视频核销、BytePlus 计费等 9 处 int(float64(...)) 强转替换为 common.QuotaFromFloat(...)。 3. 修复 relay/image_handler.go 的 int(request.N)(uint→int 符号 反转,N>=2^63 时直接变负)攻击面。 4. 在 service/quota.go、controller/task_video.go 增加 quota<0 兜底 守卫,确保退款分支不会被触发去凭空充值。 5. 入口上界早拦截:图片 n<=100、Claude max_tokens<=2,000,000 (原本无校验)、视频 duration/seconds<=600 且非负。 验证:go build ./common/... ./relay/... ./controller/... ./service/... 通过(go1.25.1);go vet 改动文件无告警。 说明:因本 fork 落后 upstream/main 约 6070 提交,上游修复依赖的 全新计费架构(pkg/billingexpr、service/text_quota.go 等)本仓库 尚不具备,故采用手工移植而非 cherry-pick。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
移植上游 v1.0.0-rc.19 的饱和钳位思路到当前计费架构。
用户传入超大的 n(图片张数)、duration(视频秒数)、max_tokens
等乘性参数时,quota 计算的中间结果会超出有符号整数范围并回绕成
负数;而计费代码的退款分支把非正 quota 当作退款处理
(IncreaseUserQuota(-quota)),导致扣费操作变成给用户增加余额。
修复要点:
验证:go build ./common/... ./relay/... ./controller/... ./service/... 通过(go1.25.1);go vet 改动文件无告警。
说明:因本 fork 落后 upstream/main 约 6070 提交,上游修复依赖的
全新计费架构(pkg/billingexpr、service/text_quota.go 等)本仓库 尚不具备,故采用手工移植而非 cherry-pick。