Skip to content

Commit 7e8a560

Browse files
cortinicometa-codesync[bot]
authored andcommitted
Use HTTPS/WSS for dev-support traffic when port is 443 (#55790)
Summary: Pull Request resolved: #55790 React Native hardcodes http:// and ws:// schemes for all dev-support URLs (inspector, message, status, bundle, debugger). When routing through the proxy on port 443, this causes plain HTTP connections to an HTTPS port, which fail. Add httpScheme()/wsScheme() helpers to DevSupportHttpClient that return https/wss when the host ends with :443, and update all URL construction sites: DevServerHelper, JSPackagerClient, and PackagerStatusCheck. Changelog: [Internal] Reviewed By: mdvacca, cipolleschi, vzaidman Differential Revision: D94529835 fbshipit-source-id: f446939a82069c0466e26f107294ec4c6dd797d9
1 parent f906121 commit 7e8a560

5 files changed

Lines changed: 39 additions & 8 deletions

File tree

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,9 @@ public final class com/facebook/react/devsupport/inspector/DevSupportHttpClient
21062106
public static final fun addRequestHeader (Ljava/lang/String;Ljava/lang/String;)V
21072107
public final fun getHttpClient ()Lokhttp3/OkHttpClient;
21082108
public final fun getWebsocketClient ()Lokhttp3/OkHttpClient;
2109+
public static final fun httpScheme (Ljava/lang/String;)Ljava/lang/String;
21092110
public static final fun removeRequestHeader (Ljava/lang/String;)V
2111+
public static final fun wsScheme (Ljava/lang/String;)Ljava/lang/String;
21102112
}
21112113

21122114
public abstract interface class com/facebook/react/devsupport/interfaces/BundleLoadCallback {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public open class DevServerHelper(
7979
}
8080

8181
public val websocketProxyURL: String
82-
get() = "ws://${packagerConnectionSettings.debugServerHost}/debugger-proxy?role=client"
82+
get() =
83+
"${DevSupportHttpClient.wsScheme(packagerConnectionSettings.debugServerHost)}://${packagerConnectionSettings.debugServerHost}/debugger-proxy?role=client"
8384

8485
private enum class BundleType(val typeID: String) {
8586
BUNDLE("bundle"),
@@ -124,7 +125,8 @@ public open class DevServerHelper(
124125
get() =
125126
String.format(
126127
Locale.US,
127-
"http://%s/inspector/device?name=%s&app=%s&device=%s&profiling=%b",
128+
"%s://%s/inspector/device?name=%s&app=%s&device=%s&profiling=%b",
129+
DevSupportHttpClient.httpScheme(packagerConnectionSettings.debugServerHost),
128130
packagerConnectionSettings.debugServerHost,
129131
Uri.encode(getFriendlyDeviceName()),
130132
Uri.encode(packageName),
@@ -287,7 +289,8 @@ public open class DevServerHelper(
287289
}
288290
return (String.format(
289291
Locale.US,
290-
"http://%s/%s.%s?platform=android&dev=%s&lazy=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s",
292+
"%s://%s/%s.%s?platform=android&dev=%s&lazy=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s",
293+
DevSupportHttpClient.httpScheme(host),
291294
host,
292295
mainModuleID,
293296
type.typeID,
@@ -362,7 +365,8 @@ public open class DevServerHelper(
362365
requestUrlBuilder.append(
363366
String.format(
364367
Locale.US,
365-
"http://%s/open-debugger?device=%s",
368+
"%s://%s/open-debugger?device=%s",
369+
DevSupportHttpClient.httpScheme(packagerConnectionSettings.debugServerHost),
366370
packagerConnectionSettings.debugServerHost,
367371
Uri.encode(inspectorDeviceId),
368372
)
@@ -440,7 +444,13 @@ public open class DevServerHelper(
440444
FLog.w(ReactConstants.TAG, "Resource path should not begin with `/`, removing it.")
441445
resourcePath = resourcePath.substring(1)
442446
}
443-
return String.format(Locale.US, "http://%s/%s", host, resourcePath)
447+
return String.format(
448+
Locale.US,
449+
"%s://%s/%s",
450+
DevSupportHttpClient.httpScheme(host),
451+
host,
452+
resourcePath,
453+
)
444454
}
445455
}
446456
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@ internal class PackagerStatusCheck(private val client: OkHttpClient) {
7979

8080
private companion object {
8181
private const val PACKAGER_OK_STATUS = "packager-status:running"
82-
private const val PACKAGER_STATUS_URL_TEMPLATE = "http://%s/status"
82+
private const val PACKAGER_STATUS_URL_TEMPLATE = "%s://%s/status"
8383

8484
private fun createPackagerStatusURL(host: String): String =
85-
String.format(Locale.US, PACKAGER_STATUS_URL_TEMPLATE, host)
85+
String.format(
86+
Locale.US,
87+
PACKAGER_STATUS_URL_TEMPLATE,
88+
DevSupportHttpClient.httpScheme(host),
89+
host,
90+
)
8691
}
8792
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/DevSupportHttpClient.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,17 @@ public object DevSupportHttpClient {
5858
public fun removeRequestHeader(name: String) {
5959
customHeaders.remove(name)
6060
}
61+
62+
/**
63+
* Returns the appropriate HTTP scheme ("http" or "https") for the given host. Uses "https" when
64+
* the host specifies port 443 explicitly (e.g. "example.com:443").
65+
*/
66+
@JvmStatic
67+
public fun httpScheme(host: String): String = if (host.endsWith(":443")) "https" else "http"
68+
69+
/**
70+
* Returns the appropriate WebSocket scheme ("ws" or "wss") for the given host. Uses "wss" when
71+
* the host specifies port 443 explicitly (e.g. "example.com:443").
72+
*/
73+
@JvmStatic public fun wsScheme(host: String): String = if (host.endsWith(":443")) "wss" else "ws"
6174
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/JSPackagerClient.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package com.facebook.react.packagerconnection
99

1010
import android.net.Uri
1111
import com.facebook.common.logging.FLog
12+
import com.facebook.react.devsupport.inspector.DevSupportHttpClient
1213
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers.getFriendlyDeviceName
1314
import com.facebook.react.packagerconnection.ReconnectingWebSocket.MessageCallback
1415
import okio.ByteString
@@ -28,7 +29,7 @@ public constructor(
2829
init {
2930
val url =
3031
Uri.Builder()
31-
.scheme("ws")
32+
.scheme(DevSupportHttpClient.wsScheme(settings.debugServerHost))
3233
.encodedAuthority(settings.debugServerHost)
3334
.appendPath("message")
3435
.appendQueryParameter("device", getFriendlyDeviceName())

0 commit comments

Comments
 (0)