Repository navigation
🐛 #4133 【微信支付】V3 应答验签增加时间戳新鲜度校验,防止重放 #4137
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
binarywang
merged 2 commits into
binarywang:develop
from
sisyfy-Zhang:fix/v3-response-timestamp-check
Oct 10, 2026
Merged
Changes from 1 commit
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
141 changes: 141 additions & 0 deletions
141
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/auth/WxPayValidatorTest.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,141 @@ | ||
| package com.github.binarywang.wxpay.v3.auth; | ||
|
|
||
| import org.apache.http.ProtocolVersion; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.entity.ContentType; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.apache.http.message.BasicHttpResponse; | ||
| import org.apache.http.message.BasicStatusLine; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * 测试 WxPayValidator 对应答时间戳的新鲜度校验(防重放)。 | ||
| * | ||
| * @author sisyfy-Zhang | ||
| */ | ||
| public class WxPayValidatorTest { | ||
|
|
||
| /** | ||
| * 固定的"当前时间"(秒),便于构造超前/滞后的应答时间戳 | ||
| */ | ||
| private static final long NOW_SECONDS = 1_700_000_000L; | ||
|
|
||
| private static final long FIVE_MINUTES = TimeUnit.MINUTES.toSeconds(5); | ||
| private static final long SIX_MINUTES = TimeUnit.MINUTES.toSeconds(6); | ||
| private static final long TEN_MINUTES = TimeUnit.MINUTES.toSeconds(10); | ||
|
|
||
| /** | ||
| * 最简 CloseableHttpResponse 实现,仅用于单元测试 | ||
| */ | ||
| private static class StubCloseableHttpResponse extends BasicHttpResponse implements CloseableHttpResponse { | ||
| StubCloseableHttpResponse() { | ||
| super(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 永远验签通过的 Verifier,用于把测试聚焦在时间戳校验上 | ||
| */ | ||
| private static final Verifier ALWAYS_VALID = new Verifier() { | ||
| @Override | ||
| public boolean verify(String serialNumber, byte[] message, String signature) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public X509Certificate getValidCertificate() { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| private static WxPayValidator validator(long toleranceSeconds) { | ||
| return new WxPayValidator(ALWAYS_VALID, toleranceSeconds, () -> NOW_SECONDS); | ||
| } | ||
|
|
||
| private static CloseableHttpResponse jsonResponse(String timestamp) { | ||
| StubCloseableHttpResponse response = new StubCloseableHttpResponse(); | ||
| response.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString()); | ||
| if (timestamp != null) { | ||
| response.setHeader("Wechatpay-Timestamp", timestamp); | ||
| } | ||
| response.setHeader("Wechatpay-Nonce", "nonce"); | ||
| response.setHeader("Wechatpay-Signature", "signature"); | ||
| response.setHeader("Wechatpay-Serial", "SERIAL"); | ||
| response.setEntity(new StringEntity("{\"code\":\"OK\"}", ContentType.APPLICATION_JSON)); | ||
| return response; | ||
| } | ||
|
|
||
| @Test | ||
| public void testFreshTimestampPasses() throws IOException { | ||
| assertTrue(validator(FIVE_MINUTES).validate(jsonResponse(String.valueOf(NOW_SECONDS)))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTimestampAtToleranceBoundaryPasses() throws IOException { | ||
| assertTrue(validator(FIVE_MINUTES).validate(jsonResponse(String.valueOf(NOW_SECONDS - FIVE_MINUTES)))); | ||
| assertTrue(validator(FIVE_MINUTES).validate(jsonResponse(String.valueOf(NOW_SECONDS + FIVE_MINUTES)))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStaleTimestampIsRejected() throws IOException { | ||
| assertFalse(validator(FIVE_MINUTES).validate(jsonResponse(String.valueOf(NOW_SECONDS - SIX_MINUTES))), | ||
| "滞后超过容差的应答应被判定为验签失败"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFutureTimestampIsRejected() throws IOException { | ||
| assertFalse(validator(FIVE_MINUTES).validate(jsonResponse(String.valueOf(NOW_SECONDS + SIX_MINUTES))), | ||
| "超前超过容差的应答应被判定为验签失败"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNonNumericTimestampIsRejected() throws IOException { | ||
| assertFalse(validator(FIVE_MINUTES).validate(jsonResponse("not-a-number"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMissingTimestampHeaderIsRejected() throws IOException { | ||
| assertFalse(validator(FIVE_MINUTES).validate(jsonResponse(null))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCustomToleranceIsHonoured() throws IOException { | ||
| assertTrue(validator(TEN_MINUTES).validate(jsonResponse(String.valueOf(NOW_SECONDS - SIX_MINUTES))), | ||
| "容差放宽到 10 分钟后,滞后 6 分钟的应答应通过"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testZeroToleranceDisablesTimestampCheck() throws IOException { | ||
| assertTrue(validator(0).validate(jsonResponse(String.valueOf(NOW_SECONDS - TEN_MINUTES))), | ||
| "容差为 0 表示关闭时间戳校验"); | ||
| assertTrue(validator(0).validate(jsonResponse("not-a-number")), | ||
| "关闭校验后不再解析时间戳"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDefaultConstructorUsesFiveMinutes() throws IOException { | ||
| long nowSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); | ||
| WxPayValidator validator = new WxPayValidator(ALWAYS_VALID); | ||
| assertTrue(validator.validate(jsonResponse(String.valueOf(nowSeconds)))); | ||
| assertFalse(validator.validate(jsonResponse(String.valueOf(nowSeconds - SIX_MINUTES)))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNonJsonResponseSkipsValidation() throws IOException { | ||
| StubCloseableHttpResponse response = new StubCloseableHttpResponse(); | ||
| response.setHeader("Content-Type", ContentType.APPLICATION_OCTET_STREAM.toString()); | ||
| // 非 JSON 应答(如下载账单)不做验签,这是既有行为,时间戳校验不应改变它 | ||
| assertTrue(validator(FIVE_MINUTES).validate(response)); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.