Skip to content

fix(miniapp,open): 防止 code2Session 凭证通过异常和日志泄露 - #4130

Open
yearliny wants to merge 1 commit into
binarywang:developfrom
yearliny:codex/fix-code2session-credentials
Open

yearliny wants to merge 1 commit into
binarywang:developfrom
yearliny:codex/fix-code2session-credentials

Conversation

@yearliny

Copy link
Copy Markdown

普通小程序和开放平台代小程序登录直接拼接 jsCode 等查询参数,特殊字符会改变 URL 结构或使 HTTP 客户端抛出包含凭证的异常;SDK 成功、错误和 token 刷新日志也可能输出请求或响应中的凭证。

本 PR 对两个登录入口的原始参数逐值进行 UTF-8 编码,并对向外传播的异常建立安全边界:保留微信错误码和栈帧,移除原始消息、响应 JSON、cause 和 suppressed 异常。登录、开放平台获取 component token,以及小程序提取 access token 的 SDK 日志不再输出原始凭证数据。

兼容性与范围

  • 保留现有 get → execute 扩展链、HTTP 客户端选择、API host/代理配置、token 刷新、系统繁忙重试和多账号切换。
  • 不重新编码通用 get(String, String) 的已序列化查询串,不修改四种公共 GET 执行器的契约;普通登录继续沿用原 token 流程。
  • 正常返回值(包括 session_key)不做脱敏。保留两个入口原有的 null 处理差异。
  • 登录失败的原始诊断文本和嵌套异常有意不再暴露;保留常见参数、状态、空值异常类别,其他运行时异常可能转换为 WxRuntimeException,已更新 Javadoc。
  • 保护范围为上述登录链路的 SDK 自有日志和异常;不改变调用方自行输出参数或第三方 HTTP 客户端 wire logging 的配置,不声称完成全框架日志审计。
  • 生产依赖不变。为使日志断言真正执行,两个模块的测试使用兼容生产 SLF4J 1.7 的 Logback 绑定,版本集中管理;open 增加已由父 POM 管理版本的 Mockito 测试依赖。

验证

新增测试已加入三个模块默认 TestNG suite,全部使用假凭证和本地服务,不调用微信:

  • Apache HttpClient、HttpComponents、OkHttp、Jodd 四客户端的参数往返与微信错误处理。
  • 空格、换行、+&=#?%、中文、非 BMP Unicode、空串和 null。
  • 异常消息、错误对象、嵌套异常,以及成功/失败/刷新/首次取 token 的 SDK 日志不含假凭证。
  • 正常 session 返回、已编码的通用 query、扩展调用链、多账号切换、API host 替换、token 刷新和系统繁忙重试。

受影响模块及依赖的默认套件:128 项通过,0 失败、0 跳过(common 24、mp 72、miniapp 9、open 23)。JDK 17 编译为 Java 8 目标,测试进程使用本机 Temurin 8u422:

mvn -pl weixin-java-open -am test -Dmaven.test.skip=false -Djvm=<JDK8>/bin/java --no-transfer-progress

另已通过定向 clean test 与 git diff --check。本机 Corretto 17 的本地 HttpServer 初始化出现 Unable to establish loopback connection,因此未将 Windows/JDK 17 的完整测试执行计为通过;Java 8 下四客户端本地服务测试成功。未执行真实微信联调或全仓库测试。

目标分支:develop,遵循 CONTRIBUTING.md 的 Fork/PR 流程。

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Token refresh still has credential-safety and sensitive-logging gaps.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

This PR protects miniapp and open-platform code2Session credentials from URL, exception, and SDK log leakage.

Changes:

  • Adds UTF-8 parameter encoding and exception sanitization.
  • Updates login, token refresh, and sensitive-request logging.
  • Adds cross-client security tests and test logging configuration.

Review findings: token refresh credentials remain unencoded in the miniapp flow, and the open-platform POST token-refresh branch can still log sensitive error content.

File Description
weixin-java-open/​src/​test/​resources/​testng.xml Registers open-platform security tests
weixin-java-open/​src/​test/​java/​me/​chanjar/​weixin/​open/​api/​impl/​WxOpenCode2SessionSecurityTest.java Tests open-platform login security
weixin-java-open/​src/​main/​java/​me/​chanjar/​weixin/​open/​api/​WxOpenComponentService.java Updates login API documentation
weixin-java-open/​src/​main/​java/​me/​chanjar/​weixin/​open/​api/​impl/​WxOpenServiceAbstractImpl.java Protects sensitive-request logging
weixin-java-open/​src/​main/​java/​me/​chanjar/​weixin/​open/​api/​impl/​WxOpenComponentServiceImpl.java Encodes parameters and sanitizes errors
weixin-java-open/​pom.xml Adds test dependencies and logging configuration
weixin-java-miniapp/​src/​test/​resources/​testng.xml Registers miniapp security tests
weixin-java-miniapp/​src/​test/​java/​cn/​binarywang/​wx/​miniapp/​api/​impl/​WxMaCode2SessionSecurityTest.java Tests miniapp login security
weixin-java-miniapp/​src/​main/​java/​cn/​binarywang/​wx/​miniapp/​api/​WxMaService.java Updates login API documentation
weixin-java-miniapp/​src/​main/​java/​cn/​binarywang/​wx/​miniapp/​api/​impl/​BaseWxMaServiceImpl.java Protects login parameters, errors, and logs
weixin-java-miniapp/​pom.xml Configures test logging
weixin-java-common/​src/​test/​resources/​testng.xml Registers common utility tests
weixin-java-common/​src/​test/​java/​me/​chanjar/​weixin/​common/​util/​http/​SensitiveRequestUtilsTest.java Tests encoding and exception sanitization
weixin-java-common/​src/​main/​java/​me/​chanjar/​weixin/​common/​util/​http/​SensitiveRequestUtils.java Provides encoding and sanitization utilities
pom.xml Centralizes test logging versions

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +276 to +277
log.warn("即将重新获取新的access_token,错误代码:{},错误信息:{}", error.getErrorCode(),
WxOpenServiceAbstractImpl.isSensitiveRequest(uri) ? "[redacted]" : error.getErrorMsg());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants