-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix: 小程序虚拟支付用户态签名 calcSig 未转小写导致 SIGNATURE_INVALID #3938
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
Merged
+76
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/bean/xpay/WxMaXPaySigParamsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package cn.binarywang.wx.miniapp.bean.xpay; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * 验证 {@link WxMaXPaySigParams} 签名方法的单元测试。 | ||
| * | ||
| * <p>修复说明:{@link WxMaXPaySigParams#calcSig(String)} 方法计算用户态签名(HMAC-SHA256)后 | ||
| * 未转小写,导致微信小程序端返回 {@code SIGNATURE_INVALID -15005} 错误。 | ||
| * 修复方案与 {@link WxMaXPaySigParams#calcPaySig(String, String)} 保持一致, | ||
| * 在返回前调用 {@code .toLowerCase()}。 | ||
| * | ||
| * @author GitHub Copilot | ||
| */ | ||
| public class WxMaXPaySigParamsTest { | ||
|
|
||
| private static final String SESSION_KEY = "your_session_key"; | ||
| private static final String APP_KEY = "your_app_key"; | ||
| private static final String POST_BODY = "{\"openid\":\"oHoSt5abc123\",\"env\":1}"; | ||
| private static final String URL = "https://api.weixin.qq.com/xpay/query_user_balance"; | ||
|
|
||
| /** | ||
| * 验证 calcSig 返回值全部为小写十六进制字符,不包含大写字母。 | ||
| */ | ||
| @Test | ||
| public void testCalcSigReturnsLowerCase() { | ||
| WxMaXPaySigParams sigParams = WxMaXPaySigParams.builder() | ||
| .sessionKey(SESSION_KEY) | ||
| .appKey(APP_KEY) | ||
| .build(); | ||
|
|
||
| String sig = sigParams.calcSig(POST_BODY); | ||
|
|
||
| assertTrue(sig.equals(sig.toLowerCase()), | ||
| "calcSig 返回值应为全小写,当前值: " + sig); | ||
| } | ||
|
|
||
| /** | ||
| * 验证 calcPaySig 返回值全部为小写十六进制字符,不包含大写字母。 | ||
| */ | ||
| @Test | ||
| public void testCalcPaySigReturnsLowerCase() { | ||
| WxMaXPaySigParams sigParams = WxMaXPaySigParams.builder() | ||
| .sessionKey(SESSION_KEY) | ||
| .appKey(APP_KEY) | ||
| .build(); | ||
|
|
||
| String paySig = sigParams.calcPaySig(URL, POST_BODY); | ||
|
|
||
| assertTrue(paySig.equals(paySig.toLowerCase()), | ||
| "calcPaySig 返回值应为全小写,当前值: " + paySig); | ||
| } | ||
|
|
||
| /** | ||
| * 验证 calcSig 与 calcPaySig 的返回值均为有效的 HMAC-SHA256 十六进制字符串(64 个字符)。 | ||
| */ | ||
| @Test | ||
| public void testCalcSigIsValidHexString() { | ||
| WxMaXPaySigParams sigParams = WxMaXPaySigParams.builder() | ||
| .sessionKey(SESSION_KEY) | ||
| .appKey(APP_KEY) | ||
| .build(); | ||
|
|
||
| String sig = sigParams.calcSig(POST_BODY); | ||
| String paySig = sigParams.calcPaySig(URL, POST_BODY); | ||
|
|
||
| assertEquals(sig.length(), 64, "HMAC-SHA256 签名应为 64 个十六进制字符"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assertEquals(paySig.length(), 64, "HMAC-SHA256 pay_sig 应为 64 个十六进制字符"); | ||
| assertTrue(sig.matches("[0-9a-f]+"), "calcSig 返回值应只含小写十六进制字符"); | ||
| assertTrue(paySig.matches("[0-9a-f]+"), "calcPaySig 返回值应只含小写十六进制字符"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
SignUtils.createHmacSha256Sign在捕获NoSuchAlgorithmException/InvalidKeyException时会返回null,这里直接调用.toLowerCase()可能触发NullPointerException并改变之前calcSig返回null的行为。是否需要明确该异常场景下的返回/抛出策略(例如保证非空或显式处理)?Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.