Skip to content

ServletServerHttpRequest.getRemoteAddress() may perform DNS lookup #35589

Closed

Description

@slovdahl

The ServletServerHttpRequest#getRemoteAddress method may perform a DNS lookup in certain scenarios (and same goes for its reactive counterpart).

If the underlying jakarta.servlet.ServletRequest#getRemoteHost implementation does a DNS lookup (as it is allowed, but not required to do), then calling ServletServerHttpRequest#getRemoteAddress will also do a DNS lookup. ServletServerHttpRequest#getRemoteAddress is for example called by Spring's ForwardedHeaderFilter.

As the ServletServerHttpRequest#getRemoteAddress return type is InetSocketAddress it feels like doing DNS resolution is quite pointless in general.

I have been trying to extract a minimal reproducer but I'm having some trouble getting it reproduced in a minimal setting. It might involve WebSocket via JSR 356 using Atmosphere and Jetty as the servlet container.

Similar to #28280.

Activity

  1. slovdahl commented on Oct 9, 2025

    @slovdahl
    Author

    Quoting #28280 (comment)

    The AbstractStandardUpgradeStrategy still tries to get InetSocketAddresses during upgrade:

    	@Override
      public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
      		@Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions,
      		@Nullable Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs)
      		throws HandshakeFailureException {
    
      	HttpHeaders headers = request.getHeaders();
      	InetSocketAddress localAddr = null;
      	try {
      		localAddr = request.getLocalAddress();
      	}
      	catch (Exception ex) {
      		// Ignore
      	}
      	InetSocketAddress remoteAddr = null;
      	try {
      		remoteAddr = request.getRemoteAddress();
      	}
      	catch (Exception ex) {
      		// Ignore
      	}
    

    and ServerHttpRequest uses constructor of InetSocketAddress, which performs DNS lookup:

    	@Override
      public InetSocketAddress getLocalAddress() {
      	return new InetSocketAddress(this.servletRequest.getLocalAddr(), this.servletRequest.getLocalPort());
      }
    
      @Override
      public InetSocketAddress getRemoteAddress() {
      	return new InetSocketAddress(this.servletRequest.getRemoteHost(), this.servletRequest.getRemotePort());
      }
    

    (checked on spring 5.3.20 and java 17)

  2. added
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    type: backportAn issue that is a backport of another issue to a maintenance branch
    on Oct 9, 2025
  3. added this to the 6.2.12 milestone on Oct 9, 2025
  4. added and removed
    type: backportAn issue that is a backport of another issue to a maintenance branch
    on Oct 9, 2025
  5. self-assigned this
    on Oct 10, 2025
  6. added a commit that references this issue on Oct 10, 2025
    2c83144
  7. github-actions commented on Oct 10, 2025

    @github-actions
    Contributor

    Fixed via 2c83144

  8. slovdahl commented on Oct 10, 2025

    @slovdahl
    Author

    Thanks a lot @rstoyanchev for fixing this so quickly, much appreciated!

  9. rstoyanchev commented on Oct 15, 2025

    @rstoyanchev
    Contributor

    I've made an an adjustment to fall back remoteHost if the remoteAddress is null for any reason. We've run into internal test failures, and although I'm not sure if that can happen at runtime, it doesn't hurt to have a fallback since otherwise the null address results an IllegalArgumentException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions