Skip to content

Commit 49c169d

Browse files
committed
Add helper for non-ascii hostnames
1 parent 2364aea commit 49c169d

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

agent/src/main/java/dev/aikido/agent/wrappers/InetAddressWrapper.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.Executable;
99
import java.lang.reflect.InvocationTargetException;
1010
import java.lang.reflect.Method;
11+
import java.net.IDN;
1112
import java.net.InetAddress;
1213
import java.net.MalformedURLException;
1314
import java.net.URL;
@@ -32,6 +33,29 @@ public static class InetAdvice {
3233
// Since we have to wrap a native Java Class stuff gets more complicated
3334
// The classpath is not the same anymore, and we can't import our modules directly.
3435
// To bypass this issue we load collectors from a .jar file
36+
37+
// Java's system resolver rejects non-ASCII hostnames, so convert IDN to Punycode
38+
// before the real lookup runs. Without this, getAllByName throws UnknownHostException
39+
// and OnMethodExit never fires — meaning DNSRecordCollector can't block or track
40+
// the hostname.
41+
@Advice.OnMethodEnter(suppress = Throwable.class)
42+
public static void before(
43+
@Advice.Argument(value = 0, readOnly = false) String hostname
44+
) {
45+
if (hostname == null) {
46+
return;
47+
}
48+
for (int i = 0; i < hostname.length(); i++) {
49+
if (hostname.charAt(i) > 0x7F) {
50+
try {
51+
hostname = IDN.toASCII(hostname, IDN.ALLOW_UNASSIGNED);
52+
} catch (IllegalArgumentException ignored) {
53+
}
54+
return;
55+
}
56+
}
57+
}
58+
3559
@Advice.OnMethodExit
3660
public static void after(
3761
@Advice.Argument(0) String hostname,

0 commit comments

Comments
 (0)