Spring-cloud-gateway: How to increase HTTP Header Size, getting 413 error

Created on 28 Jun 2018  路  16Comments  路  Source: spring-cloud/spring-cloud-gateway

The layer that calls Spring Cloud Gateway API passes some SAML tokens that are more than the default header size on Netty, and we get a 413 or 'Request entity too large' error.

We tried increasing it by adding this -- http.netty.maxHeaderSize=52428800 -- to our properties file but it hasn't helped. We increased the header size on our own api as well which is on JBoss, and when we bypass SCG we are able to get responses directly.

We use Finchley.RC1 version for SCG and 2.0.2 for spring boot.

Please let us know if you have any insight on this issue. Thanks!

enhancement

Most helpful comment

Just as a little note, because I found this on my search how I can configure something like the maximum header size or the maximum initial line length, the configuration has moved to HttpRequestDecoder:

@Component
public class NettyConfiguration implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Value("${server.max-initial-line-length:65536}")
    private int maxInitialLingLength;

    public void customize(NettyReactiveWebServerFactory container) {
        container.addServerCustomizers(
            httpServer -> httpServer.httpRequestDecoder(
                httpRequestDecoderSpec -> httpRequestDecoderSpec.maxInitialLineLength(maxInitialLingLength)
            )
        );
    }

}

All 16 comments

I don't know what the proper way to do that. @violetagg? There is no 2.0.2 of gateway. Just 2.0.0 from Finchley.RELEASE.

Thanks @spencergibb @violetagg !!

We were able to solve the problem by creating a component that implemented:
WebServerFactoryCustomizer
Overrided customize(NettyReactiveWebServerFactory container) method
and performed the following customization:
container.addServerCustomizers(builder -> builder.maxHeaderSize([maxHeaderSizeVal]));

Thanks again!!

I want to offer a configuration option

Just realized this is a server option and if anything the enhancement for property based config should be in boot. Does that sound reasonable @smaldini?

@spencergibb Is there any way to control these option in config?

@tony-clarke-amdocs not that I have seen, I agree with @spencergibb's comment above that the property based config should live in Boot. Might be worth opening an enhancement in Boot.

Thanks @spencergibb @violetagg !!

We were able to solve the problem by creating a component that implemented:
WebServerFactoryCustomizer
Overrided customize(NettyReactiveWebServerFactory container) method
and performed the following customization:
container.addServerCustomizers(builder -> builder.maxHeaderSize([maxHeaderSizeVal]));

Thanks again!!

I would expect spring cloud gateway to respect the server.max-http-header-size setting. Like juhi-devops says, the following code does the trick:

@Component
public class CustomizeNetty implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Value("${server.max-http-header-size:65536}")
    private int maxHeaderSize;

    public void customize(NettyReactiveWebServerFactory container) {
        container.addServerCustomizers(builder -> builder.maxHeaderSize(maxHeaderSize));
    }

}

Just as a little note, because I found this on my search how I can configure something like the maximum header size or the maximum initial line length, the configuration has moved to HttpRequestDecoder:

@Component
public class NettyConfiguration implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Value("${server.max-initial-line-length:65536}")
    private int maxInitialLingLength;

    public void customize(NettyReactiveWebServerFactory container) {
        container.addServerCustomizers(
            httpServer -> httpServer.httpRequestDecoder(
                httpRequestDecoderSpec -> httpRequestDecoderSpec.maxInitialLineLength(maxInitialLingLength)
            )
        );
    }

}

@tobske Seems not working from Greenwish.SR2.

@Component
public class NettyConfiguration implements WebServerFactoryCustomizer {

  @Value("${server.max-initial-line-length:65536}")
  private int maxInitialLingLength;

  public void customize(NettyReactiveWebServerFactory container) {
      container.addServerCustomizers(
          httpServer -> httpServer.httpRequestDecoder(
                httpRequestDecoderSpec -> httpRequestDecoderSpec.maxHeaderSize(maxInitialLingLength)
          )
      );
  }

}

Not working with Greenwich.RELEASE. How do i overwrite the max-header-size?

Not working.
Stack trace (Cloud Gateway):
at io.netty.handler.codec.http.HttpObjectDecoder$HeaderParser.newException(HttpObjectDecoder.java:843) ~[netty-codec-http-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.http.HttpObjectDecoder$HeaderParser.process(HttpObjectDecoder.java:835) ~[netty-codec-http-4.1.43.Final.jar:4.1.43.Final] at io.netty.buffer.AbstractByteBuf.forEachByteAsc0(AbstractByteBuf.java:1350) ~[netty-buffer-4.1.43.Final.jar:4.1.43.Final] at io.netty.buffer.AbstractByteBuf.forEachByte(AbstractByteBuf.java:1330) ~[netty-buffer-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.http.HttpObjectDecoder$HeaderParser.parse(HttpObjectDecoder.java:807) ~[netty-codec-http-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.http.HttpObjectDecoder.readHeaders(HttpObjectDecoder.java:572) ~[netty-codec-http-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.http.HttpObjectDecoder.decode(HttpObjectDecoder.java:218) ~[netty-codec-http-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.http.HttpClientCodec$Decoder.decode(HttpClientCodec.java:202) ~[netty-codec-http-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:503) ~[netty-codec-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:442) ~[netty-codec-4.1.43.Final.jar:4.1.43.Final] at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:281) ~[netty-codec-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1422) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:931) ~[netty-transport-4.1.43.Final.jar:4.1.43.Final] at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:792) ~[netty-transport-native-epoll-4.1.43.Final-linux-x86_64.jar:4.1.43.Final] at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:502) ~[netty-transport-native-epoll-4.1.43.Final-linux-x86_64.jar:4.1.43.Final] at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:407) ~[netty-transport-native-epoll-4.1.43.Final-linux-x86_64.jar:4.1.43.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1050) ~[netty-common-4.1.43.Final.jar:4.1.43.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.43.Final.jar:4.1.43.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.43.Final.jar:4.1.43.Final] at java.base/java.lang.Thread.run(Thread.java:844) ~[na:na]

@Incarnation-p-lee add maxInitialLineLength(), how it works ,anyone can tell me?

@sqtds

this works from our code, from Greenwich.SR3.

@Component
public class WebServerConfiguration implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    // The default value is 8192 (8K) but may result in 413 when header is too lager.
    // Enlarge the header size to 16384 (16K) which is enough for most cases.
    private static final int MAX_HEADER_SIZE = 16384;

    public void customize(NettyReactiveWebServerFactory factory) {
        factory.addServerCustomizers(server ->
                server.httpRequestDecoder(decoder -> decoder.maxHeaderSize(MAX_HEADER_SIZE)));
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

re6exp picture re6exp  路  36Comments

zjengjie picture zjengjie  路  27Comments

vpavlyuk picture vpavlyuk  路  37Comments

re6exp picture re6exp  路  37Comments

spencergibb picture spencergibb  路  36Comments