Skip to content

[Visibility] Correct QueryAdvisor conversion with non-empty AuthorizedResources #15603

Closed

Description

@KomachiSion

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:

  • F be the business query filters.
  • B be the BaseVisibilityPredicate.
  • G be name 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; or
  • alwaysEmpty, even when F AND G can 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:

  1. Range write query with OWNER + G

Expected:

owner = currentUser OR name IN G

Possible current result:

owner = currentUser AND name IN G

  1. A visibility plugin returns PUBLIC + G

Expected:

scope = PUBLIC OR name IN G

Possible current result:

scope = PUBLIC AND name IN G

  1. A visibility plugin returns ALL + G

Expected:

No visibility restriction, because ALL OR G is still ALL.

Possible current result:

name IN G

  1. PUBLIC_AND_OWNER + G with scope or owner already 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.resources is empty.
  • ALL + G does not restrict the query.
  • PUBLIC + G preserves PUBLIC OR G.
  • OWNER + G preserves OWNER OR G.
  • PUBLIC_AND_OWNER + G works correctly with empty or pre-populated scope and owner filters.
  • alwaysEmpty is 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

  1. added this to the 3.3.0 milestone on Jul 29, 2026
  2. KomachiSion commented on Jul 29, 2026

    @KomachiSion
    CollaboratorAuthor

    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. 统一转换原则

    转换过程调整为:

    1. 保留业务条件 F。
    2. 根据 basePredicate 构造基础授权分支。
    3. 将 G 作为额外授权分支加入。
    4. 得到完整授权谓词 A = BasePredicate OR G。
    5. 结合 F.scope 和 F.owner 对授权分支进行裁剪。
    6. 如果 F 已经保证命中某个授权分支,则不再追加授权限制。
    7. 如果所有授权分支都与 F 冲突,设置 alwaysEmpty=true。
    8. 如果只剩一个授权分支,化简为普通 AND 条件。
    9. 如果剩余多个授权分支,写入 orGroup。

    修改后的流程为:

    输入 QueryCondition + identity + QueryAdvisor
    → 构造 basePredicate 授权分支
    → 合并 AuthorizedResources
    → 结合业务 scope/owner 裁剪授权分支
    → 判断授权条件是否已满足或必然为空
    → simplifyOrGroup
    → 输出 QueryCondition

    4. BasePredicate 初始授权分支

    BasePredicate identity 完整授权谓词
    ALL 任意 TRUE
    PUBLIC 任意 P OR G
    OWNER 非空 O OR G
    OWNER 空 G;如果 G 也为空则为空集
    PUBLIC_AND_OWNER 非空 P OR O OR G
    PUBLIC_AND_OWNER 空 按 PUBLIC 处理,即 P OR G

    说明:

    • ALL OR G 仍然是 ALL,因此 ALL 场景不能被 G 收窄。
    • 默认模型不支持匿名用户获得显式资源授权,但 PUBLIC + G 仍需要为自定义 visibility 插件保留正确语义。
    • 当前没有写范围查询,但 OWNER + G 需要保证未来扩展时语义正确。

    5. 授权分支与业务条件的裁剪规则

    5.1 PUBLIC 分支

    输入 scope P 分支状态
    scope 为空 保留 P
    scope 为 PUBLIC 业务条件已经保证命中 P,整个授权谓词已满足
    scope 非 PUBLIC 删除 P 分支

    5.2 OWNER 分支

    输入 owner O 分支状态
    owner 为空 保留 O
    owner 等于 identity 业务条件已经保证命中 O,整个授权谓词已满足
    owner 不等于 identity 删除 O 分支

    5.3 AuthorizedResources 分支

    G 保持为 name IN authorizedResources。

    nameLike、bizTags、namespaceId 和 type 等业务过滤条件继续作为顶层 AND 条件,由数据库与 G 求交,不需要在 converter 中提前判断。

    6. ALL 完整矩阵

    输入条件 G 为空 G 非空
    任意 scope/owner 保留 F,不追加授权限制 保留 F,不追加授权限制

    原因:

    ALL OR G = ALL

    7. PUBLIC 完整矩阵

    owner 只作为业务过滤条件,不影响 PUBLIC 授权分支判断。

    输入 scope G 为空 G 非空
    空 F AND P F AND (P OR G)
    PUBLIC F F
    非 PUBLIC alwaysEmpty F AND G

    关键变化:

    当 scope 非 PUBLIC 时,只能说明 PUBLIC 分支无交集;如果 G 非空,仍应保留授权资源分支,不能直接 alwaysEmpty。

    8. OWNER 完整矩阵

    8.1 identity 为空

    G 为空 G 非空
    alwaysEmpty F AND G

    默认实现通常不会在 identity 为空时返回授权资源,但通用 converter 应保持逻辑完整。

    8.2 identity 非空

    scope 只作为业务过滤条件,不影响 OWNER 授权分支判断。

    输入 owner G 为空 G 非空
    空 F AND O F AND (O OR G)
    等于 identity F F
    不等于 identity alwaysEmpty F AND G

    关键变化:

    当 owner 不等于 identity 时,只能删除 OWNER 分支。如果 G 可以匹配,查询仍然不能判为空。

    9. PUBLIC_AND_OWNER 完整矩阵

    identity 为空时按 PUBLIC 矩阵处理。

    identity 非空时:

    输入 scope 输入 owner G 为空 G 非空
    空 空 F AND (P OR O) F AND (P OR O OR G)
    空 identity F F
    空 非 identity F AND P F AND (P OR G)
    PUBLIC 空 F F
    PUBLIC identity F F
    PUBLIC 非 identity F F
    非 PUBLIC 空 F AND O F AND (O OR G)
    非 PUBLIC identity F F
    非 PUBLIC 非 identity alwaysEmpty F AND G

    说明:

    • 只要业务条件已经保证 scope=PUBLIC,授权谓词必然满足,不能再追加 AND G。
    • 只要业务条件已经保证 owner=identity,授权谓词必然满足,不能再追加 AND G。
    • scope!=PUBLIC 且 owner!=identity 时,只能排除 P 和 O,不能在 G 非空时直接判空。
    • G 为空时,该矩阵与当前实现保持一致。

    10. 化简规则

    完成所有授权分支构造和裁剪后再执行化简。

    剩余授权分支 输出
    授权条件已由 F 满足 不追加授权条件
    0 个 alwaysEmpty=true
    1 个 P 如果 scope 为空,回写 scope=PUBLIC
    1 个 O 如果 owner 为空,回写 owner=identity
    1 个 G 保留单一 orGroup.name;Mapper 将其作为 AND 条件执行
    2 个及以上 保留 orGroup,生成括号内 OR 条件

    单一 G 被 Mapper 转换为 AND 是正确的,因为此时它已经是唯一剩余的授权路径。

    11. 建议实现结构

    DefaultVisibilityAdvisorConverter.convert() 可以拆分为以下逻辑:

    1. buildBaseAuthorizationPlan(basePredicate, identity)

      • 返回是否 unrestricted。
      • 返回初始 scope、owner 授权分支。
    2. appendAuthorizedResources(plan, authorizedResources)

      • ALL 场景不追加查询限制。
      • 其他场景加入 name IN resources 分支。
      • 同时保留 authorizedResourceNames 供诊断或后续扩展。
    3. reduceAuthorizationPlan(plan, queryCondition)

      • 判断 scope/owner 分支是已满足、冲突还是需要保留。
      • 业务条件已满足任意授权分支时,整个授权谓词无需追加。
      • 删除与业务条件冲突的授权分支。
    4. applyAuthorizationPlan(plan, queryCondition)

      • 无分支时设置 alwaysEmpty。
      • 单分支时化简。
      • 多分支时写入 orGroup。

    伪代码:

    QueryCondition result = preserveBusinessCondition(condition);
    AuthorizationPlan plan = buildBaseAuthorizationPlan(basePredicate, identity);
    
    if (plan.isUnrestricted()) {
        return result;
    }
    
    plan.addAuthorizedResources(authorizedResources);
    plan.reduceBy(result.getScope(), result.getOwner());
    
    if (plan.isSatisfiedByBusinessCondition()) {
        return result;
    }
    
    if (plan.hasNoRemainingBranch()) {
        result.setAlwaysEmpty(true);
        return result;
    }
    
    result.setOrGroup(plan.getRemainingBranches());
    simplifyOrGroup(result);
    return result;
    

    12. 代码修改范围

    必须修改:

    • DefaultVisibilityAdvisorConverter
    • DefaultVisibilityAdvisorConverterTest
    • visibility plugin 中 QueryAdvisor 转换相关规范
    • 原设计文档 8.5 和转换流程图

    原则上不需要修改:

    • QueryCondition 数据结构
    • AiResourceMapper
    • 数据库 Schema
    • Role/Permission 存储
    • visibility grant/revoke API
    • 单资源读写权限判断

    AiResourceMapper 当前“单一 orGroup 条件按 AND 执行”的行为可以保留,因为统一裁剪后,单一分支确实代表唯一剩余的授权条件。

    13. 测试矩阵

    13.1 AuthorizedResources 为空

    保留并执行当前全部 converter 测试,证明行为无回归:

    • ALL
    • PUBLIC:scope 空、PUBLIC、PRIVATE
    • OWNER:identity 空、owner 空、相同、不同
    • PUBLIC_AND_OWNER:scope/owner 全组合
    • orGroup 单分支化简
    • alwaysEmpty

    13.2 AuthorizedResources 非空

    新增测试:

    BasePredicate 测试场景
    ALL G 不得收窄查询
    PUBLIC scope 空时 P OR G
    PUBLIC scope=PUBLIC 时不得追加 AND G
    PUBLIC scope=PRIVATE 时仅保留 G
    OWNER owner 空时 O OR G
    OWNER owner=identity 时不得追加 AND G
    OWNER owner!=identity 时仅保留 G
    PUBLIC_AND_OWNER 无 scope/owner 时生成 P OR O OR G
    PUBLIC_AND_OWNER scope=PUBLIC 时不得被 G 收窄
    PUBLIC_AND_OWNER owner=identity 时不得被 G 收窄
    PUBLIC_AND_OWNER scope=PRIVATE 时生成 O OR G
    PUBLIC_AND_OWNER owner!=identity 时生成 P OR G
    PUBLIC_AND_OWNER scope=PRIVATE、owner!=identity 时仅保留 G

    13.3 调用顺序一致性

    对同一组业务条件分别验证:

    1. scope/owner 在 converter 前设置。
    2. scope/owner 在 converter 后设置。

    最终 SQL 逻辑和查询结果必须一致。

    13.4 分页一致性

    验证 count 和 fetch 使用完全相同的授权条件:

    • totalCount 等于可见资源总数。
    • pageItems 不包含越权资源。
    • 授权资源不会因分页后过滤而丢失。
    • scope/owner/nameLike/bizTag 过滤与授权谓词正确求交。

    14. 兼容性结论

    统一方案在 AuthorizedResources.resources 为空时会自然退化为当前转换规则,因此不会改变历史查询行为。

    在授权列表非空时,统一方案可以保证:

    最终查询 = 业务过滤条件 AND (基础可见性 OR 显式授权资源)

    同时消除 converter 对 scope/owner 设置顺序的隐式依赖,并为范围写查询及自定义 visibility 插件提供稳定的通用语义。

  3. elnafateh commented on Jul 31, 2026

    @elnafateh
    Contributor

    Hi @KomachiSion, I'd like to take this on if that's okay

  4. added a commit that references this issue on Aug 4, 2026
  5. added a commit that references this issue on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions