-
Notifications
You must be signed in to change notification settings - Fork 451
Make SSL certificate validation respect wildcards #836
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
Merged
Changes from 27 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d260850
respect wildcard
peterbae 9023466
update logic to reflect new findings.
peterbae e4225ae
improved logic + test cases
peterbae 725fc70
apply formatter
peterbae 67ace3d
run with junit
peterbae a230a82
do not allow wildcards past the first period, and update the test acc…
peterbae 3d8e6c3
make some improvements with speed of logic
peterbae 5bde5b7
Fix test comments
peterbae 1d36666
make changes to handle more negative cases, and add testing for it
peterbae 2c7271a
comment changes
peterbae 70040a8
update parsing logic
peterbae e949547
clean up logic
peterbae 20e86e3
log more
peterbae 6de6bbb
modify logic + add comment
peterbae 9be9d11
Merge branch 'dev' of https://github.com/Microsoft/mssql-jdbc into gi…
peterbae e31a51e
add license header
peterbae 0c3255c
strenghen logic
peterbae 67c553b
add test for latest logic
peterbae 5fba39f
fix typo
peterbae 4f2fbeb
remove unnecessary exception
peterbae 6daf7e9
thank you rene
peterbae ed695ce
Merge branch 'dev' of https://github.com/Microsoft/mssql-jdbc into gi…
peterbae 4105f34
Merge branch 'github-816' of https://github.com/peterbae/mssql-jdbc i…
peterbae d14af0a
use original logic
peterbae 54bf5be
remove unnecessary exception
peterbae 0da2160
more tests
peterbae fb4de5b
Merge remote-tracking branch 'upstream/dev' into PR836
ulvii b1905b7
Add | Adding a different way of wildcard certificate validation
ulvii b9bcc21
Merge branch 'ms-dev' into github-816
cheenamalhotra cd6d8f9
Merge branch 'github-816' of https://github.com/peterbae/mssql-jdbc i…
ulvii 7beb2c4
Fix | Fix the issue where sub-domain contains wildcard at the end and…
ulvii ad7e3f0
Fix | Fix comment
ulvii 7c07349
Fix | Add new line to the end of the file
ulvii cb33eea
Merge branch 'dev' of https://github.com/Microsoft/mssql-jdbc into gi…
peterbae 9602a75
Merge pull request #7 from ulvii/PR836
peterbae a09ecb7
reflect comment
peterbae abd362d
reflect ulvi's changes
peterbae bfa48c6
change comment style
peterbae 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
135 changes: 135 additions & 0 deletions
135
src/test/java/com/microsoft/sqlserver/jdbc/SSLCertificateValidation.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,135 @@ | ||
| /* | ||
| * Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
| * available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
| */ | ||
|
|
||
| package com.microsoft.sqlserver.jdbc; | ||
ulvii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.platform.runner.JUnitPlatform; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import com.microsoft.sqlserver.jdbc.SQLServerConnection; | ||
|
|
||
|
|
||
| @RunWith(JUnitPlatform.class) | ||
| public class SSLCertificateValidation { | ||
|
|
||
| /** | ||
| * Tests our internal method, validateServerName() against different possible names in SSL certificate. | ||
| * | ||
| * @throws Exception | ||
| */ | ||
| @Test | ||
| public void testValidateServerName() throws Exception { | ||
|
|
||
| String serverName = "msjdbc.database.windows.net"; | ||
| String serverName2 = "bbbbuuzzuzzzzzz.example.net"; | ||
| String serverName3 = "xn--ms.database.windows.net"; | ||
|
|
||
| // Set up the HostNameOverrideX509TrustManager object using reflection | ||
| TDSChannel tdsc = new TDSChannel(new SQLServerConnection("someConnectionProperty")); | ||
| Class<?> hsoClass = Class.forName("com.microsoft.sqlserver.jdbc.TDSChannel$HostNameOverrideX509TrustManager"); | ||
| Constructor<?> constructor = hsoClass.getDeclaredConstructors()[0]; | ||
| constructor.setAccessible(true); | ||
| Object hsoObject = constructor.newInstance(null, tdsc, null, serverName); | ||
| Method method = hsoObject.getClass().getDeclaredMethod("validateServerName", String.class); | ||
| method.setAccessible(true); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = msjdbc.database.windows.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "msjdbc.database.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = ms*bc.database.windows.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "ms*bc.database.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = *bc.database.windows.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "*bc.database.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = ms*.database.windows.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "ms*.database.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = *jd*.database.windows.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "*jd*.database.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = ms.*.net | ||
| // Expected result: false | ||
| assertFalse((boolean) method.invoke(hsoObject, "ms.*.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = msjdbc.asd*dsa.windows.net | ||
| // Expected result: false | ||
| assertFalse((boolean) method.invoke(hsoObject, "msjdbc.asd*dsa.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = .*.windows.net | ||
| // Expected result: false | ||
| assertFalse((boolean) method.invoke(hsoObject, ".*.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = msjdbc.*.windows.net | ||
| // Expected result: false | ||
| assertFalse((boolean) method.invoke(hsoObject, "msjdbc.*.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = *.*.windows.net | ||
| // Expected result: false | ||
| // Note: multiple wildcards are not allowed, so this case shouldn't happen, but we still make sure to fail this. | ||
| assertFalse((boolean) method.invoke(hsoObject, "*.*.windows.net")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = *.com | ||
| // Expected result: false | ||
| // A cert with * plus a top-level domain is not allowed. | ||
| assertFalse((boolean) method.invoke(hsoObject, "*.com")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = xn--caf-dma*.com | ||
| // Expected result: fail | ||
| assertFalse((boolean) method.invoke(hsoObject, "xn--caf-dma*.com")); | ||
peterbae marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = * | ||
| // Expected result: fail | ||
| assertFalse((boolean) method.invoke(hsoObject, "*")); | ||
|
|
||
| // Server Name = msjdbc.database.windows.net | ||
| // SAN = ms*atabase.windows.net | ||
| // Expected result: fail | ||
| assertFalse((boolean) method.invoke(hsoObject, "ms*atabase.windows.net")); | ||
|
|
||
| hsoObject = constructor.newInstance(null, tdsc, null, serverName2); | ||
| method = hsoObject.getClass().getDeclaredMethod("validateServerName", String.class); | ||
| method.setAccessible(true); | ||
|
|
||
| // Server Name = bbbbuuzzuzzzzzz.example.net | ||
| // SAN = b*zzz.example.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "b*zzz.example.net")); | ||
|
|
||
| hsoObject = constructor.newInstance(null, tdsc, null, serverName3); | ||
| method = hsoObject.getClass().getDeclaredMethod("validateServerName", String.class); | ||
| method.setAccessible(true); | ||
|
|
||
| // Server Name = xn--ms.database.windows.net | ||
| // SAN = xn--ms.database.windows.net | ||
| // Expected result: true | ||
| assertTrue((boolean) method.invoke(hsoObject, "xn--ms.database.windows.net")); | ||
| } | ||
| } | ||
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.