Hacker News new | ask | show | jobs
by Izkata 52 days ago
> Slack puts {"ok": false} inside a 200.

I found out due to a live bug that Apache strips out the response body for 5xx HTTP codes for security, and it's not configurable, so you can't use any of those if you want to return the error message to the user. Don't know what else might do the same, so nowadays I only use HTTP error codes for protocol-level issues, and application-level issues all go into a 200 OK with JSON just like that.

4 comments

Do you mean Apache APISIX, https://github.com/apache/apisix/issues/13501? That sounds like a simple bug which existed for a short time and has been fixed. For Apache HTTP Server (what people mean by “Apache” in such a context), what you describe is thoroughly unbelievable.
Sorry, httpd. It was so weird and I couldn't find it in the documentation (and a different server we used on dev didn't do it, which was how the bug made it live) that I pulled down the official source code and found where it was happening so I could explain it to my team.

I'm pretty sure it was all 5xx but can't remember for certain. This was back in 2019, with a much older project, and I'm pretty sure that part of the code was the same as of 2019.

I would be interested to hear more, because even with you saying this I still find it genuinely unbelievable.
It really shouldn't be, this error page should be familiar looking: https://i.sstatic.net/FRlUG.png

It comes from ap_send_error_response(), near the bottom starting at https://github.com/apache/httpd/blob/b2dc966be4178ded1499d5d...

Scrolling up from there a bit you can see some of the handling for ErrorDocument, which is configurable ( https://httpd.apache.org/docs/current/custom-error.html ) but also loses the original response body.

ap_send_error_response() is only called from the last line of ap_die_r() ( https://github.com/apache/httpd/blob/b2dc966be4178ded1499d5d... ), as the fallback for errors when none of the other conditions pass. There's some other handling for redirects and custom ErrorDocument, but again, if it reaches those it's using the contents of "custom_response" instead of the original response body.

ap_die_r() is called unconditionally for completed requests ( https://github.com/apache/httpd/blob/b2dc966be4178ded1499d5d... ), which is why api_die_r() starts with an early return for an OK or DONE response. Its wrapper ap_die() is also used in plenty of other locations as a general error handler.

It's been a while so I had to go through it again, the code isn't exactly what I'm half remembering, but I do remember the "assbackwards" lines in ap_send_error_response() ( https://github.com/apache/httpd/blob/b2dc966be4178ded1499d5d... and https://github.com/apache/httpd/blob/b2dc966be4178ded1499d5d... ). If the particular check I think I remember did exist (explicit status codes in an "if", similar to your earlier link), it was probably in the even older version we were using. The early return I'm pretty sure wasn't there (blame puts it at 12 years old), so all responses would have gone through more of the error handling code back then.

Edit: Replacing "trunk" branch with most recent commit so line numbers continue to work in the future

4xx errors are for when the client did something wrong and needs to know about it. 5xx errors are for when the server is having problems and the only people who can do anything about it are the operators, who should be looking for errors in the logs rather than the HTTP client.

Truly "application level" issues are therefore correct as 200, although some application communication issues lead to 4xx errors that should be handled as such. But the case of an application trying to communicate via 5xx errors should never have been the case anyway.

I worked in environment where every HTTP 500 error was replaced by some middle ware by user "friendly" error page, and that included REST calls between services. That HTML also included one huge header image(as base64), so my logs were flooded by that junk response.
We run into that a lot using Mulesoft. 3 layer APIs, and each layer will discard error messages returned by the underlying layers unless we do tedious custom error handling. Makes it impossible for the teams using our integrations to know what they're doing wrong.