Repository navigation
Description
After PR #15513 和 Issue #15476 finished. There is one hiden problem will caused.
Motivation
DefaultVisibilityAdvisorConverter converts the visibility plugin's QueryAdvisor into an executable QueryCondition.
The existing conversion and simplification rules work correctly while AuthorizedResources.resources is empty. Once explicitly authorized resources are returned, some combinations no longer preserve the expected authorization semantics.
Problem
Let:
Fbe the business query filters.Bbe theBaseVisibilityPredicate.Gbename IN authorizedResources.
The expected query is:
F AND (B OR G)
The current converter first applies and simplifies B, then appends G to orGroup. If B has already been moved to a top-level AND condition, omitted because the input already satisfies it, or converted to alwaysEmpty, adding G afterward cannot preserve the intended union.
The result may incorrectly become:
F AND B AND G; oralwaysEmpty, even whenF AND Gcan match.
Affected scenarios
The normal authenticated read path currently uses PUBLIC_AND_OWNER, with scope and owner filters appended after conversion, so its existing main flow generally works.
However, the following range-query scenarios can produce incorrect results:
- Range write query with
OWNER + G
Expected:
owner = currentUser OR name IN G
Possible current result:
owner = currentUser AND name IN G
- A visibility plugin returns
PUBLIC + G
Expected:
scope = PUBLIC OR name IN G
Possible current result:
scope = PUBLIC AND name IN G
- A visibility plugin returns
ALL + G
Expected:
No visibility restriction, because ALL OR G is still ALL.
Possible current result:
name IN G
PUBLIC_AND_OWNER + Gwithscopeorowneralready present before conversion
Some inputs may be incorrectly narrowed by G, while conflicting base conditions may set alwaysEmpty before checking whether G can match.
Anonymous users receiving explicit resource grants are excluded from this issue because that scenario is not supported by the intended authorization model.
Single-resource read/write checks are not affected. The issue only concerns list/search/count range-query conversion.
Expected behavior
The converter should first construct the complete authorization predicate:
B OR G
It should then combine that predicate with business filters F, remove impossible or already-satisfied branches, and only afterward decide whether to:
- keep multiple branches in
orGroup; - simplify a single branch into an AND condition; or
- set
alwaysEmpty=true.
alwaysEmpty must not be decided from the base predicate alone when authorized resources are non-empty.
Acceptance criteria
- Existing behavior remains unchanged when
AuthorizedResources.resourcesis empty. ALL + Gdoes not restrict the query.PUBLIC + GpreservesPUBLIC OR G.OWNER + GpreservesOWNER OR G.PUBLIC_AND_OWNER + Gworks correctly with empty or pre-populatedscopeandownerfilters.alwaysEmptyis calculated after considering authorized resources.- Count and paginated query results use the same corrected visibility condition.
- Focused tests cover the affected base-predicate and filter combinations.
Activity
KomachiSion commentedon Jul 29, 2026
QueryAdvisor AuthorizedResources 转换修复方案
1. 背景
DefaultVisibilityAdvisorConverter负责将 visibility 插件返回的QueryAdvisor转换为数据库可执行的QueryCondition。当前转换规则在
AuthorizedResources.resources为空时工作正常。但资源列表非空后,现有“先处理和化简 basePredicate,再追加 authorized resources”的流程不能始终保持并集语义。设:
F:业务查询条件,包括 namespaceId、type、nameLike、bizTagsLike、scope、owner 等。P:scope = PUBLIC。O:owner = currentIdentity。G:name IN authorizedResources。正确结果应始终满足:
F AND (BasePredicate OR G)当前实现可能错误生成:
F AND BasePredicate AND G;alwaysEmpty,即使F AND G实际可以匹配。2. 方案选择
G为空时走原逻辑,非空时走新增逻辑G为空时自然退化为原行为;不依赖调用顺序DefaultVisibilityAdvisorConverter推荐采用方案二。
统一方案可以证明:当
G为空时,所有结果都与当前 basePredicate 转换矩阵一致,因此不需要维护两套代码。3. 统一转换原则
转换过程调整为:
F。basePredicate构造基础授权分支。G作为额外授权分支加入。A = BasePredicate OR G。F.scope和F.owner对授权分支进行裁剪。F已经保证命中某个授权分支,则不再追加授权限制。F冲突,设置alwaysEmpty=true。orGroup。修改后的流程为:
输入 QueryCondition + identity + QueryAdvisor
→ 构造 basePredicate 授权分支
→ 合并 AuthorizedResources
→ 结合业务 scope/owner 裁剪授权分支
→ 判断授权条件是否已满足或必然为空
→ simplifyOrGroup
→ 输出 QueryCondition
4. BasePredicate 初始授权分支
ALLTRUEPUBLICP OR GOWNERO OR GOWNERG;如果G也为空则为空集PUBLIC_AND_OWNERP OR O OR GPUBLIC_AND_OWNERPUBLIC处理,即P OR G说明:
ALL OR G仍然是ALL,因此ALL场景不能被G收窄。PUBLIC + G仍需要为自定义 visibility 插件保留正确语义。OWNER + G需要保证未来扩展时语义正确。5. 授权分支与业务条件的裁剪规则
5.1 PUBLIC 分支
P分支状态PPUBLICP,整个授权谓词已满足PUBLICP分支5.2 OWNER 分支
O分支状态OO,整个授权谓词已满足O分支5.3 AuthorizedResources 分支
G保持为name IN authorizedResources。nameLike、bizTags、namespaceId 和 type 等业务过滤条件继续作为顶层 AND 条件,由数据库与G求交,不需要在 converter 中提前判断。6. ALL 完整矩阵
F,不追加授权限制F,不追加授权限制原因:
ALL OR G = ALL7. PUBLIC 完整矩阵
owner 只作为业务过滤条件,不影响 PUBLIC 授权分支判断。
F AND PF AND (P OR G)PUBLICFFPUBLICalwaysEmptyF AND G关键变化:
当 scope 非 PUBLIC 时,只能说明 PUBLIC 分支无交集;如果
G非空,仍应保留授权资源分支,不能直接alwaysEmpty。8. OWNER 完整矩阵
8.1 identity 为空
alwaysEmptyF AND G默认实现通常不会在 identity 为空时返回授权资源,但通用 converter 应保持逻辑完整。
8.2 identity 非空
scope 只作为业务过滤条件,不影响 OWNER 授权分支判断。
F AND OF AND (O OR G)FFalwaysEmptyF AND G关键变化:
当 owner 不等于 identity 时,只能删除 OWNER 分支。如果
G可以匹配,查询仍然不能判为空。9. PUBLIC_AND_OWNER 完整矩阵
identity 为空时按 PUBLIC 矩阵处理。
identity 非空时:
F AND (P OR O)F AND (P OR O OR G)FFF AND PF AND (P OR G)PUBLICFFPUBLICFFPUBLICFFPUBLICF AND OF AND (O OR G)PUBLICFFPUBLICalwaysEmptyF AND G说明:
scope=PUBLIC,授权谓词必然满足,不能再追加AND G。owner=identity,授权谓词必然满足,不能再追加AND G。scope!=PUBLIC且owner!=identity时,只能排除P和O,不能在G非空时直接判空。G为空时,该矩阵与当前实现保持一致。10. 化简规则
完成所有授权分支构造和裁剪后再执行化简。
F满足alwaysEmpty=truePscope=PUBLICOowner=identityGorGroup.name;Mapper 将其作为 AND 条件执行orGroup,生成括号内 OR 条件单一
G被 Mapper 转换为 AND 是正确的,因为此时它已经是唯一剩余的授权路径。11. 建议实现结构
DefaultVisibilityAdvisorConverter.convert()可以拆分为以下逻辑:buildBaseAuthorizationPlan(basePredicate, identity)scope、owner授权分支。appendAuthorizedResources(plan, authorizedResources)ALL场景不追加查询限制。name IN resources分支。authorizedResourceNames供诊断或后续扩展。reduceAuthorizationPlan(plan, queryCondition)applyAuthorizationPlan(plan, queryCondition)alwaysEmpty。orGroup。伪代码:
12. 代码修改范围
必须修改:
DefaultVisibilityAdvisorConverterDefaultVisibilityAdvisorConverterTest原则上不需要修改:
QueryCondition数据结构AiResourceMapperAiResourceMapper当前“单一 orGroup 条件按 AND 执行”的行为可以保留,因为统一裁剪后,单一分支确实代表唯一剩余的授权条件。13. 测试矩阵
13.1 AuthorizedResources 为空
保留并执行当前全部 converter 测试,证明行为无回归:
13.2 AuthorizedResources 非空
新增测试:
P OR GAND GGO OR GAND GGP OR O OR GO OR GP OR GG13.3 调用顺序一致性
对同一组业务条件分别验证:
最终 SQL 逻辑和查询结果必须一致。
13.4 分页一致性
验证 count 和 fetch 使用完全相同的授权条件:
totalCount等于可见资源总数。pageItems不包含越权资源。14. 兼容性结论
统一方案在
AuthorizedResources.resources为空时会自然退化为当前转换规则,因此不会改变历史查询行为。在授权列表非空时,统一方案可以保证:
最终查询 = 业务过滤条件 AND (基础可见性 OR 显式授权资源)同时消除 converter 对 scope/owner 设置顺序的隐式依赖,并为范围写查询及自定义 visibility 插件提供稳定的通用语义。
elnafateh commentedon Jul 31, 2026
Hi @KomachiSion, I'd like to take this on if that's okay
[ISSUE alibaba#15603] Address review: fix OWNER blank-identity resolu…
[ISSUE #15603] Correct QueryAdvisor conversion when AuthorizedResourc… (