Skip to content

【Fix issue #13600】 auth error when non-admin users clone config#13674

Merged
KomachiSion merged 4 commits intoalibaba:developfrom
GoofySatoshi:develop-issue#13600
Aug 5, 2025
Merged

【Fix issue #13600】 auth error when non-admin users clone config#13674
KomachiSion merged 4 commits intoalibaba:developfrom
GoofySatoshi:develop-issue#13600

Conversation

@GoofySatoshi
Copy link
Contributor

fix: #13600
Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

This change is to address the problem in ISSUE #13600 where regular users with read and write permissions fail to clone configurations.

Brief changelog

Fixed the cloning exception caused by the mismatch between the namespaceId that requires authentication in the ordinary user's configuration cloning interface and the currently used authentication namespaceId. Actually, the targetNamespaceId should be obtained for authentication. However, in line 105 of the doFilter method in AbstractWebAuthFilter.java, the resource fails to obtain the targetNamespaceId, and the default public namespace is used, resulting in authentication errors and configuration cloning failures. This commit fixes the way the namespace requiring authentication is obtained inside the resource for the cloning interface.

Verifying this change

XXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@CLAassistant
Copy link

CLAassistant commented Aug 5, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

github-actions bot commented Aug 5, 2025

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

@lingma-agents
Copy link

lingma-agents bot commented Aug 5, 2025

修复非管理员用户克隆配置时的认证错误

变更概述
  • 问题修复

    • 修复了非管理员用户在克隆配置时因命名空间ID认证不匹配导致的权限错误问题。
    • 修改了AbstractWebAuthFilter中资源获取逻辑,确保在克隆接口中正确获取目标命名空间ID(targetNamespaceId)进行权限验证。
    • 调整了认证解析器逻辑,支持从@Secured注解的tags中提取命名空间参数,增强认证灵活性。
  • 新功能

    • Constants类中新增TARGET_NAMESPACE_ID常量,用于标识目标命名空间参数。
    • 扩展了AbstractResourceParser类,新增带Secured参数的getNamespaceId方法,支持更复杂的命名空间解析逻辑。
    • ConfigControllerV3的克隆接口上添加了@Secured注解,明确指定权限控制规则和特殊标签。
  • 测试更新

    • ConfigHttpResourceParser新增多个单元测试用例,覆盖了不同场景下的命名空间ID解析逻辑,包括正常、空参数、多标签、无效标签等情况。
    • 使用Mockito框架模拟请求参数,确保测试的准确性和覆盖率。
  • 依赖更新

    • ConfigHttpResourceParser中引入了Secured注解和Arrays工具类,支持更灵活的认证参数解析。
变更文件
文件路径 变更说明
api/​src/​main/​java/​com/​alibaba/​nacos/​api/​common/​Constants.​java 新增`TARGET_NAMESPACE_ID`常量,用于标识目标命名空间参数。
auth/​src/​main/​java/​com/​alibaba/​nacos/​auth/​parser/​AbstractResourceParser.​java 扩展`getNamespaceId`方法,新增带`Secured`参数的重载方法,支持更灵活的命名空间解析。
auth/​src/​main/​java/​com/​alibaba/​nacos/​auth/​parser/​http/​ConfigHttpResourceParser.​java 重写`getNamespaceId`方法,支持从`@Secured`注解的tags中提取命名空间参数,并增加相关处理逻辑。
auth/​src/​test/​java/​com/​alibaba/​nacos/​auth/​parser/​http/​ConfigHttpResourceParserTest.​java 新增多个测试用例,覆盖不同场景下的命名空间ID解析逻辑,确保解析逻辑的正确性。
config/​src/​main/​java/​com/​alibaba/​nacos/​config/​server/​controller/​v3/​ConfigControllerV3.​java 在克隆配置接口上添加`@Secured`注解,明确指定权限控制规则和特殊标签。
plugin/​auth/​src/​main/​java/​com/​alibaba/​nacos/​plugin/​auth/​constant/​Constants.​java 新增`SECURED_SPECIAL_TAGS`常量,用于标识需要特殊处理的认证标签。
时序图
sequenceDiagram
    participant CC as ConfigControllerV3
    participant ARP as AbstractResourceParser
    participant CHP as ConfigHttpResourceParser
    participant AWAF as AbstractWebAuthFilter

    CC->>CC: cloneConfig接口添加@Secured注解
    CC->>ARP: parse(request, secured)
    ARP->>CHP: getNamespaceId(request, secured)
    CHP->>CHP: 从secured.tags中提取namespaceId参数名
    CHP->>CHP: 从request中获取对应参数值
    CHP-->>ARP: 返回namespaceId
    ARP-->>CC: 返回解析后的资源信息
    CC->>AWAF: doFilter(resource)
    AWAF->>AWAF: 使用正确namespaceId进行权限验证
