-
Notifications
You must be signed in to change notification settings - Fork 357
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
Fix custom SSLSocketFactory not being set because of an unsafe lazy-initialization in JDK #4566
Conversation
This prevents calling HttpsURLConnection.getDefaultSSLSocketFactory() in an unsafe manner due to the poorly implemented lazy-initialization on the JDK. When multiple threads call that method concurrently (calling secureConnection()) the SSLSocketFactory is instantiated two times, making one thread fail the check and overriding the custom socket factory with the default one. Signed-off-by: Adrian Haasler García <adrian.haasler@amplia.es>
483b094
to
7dc7d89
Compare
Test made by Kevin Conaway @kevinconaway Also-by: Kevin Conaway <kevin.conaway@walmart.com> Signed-off-by: Adrian Haasler García <adrian.haasler@amplia.es>
The test nicely tests what is fixed by this PR and I think it would be good to include it. Given that the original PR of @kevinconaway contained the code, and it could have been contributed to the Eclipse Foundation along with other Jersey code, I think it is safe to include it. |
Please let me know if you need me to sign a CLA or something for the original code. |
I've added you with the same name and email as your original commit:
I don't know if the |
@kevinconaway If you can sign the ECA, it would be great. Please follow the steps here. |
How should I proceed with this contribution? |
@kevinconaway We would be happy if you help us here. If you cannot, please let us know. Thank you. |
I have signed the ECA |
Thanks @kevinconaway, did you sign it with the same email that appears on the commit message amplia-iiot@6a8e80e or should I update it? |
Some tests have failed:
I doubt they have anything to do with the change in the first commit, how may I help? |
You may update it to the email address that was used to sign the ECA |
Are we sure this fixes something?. I don't agree with what is written here: https://github.com/jersey/jersey/issues/3293
It is true that HttpsURLConnection.getDefaultSSLSocketFactory() is not thread safe, but it doesn't really matter because it relies in SSLSocketFactory.getDefault(). This one is thread safe and it will always return the same instance. So it is possible that 1+ threads invokes HttpsURLConnection.getDefaultSSLSocketFactory() when defaultSSLSocketFactory is null, and then it is set more than 1 time. But it doesn't matter because the instance is the same. |
This is the piece of code in the SDK that contains the lazy-initialization unsafe code: public static SSLSocketFactory getDefaultSSLSocketFactory() {
if (defaultSSLSocketFactory == null) {
defaultSSLSocketFactory =
(SSLSocketFactory)SSLSocketFactory.getDefault();
}
return defaultSSLSocketFactory;
} It doesn't matter that
This causes the jersey comparison to fail and the custom SSLSocketFactory is not set. We have seen this in production and this patch solves it. |
I see, 'theFactory' is not always set in the SSLSocketFactory.getDefault(). Thanks. Initially I saw that SSLSocketFactory.getDefault() returns the already instanced 'theFactory':
But sometimes 'theFactory' is not instanced. |
Hi,
this PR fixes #3293 with an approach similar to the one contributed by @kevinconaway on jersey/jersey#3738 (check up his well written explanation). We (@amplia-iiot) think his contribution was forgotten when the project was transitioned (#3738) over to the Eclipse Foundation and have implemented this more maintainable approach (the change is easier to notice when looking at
secureConnection(JerseyClient, HttpURLConnection)
and avoids multiple calls toHttpsURLConnection.getDefaultSSLSocketFactory()
making it cleaner).I've not included the original test by @kevinconaway because I'm not aware of his status regarding the Eclipse Contributor Agreement, but it can be checked at @6a8e80ec625f2ce9a7585dc45e645fab61aa504c (bugfix/jersey#3293-test).
We think this is the way to go for Jersey until the JDK properly makes this lazy-initialization thread-safe like the implementation in Android's JDK.
Nevertheless, we are open to feedback regarding this change and discussing alternate approaches.