Summary Rack::Multipart::Parser#tag_multipart_encoding calls Encoding.find once for every charset parameter in the Content-Type header of a multipart part
Description
Rack::Multipart::Parser#tag_multipart_encoding calls Encoding.find once for every charset parameter in the Content-Type header of a multipart part. When the name is not a known encoding, Ruby's Encoding.find attempts to load an encoding library of that name, which searches every directory on $LOAD_PATH before failing. Rack rescues the resulting ArgumentError and falls back to binary, does not remember the failure, and does not limit the number of charset parameters a part may carry. An attacker can send a part whose Content-Type contains thousands of unknown charset parameters, and each one costs a full load-path search. This results in a denial of service condition for applications that accept multipart requests.
Details
The relevant code is:
if k == "charset"
encoding = find_encoding(v)
end
def find_encoding(enc)
Encoding.find enc
rescue ArgumentError
Encoding::BINARY
endFor a name absent from Encoding.name_list, Encoding.find calls load_encoding in encoding.c, which requires enc/<name>.so through Ruby's internal loader. The lookup is not cached, is not visible to RubyGems or Bootsnap, and costs roughly 3–5 µs per $LOAD_PATH entry: about 30 µs on a bare Ruby, about 500 µs in a Bundler application with 200 load-path entries on macOS, and about 2.5 ms per lookup measured on Linux with 167 entries.
Each ;charset=x costs 10 bytes. Existing limits do not bound the attack: the part limit (RACK_MULTIPART_PART_LIMIT, 4096) counts parts, not parameters; the 64 KiB part header limit is only enforced when a header spans two reads, so a single header of up to the 1 MiB buffer size is accepted; and part headers count toward the 16 MiB retained-size limit, which allows about 1.6 million lookups per request. A single part with 4000 unknown charset parameters (64 KB) takes about 1.7 s to parse in a mid-sized Bundler application; a 1 MB request with 16 such parts takes 27–40 s. Through Puma in a default Rails application, four concurrent 1 MB requests delayed an unrelated health-check endpoint by over 10 s. The result is binary regardless of how many unknown names appear, so none of the lookups beyond the first are useful.
Rack 3.0, 3.1 and 3.2 are affected. Rack 2.2 is not affected by the denial of service: it does not rescue the ArgumentError, so the first unknown charset aborts parsing after a single lookup.
Impact
Any application that parses multipart bodies through Rack::Request#POST or #params is affected in its default configuration. In a default Rails application a POST to any path, including one with no route, is sufficient, because Rack::MethodOverride parses the body before routing. An unauthenticated attacker can occupy a worker thread for tens of seconds with a request under 1 MB, and for minutes with a larger one; concurrent requests exhaust the thread pool. The cost grows with the number of gems in the application's bundle. No authentication, malformed HTTP, or unusual configuration is required.
Mitigation
- Update to a patched version of Rack, in which
Encoding.findis only called for names present inEncoding.name_list, so unknowncharsetvalues are mapped to binary without searching the load path. - At a reverse proxy, limit the size of multipart requests and reject requests whose part headers contain many repeated
charsetparameters.