- Notifications
You must be signed in to change notification settings - Fork1
fix: relaxed SNI hostname resolution#197
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletionsCHANGELOG.md
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 |
---|---|---|
@@ -2,6 +2,10 @@ | ||
## Unreleased | ||
### Fixed | ||
- relaxed SNI hostname resolution | ||
## 0.6.5 - 2025-09-16 | ||
### Fixed | ||
2 changes: 1 addition & 1 deletiongradle.properties
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
version=0.6.6 | ||
group=com.coder.toolbox | ||
name=coder-toolbox |
22 changes: 17 additions & 5 deletionssrc/main/kotlin/com/coder/toolbox/util/TLS.kt
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
237 changes: 237 additions & 0 deletionssrc/test/kotlin/com/coder/toolbox/util/AlternateNameSSLSocketFactoryTest.kt
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,237 @@ | ||
package com.coder.toolbox.util | ||
import io.mockk.Runs | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import java.net.InetAddress | ||
import java.net.Socket | ||
import javax.net.ssl.SSLParameters | ||
import javax.net.ssl.SSLSocket | ||
import javax.net.ssl.SSLSocketFactory | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertSame | ||
class AlternateNameSSLSocketFactoryTest { | ||
@Test | ||
fun `createSocket with no parameters should customize socket with alternate name`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
every { mockFactory.createSocket() } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "alternate.example.com") | ||
// When | ||
val result = alternateFactory.createSocket() | ||
// Then | ||
verify { mockSocket.sslParameters = any() } | ||
assertSame(mockSocket, result) | ||
} | ||
@Test | ||
fun `createSocket with host and port should customize socket with alternate name`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
every { mockFactory.createSocket("original.com", 443) } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "alternate.example.com") | ||
// When | ||
val result = alternateFactory.createSocket("original.com", 443) | ||
// Then | ||
verify { mockSocket.sslParameters = any() } | ||
assertSame(mockSocket, result) | ||
} | ||
@Test | ||
fun `createSocket with host port and local address should customize socket`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
val localHost = mockk<InetAddress>() | ||
every { mockFactory.createSocket("original.com", 443, localHost, 8080) } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "alternate.example.com") | ||
// When | ||
val result = alternateFactory.createSocket("original.com", 443, localHost, 8080) | ||
// Then | ||
verify { mockSocket.sslParameters = any() } | ||
assertSame(mockSocket, result) | ||
} | ||
@Test | ||
fun `createSocket with InetAddress should customize socket with alternate name`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
val address = mockk<InetAddress>() | ||
every { mockFactory.createSocket(address, 443) } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "alternate.example.com") | ||
// When | ||
val result = alternateFactory.createSocket(address, 443) | ||
// Then | ||
verify { mockSocket.sslParameters = any() } | ||
assertSame(mockSocket, result) | ||
} | ||
@Test | ||
fun `createSocket with InetAddress and local address should customize socket`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
val address = mockk<InetAddress>() | ||
val localAddress = mockk<InetAddress>() | ||
every { mockFactory.createSocket(address, 443, localAddress, 8080) } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "alternate.example.com") | ||
// When | ||
val result = alternateFactory.createSocket(address, 443, localAddress, 8080) | ||
// Then | ||
verify { mockSocket.sslParameters = any() } | ||
assertSame(mockSocket, result) | ||
} | ||
@Test | ||
fun `createSocket with existing socket should customize socket with alternate name`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSSLSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
val existingSocket = mockk<Socket>() | ||
every { mockFactory.createSocket(existingSocket, "original.com", 443, true) } returns mockSSLSocket | ||
every { mockSSLSocket.sslParameters } returns mockParams | ||
every { mockSSLSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "alternate.example.com") | ||
// When | ||
val result = alternateFactory.createSocket(existingSocket, "original.com", 443, true) | ||
// Then | ||
verify { mockSSLSocket.sslParameters = any() } | ||
assertSame(mockSSLSocket, result) | ||
} | ||
@Test | ||
fun `customizeSocket should set SNI hostname to alternate name for valid hostname`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
every { mockFactory.createSocket() } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "valid-hostname.example.com") | ||
// When & Then - This should work without throwing an exception | ||
assertNotNull(alternateFactory.createSocket()) | ||
verify { mockSocket.sslParameters = any() } | ||
} | ||
@Test | ||
fun `customizeSocket should NOT throw IllegalArgumentException for hostname with underscore`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
every { mockFactory.createSocket() } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "non_compliant_hostname.example.com") | ||
// When & Then - This should work without throwing an exception | ||
assertNotNull(alternateFactory.createSocket()) | ||
verify { mockSocket.sslParameters = any() } | ||
assertEquals(0, mockSocket.sslParameters.serverNames.size) | ||
} | ||
@Test | ||
fun `createSocket should work with valid international domain names`() { | ||
// Given | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
every { mockFactory.createSocket() } returns mockSocket | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
val alternateFactory = AlternateNameSSLSocketFactory(mockFactory, "test-server.example.com") | ||
// When & Then - This should work as hyphens are valid | ||
assertNotNull(alternateFactory.createSocket()) | ||
verify { mockSocket.sslParameters = any() } | ||
} | ||
private fun createMockSSLSocketFactory(): SSLSocketFactory { | ||
val mockFactory = mockk<SSLSocketFactory>() | ||
val mockSocket = mockk<SSLSocket>(relaxed = true) | ||
val mockParams = mockk<SSLParameters>(relaxed = true) | ||
// Setup default behavior | ||
every { mockFactory.defaultCipherSuites } returns arrayOf("TLS_AES_256_GCM_SHA384") | ||
every { mockFactory.supportedCipherSuites } returns arrayOf("TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256") | ||
// Make all createSocket methods return our mock socket | ||
every { mockFactory.createSocket() } returns mockSocket | ||
every { mockFactory.createSocket(any<String>(), any<Int>()) } returns mockSocket | ||
every { mockFactory.createSocket(any<String>(), any<Int>(), any<InetAddress>(), any<Int>()) } returns mockSocket | ||
every { mockFactory.createSocket(any<InetAddress>(), any<Int>()) } returns mockSocket | ||
every { | ||
mockFactory.createSocket( | ||
any<InetAddress>(), | ||
any<Int>(), | ||
any<InetAddress>(), | ||
any<Int>() | ||
) | ||
} returns mockSocket | ||
every { mockFactory.createSocket(any<Socket>(), any<String>(), any<Int>(), any<Boolean>()) } returns mockSocket | ||
// Setup SSL parameters | ||
every { mockSocket.sslParameters } returns mockParams | ||
every { mockSocket.sslParameters = any() } just Runs | ||
return mockFactory | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.