Skip to content

AwsHttpServletRequest.getLocalAddr() returns null, so Spring's ForwardedHeaderFilter answers every request 400 #1733

Backlog

Description

@chaserb

Summary

AwsHttpServletRequest.getLocalAddr() always returns null. Spring Framework 7's ForwardedHeaderFilter
(enabled by Spring Boot's server.forward-headers-strategy=framework) builds an InetSocketAddress from the local
address on every request, gets IllegalArgumentException: hostname can't be null, catches it, and answers 400 with
an empty body
. With the filter enabled, every request through the container fails. The cause is logged only at DEBUG.

The Servlet API describes getLocalAddr() as "the IP address of the interface on which the request was received"
and doesn't provide for null. getLocalName() already returns a placeholder (lambda.amazonaws.com), and
getLocalPort() returns 0.

Possibly related: #816 reported the same hostname can't be null message, but from Spring Cloud Gateway and
single-value headers; this one is the local address.

Versions

  • aws-serverless-java-container-springboot4 / -core 3.0.5
  • Spring Boot 4.1.2, Spring Framework (spring-web) 7.0.10
  • Java 25, Lambda Function URL (payload format 2.0, SpringBootProxyHandlerBuilder.defaultHttpApiV2Proxy())

Reproduce

  1. A Spring Boot 4 web app on aws-serverless-java-container-springboot4, with
    server.forward-headers-strategy: framework in application.yml.
  2. Invoke the handler with any HTTP API v2 / Function URL event, e.g. GET /hello with an ordinary host header.
  3. The response is {"statusCode":400,"body":"",...}. The controller is never reached.

With logging.level.org.springframework.web.filter=DEBUG:

Failed to apply forwarded headers to HTTP GET "/hello"
java.lang.IllegalArgumentException: hostname can't be null
    at java.base/java.net.InetSocketAddress.checkHost(InetSocketAddress.java:159)
    at java.base/java.net.InetSocketAddress.<init>(InetSocketAddress.java:219)
    at org.springframework.http.server.ServletServerHttpRequest.getLocalAddress(ServletServerHttpRequest.java:207)
    at org.springframework.web.filter.ForwardedHeaderFilter$ForwardedHeaderExtractingRequest.<init>(ForwardedHeaderFilter.java:332)
    at org.springframework.web.filter.ForwardedHeaderFilter.doFilterInternal(ForwardedHeaderFilter.java:203)

Expected

getLocalAddr() returns a non-null IP literal, for example 127.0.0.1 or 0.0.0.0, the same way getLocalName()
returns a placeholder. It should be a literal, because new InetSocketAddress(String, int) resolves a hostname by DNS.

Workaround

Register ForwardedHeaderFilter yourself (Boot's registration steps aside through @ConditionalOnMissingFilterBean)
as a subclass that wraps the request when getLocalAddr() is null:

class LocalAddressDefaulting extends ForwardedHeaderFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
        super.doFilterInternal(request.getLocalAddr() != null ? request : new HttpServletRequestWrapper(request) {
            @Override public String getLocalAddr() { return "0.0.0.0"; }
        }, response, chain);
    }
}
// FilterRegistrationBean: order Ordered.HIGHEST_PRECEDENCE, dispatcher types REQUEST, ASYNC, ERROR

I'm happy to send a PR if a placeholder value is acceptable.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions