ruby / ruby/net-http

Generation of multipart post fail if inconsistent encodings given as input

Open
#187 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
148
Forks
95
Avg merge
10h 54m
Merged PRs (30d)
4

Description

When attaching multipart entities of different encodings it is easy to get failures due to intermediary strings inside encode_multipart_form_data that care about encoding despite there being no need to care about them.

Example:

require "net/http"
require "stringio"

utf_string = "räksmörgås"
ascii_string = "räkmacka"
ascii_string.force_encoding("ASCII-8BIT")

form_data = {
  "foo"   => "bar",
  "utf8"  => StringIO.new(utf_string),
  "ascii" => StringIO.new(ascii_string)
}

url = URI("http://www.example.com")

request = Net::HTTP::Post.new(url)
request.set_form(form_data, "multipart/form-data")

Net::HTTP.start(url.hostname, url.port) do |http|
  http.request(request)
end

This results in

net-http-0.4.1/lib/net/http/generic_request.rb:354:in `block in encode_multipart_form_data': incompatible character encodings: UTF-8 and ASCII-8BIT (Encoding::CompatibilityError)
	from /home/linus/.gem/ruby/3.3.3/gems/net-http-0.4.1/lib/net/http/generic_request.rb:320:in `each'
	from /home/linus/.gem/ruby/3.3.3/gems/net-http-0.4.1/lib/net/http/generic_request.rb:320:in `encode_multipart_form_data'
	from /home/linus/.gem/ruby/3.3.3/gems/net-http-0.4.1/lib/net/http/generic_request.rb:303:in `send_request_with_body_data'
	from /home/linus/.gem/ruby/3.3.3/gems/net-http-0.4.1/lib/net/http/generic_request.rb:204:in `exec'
[...]

There are real use cases that are reasonable like posting both a text file and a pdf at the same time.

A workaround is to always encode absolutely everything as ascii-8bit before posting it.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/net/http/generic_request.rb at encode_multipart_form_data and reproduce the supplied mixed UTF-8 and ASCII-8BIT StringIO example. Trace the multipart assembly that raises Encoding::CompatibilityError, then verify that a multipart request containing differently encoded inputs can be generated and sent without that failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.