Skip to content

Commit e5188d3

Browse files
committed
Context: clean up (useProxy) methods now we have setTrustProxy
Add router.domain operator now we have setTrustProxy fix #1292 fix #1644 fix #1645
1 parent 9a0d0f5 commit e5188d3

18 files changed

Lines changed: 106 additions & 235 deletions

File tree

docs/asciidoc/handlers.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ include::handlers/csrf.adoc[]
1010

1111
include::handlers/head.adoc[]
1212

13-
include::handlers/proxy-peer-address.adoc[]
14-
1513
include::handlers/rate-limit.adoc[]
1614

1715
include::handlers/ssl.adoc[]

docs/asciidoc/handlers/access-log.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ Extra request or response headers can be appended at the end using the available
5757
[TIP]
5858
====
5959
If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
60-
please consider to add the <<handlers-proxypeeraddresshandler, ProxyPeerAddressHandler>> to your pipeline.
60+
please consider to use <<router-trust-proxy, trust proxy>> option.
6161
====

docs/asciidoc/handlers/proxy-peer-address.adoc

Lines changed: 0 additions & 54 deletions
This file was deleted.

docs/asciidoc/handlers/ssl.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ import io.jooby.SSHandler
4242
<1> Install SSLHandler
4343

4444
The SSL Handler recreates the HTTPs URL version using the `Host` header, if you are behind a proxy
45-
you will need to use the `X-Forwarded-Host` header. To do that install the
46-
<<handlers-proxypeeraddresshandler, ProxyPeerAddressHandler>> into your pipeline.
45+
you will need to use the `X-Forwarded-Host` header. To do that set the <<router-trust-proxy, trust proxy>> option.
4746

4847
Optionally, you can specify the host to use:
4948

docs/asciidoc/routing.adoc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,61 @@ one or more routes under a common path pattern.
11161116
<3> `GET /api/user`
11171117
<4> `POST /api/user`
11181118

1119+
=== Trust Proxy
1120+
1121+
The javadoc:Router[setTrustProxy, boolean] option enables parsing of `X-Forwarded-*` headers.
1122+
1123+
.Usage
1124+
[source, java, role = "primary"]
1125+
----
1126+
import io.jooby.Jooby;
1127+
import io.jooby.ProxyPeerAddressHandler;
1128+
...
1129+
{
1130+
1131+
setTrustProxy(true) <1>
1132+
1133+
get("/", ctx -> {
1134+
String remoteAddress = ctx.getRemoteAddress(); <2>
1135+
String scheme = ctx.getScheme(); <3>
1136+
String host = ctx.getHost(); <4>
1137+
int port = ctx.getPort(); <5>
1138+
...
1139+
});
1140+
}
1141+
----
1142+
1143+
.Kotlin
1144+
[source, kotlin, role = "secondary"]
1145+
----
1146+
import io.jooby.Jooby
1147+
import io.jooby.ProxyPeerAddressHandler
1148+
...
1149+
{
1150+
trustProxy = true <1>
1151+
1152+
get("/") {
1153+
val remoteAddress = ctx.remoteAddress <2>
1154+
val scheme = ctx.scheme <3>
1155+
val host = ctx.host <4>
1156+
val port = ctx.port <5>
1157+
...
1158+
}
1159+
}
1160+
----
1161+
1162+
<1> Set trust proxy
1163+
<2> Set `remote address` from `X-Forwarded-For`
1164+
<3> Set `scheme` from `X-Forwarded-Proto`
1165+
<4> Set `host` from `X-Forwarded-Host`
1166+
<5> Set `port` from `X-Forwarded-Host` or `X-Forwarded-Port`
1167+
1168+
[IMPORTANT]
1169+
====
1170+
This should only be installed behind a reverse proxy that has been configured to send the
1171+
`X-Forwarded-*` header, otherwise a remote user can spoof their address by sending a header with
1172+
bogus values.
1173+
====
11191174

