KTor server - 400 error with azure app service

158 Views Asked by At

I am running ktor server with netty. We had EmbeddedServer() earlier and we moved to EngineMain() to accommodate yaml(conf) config file. It looks like we are getting 400 Bad Request after the changes. Does KTor server validates the headers by default to send 400?

ktorVersion=2.3.0

The application does not have RequestValidation{} nor StatusPages modules, not sure if adding them will help?

OK, added the statuspage, but hitting it for any error. Strangly this happens with Azure App Service, but when run locally the Ktor service responds and works as expected.

Request:

GET / HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Host: xxx-yyyyyy-zzzzzzzzzz-aaaaa.azurewebsites.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US
Max-Forwards: 10
DNT: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
X-ARR-LOG-ID: c0e417c3-aaaa-bbbb-cccc-ddddcd626ef9
CLIENT-IP: x.x.x.x:45628
X-Client-IP: x.x.x.x
DISGUISED-HOST: xxx-yyyyyy-zzzzzzzzzz-aaaaa.azurewebsites.net
X-SITE-DEPLOYMENT-ID: xxx-yyyyyy-zzzzzzzzzz-aaaaa-aaa__f6ad
WAS-DEFAULT-HOSTNAME: xxx-yyyyyy-zzzzzzzzzz-aaaaa.azurewebsites.net
X-Forwarded-Proto: https
X-AppService-Proto: https
X-ARR-SSL: 2048|256|CN=Microsoft Azure TLS Issuing CA 02, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US
X-Forwarded-TlsVersion: 1.2
X-Forwarded-For: x.x.x.x:45628
X-Original-URL: /
X-WAWS-Unencoded-URL: /
X-Client-Port: 45628

Response

HTTP/1.1 400 Bad Request
Content-Length: 0
Connection: close
1

There are 1 best solutions below

0
On

OK, just for reference, the issue was that the app service was appending large header to the request and the Ktor server was responding with 400. Updating application.conf with max header/request length solved the issue.

deployment {
    maxInitialLineLength = 10240
    maxHeaderSize = 8192
    maxChunkSize = 42
}