Repository navigation
Description
I did this
- Create a file of the form
"foo.jpg(note the leading quote) - Submit as a form using
curl -i -F encoded_image=@\"foo.jpg https://www.google.com/searchbyimage/upload
I expected the following
Within the multipart/form-data that is generated, the filename parameter of the request will be in the following form: filename="\"foo.jpg" - using the quoted-string production of RFC 822.
However, I would expect that the filename would of the form filename="%22foo.jpg", using the Percent-Encoding Option of RFC 7578
curl/libcurl version
ToT
operating system
Further Details
The context here is that multipart/form-data encoding is handled by lib/formdata.c. The form-data support is implemented using the generic MIME encoder, in the family of curl_mime_* functions. For example, in the above example of attaching a file, this is handled by
Line 882 in e081048
| result = curl_mime_filedata(part, file->contents); |
curl_mime_filedata
curl_mime_filedata passes the filename onwards using curl_mime_filename, passing only the basename of the filename.
Later, when compiling the multipart message, the field name and filename for the Content-Disposition header are escaped using the escape_string function, as shown at
Lines 1867 to 1886 in 52fab72
| if(part->name) { | |
| name = escape_string(part->name); | |
| if(!name) | |
| ret = CURLE_OUT_OF_MEMORY; | |
| } | |
| if(!ret && part->filename) { | |
| filename = escape_string(part->filename); | |
| if(!filename) | |
| ret = CURLE_OUT_OF_MEMORY; | |
| } | |
| if(!ret) | |
| ret = Curl_mime_add_header(&part->curlheaders, | |
| "Content-Disposition: %s%s%s%s%s%s%s", | |
| disposition, | |
| name? "; name=\"": "", | |
| name? name: "", | |
| name? "\"": "", | |
| filename? "; filename=\"": "", | |
| filename? filename: "", | |
| filename? "\"": ""); |
These is a complex area due to issues of character sets and filenames. Within this space, RFC 6266, Section 4.3 is relevant, and further expanded upon in RFC 6266, Appendix C.2. RFC 7578, Section 4.2 attempts to provide guidance here with respect to the filename handling inter-operably within a Content-Disposition: form-data part.
The HTML Living Standard Multipart form data specification places a normative dependency on RFC 7578, providing further guidance with respect to the encoding and escaping of field names and filenames for file fields, the two areas escaped via escape_string, to encode to the appropriate encoding and then replace { 0x0A, 0X0D, 0X22 } with { "%0A", "%0D", "%22" }, respectively. This is compatible with the guidance of RFC 7578, and reflects widespread use within browsers (e.g. Chromium/Chrome or WebKit/Safari)
This is an area where MIME attachments differ in practice than form data. It seems that it may be useful to at least align the form encoder to match the approach mentioned within RFC 7578, for greater harmonization.
Activity
bagder commentedon Sep 29, 2021
I agree. @monnerat, do you want to grab this?
monnerat commentedon Sep 29, 2021
This is a picky area: we have something that seems to work for forms and featuring mime did not change this escaping that was already present in original formdata code.
In addition, there are contradictory informations in the mentioned documents:
In https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart-form-data:
In https://datatracker.ietf.org/doc/html/rfc6266#section-4.3:
So: %-escape or not, and what about the backslash ???
I fear this change may break a lot of things: in particular, tests 39 and 1158.
I'm not convinced it is wise to introduce problems where there is no bug, just because of an inconsistency with third party applications.
As a consequence, I can do it, but things have to be clarified first.
bagder commentedon Sep 29, 2021
This is rather tricky. It seems the three specs have very different ideas on how to encode some basic bytes:
Or did I get any of them wrong?
Those tests verify curl's behavior. If we change behavior, we of course also need to update those tests accordingly!
When specs are hard to follow (like here) I think we need to check what other tools and libraries use and what widely deployed server-side applications accept/support. I think unifying with browser behavior on this seems reasonable as they are probably the major producers of formdata bodies.
sleevi commentedon Sep 29, 2021
So this isn’t quite the problem with URLs, but similar.
In order of precedence, and recognizing that cURL may ignore the WHATWG work due to the Living Standard nature:
I say “ish”, because virtually no one is using the
paramname*construction to extendparamnamewith character set details.(
Edited as previous comment was edited)bagder commentedon Sep 29, 2021
Yes sorry, I realized my mistake and edited the table. WHATWG and RFC 7578 agree on 0x0a, 0x0d and 0x22.
I think I'm still wrong with the backslash there as 7578 doesn't say it should be encoded I think...
sleevi commentedon Sep 29, 2021
@bagder I think your table is correct now, in that Section 2 of RFC 7578 says you can percent encode the
disallowedcharacters - which naively would include such.However, I don’t think
\is necessarily an interesting case, primarily because I understand browsers treat it as a path separator, and thus it’s elided from the filename before encoding, similar to how cURL grabs the basename of the file. That said, I could see it being entirely reasonable for WHATWG to make that a bit more explicit, either in processing or making sure it’s addressed for encoding (to avoid mis-escaped sequences, similar to the”handling)monnerat commentedon Sep 29, 2021
Seems not. But you know http better than I do.
Sure! But this shows we have no bug, but perhaps a misinterpretation. That said, if tests break, applications running perfectly for more that 13 years now may fail too.
I think it's the most important. Servers themselves may parse it too (Apache modules?) As we only generate these and do not parse them, It would be good to know how widespread CGI-like apps and servers (i.e.: PHP) understand them.
Curl does not support charset encoding in http/mime header fields. This would be rather cumbersome to implement.
AFAICS, you're as puzzled as I am :-)
As the various documents are not OK with each other, this introduces a huge spelunking work for us in third-party products. Even the latter may not all stick to the same behavior. I'm not sure I'm ready for thst :-(
sleevi commentedon Sep 29, 2021
Indeed! I mentioned separately to @bagder that this began as an “Oh, that’s odd” when someone noticed a webpage behaving differently if browsers sent the request vs cURL, and the deeper I went, the more boxes of spiders I found myself opening.
I thought it useful to write this down and try to capture the references. cURL’s ubiquity suggests this would be the main place people run into it.
From a browser evolution space, there’s been a wide variety of behaviours before converging on the current approach. For example, one was just to replace the
”with a?(as with any other high-bit character), another is to ignore the input filename and generate a sequential or random filename if it couldn’t be represented.Although ultimately this is only intended as a hint for servers (for the reasons and risks noted in the various specs), and to that end, many behaviors are valid, I figured I would at least file this so it’s on your radar, even if the risk/complexity/ineffability is too much :)
monnerat commentedon Sep 29, 2021
Before speaking semantics, we should care about lexical and syntax. If we do not encode a backslash, would it be correct or would it be interpreted as a
quoted-pair(http uses non-terminalquoted-stringwhile it is defined in RFC 822!). Furthermore, from what I understand from the documents, this should be applied to field names too (although non-ascii is discouraged for them). And those are not just hints...bagder commentedon Sep 29, 2021
Test form:
Firefox 88 on Linux for the file name
"foo:Chrome 94 on the same box same name:
bagder commentedon Sep 29, 2021
It's even worse. I edited my html for the form to be:
(the newline there is intended)
The results from the jury:
Firefox 88:
Chrome 94:
monnerat commentedon Sep 29, 2021
I tried with firefox 92.0 and it uses percent encoding without backslash escaping.
monnerat commentedon Sep 29, 2021
Maybe some M$ tester could check with IE and edge?
bagder commentedon Sep 29, 2021
It still means that receivers of multipart formposts today have to deal with both the \-escaped and percent encoded fields for now and the near-term future. Not sure that helps us with this issue, I'm just stating the obvious.
6 remaining items
[REM] website: test case for a form key ending with `\`
[REM] website: test case for a form key ending with `\`
[REM] website: test case for a form key ending with `\`
[REM] website: test case for a form key ending with `\`
[REM] website: test case for a form key ending with `\`
[REM] website: test case for a form key ending with `\`
[REM] website: test case for a form key ending with `\`