Loading

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 0 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 1 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 3 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 4 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
☕ auth/src/main/java/com/alibaba/nacos/auth/parser/AbstractResourceParser.java (1 💬)
☕ auth/src/main/java/com/alibaba/nacos/auth/parser/http/ConfigHttpResourceParser.java (1 💬)
☕ auth/src/test/java/com/alibaba/nacos/auth/parser/http/ConfigHttpResourceParserTest.java (1 💬)
☕ plugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. 权限解析逻辑与业务逻辑耦合度较高

当前在ConfigHttpResourceParser中,通过Secured注解的标签来解析命名空间ID的逻辑与HTTP请求处理紧密耦合。这种设计可能导致未来在扩展其他资源类型时重复类似的解析逻辑,增加维护成本。建议将标签解析逻辑抽象为独立的策略类或工具类,以提高代码复用性和可维护性。

📌 关键代码

@Override
protected String getNamespaceId(HttpServletRequest request, Secured secured) {
    return Arrays.stream(secured.tags())
            .filter(tag -> tag.startsWith(Constants.NAMESPACE_ID))
            .map(tag -> tag.split(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.SPLITTER))
            .filter(splitTags -> splitTags.length >= 2)
            .map(splitTags -> request.getParameter(splitTags[1]))
            .filter(StringUtils::isNotBlank)
            .findFirst()
            .orElseGet(() -> getNamespaceId(request));
}

⚠️ 潜在风险

随着系统功能扩展,类似的解析逻辑可能在多个地方重复出现,导致代码冗余和维护困难。

🔍2. 安全标签定义缺乏统一管理

Constants.javaplugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java中分别定义了安全相关的常量,存在分散管理的问题。建议建立统一的安全标签管理机制,避免因标签命名不一致或重复定义引发的安全漏洞或解析错误。

📌 关键代码

public static final String TARGET_NAMESPACE_ID = "targetNamespaceId";
public static final String SECURED_SPECIAL_TAGS = com.alibaba.nacos.api.common.Constants.NAMESPACE_ID
        + Resource.SPLITTER +com.alibaba.nacos.api.common.Constants.TARGET_NAMESPACE_ID;

⚠️ 潜在风险

标签定义分散可能导致命名冲突、使用不一致,进而影响权限控制的准确性。

🔍3. 测试覆盖未充分验证边界条件

虽然新增了多个测试用例,但部分边界情况如标签格式异常、参数缺失等场景的测试仍显不足。例如,对于splitTag:extra这类多分隔符的情况,测试仅验证了正常路径,未覆盖所有可能的异常路径。建议补充更多边界条件测试,确保解析逻辑的健壮性。

📌 关键代码

@Test
@Secured(tags = {"namespaceId:splitTag:extra"})
void testParseWithInvalidSplitTag() throws NoSuchMethodException {
    Secured secured = getMethodSecure();
    when(request.getParameter("splitTag")).thenReturn("splitNs");

    String actualNamespaceId = resourceParser.getNamespaceId(request, secured);
    assertEquals("splitNs", actualNamespaceId);
}

⚠️ 潜在风险

未充分测试边界条件可能导致在生产环境中出现意外行为,影响系统的稳定性和安全性。

🔍4. 权限控制粒度可能过于宽泛

