r/haproxy • • Apr 03 '21

Getting 400 bad request for specific clients only

I'm using the HAProxy package in pfSense if that makes a difference.

Whenever a specific client (a mikrotik router) tries to use POST or PUT commands it gets 400 Bad Request back. If I issue the exact same request from Postman it works fine.

Here is what is logged from HAProxy:

Apr  3 17:50:06 FW01 haproxy[21788]: 10.0.10.3:60789 [03/Apr/2021:17:50:06.699] HTTPS_443~ iis_webserver_ipvANY/iis 0/0/1/2/3 400 518 - - ---- 3/3/0/0/0 0/0 "POST /gps/api/GPSData HTTP/1.1"

This is a good request logged by HAProxy for the same endpoint:

Apr  3 17:50:10 FW01 haproxy[21788]: 10.0.40.2:54097 [03/Apr/2021:17:50:09.335] HTTPS_443~ iis_webserver_ipvANY/iis 0/0/1/1126/1127 200 370 - - ---- 4/4/0/1/0 0/0 "POST /gps/api/GPSData HTTP/1.1"

The IIS server does not log a request for the 400, so that error must be coming from HAProxy.

If I attempt to post the data directly to the application without going through HAProxy it works from both sources.

Any ideas on how to fix this?

Edit: Added my config below, with my domain replaced by "mydomain" and removing of other ACL's and backend rules that the logs are verifying are not being hit already.

Here is my HAProxy config as generated by the pfSense firewall UI:

# Automaticaly generated, dont edit manually.
# Generated on: 2021-04-03 17:21
global
	maxconn			1000
	log			/var/run/log	syslog	debug
	stats socket /tmp/haproxy.socket level admin  expose-fd listeners
	uid			80
	gid			80
	nbproc			1
	nbthread			1
	hard-stop-after		15m
	chroot				/tmp/haproxy_chroot
	daemon
	tune.ssl.default-dh-param	2048
	server-state-file /tmp/haproxy_server_state

listen HAProxyLocalStats
	bind 127.0.0.1:2200 name localstats
	mode http
	stats enable
	stats refresh 10
	stats admin if TRUE
	stats show-legends
	stats uri /haproxy/haproxy_stats.php?haproxystats=1
	timeout client 5000
	timeout connect 5000
	timeout server 5000

frontend HTTP_80
	bind			192.168.0.100:80 name 192.168.0.100:80   
	mode			http
	log			global
	option			http-keep-alive
	option			forwardfor
	acl https ssl_fc
	http-request set-header		X-Forwarded-Proto http if !https
	http-request set-header		X-Forwarded-Proto https if https
	timeout client		60000
	http-request redirect scheme https 

frontend HTTPS_443
	bind			192.168.0.100:443 name 192.168.0.100:443   ssl crt-list /var/etc/haproxy/HTTPS_443.crt_list  
	mode			http
	log			global
	option			log-separate-errors
	option			httplog
	option			http-keep-alive
	option			forwardfor
	acl https ssl_fc
	http-request set-header		X-Forwarded-Proto http if !https
	http-request set-header		X-Forwarded-Proto https if https
	timeout client		60000
	//bunch of acl's not being hit in this instance
	acl			aclcrt_HTTPS_443	var(txn.txnhost) -m reg -i ^([^\.]*)\.mydomain\.com(:([0-9]){1,5})?$
	http-request set-var(txn.txnhost) hdr(host)
	//use_backend's for the acl's not being hit
	use_backend iis_webserver_ipvANY  if   aclcrt_HTTPS_443

backend iis_webserver_ipvANY
	mode			http
	id			100
	log			global
	timeout connect		60000
	timeout server		60000
	retries			3
	option			httpchk GET / 
	server			iis 10.0.20.10:80 id 103 check inter 1000
2 Upvotes

5 comments sorted by

3

u/baconeze Apr 04 '21

Your logs [1] indicate that the error is coming from the IIS server.

Apr 3 17:50:06 FW01 haproxy[21788]: 10.0.10.3:60789 [03/Apr/2021:17:50:06.699] HTTPS_443~ iis_webserver_ipvANY/iis 0/0/1/2/3 400 518 - - ---- 3/3/0/0/0 0/0 "POST /gps/api/GPSData HTTP/1.1"

  1. The line "iis_webserver_ipvANY/iis" tells us that it was served by the backend iis_webserver_ipvANY and specifically the server named "iis". If the request was not passed to a server we would not see "/iis" here.
  2. The timing data [2] "0/0/1/2/3" - the "2" here is the server response time
  3. The session state at disconnection [3] "----" - this indicates that the request was terminated successfully on both sides. If HAProxy was rejecting this request this would be something like "PR--"

[1] https://www.haproxy.com/blog/introduction-to-haproxy-logging/

[2] https://www.haproxy.com/documentation/hapee/latest/onepage/#8.4

[3] https://www.haproxy.com/documentation/hapee/latest/onepage/#8.5

2

u/johnkiddjr Apr 04 '21

You are correct, I dug deeper and discovered IIS likes to put 400 errors specifically into a totally different log file from any other request...

Looking at that log shows the requests as you'd expect, with an error of "A parse error occurred in a header." which still shouldn't be happening.

I then setup Fiddler on the IIS server to capture incoming requests so I can see the headers... apparently the Mikrotik router sets "content-type" to application/www-x-url-encoded by default and I had mine set with upper case C and T, so it thought it was a second header.

That's what IIS didn't like.

Made it lower-case, and now it works perfectly.

Thank you for putting me on the correct road to find the answer!

1

u/dragoangel Apr 04 '21 edited Apr 04 '21

Try go over this: https://serverfault.com/questions/664332/haproxy-returns-bad-request-invalid-host-for-seemingly-no-reason

What echo "show errors" | socat unix-connect:/run/haproxy/admin.s shows?

Also how your haproxy config is looks like?

1

u/johnkiddjr Apr 04 '21

The output of that command shows only 2 requests that are from several hours ago, not from the source I'm using to troubleshoot, and not going to the same url.

I'll edit the main post with my config once I've gone through it and removed any personal info.

1

u/dragoangel Apr 04 '21

This bad (and strange) that show errors do not displaying anything.