Skip to content

Commit be28696

Browse files
committed
[GR-21381] Encode hostnames passed to gethostbyname with IDN
PullRequest: graalpython/827
2 parents ecc779c + 6c3f898 commit be28696

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SocketModuleBuiltins.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import java.io.BufferedReader;
4444
import java.io.IOException;
45+
import java.net.IDN;
4546
import java.net.Inet4Address;
4647
import java.net.Inet6Address;
4748
import java.net.InetAddress;
@@ -357,7 +358,7 @@ public abstract static class GetHostByNameNode extends PythonBuiltinNode {
357358
@Specialization
358359
Object getHostByName(String name) {
359360
try {
360-
InetAddress[] adresses = getAllByName(name);
361+
InetAddress[] adresses = getAllByName(IDNtoASCII(name));
361362
if (adresses.length == 0) {
362363
return PNone.NONE;
363364
}
@@ -366,6 +367,15 @@ Object getHostByName(String name) {
366367
throw raise(PythonBuiltinClassType.OSError);
367368
}
368369
}
370+
371+
@TruffleBoundary
372+
private String IDNtoASCII(String name) {
373+
try {
374+
return IDN.toASCII(name);
375+
} catch (IllegalArgumentException e) {
376+
throw raise(PythonBuiltinClassType.UnicodeError, "IDN encoding failed: %s", e.getMessage());
377+
}
378+
}
369379
}
370380

371381
@Builtin(name = "getservbyname", parameterNames = {"servicename", "protocolname"})

0 commit comments

Comments
 (0)