iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙃

Compatibility Issues Between Rack and Go http.Client

に公開
2026/02/28
2件

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:

https://github.com/rack/rack/issues/1787
https://github.com/golang/go/issues/52519

Background

I previously created the following repository for an in-house ISUCON:

https://github.com/catatsuy/private-isu

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:

https://github.com/catatsuy/private-isu/pull/204

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-data is 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-data via GET.
    • This can be achieved by overriding CheckRedirect.
  • Rack should not validate Content-Type: multipart/form-data for 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.

https://go-review.googlesource.com/c/go/+/710395

Discussion

yzxyzx

HTTP関連RFCでこの問題に関係しそうな箇所をいくつか挙げてみます。
(すべてをちゃんと読んだわけではないので、他にもこの問題に関係する記述や別のRFCがあるかもしれません)

RFC 9112 §6

The message body is identical to the content unless a transfer coding has been applied, as described in Section 6.1.

The presence of a message body in a request is signaled by a Content-Length or Transfer-Encoding header field.

RFC 9110 §9.3.1

Although request message framing is independent of the method used, content received in a GET request has no generally defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request and close the connection because of its potential as a request smuggling attack (Section 11.2 of [HTTP/1.1]). A client SHOULD NOT generate content in a GET request unless it is made directly to an origin server that has previously indicated, in or out of band, that such a request has a purpose and will be adequately supported. An origin server SHOULD NOT rely on private agreements to receive content, since participants in HTTP communication are often unaware of intermediaries along the request chain.

RFC 9110 §8.6

A user agent SHOULD send Content-Length in a request when the method defines a meaning for enclosed content and it is not sending Transfer-Encoding. For example, a user agent normally sends Content-Length in a POST request even when the value is 0 (indicating empty content). A user agent SHOULD NOT send a Content-Length header field when the request message does not contain content and the method semantics do not anticipate such data

例えばGETメソッドでリクエストにコンテンツが存在しない場合にUAはContent-Lengthを送信すべきでないとありますが、Content-Typeに関してはそのような記述は(ぱっと見)なさそうです。


RFC 9110 §8.2

Representation header fields provide metadata about the representation. When a message includes content, the representation header fields describe how to interpret that data.

とあるので、(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だけを見て言うと、

  • Goのhttp.ClientはリダイレクトでGETリクエストを送信する場合、Content-Typeヘッダフィールドを(も)削除する
  • RackはGETリクエストでメッセージボディがない(Content-Length、Transfer-Encodingいずれも存在しない)場合、Content-Typeヘッダフィールドがあってもそれを無視する(メッセージボディが存在すると想定した処理をしない)

あたりが、妥当な振る舞いなのかなと思います。