Skip to content

Commit a9bb4a2

Browse files
committed
Allow commonly used probe URLs to be accessed without a content type
1 parent baa7a75 commit a9bb4a2

6 files changed

Lines changed: 38 additions & 10 deletions

File tree

java/server/src/org/openqa/selenium/grid/server/NetworkOptions.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.grid.server;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableSet;
2122
import org.openqa.selenium.grid.config.Config;
2223
import org.openqa.selenium.grid.web.CheckContentTypeHeader;
2324
import org.openqa.selenium.grid.web.CheckOriginHeader;
@@ -29,10 +30,13 @@
2930

3031
import java.util.List;
3132
import java.util.Optional;
33+
import java.util.Set;
3234

3335
public class NetworkOptions {
3436

3537
private final Config config;
38+
// These are commonly used by process which can't set various headers.
39+
private final Set<String> SKIP_CHECKS_ON = ImmutableSet.of("/status", "/readyz");
3640

3741
public NetworkOptions(Config config) {
3842
this.config = Require.nonNull("Config", config);
@@ -51,14 +55,14 @@ public Filter getSpecComplianceChecks() {
5155
}
5256

5357
if (config.getBool("network", "check_content_type").orElse(true)) {
54-
toReturn = toReturn.andThen(new CheckContentTypeHeader());
58+
toReturn = toReturn.andThen(new CheckContentTypeHeader(SKIP_CHECKS_ON));
5559
}
5660

5761
boolean checkOrigin = config.getBool("network", "check_origin_header").orElse(true);
5862
Optional<List<String>> allowedOrigins = config.getAll("network", "allowed_origins");
5963

6064
if (checkOrigin || allowedOrigins.isPresent()) {
61-
toReturn = toReturn.andThen(new CheckOriginHeader(allowedOrigins.orElse(ImmutableList.of())));
65+
toReturn = toReturn.andThen(new CheckOriginHeader(allowedOrigins.orElse(ImmutableList.of()), SKIP_CHECKS_ON));
6266
}
6367

6468
return toReturn;

java/server/src/org/openqa/selenium/grid/web/CheckContentTypeHeader.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
package org.openqa.selenium.grid.web;
1919

2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.collect.ImmutableSet;
2122
import com.google.common.net.MediaType;
2223
import org.openqa.selenium.internal.Require;
2324
import org.openqa.selenium.remote.http.Contents;
2425
import org.openqa.selenium.remote.http.Filter;
2526
import org.openqa.selenium.remote.http.HttpHandler;
2627
import org.openqa.selenium.remote.http.HttpResponse;
2728

29+
import java.util.Set;
30+
2831
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
2932

3033
public class CheckContentTypeHeader implements Filter {
@@ -36,11 +39,21 @@ public class CheckContentTypeHeader implements Filter {
3639
"message", "Content-Type header is missing",
3740
"stacktrace", ""))));
3841

