-
Notifications
You must be signed in to change notification settings - Fork 569
fix(server): fix npe in non-auth mode #2912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
a5afd91 to
310a9d1
Compare
310a9d1 to
fbf80ee
Compare
fbf80ee to
d7bfdb6
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2912 +/- ##
============================================
- Coverage 35.57% 29.48% -6.10%
+ Complexity 333 286 -47
============================================
Files 802 802
Lines 67568 67572 +4
Branches 8777 8778 +1
============================================
- Hits 24040 19926 -4114
- Misses 40967 45371 +4404
+ Partials 2561 2275 -286 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| if (context == null) { | ||
| return "anonymous"; | ||
| } | ||
| return context.user.username(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
新增的 username() 方法在非认证模式下返回 "anonymous",但需要考虑:
-
业务逻辑正确性: 在
ManagerAPI.createManager()和GraphSpaceAPI.create()中,这个 username 被用作creator字段。如果在非认证模式下所有创建者都是 "anonymous",会导致无法追踪真实的资源创建者。 -
建议方案:
- 如果非认证模式下不需要追踪创建者,应该返回
null而不是 "anonymous",让调用方明确处理 - 或者在调用方检查认证模式,非认证模式下使用其他标识(如 IP、session ID 等)
- 如果非认证模式下不需要追踪创建者,应该返回
| return context.user.username(); | |
| public static String username() { | |
| Context context = HugeGraphAuthProxy.getContext(); | |
| if (context == null || context.user == null) { | |
| return null; // 让调用方明确处理非认证场景 | |
| } | |
| return context.user.username(); | |
| } |
请确认非认证模式下的业务预期行为。
| } | ||
|
|
||
| public static String username() { | ||
| Context context = HugeGraphAuthProxy.getContext(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
虽然修复了 context 为 null 的情况,但没有处理 context.user 为 null 的场景。建议增加防御性检查:
| Context context = HugeGraphAuthProxy.getContext(); | |
| public static String username() { | |
| Context context = HugeGraphAuthProxy.getContext(); | |
| if (context == null || context.user == null) { | |
| return "anonymous"; | |
| } | |
| return context.user.username(); | |
| } |
或者检查一下 Context 的构造函数是否保证 user 字段非空。
| this.hugegraph.updateTime(updateTime); | ||
| } | ||
|
|
||
| public static String username() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是一个 NPE 修复,应该添加单元测试来验证:
- 非认证模式下调用
username()的行为 getContext()返回 null 时的处理- 各个调用方(ManagerAPI, GraphsAPI, GraphSpaceAPI)在非认证模式下的正确性
建议添加测试类或测试方法覆盖这些场景。
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java
Show resolved
Hide resolved
| validUser(authManager, user); | ||
|
|
||
| String creator = HugeGraphAuthProxy.getContext().user().username(); | ||
| String creator = HugeGraphAuthProxy.username(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议搜索整个代码库中所有 HugeGraphAuthProxy.getContext().user().username() 的调用点,确保都已替换为新的 username() 方法,避免遗漏其他潜在的 NPE 风险点。
可以使用以下命令检查:
grep -r "HugeGraphAuthProxy.getContext().user().username()" --include="*.java"
fix npe in server non-auth mode