Skip to content

Commit 773ac50

Browse files
authored
add CORS Access Control for Private Networks (#12070)
Motivation: Some browsers started to create warnings when requesting resources from a server on a private network. In the future these requests will quite likely be blocked. See: [private-network-access-preflight](https://developer.chrome.com/blog/private-network-access-preflight/) Modification: Added HttpHeaderNames for `Access-Control-Request-Private-Network` and `Access-Control-Allow-Private-Network` Modified the `CorsHandler` to act on the access control request Result: Added CORS Access Control for Private Networks Signed-off-by: Lukas Prettenthaler <rdx@wsn.at>
1 parent 77ed043 commit 773ac50

5 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderNames.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public final class HttpHeaderNames {
6969
*/
7070
public static final AsciiString ACCESS_CONTROL_ALLOW_ORIGIN =
7171
AsciiString.cached("access-control-allow-origin");
72+
/**
73+
* {@code "access-control-allow-origin"}
74+
*/
75+
public static final AsciiString ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK =
76+
AsciiString.cached("access-control-allow-private-network");
7277
/**
7378
* {@code "access-control-expose-headers"}
7479
*/
@@ -88,6 +93,11 @@ public final class HttpHeaderNames {
8893
*/
8994
public static final AsciiString ACCESS_CONTROL_REQUEST_METHOD =
9095
AsciiString.cached("access-control-request-method");
96+
/**
97+
* {@code "access-control-request-private-network"}
98+
*/
99+
public static final AsciiString ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK =
100+
AsciiString.cached("access-control-request-private-network");
91101
/**
92102
* {@code "age"}
93103
*/

‎codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsConfig.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public final class CorsConfig {
4545
private final boolean allowNullOrigin;
4646
private final Map<CharSequence, Callable<?>> preflightHeaders;
4747
private final boolean shortCircuit;
48+
private final boolean allowPrivateNetwork;
4849

4950
CorsConfig(final CorsConfigBuilder builder) {
5051
origins = new LinkedHashSet<String>(builder.origins);
@@ -58,6 +59,7 @@ public final class CorsConfig {
5859
allowNullOrigin = builder.allowNullOrigin;
5960
preflightHeaders = builder.preflightHeaders;
6061
shortCircuit = builder.shortCircuit;
62+
allowPrivateNetwork = builder.allowPrivateNetwork;
6163
}
6264

6365
/**
@@ -109,6 +111,20 @@ public boolean isNullOriginAllowed() {
109111
return allowNullOrigin;
110112
}
111113

114+
/**
115+
* Web browsers may set the 'Access-Control-Request-Private-Network' request header if a resource is loaded
116+
* from a local network.
117+
* By default direct access to private network endpoints from public websites is not allowed.
118+
*
119+
* If isPrivateNetworkAllowed is true the server will response with the CORS response header
120+
* 'Access-Control-Request-Private-Network'.
121+
*
122+
* @return {@code true} if private network access should be allowed.
123+
*/
124+
public boolean isPrivateNetworkAllowed() {
125+
return allowPrivateNetwork;
126+
}
127+
112128
/**
113129
* Returns a set of headers to be exposed to calling clients.
114130
*
@@ -253,7 +269,8 @@ public String toString() {
253269
", maxAge=" + maxAge +
254270
", allowedRequestMethods=" + allowedRequestMethods +
255271
", allowedRequestHeaders=" + allowedRequestHeaders +
256-
", preflightHeaders=" + preflightHeaders + ']';
272+
", preflightHeaders=" + preflightHeaders +
273+
", isPrivateNetworkAllowed=" + allowPrivateNetwork + ']';
257274
}
258275

259276
/**

‎codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsConfigBuilder.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static CorsConfigBuilder forOrigins(final String... origins) {
7777
final Map<CharSequence, Callable<?>> preflightHeaders = new HashMap<CharSequence, Callable<?>>();
7878
private boolean noPreflightHeaders;
7979
boolean shortCircuit;
80+
boolean allowPrivateNetwork;
8081

8182
/**
8283
* Creates a new Builder instance with the origin passed in.
@@ -352,6 +353,19 @@ public CorsConfigBuilder shortCircuit() {
352353
return this;
353354
}
354355

356+
/**
357+
* Web browsers may set the 'Access-Control-Request-Private-Network' request header if a resource is loaded
358+
* from a local network.
359+
* By default direct access to private network endpoints from public websites is not allowed.
360+
* Calling this method will set the CORS 'Access-Control-Request-Private-Network' response header to true.
361+
*
362+
* @return {@link CorsConfigBuilder} to support method chaining.
363+
*/
364+
public CorsConfigBuilder allowPrivateNetwork() {
365+
allowPrivateNetwork = true;
366+
return this;
367+
}
368+
355369
/**
356370
* Builds a {@link CorsConfig} with settings specified by previous method calls.
357371
*

‎codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public CorsHandler(final CorsConfig config) {
6969
* config matches a certain origin, the first in the List will be used.
7070
*
7171
* @param configList List of {@link CorsConfig}
72-
* @param isShortCircuit Same as {@link CorsConfig#shortCircuit} but applicable to all supplied configs.
72+
* @param isShortCircuit Same as {@link CorsConfig#isShortCircuit} but applicable to all supplied configs.
7373
*/
7474
public CorsHandler(final List<CorsConfig> configList, boolean isShortCircuit) {
7575
checkNonEmpty(configList, "configList");
@@ -103,6 +103,7 @@ private void handlePreflight(final ChannelHandlerContext ctx, final HttpRequest
103103
setAllowCredentials(response);
104104
setMaxAge(response);
105105
setPreflightHeaders(response);
106+
setAllowPrivateNetwork(response);
106107
}
107108
if (!response.headers().contains(HttpHeaderNames.CONTENT_LENGTH)) {
108109
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO);
@@ -214,6 +215,16 @@ private void setMaxAge(final HttpResponse response) {
214215
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, config.maxAge());
215216
}
216217

218+
private void setAllowPrivateNetwork(final HttpResponse response) {
219+
if (request.headers().contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK)) {
220+
if (config.isPrivateNetworkAllowed()) {
221+
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK, "true");
222+
} else {
223+
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK, "false");
224+
}
225+
}
226+
}
227+
217228
@Override
218229
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
219230
throws Exception {

‎codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,32 @@ public void specificConfigPrecedenceOverGeneric() {
482482
assertThat(host2Response.headers().getAsString(ACCESS_CONTROL_MAX_AGE), equalTo("1800"));
483483
}
484484

485+
@Test
486+
public void simpleRequestAllowPrivateNetwork() {
487+
final CorsConfig config = forOrigin("http://localhost:8888").allowPrivateNetwork().build();
488+
final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
489+
final FullHttpRequest request = optionsRequest("http://localhost:8888", "", null);
490+
request.headers().set(ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK, "true");
491+
assertThat(channel.writeInbound(request), is(false));
492+
final HttpResponse response = channel.readOutbound();
493+
494+
assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK), equalTo("true"));
495+
assertThat(ReferenceCountUtil.release(response), is(true));
496+
}
497+
498+
@Test
499+
public void simpleRequestDoNotAllowPrivateNetwork() {
500+
final CorsConfig config = forOrigin("http://localhost:8888").build();
501+
final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
502+
final FullHttpRequest request = optionsRequest("http://localhost:8888", "", null);
503+
request.headers().set(ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK, "true");
504+
assertThat(channel.writeInbound(request), is(false));
505+
final HttpResponse response = channel.readOutbound();
506+
507+
assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK), equalTo("false"));
508+
assertThat(ReferenceCountUtil.release(response), is(true));
509+
}
510+
485511
private static HttpResponse simpleRequest(final CorsConfig config, final String origin) {
486512
return simpleRequest(config, origin, null);
487513
}

0 commit comments

Comments
 (0)