-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix(security): 默认校验服务器证书,随机串改用 SecureRandom,签名比较改为定时安全 #4081
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
Open
devin-ai-integration
wants to merge
3
commits into
develop
Choose a base branch
from
devin/1785469108-security-tls-and-random
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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
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
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
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
44 changes: 44 additions & 0 deletions
44
...n/src/test/java/me/chanjar/weixin/common/util/http/ServerCertificateVerificationTest.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,44 @@ | ||
| package me.chanjar.weixin.common.util.http; | ||
|
|
||
| import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; | ||
| import me.chanjar.weixin.common.util.http.hc.DefaultHttpComponentsClientBuilder; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
|
|
||
| /** | ||
| * 验证默认情况下不会跳过服务器端证书校验,避免中间人攻击. | ||
| */ | ||
| public class ServerCertificateVerificationTest { | ||
|
|
||
| @Test | ||
| public void testApacheBuilderVerifiesCertificateByDefault() throws Exception { | ||
| Constructor<DefaultApacheHttpClientBuilder> constructor = | ||
| DefaultApacheHttpClientBuilder.class.getDeclaredConstructor(); | ||
| constructor.setAccessible(true); | ||
| DefaultApacheHttpClientBuilder builder = constructor.newInstance(); | ||
|
|
||
| Assert.assertFalse(builder.isSkipServerCertificateVerification(), | ||
| "默认应校验服务器端证书"); | ||
|
|
||
| builder.setSkipServerCertificateVerification(true); | ||
| Assert.assertTrue(builder.isSkipServerCertificateVerification(), | ||
| "特殊调试场景下应允许显式跳过证书校验"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHttpComponentsBuilderVerifiesCertificateByDefault() throws Exception { | ||
| Constructor<DefaultHttpComponentsClientBuilder> constructor = | ||
| DefaultHttpComponentsClientBuilder.class.getDeclaredConstructor(); | ||
| constructor.setAccessible(true); | ||
| DefaultHttpComponentsClientBuilder builder = constructor.newInstance(); | ||
|
|
||
| Assert.assertFalse(builder.isSkipServerCertificateVerification(), | ||
| "默认应校验服务器端证书"); | ||
|
|
||
| builder.setSkipServerCertificateVerification(true); | ||
| Assert.assertTrue(builder.isSkipServerCertificateVerification(), | ||
| "特殊调试场景下应允许显式跳过证书校验"); | ||
| } | ||
| } |
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
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.
当调用者把
DefaultApacheHttpClientBuilder.get().setSkipServerCertificateVerification(true)用于自签名抓包或测试环境时,这里创建的 trust-allSSLConnectionSocketFactory实际不会参与建连:prepare()已经用注册表里的this.sslConnectionSocketFactory构造了PoolingHttpClientConnectionManager,随后在 builder 上调用的setSSLSocketFactory(...)会被该 connection manager 覆盖。因此 Apache 4 默认客户端仍会按默认 trust store 校验证书,和本次改好的 HC5 客户端行为不一致;应把根据开关生成的 socket factory 注册进 connection manager。Useful? React with 👍 / 👎.