Repository navigation
fix(admin): reject negative cache_hit_input_per_m in the model validator - #190
Merged
Merged
Conversation
validate_common checked 5 of the model's 6 price fields for non-negativity but skipped cache_hit_input_per_m (the non-peak sibling of the peak variant it does check), so POST/PATCH /api/admin/models accepted a negative cache-hit price. That value reaches billing::raw_cost unclamped, and billing::settle does balance - pts, so a negative pts credits the consumer and makes the sharing owner's earn negative. Add the missing check so all six price fields follow one rule.
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.
Summary
validate_common(src/routes/admin_models.rs)此前校验了模型 6 个价格字段中的 5 个(input_per_m/output_per_m/peak_input_per_m/peak_output_per_m/peak_cache_hit_input_per_m),独漏cache_hit_input_per_m—— 恰是已校验的高峰变体peak_cache_hit_input_per_m的非高峰兄弟。因此POST/PATCH /api/admin/models可以存入负数的缓存命中价。该值会一路无钳制地流入结算:
billing::effective_prices→billing::raw_cost/calc_points→gateway.rs的pts→billing::settle。在settle中UPDATE quotas SET balance = balance - pts:当pts < 0时这是加法(消费者被充值),而earn = round5(pts * 0.9)为负 ⇒ 分享者被倒扣。也就是说,一次管理员写入即可把「收费」翻转为对该模型所有消费者的充值。本 PR 补上这条缺失的校验,使 6 个价格字段遵循同一规则(
≥ 0)。实测:
calc_points(1M 未命中 @1.0, 1M 命中 @-9.0)= -8(同一调用在文档默认命中价0下为+1)。Related Issue
无关联 issue —— 该缺陷由本项目自查(运行期探针复现)发现,非社区 issue。
Changes
validate_common新增cache_hit_input_per_m参数并加入>= 0判据(复用既有文案「价格不能为负数」);create/patch两个调用点各传入该值config.example.toml的该字段含义不变)Tests
cargo test全部通过(179 passed,基线 178 → 179)cargo fmt --check通过cargo clippy -- -D warnings通过admin_models_rejects_negative_cache_hit_pricecache_hit_input_per_m: -3→ 400-9→ 400,且断言库中原值未被写入cache_hit_input_per_m: 0→ 200(0 是文档默认「命中部分免费」,判据为< 0.0,不得误拒)peak_cache_hit_input_per_m: -3仍 → 400(回归保护)A/B 取证:仅回退
|| cache_hit_input_per_m < 0.0一行 ⇒ 恰好 1 红 / 178 绿(红者为「CREATE 负数应 400」断言,实际观测到 200),边界0对照与其余测试保持绿。文件已逐字节还原(md5 一致)。Checklist
fix/)