iTranslated by AI
Compatibility Issues Between Rack and Go http.Client
I've been struggling with this for a while and would like to hear various opinions, so I'll briefly summarize the information.
The related PRs are below:
Background
I previously created the following repository for an in-house ISUCON:
Five years after creating this repository, it was decided that it would be published as a book based on this problem. While I'm certainly happy about this, when I created this repository, I was a third-year new graduate engineer with limited abilities, and the industry hadn't accumulated much know-how for creating ISUCON problems. Moreover, I was unsure if I could even create such a problem myself, so I developed it through trial and error. As such, the implementation is quite naive, and honestly, there are many points to criticize.
While I'd like to ask for understanding, as it was the result of my courageous challenge with my technical skills at the time, a problem that I will now explain has arisen. So, I will elaborate on that issue.
First, the reference implementation of private-isu does not use SPA and has a slightly older, orthodox web application structure like this:
POSTing a submission -> saving it to the DB -> redirecting to another page with HTTP status code 302
At the time, with limited Go knowledge, I found it interesting that Go's http.Client interprets redirects on its own and actually sends GET requests, and I created a benchmark that relies on this behavior. So, in fact, it only tests for redirection; I believe the benchmark would not fail even if it were rewritten to other redirect status codes (behavior unconfirmed).
It was discovered that this behavior causes the following problem:
To briefly explain the situation:
- Go's http.Client sends a GET request while retaining Content-Type information when redirecting after a POST request using
multipart/form-data. - Rack attempts to retrieve information when
Content-Type: multipart/form-datais sent, even for a GET request, but since Go's http.Client does not carry over information other than Content-Type, it judges the boundary to be empty and throws an exception.
The following two solutions are conceivable for this:
- Go's http.Client should not send
Content-Type: multipart/form-datavia GET.- This can be achieved by overriding
CheckRedirect.
- This can be achieved by overriding
- Rack should not validate
Content-Type: multipart/form-datafor GET requests.
It would have been great if Go and Rack recognized this issue and acted on it, but after opening the issue I mentioned earlier, neither officially seems to be moving towards a solution (and I'm hoping someone can help with this).
You might think that when implementing the benchmark, you can simply override CheckRedirect in Go. However, there's another troublesome aspect.
That is, the book introduces grafana/k6, which uses Go's http.Client. Currently, there's likely no way to override CheckRedirect as a feature in k6.
Furthermore, the reference implementation of private-isu originated as a Ruby implementation, and all other language implementations were ported from it. Therefore, while it might be acceptable if it doesn't work with other language implementations, it's not acceptable from the repository's perspective if it doesn't work with the Ruby implementation. The default startup is also the Ruby implementation, and not being able to run k6 with the Ruby implementation is difficult from the book's standpoint.
As for what can be done from the Ruby application side, if Rack 3 is used, it's designed to throw a Rack::Multipart::EmptyContentError exception. Theoretically, if we just rescue this exception and allow the original implementation to proceed, it should work. However, to make it behave the same as other language implementations, subsequent processing needs to be resumed, and I haven't figured out how to implement that. Also, the current Sinatra doesn't use Rack 3 yet. So, I think there are two possible approaches:
- Ignore k6 and upgrade Rack and Sinatra.
- This would make the content of the book no longer work.
- Wait for Sinatra to upgrade to Rack 3, then rewrite the code based on Rack 3.
- I haven't figured out how to implement this.
This issue was discovered during the book writing process, and we've currently postponed upgrading Rack and Sinatra. However, given the security implications, I believe it's time to resolve it. I'd appreciate any opinions you might have.
(Added: 2026/02/28)
Changes in Go 1.26 (already released, undocumented for some reason) mean that the Go-side patch is no longer needed.
Discussion
HTTP関連RFCでこの問題に関係しそうな箇所をいくつか挙げてみます。
(すべてをちゃんと読んだわけではないので、他にもこの問題に関係する記述や別のRFCがあるかもしれません)
RFC 9112 §6
RFC 9110 §9.3.1
RFC 9110 §8.6
例えばGETメソッドでリクエストにコンテンツが存在しない場合にUAはContent-Lengthを送信すべきでないとありますが、Content-Typeに関してはそのような記述は(ぱっと見)なさそうです。
RFC 9110 §8.2
とあるので、(Content-Typeのような)表現ヘッダはコンテンツが存在する場合に意味を持つと考えると、 https://github.com/rack/rack/issues/1787#issuecomment-1013917108 で書かれている
空のボディでContent-Typeが
multipart/form-dataだからRFC 2046 section 5.1に違反していると言えるかは微妙な気がします。ただ、RFC 9110 §15.4 にUAがリダイレクトを自動追従する場合の記述があり、その中に、リクエストメソッドがGETまたはHEADに変更された場合、
Content-Encoding,
Content-Language,
Content-Location,
Content-Type,
Content-Length,
Digest,
Last-Modified
などのコンテンツ固有のヘッダーフィールドを削除するべきとあります。
というわけで、RFC 9110とRFC 9112だけを見て言うと、
あたりが、妥当な振る舞いなのかなと思います。
ありがとうございます。本件ですが、進展がありまして、今こちらのissueなどで議論中です。 ぜひ議論に参加してもらえると助かります。よろしくお願いします。