11201175
=== Composing routes
11211176

jooby/src/main/java/io/jooby/AccessLogHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* </p>
3131
*
3232
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
33-
* please consider to add {@link ProxyPeerAddressHandler} to your pipeline.
33+
* please consider to set {@link Router#setTrustProxy(boolean)} option.
3434
*
3535
* <h2>usage</h2>
3636
*

jooby/src/main/java/io/jooby/Context.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public interface Context extends Registry {
453453
* Recreates full/entire request url using the <code>Host</code> header.
454454
*
455455
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
456-
* please consider to add {@link ProxyPeerAddressHandler} to your pipeline.
456+
* please consider to set {@link Router#setTrustProxy(boolean)} option.
457457
*
458458
* @return Full/entire request url using the <code>Host</code> header.
459459
*/
@@ -463,43 +463,18 @@ public interface Context extends Registry {
463463
* Recreates full/entire request url using the <code>Host</code> header.
464464
*
465465
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
466-
* please consider to add {@link ProxyPeerAddressHandler} to your pipeline.
466+
* please consider to set {@link Router#setTrustProxy(boolean)} option.
467467
*
468468
* @param path Path to use.
469469
* @return Full/entire request url using the <code>Host</code> header.
470470
*/
471471
@Nonnull String getRequestURL(@Nonnull String path);
472472

473-
/**
474-
* Recreates full/entire request url using the <code>X-Forwarded-Host</code> when present
475-
* or fallback to <code>Host</code> header when missing.
476-
*
477-
* @param useProxy True to trust/use the <code>X-Forwarded-Host</code>.
478-
* @return Full/entire request url using the <code>X-Forwarded-Host</code> when present
479-
* or fallback to <code>Host</code> header when missing.
480-
* @deprecated Use {@link ProxyPeerAddressHandler}.
481-
*/
482-
@Deprecated
483-
@Nonnull String getRequestURL(boolean useProxy);
484-
485-
/**
486-
* Recreates full/entire request url using the <code>X-Forwarded-Host</code> when present
487-
* or fallback to <code>Host</code> header when missing.
488-
*
489-
* @param path Path to use.
490-
* @param useProxy True to trust/use the <code>X-Forwarded-Host</code>.
491-
* @return Full/entire request url using the <code>X-Forwarded-Host</code> when present
492-
* or fallback to <code>Host</code> header when missing.
493-
* @deprecated Use {@link ProxyPeerAddressHandler}.
494-
*/
495-
@Deprecated
496-
@Nonnull String getRequestURL(@Nonnull String path, boolean useProxy);
497-
498473
/**
499474
* The IP address of the client or last proxy that sent the request.
500475
*
501476
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
502-
* please consider to add {@link ProxyPeerAddressHandler} to your pipeline.
477+
* please consider to set {@link Router#setTrustProxy(boolean)} option.
503478
*
504479
* @return The IP address of the client or last proxy that sent the request.
505480
*/
@@ -519,7 +494,7 @@ public interface Context extends Registry {
519494
* {@link #setHost(String)} method.
520495
*
521496
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
522-
* please consider to add {@link ProxyPeerAddressHandler} to your pipeline.
497+
* please consider to set {@link Router#setTrustProxy(boolean)} option.
523498
*
524499
* @return Return the host that this request was sent to, in general this will be the
525500
* value of the Host header, minus the port specifier.
@@ -541,25 +516,13 @@ public interface Context extends Registry {
541516
* value of the Host.
542517
*
543518
* If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
544-
* please consider to add {@link ProxyPeerAddressHandler} to your pipeline.
519+
* please consider to set {@link Router#setTrustProxy(boolean)} option.
545520
*
546521
* @return Return the host that this request was sent to, in general this will be the
547522
* value of the Host header.
548523
*/
549524
@Nonnull String getHostAndPort();
550525

551-
/**
552-
* Return the host and port that this request was sent to, in general this will be the
553-
* value of the Host or X-Forwarded-Host header.
554-
*
555-
* @param useProxy When true this method looks for host data in the X-Forwarded-Host header.
556-
* @return Return the host that this request was sent to, in general this will be the
557-
* value of the Host header.
558-
* @deprecated Use {@link ProxyPeerAddressHandler}.
559-
*/
560-
@Deprecated
561-
@Nonnull String getHostAndPort(boolean useProxy);
562-
563526
/**
564527
* Return the port that this request was sent to. In general this will be the value of the Host
565528
* header, minus the host name.

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Date;
3232
import java.util.List;
3333
import java.util.Map;
34+
import java.util.Optional;
3435

3536
/***
3637
* Like {@link Context} but with couple of default methods.
@@ -220,18 +221,10 @@ public interface DefaultContext extends Context {
220221
}
221222

222223
@Override default @Nonnull String getRequestURL() {
223-
return getRequestURL(false);
224+
return getRequestURL("");
224225
}
225226

226227
@Override default @Nonnull String getRequestURL(@Nonnull String path) {
227-
return getRequestURL(path, false);
228-
}
229-
230-
@Override default @Nonnull String getRequestURL(boolean useProxy) {
231-
return getRequestURL("", useProxy);
232-
}
233-
234-
@Override default @Nonnull String getRequestURL(@Nonnull String path, boolean useProxy) {
235228
String scheme = getScheme();
236229
String host = getHost();
237230
int port = getPort();
@@ -274,20 +267,17 @@ public interface DefaultContext extends Context {
274267
}
275268

276269
@Override default @Nullable String getHostAndPort() {
277-
return getHostAndPort(false);
278-
}
279-
280-
@Override default @Nullable String getHostAndPort(boolean useProxy) {
281-
return header(useProxy ? "X-Forwarded-Host" : "Host").toOptional()
282-
.map(value -> {
283-
int i = value.indexOf(',');
284-
String host = i > 0 ? value.substring(0, i).trim() : value;
285-
if (host.startsWith("[") && host.endsWith("]")) {
286-
return host.substring(1, host.length() - 1).trim();
287-
}
288-
return host;
289-
})
290-
.orElseGet(() -> getServerHost() + ":" + getServerPort());
270+
Optional<String> header = getRouter().isTrustProxy()
271+
? header("X-Forwarded-Host").toOptional()
272+
: Optional.empty();
273+
String value = header
274+
.orElseGet(() -> header("Host").value(getServerHost() + ":" + getServerPort()));
275+
int i = value.indexOf(',');
276+
String host = i > 0 ? value.substring(0, i).trim() : value;
277+
if (host.startsWith("[") && host.endsWith("]")) {
278+
return host.substring(1, host.length() - 1).trim();
279+
}
280+
return host;
291281
}
292282

293283
@Override default @Nonnull String getServerHost() {

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,14 @@ public ForwardingContext(@Nonnull Context context) {
254254
return ctx.getHostAndPort();
255255
}
256256

257-
@Nullable @Override public String getHostAndPort(boolean useProxy) {
258-
return ctx.getHostAndPort(useProxy);
259-
}
260-
261257
@Nonnull @Override public String getRequestURL() {
262258
return ctx.getRequestURL();
263259
}
264260

265-
@Nonnull @Override public String getRequestURL(boolean useProxy) {
266-
return ctx.getRequestURL(useProxy);
267-
}
268-
269261
@Nonnull @Override public String getRequestURL(@Nonnull String path) {
270262
return ctx.getRequestURL(path);
271263
}
272264

273-
@Nonnull @Override public String getRequestURL(@Nonnull String path, boolean useProxy) {
274-
return ctx.getRequestURL(path, useProxy);
275-
}
276-
277265
@Override @Nonnull public String getProtocol() {
278266
return ctx.getProtocol();
279267
}

jooby/src/main/java/io/jooby/ProxyPeerAddressHandler.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)