ConfigControllerV3.java中,克隆配置操作使用了SECURED_SPECIAL_TAGS作为权限标签,该标签包含了源和目标命名空间ID。然而,实际业务场景中可能需要更细粒度的权限控制,例如区分只读和读写权限。建议评估是否需要引入更精细的权限控制机制,以满足复杂业务需求。

📌 关键代码

@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG, apiType = ApiType.CONSOLE_API,
        tags = com.alibaba.nacos.plugin.auth.constant.Constants.Tag.SECURED_SPECIAL_TAGS)

⚠️ 潜在风险

权限控制粒度过粗可能导致非预期的操作被执行,带来潜在的安全风险。

审查详情
📒 文件清单 (6 个文件)
📝 变更: 6 个文件

📝 变更文件:

  • api/src/main/java/com/alibaba/nacos/api/common/Constants.java
  • auth/src/main/java/com/alibaba/nacos/auth/parser/AbstractResourceParser.java
  • auth/src/main/java/com/alibaba/nacos/auth/parser/http/ConfigHttpResourceParser.java
  • auth/src/test/java/com/alibaba/nacos/auth/parser/http/ConfigHttpResourceParserTest.java
  • config/src/main/java/com/alibaba/nacos/config/server/controller/v3/ConfigControllerV3.java
  • plugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Comment on lines 60 to 62
protected String getNamespaceId(R request, Secured secured){
return getNamespaceId(request);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNamespaceId方法添加一个带Secured参数的重载版本,以支持更灵活的命名空间ID解析。

🟢 Minor | 🧹 Code Smells

📋 问题详情

原有的getNamespaceId方法签名不包含Secured对象,导致子类无法直接访问与请求相关的安全注解信息。通过提供一个新的重载方法并传入Secured对象,可以允许子类根据安全注解动态解析命名空间ID,提升权限控制的灵活性和准确性。

💡 解决方案

当前实现是合理的默认行为,它调用了原始的getNamespaceId方法。如果子类需要特殊处理,可以覆盖此方法。建议保持现状,因为这为未来的扩展提供了良好的基础。


您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Comment on lines 70 to 71
public static final String SECURED_SPECIAL_TAGS = com.alibaba.nacos.api.common.Constants.NAMESPACE_ID
+ Resource.SPLITTER +com.alibaba.nacos.api.common.Constants.TARGET_NAMESPACE_ID;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定义一个新的安全标签,用于标识需要特殊处理的命名空间ID。

🟢 Minor | 🧹 Code Smells

📋 问题详情

为了支持源和目标命名空间的区分,需要引入一个新的安全标签SECURED_SPECIAL_TAGS。这个标签将帮助权限验证模块正确解析和处理涉及两个不同命名空间的操作,如配置克隆。

💡 解决方案

该定义正确地组合了源和目标命名空间ID的键名,形成了一个可用于权限验证的特殊标签。这是一个必要的更改,以支持更复杂的权限控制场景。


您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

li-xiao-shuang
li-xiao-shuang previously approved these changes Aug 5, 2025
li-xiao-shuang
li-xiao-shuang previously approved these changes Aug 5, 2025
@wuyfee
Copy link

wuyfee commented Aug 5, 2025

$\color{red}{FAILURE}$
DETAILS
✅ - docker: success
❌ - deploy (standalone & cluster & standalone_auth): failure
❌ - e2e-java-test (standalone & cluster & standalone_auth): skipped
❌ - e2e-go-test (standalone & cluster): skipped
❌ - e2e-cpp-test (standalone & cluster): skipped
❌ - e2e-csharp-test (standalone & cluster): skipped
❌ - e2e-nodejs-test (standalone & cluster): skipped
❌ - e2e-python-test (standalone & cluster): skipped
✅ - clean (standalone & cluster & standalone_auth): success

@KomachiSion KomachiSion merged commit 7f0e931 into alibaba:develop Aug 5, 2025
3 checks passed
@KomachiSion KomachiSion added this to the 3.0.3 milestone Aug 5, 2025
@KomachiSion KomachiSion added kind/bug Category issues or prs related to bug. area/Config labels Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/Config kind/bug Category issues or prs related to bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nacos 3.0.2 CloneConfig got 403 Message: authorization failed

5 participants