Skip to content

Conversation

@asloobq
Copy link
Contributor

@asloobq asloobq commented Feb 27, 2025

When the value of X-Forward-Host is invalid, Vertx throws a NPE when trying to get the remote address from the request context. The NPE results in a Http Status code 500. To avoid this and return an appropriate 4xx error, handling this NPE gracefully and continuing request processing.

Exceptions seen in logs as

2025-02-25 13:46:55.859	
	at com.uid2.shared.vertx.RequestCapturingHandler.handle(RequestCapturingHandler.java:31)
	2025-02-25 13:46:55.859	
	at com.uid2.shared.vertx.RequestCapturingHandler.handle(RequestCapturingHandler.java:65)
	2025-02-25 13:46:55.859	
	at io.vertx.ext.web.impl.HttpServerRequestWrapper.remoteAddress(HttpServerRequestWrapper.java:154)
	2025-02-25 13:46:55.859	
	at io.vertx.ext.web.impl.ForwardedParser.remoteAddress(ForwardedParser.java:102)
	2025-02-25 13:46:55.859	
	at io.vertx.ext.web.impl.ForwardedParser.calculate(ForwardedParser.java:122)
	2025-02-25 13:46:55.859	
	at io.vertx.ext.web.impl.ForwardedParser.calculateXForward(ForwardedParser.java:184)
	2025-02-25 13:46:55.859	
	at io.vertx.ext.web.impl.ForwardedParser.setHostAndPort(ForwardedParser.java:199)
	2025-02-25 13:46:55.859	
21:46:55.856 [vert.x-eventloop-thread-17] ERROR c.u.o.vertx.GenericFailureHandler - URL: [/static/js/uid2-esp-0.0.1a.js] - Error response code: [500] - Error: java.lang.NullPointerException: Cannot invoke "io.vertx.core.net.HostAndPort.host()" because "authority" is null

try {
SocketAddress remoteAddress = context.request().remoteAddress();
remoteClient = getClientAddress(remoteAddress);
} catch (NullPointerException ex) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching a NullPointerException (NPE) directly is generally not ideal in Java. Instead, it's better to prevent the NPE by checking for null conditions before access

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem isn't that the return value is null. The NPE is thrown from within the vertx library.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore above, It is actually verx throw the NPE while passing the x-forward header and we have no control over it.

@asloobq asloobq merged commit 0699fa0 into main Mar 3, 2025
3 checks passed
@asloobq asloobq deleted the aaq-UID2-5016-handle-x-forwarded-header-gracefully branch March 3, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants