Skip to content

Commit

Permalink
Enforce HTTPs for all network requests.
Browse files Browse the repository at this point in the history
This is more or less a manual revert of #4851.
  • Loading branch information
jaynewstrom-stripe committed Jun 25, 2024
1 parent 91b1a9a commit da517e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private class NetworkStatement(
override fun open(
request: StripeRequest,
callback: HttpURLConnection.(request: StripeRequest) -> Unit
): HttpURLConnection {
): HttpsURLConnection {
val requestHost = request.url.hostFromUrl()
if (!hostsToTrack.contains(requestHost)) {
throw RequestNotFoundException(
Expand All @@ -109,10 +109,8 @@ private class NetworkStatement(
mockWebServer.baseUrl.toString().removeSuffix("/")
)
)
return (URL(delegatingRequest.url).openConnection() as HttpURLConnection).apply {
if (this is HttpsURLConnection) {
sslSocketFactory = mockWebServer.clientSocketFactory()
}
return (URL(delegatingRequest.url).openConnection() as HttpsURLConnection).apply {
sslSocketFactory = mockWebServer.clientSocketFactory()
callback(delegatingRequest)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.TimeUnit
import javax.net.ssl.HttpsURLConnection

/**
* Factory to create [StripeConnection], which encapsulates an [HttpURLConnection], triggers the
* Factory to create [StripeConnection], which encapsulates an [HttpsURLConnection], triggers the
* request and parses the response with different body type as [StripeResponse].
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand All @@ -31,15 +32,15 @@ interface ConnectionFactory {
fun open(
request: StripeRequest,
callback: HttpURLConnection.(request: StripeRequest) -> Unit
): HttpURLConnection
): HttpsURLConnection

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
object Default : ConnectionOpener {
override fun open(
request: StripeRequest,
callback: HttpURLConnection.(request: StripeRequest) -> Unit
): HttpURLConnection {
return (URL(request.url).openConnection() as HttpURLConnection).apply {
): HttpsURLConnection {
return (URL(request.url).openConnection() as HttpsURLConnection).apply {
callback(request)
}
}
Expand Down Expand Up @@ -69,7 +70,7 @@ interface ConnectionFactory {

private fun openConnectionAndApplyFields(
originalRequest: StripeRequest
): HttpURLConnection {
): HttpsURLConnection {
return connectionOpener.open(originalRequest) { request ->
connectTimeout = CONNECT_TIMEOUT
readTimeout = READ_TIMEOUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.io.InputStream
import java.net.HttpURLConnection
import java.nio.charset.StandardCharsets
import java.util.Scanner
import javax.net.ssl.HttpsURLConnection

/**
* A wrapper for accessing a [HttpURLConnection]. Implements [Closeable] to simplify closing related
Expand All @@ -22,7 +23,7 @@ interface StripeConnection<ResponseBodyType> : Closeable {

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
abstract class AbstractConnection<ResponseBodyType>(
private val conn: HttpURLConnection
private val conn: HttpsURLConnection
) : StripeConnection<ResponseBodyType> {
override val responseCode: Int
@JvmSynthetic
Expand Down Expand Up @@ -67,7 +68,7 @@ interface StripeConnection<ResponseBodyType> : Closeable {
* Default [StripeConnection] that converts the ResponseStream to a String.
*/
class Default internal constructor(
conn: HttpURLConnection
conn: HttpsURLConnection
) : AbstractConnection<String>(conn = conn) {

/**
Expand Down Expand Up @@ -95,7 +96,7 @@ interface StripeConnection<ResponseBodyType> : Closeable {
* [StripeConnection] that writes the ResponseStream to a File.
*/
class FileConnection internal constructor(
conn: HttpURLConnection,
conn: HttpsURLConnection,
private val outputFile: File
) : AbstractConnection<File>(conn = conn) {

Expand Down

0 comments on commit da517e3

Please sign in to comment.