Skip to content

Commit c3cde6d

Browse files
committed
Don't use SNI when connecting with IP address
1 parent 78e1a23 commit c3cde6d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/SSLContextBuiltins.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
115115
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
116116
import com.oracle.graal.python.runtime.exception.PException;
117+
import com.oracle.graal.python.util.IPAddressUtil;
117118
import com.oracle.graal.python.util.PythonUtils;
118119
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
119120
import com.oracle.truffle.api.TruffleFile;
@@ -196,7 +197,8 @@ static SSLEngine createSSLEngine(Frame frame, PConstructAndRaiseNode constructAn
196197
}
197198
SSLParameters parameters = new SSLParameters();
198199
SSLEngine engine;
199-
if (serverHostname != null) {
200+
// Set SNI hostname only for non-IP hostnames
201+
if (serverHostname != null && !isIPAddress(serverHostname)) {
200202
try {
201203
parameters.setServerNames(Collections.singletonList(new SNIHostName(serverHostname)));
202204
} catch (IllegalArgumentException e) {
@@ -245,6 +247,12 @@ static SSLEngine createSSLEngine(Frame frame, PConstructAndRaiseNode constructAn
245247
return engine;
246248
}
247249

250+
@TruffleBoundary
251+
private static boolean isIPAddress(String str) {
252+
return IPAddressUtil.isIPv4LiteralAddress(str) || IPAddressUtil.isIPv6LiteralAddress(str) ||
253+
str.startsWith("[") && str.endsWith("]") && IPAddressUtil.isIPv6LiteralAddress(str.substring(1, str.length() - 1));
254+
}
255+
248256
@Builtin(name = "_wrap_socket", minNumOfPositionalArgs = 3, parameterNames = {"$self", "sock", "server_side", "server_hostname"}, keywordOnlyNames = {"owner", "session"})
249257
@ArgumentClinic(name = "server_side", conversion = ArgumentClinic.ClinicConversion.Boolean)
250258
@GenerateNodeFactory

0 commit comments

Comments
 (0)