42+
private final Set<String> skipChecksOn;
43+
44+
public CheckContentTypeHeader(Set<String> skipChecksOn) {
45+
this.skipChecksOn = ImmutableSet.copyOf(Require.nonNull("URLs where checks are skipped", skipChecksOn));
46+
}
47+
3948
@Override
4049
public HttpHandler apply(HttpHandler httpHandler) {
4150
Require.nonNull("Next handler", httpHandler);
4251

4352
return req -> {
53+
if (skipChecksOn.contains(req.getUri())) {
54+
return httpHandler.execute(req);
55+
}
56+
4457
String type = req.getHeader("Content-Type");
4558
if (type == null) {
4659
return NO_HEADER;

java/server/src/org/openqa/selenium/grid/web/CheckOriginHeader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,23 @@
3434
public class CheckOriginHeader implements Filter {
3535

3636
private final Set<String> allowedHosts;
37+
private final Set<String> skipChecksOn;
3738

38-
public CheckOriginHeader(Collection<String> allowedOriginHosts) {
39+
public CheckOriginHeader(Collection<String> allowedOriginHosts, Set<String> skipChecksOn) {
3940
Require.nonNull("Allowed origins list", allowedOriginHosts);
4041
allowedHosts = ImmutableSet.copyOf(allowedOriginHosts);
42+
this.skipChecksOn = ImmutableSet.copyOf(Require.nonNull("URLs where checks are skipped", skipChecksOn));
4143
}
4244

4345
@Override
4446
public HttpHandler apply(HttpHandler httpHandler) {
4547
Require.nonNull("Next handler", httpHandler);
4648

4749
return req -> {
50+
if (skipChecksOn.contains(req.getUri())) {
51+
return httpHandler.execute(req);
52+
}
53+
4854
String origin = req.getHeader("Origin");
4955
if (origin != null && !allowedHosts.contains(origin)) {
5056
return new HttpResponse()

java/server/src/org/openqa/selenium/grid/web/EnsureSpecCompliantHeaders.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
import org.openqa.selenium.remote.http.HttpHandler;
2323

2424
import java.util.Collection;
25+
import java.util.Set;
2526

2627
public class EnsureSpecCompliantHeaders implements Filter {
2728

2829
private final Filter filter;
2930

30-
public EnsureSpecCompliantHeaders(Collection<String> allowedOriginHosts) {
31+
public EnsureSpecCompliantHeaders(Collection<String> allowedOriginHosts, Set<String> skipChecksOn) {
3132
Require.nonNull("Allowed origins list", allowedOriginHosts);
33+
Require.nonNull("URLs to skip checks on", skipChecksOn);
3234

33-
filter = new CheckOriginHeader(allowedOriginHosts).andThen(new CheckContentTypeHeader());
35+
filter = new CheckOriginHeader(allowedOriginHosts, skipChecksOn).andThen(new CheckContentTypeHeader(skipChecksOn));
3436
}
3537

3638
@Override

java/server/test/org/openqa/selenium/grid/router/NewSessionCreationTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.ImmutableMap;
22+
import com.google.common.collect.ImmutableSet;
2223
import org.junit.Before;
2324
import org.junit.Test;
2425
import org.openqa.selenium.WebDriverInfo;
@@ -81,7 +82,8 @@ public void ensureJsCannotCreateANewSession() throws URISyntaxException {
8182

8283
SessionMap sessions = new LocalSessionMap(tracer, events);
8384
Distributor distributor = new LocalDistributor(tracer, events, clientFactory, sessions, null);
84-
Routable router = new Router(tracer, clientFactory, sessions, distributor).with(new EnsureSpecCompliantHeaders(ImmutableList.of()));
85+
Routable router = new Router(tracer, clientFactory, sessions, distributor)
86+
.with(new EnsureSpecCompliantHeaders(ImmutableList.of(), ImmutableSet.of()));
8587

8688
Server<?> server = new NettyServer(
8789
new BaseServerOptions(new MapConfig(ImmutableMap.of())),

java/server/test/org/openqa/selenium/grid/web/EnsureSpecCompliantHeadersTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.grid.web;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableSet;
2122
import org.junit.Test;
2223
import org.openqa.selenium.remote.http.Contents;
2324
import org.openqa.selenium.remote.http.HttpHandler;
@@ -37,7 +38,7 @@ public class EnsureSpecCompliantHeadersTest {
3738

3839
@Test
3940
public void shouldBlockRequestsWithNoContentType() {
40-
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of())
41+
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of(), ImmutableSet.of())
4142
.apply(alwaysOk)
4243
.execute(new HttpRequest(POST, "/session"));
4344

@@ -46,7 +47,7 @@ public void shouldBlockRequestsWithNoContentType() {
4647

4748
@Test
4849
public void requestsWithAnOriginHeaderShouldBeBlocked() {
49-
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of())
50+
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of(), ImmutableSet.of())
5051
.apply(alwaysOk)
5152
.execute(
5253
new HttpRequest(POST, "/session")
@@ -58,7 +59,7 @@ public void requestsWithAnOriginHeaderShouldBeBlocked() {
5859

5960
@Test
6061
public void requestsWithAnAllowedOriginHeaderShouldBeAllowed() {
61-
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of("example.com"))
62+
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of("example.com"), ImmutableSet.of())
6263
.apply(alwaysOk)
6364
.execute(
6465
new HttpRequest(POST, "/session")
@@ -71,7 +72,7 @@ public void requestsWithAnAllowedOriginHeaderShouldBeAllowed() {
7172

7273
@Test
7374
public void shouldAllowRequestsWithNoOriginHeader() {
74-
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of())
75+
HttpResponse res = new EnsureSpecCompliantHeaders(ImmutableList.of(), ImmutableSet.of())
7576
.apply(alwaysOk)
7677
.execute(
7778
new HttpRequest(POST, "/session")

0 commit comments

Comments
 (0)