Commit 98864e03 by 崔彦峰

refactor(goods-listing): 简化商品上架审批服务中的规则验证逻辑

- 移除冗余的商户绑定验证方法 validateModifyRuleBoundToCurrentMerchant
- 删除相关的规则绑定商户ID查询方法 resolveRuleBindingMerchantIds
- 移除财务规则和对账规则的商户反查逻辑
- 简化修改型发布的数据校验流程,统一使用 fillModifyExcludeOldCodes 方法
- 减少代码复杂度并提高发布流程的执行效率
1 parent a3a2a445
...@@ -2467,14 +2467,7 @@ public class GoodsListingApprovalService { ...@@ -2467,14 +2467,7 @@ public class GoodsListingApprovalService {
if (!isSingleStoreModifyRulePublish(context)) { if (!isSingleStoreModifyRulePublish(context)) {
return; return;
} }
GoodsListingMerchantRef currentMerchantRef = getCurrentMerchantRef(context); // 修改型发布只收集本次原上架ID,用于copyOld继承旧规则时过滤旧商品/套餐关系。
if (currentMerchantRef == null) {
return;
}
// 这里仅保留修改型规则链路的数据校验:原上架ID必须能查到旧规则,且旧规则必须绑定当前门店。
// 发布阶段不再按单门店/多门店分流为原地更新,统一新建规则并在copyOld时过滤旧上架ID关系。
validateModifyRuleBoundToCurrentMerchant(context, currentMerchantRef, RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue());
validateModifyRuleBoundToCurrentMerchant(context, currentMerchantRef, RuleTypeEnum.RECONCILIATION_RULE.getValue());
fillModifyExcludeOldCodes(context); fillModifyExcludeOldCodes(context);
} }
...@@ -2522,121 +2515,6 @@ public class GoodsListingApprovalService { ...@@ -2522,121 +2515,6 @@ public class GoodsListingApprovalService {
&& StringUtils.isNotBlank(product.getOldMerchantGoodsCode()); && StringUtils.isNotBlank(product.getOldMerchantGoodsCode());
} }
private void validateModifyRuleBoundToCurrentMerchant(ApprovalPublishContext context, GoodsListingMerchantRef currentMerchantRef, Byte ruleType) {
if (context == null || currentMerchantRef == null || ruleType == null) {
return;
}
List<GoodsListingProductBaseDTO> modifyProducts = listModifyProducts(context);
if (CollectionUtils.isEmpty(modifyProducts)) {
return;
}
for (GoodsListingProductBaseDTO product : modifyProducts) {
Set<Integer> merchantIds = resolveRuleBindingMerchantIds(product, ruleType);
if (CollectionUtils.isEmpty(merchantIds)) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"未找到原上架ID对应的规则绑定商户,oldMerchantGoodsCode:" + product.getOldMerchantGoodsCode());
}
if (!merchantIds.contains(currentMerchantRef.getMerchantId())) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"原上架ID未绑定当前门店,oldMerchantGoodsCode:" + product.getOldMerchantGoodsCode());
}
}
}
// 先按原上架ID定位商品组关系,再沿规则表链路统计绑定的商户数。
private Set<Integer> resolveRuleBindingMerchantIds(GoodsListingProductBaseDTO product, Byte ruleType) {
List<GoodsTypeGroupRelation> relationList = listRuleRelatedGoodsTypeGroupRelations(product, ruleType);
if (CollectionUtils.isEmpty(relationList)) {
return Collections.emptySet();
}
Set<Integer> merchantIds = new LinkedHashSet<>();
for (GoodsTypeGroupRelation relation : relationList) {
if (relation == null || relation.getGoodsTypeGroupRuleId() == null) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"原上架ID对应的商品组关系缺少 goodsTypeGroupRuleId,oldMerchantGoodsCode:" + product.getOldMerchantGoodsCode());
}
if (Objects.equals(ruleType, RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue())) {
merchantIds.addAll(resolveSettleRuleBindingMerchantIds(relation.getGoodsTypeGroupRuleId()));
} else if (Objects.equals(ruleType, RuleTypeEnum.RECONCILIATION_RULE.getValue())) {
merchantIds.addAll(resolveReconciliationRuleBindingMerchantIds(relation.getGoodsTypeGroupRuleId()));
} else {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID, "不支持的规则类型:" + ruleType);
}
}
return merchantIds;
}
private List<GoodsTypeGroupRelation> listRuleRelatedGoodsTypeGroupRelations(GoodsListingProductBaseDTO product, Byte ruleType) {
if (product == null || ruleType == null || StringUtils.isBlank(product.getOldMerchantGoodsCode())) {
return Collections.emptyList();
}
GoodsTypeGroupRelation condition = GoodsTypeGroupRelation.builder()
.ruleType(ruleType)
.deletedFlag(DeletedFlagEnum.N.name())
.build();
if (Objects.equals(product.getProductType(), ProductTypeEnum.PACKAGE.getValue())) {
condition.setPackageCode(product.getOldMerchantGoodsCode());
} else {
condition.setMerchantGoodsCode(product.getOldMerchantGoodsCode());
}
List<GoodsTypeGroupRelation> relationList = smartcarGoodsTypeGroupRelationMapper.select(condition);
return CollectionUtils.isEmpty(relationList) ? Collections.emptyList() : relationList;
}
// 财务规则通过 goods_type_group_relation -> merchant_goods_type_group_settle_rule -> merchant_settle_account_rule 反查商户。
private Set<Integer> resolveSettleRuleBindingMerchantIds(Integer goodsTypeGroupRuleId) {
MerchantGoodsTypeGroupSettleRule condition = MerchantGoodsTypeGroupSettleRule.builder()
.goodsTypeGroupId(goodsTypeGroupRuleId)
.deletedFlag(DeletedFlagEnum.N.name())
.build();
List<MerchantGoodsTypeGroupSettleRule> settleGroupRules = smartcarMerchantGoodsTypeGroupSettleRuleMapper.select(condition);
if (CollectionUtils.isEmpty(settleGroupRules)) {
return Collections.emptySet();
}
Set<Integer> merchantIds = new LinkedHashSet<>();
for (MerchantGoodsTypeGroupSettleRule settleGroupRule : settleGroupRules) {
if (settleGroupRule == null || settleGroupRule.getSettleRuleId() == null) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"原上架ID对应的财务规则链路缺少 settleRuleId,goodsTypeGroupRuleId:" + goodsTypeGroupRuleId);
}
MerchantSettleAccountRule settleRule = smartcarMerchantSettleAccountRuleMapper.selectByPrimaryKey(settleGroupRule.getSettleRuleId());
if (settleRule == null || !Objects.equals(settleRule.getDeletedFlag(), DeletedFlagEnum.N.name())
|| settleRule.getMerchantId() == null) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"原上架ID对应的财务规则不存在或已删除,settleRuleId:" + settleGroupRule.getSettleRuleId());
}
merchantIds.add(settleRule.getMerchantId());
}
return merchantIds;
}
// 对账规则通过 goods_type_group_relation -> merchant_goods_type_group_reconciliation_rule -> merchant_reconciliation_rule 反查商户。
private Set<Integer> resolveReconciliationRuleBindingMerchantIds(Integer goodsTypeGroupRuleId) {
MerchantGoodsTypeGroupReconciliationRule condition = MerchantGoodsTypeGroupReconciliationRule.builder()
.id(goodsTypeGroupRuleId)
.deletedFlag(DeletedFlagEnum.N.name())
.build();
List<MerchantGoodsTypeGroupReconciliationRule> reconciliationGroupRules = smartcarMerchantGoodsTypeGroupReconciliationRuleMapper.select(condition);
if (CollectionUtils.isEmpty(reconciliationGroupRules)) {
return Collections.emptySet();
}
Set<Integer> merchantIds = new LinkedHashSet<>();
for (MerchantGoodsTypeGroupReconciliationRule reconciliationGroupRule : reconciliationGroupRules) {
if (reconciliationGroupRule == null || reconciliationGroupRule.getReconciliationRuleId() == null) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"原上架ID对应的对账规则链路缺少 reconciliationRuleId,goodsTypeGroupRuleId:" + goodsTypeGroupRuleId);
}
MerchantReconciliationRule reconciliationRule = smartcarMerchantReconciliationRuleMapper.selectByPrimaryKey(reconciliationGroupRule.getReconciliationRuleId());
if (reconciliationRule == null || !Objects.equals(reconciliationRule.getDeletedFlag(), DeletedFlagEnum.N.name())
|| reconciliationRule.getMerchantId() == null) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"原上架ID对应的对账规则不存在或已删除,reconciliationRuleId:" + reconciliationGroupRule.getReconciliationRuleId());
}
merchantIds.add(reconciliationRule.getMerchantId());
}
return merchantIds;
}
private List<Integer> collectEffectRecordIds(Map<Integer, List<DivisionRuleEffectRecord>> effectRecordMap, private List<Integer> collectEffectRecordIds(Map<Integer, List<DivisionRuleEffectRecord>> effectRecordMap,
List<GoodsListingMerchantRef> merchantRefs) { List<GoodsListingMerchantRef> merchantRefs) {
if (MapUtil.isEmpty(effectRecordMap) || CollectionUtils.isEmpty(merchantRefs)) { if (MapUtil.isEmpty(effectRecordMap) || CollectionUtils.isEmpty(merchantRefs)) {
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 2)

    1. 移除商户绑定校验可能导致水平越权风险(IDOR)(安全性与功能性)

    • 问题描述:本次 PR 删除了 validateModifyRuleBoundToCurrentMerchant 方法及其相关链路。该方法原本的核心作用是校验传入的 oldMerchantGoodsCode(原上架ID)是否真实绑定在当前门店(currentMerchantRef.getMerchantId())下。
    • 影响分析:如果系统在后续的 fillModifyExcludeOldCodes 或是 copyOld(继承旧规则)的底层逻辑中,没有强制加入 merchant_id = 当前商户ID 的过滤条件,恶意用户可以通过抓包篡改 oldMerchantGoodsCode 参数,从而读取、复制甚至篡改其他商户的敏感规则(如财务结算规则、对账规则)。这是一个典型的水平越权(IDOR)安全漏洞,可能导致严重的业务资产安全问题。
    • 优化建议:
      • 核实鉴权链路:请务必确认重构后的新链路(如底层 SQL 查询)中是否已经隐式或显式地包含了对当前商户身份的隔离限制。
      • 重构而非直接删除:如果后续链路缺少校验,必须将归属权校验加回。建议不要恢复原来的代码,而是采用批量查询(Batch Query)的方式,一次性查出所有 oldMerchantGoodsCode 对应的 merchant_id 并与当前商户比对,以兼顾安全与性能。

    2. 旧代码存在严重的 N+1 性能问题,删除操作对性能有益但需防范新链路重蹈覆辙(性能与最佳实践)

    • 问题描述:审查被删除的代码发现,原逻辑在 for (GoodsListingProductBaseDTO product : modifyProducts) 循环内部,嵌套调用了多次数据库单条查询(如 smartcarMerchantSettleAccountRuleMapper.selectByPrimaryKey)。
    • 影响分析:这种 N+1 查询反模式在批量修改商品时,会导致大量的数据库网络 IO 开销,极易引发接口超时和数据库连接池耗尽。本次重构将其移除,从性能角度来看是极大的正向优化。
    • 优化建议:这是一个优秀的重构方向。提出此点的目的是提醒:在替代方案(如 fillModifyExcludeOldCodes 方法)的实现中,请确保所有的数据收集和校验操作均采用了 IN (...) 等批量查询方式,严格避免在循环中操作数据库。

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分): 代码精简了复杂的校验流程,统一了发布阶段的规则新建逻辑,符合重构目标。但由于移除了显式的数据归属权前置拦截,若下游处理不当,可能引发边界异常,故扣除5分。
    • 安全性与潜在风险(20/30分): 直接移除对 oldMerchantGoodsCode 的商户归属验证,带来了极高的水平越权(IDOR)潜在风险。考虑到可能在其他层做了统一处理(PR中未体现),保守扣除10分,需重点 review 鉴权闭环。
    • 最佳实践与可维护性(18/20分): 移除了大量冗余且高耦合的校验代码,降低了圈复杂度,代码可读性和可维护性大幅提升。
    • 性能与资源利用(5/5分): 成功移除了原逻辑中潜在的数据库 N+1 查询灾难,对接口性能和资源利用率有显著提升,满分。
    • 提交信息质量(5/5分): Commit message 清晰、准确地描述了重构的内容(移除冗余验证、简化流程)以及目的(提高执行效率),具有很好的可追溯性,满分。

    总分:83分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!