Skip to content

Commit 98a0d09

Browse files
Fix Selenium instrumentation to avoid trying to set cookies for about:blank and other similar pages (#6973)
1 parent 2500e38 commit 98a0d09

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

dd-java-agent/instrumentation/selenium/src/main/java/datadog/trace/instrumentation/selenium/SeleniumUtils.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,43 +71,44 @@ public static void afterPageOpen(WebDriver driver) {
7171

7272
private static void injectRumContext(WebDriver driver, DDTraceId traceId) {
7373
WebDriver.Options options = driver.manage();
74-
// options can be null if the driver is not finished initialization yet
74+
// options can be null if the driver has not finished initializing yet
7575
// (which is the case when the driver's home page is opened)
76-
if (options != null) {
76+
if (options == null) {
77+
return;
78+
}
79+
try {
7780
String domain = getCookieDomain(driver.getCurrentUrl());
7881
options.addCookie(new Cookie(RUM_CONTEXT_COOKIE_NAME, traceId.toString(), domain, "/", null));
82+
} catch (MalformedURLException e) {
83+
// could be "about:blank" or other similar pages,
84+
// trying to set a cookie will cause exceptions
7985
}
8086
}
8187

8288
private static void clearRumContext(WebDriver driver) {
8389
WebDriver.Options options = driver.manage();
84-
// options can be null if the driver is not finished initialization yet
90+
// options can be null if the driver has not finished initializing yet
8591
// (which is the case when the driver's home page is opened)
8692
if (options != null) {
8793
options.deleteCookieNamed(RUM_CONTEXT_COOKIE_NAME);
8894
}
8995
}
9096

9197
@SuppressForbidden
92-
static String getCookieDomain(String urlString) {
93-
try {
94-
URL url = new URL(urlString);
95-
String host = url.getHost();
96-
if (isIPV4Address(host)) {
97-
return null;
98-
}
99-
100-
int idx = host.length();
101-
int tokenCount = 0;
102-
while (tokenCount < 2 && idx > 0) {
103-
idx = host.lastIndexOf('.', idx - 1);
104-
tokenCount++;
105-
}
106-
return idx == -1 ? null : host.substring(idx + 1);
107-
108-
} catch (MalformedURLException e) {
98+
static String getCookieDomain(String urlString) throws MalformedURLException {
99+
URL url = new URL(urlString);
100+
String host = url.getHost();
101+
if (isIPV4Address(host)) {
109102
return null;
110103
}
104+
105+
int idx = host.length();
106+
int tokenCount = 0;
107+
while (tokenCount < 2 && idx > 0) {
108+
idx = host.lastIndexOf('.', idx - 1);
109+
tokenCount++;
110+
}
111+
return idx == -1 ? null : host.substring(idx + 1);
111112
}
112113

113114
@SuppressForbidden

dd-java-agent/instrumentation/selenium/src/test/groovy/datadog/trace/instrumentation/selenium/SeleniumUtilsTest.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class SeleniumUtilsTest extends AgentTestRunner {
2929

3030
where:
3131
host | expectedValue
32-
null | null
33-
"malformed-url" | null
3432
"http://192.168.0.1" | null
3533
"http://localhost:8080" | null
3634
"http://website.com" | null

0 commit comments

Comments
 (0)