Skip to content

Commit

Permalink
[java] Adding remote-allow-origins for Chrome
Browse files Browse the repository at this point in the history
Fixes #11750
  • Loading branch information
diemol committed Mar 16, 2023
1 parent d60cb15 commit 3f7f57c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions java/src/org/openqa/selenium/chromium/ChromiumOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Stream;
Expand Down Expand Up @@ -74,6 +75,11 @@ public class ChromiumOptions<T extends ChromiumOptions<?>> extends AbstractDrive
public ChromiumOptions(String capabilityType, String browserType, String capability) {
this.capabilityName = capability;
setCapability(capabilityType, browserType);
// Allowing any origin "*" might sound risky but an attacker would need to know
// the port used to start DevTools to establish a connection. Given these sessions
// are relatively short-lived, the risk is reduced. Also, this will be removed when
// we only support Java 11 and above.
addArguments("--remote-allow-origins=*");
}

/**
Expand Down Expand Up @@ -125,6 +131,20 @@ public T addArguments(String... arguments) {
* @param arguments The arguments to use when starting Chrome.
*/
public T addArguments(List<String> arguments) {
/*
--remote-allow-origins is being added by default since Chrome 111. We need to check
if the argument already exists and then remove it.
*/
String remoteAllowOrigins = "remote-allow-origins";
Optional<String> newArg = arguments.stream()
.filter(arg -> arg.contains(remoteAllowOrigins))
.findFirst();
Optional<String> existingArg = args.stream()
.filter(arg -> arg.contains(remoteAllowOrigins))
.findFirst();
if (newArg.isPresent() && existingArg.isPresent()) {
args.remove(existingArg.get());
}
args.addAll(arguments);
return (T) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ void driversCanBeConfiguredWithASpecificArguments() {
assertThat(reported.get(0).asMap()).asInstanceOf(MAP)
.extractingByKey(ChromeOptions.CAPABILITY).asInstanceOf(MAP)
.extractingByKey("args").asInstanceOf(LIST)
.containsExactly("--homepage=https://www.selenium.dev");
.containsAnyOf("--homepage=https://www.selenium.dev");
}

@Test
Expand Down

0 comments on commit 3f7f57c

Please sign in to comment.