News for hackers. Stuff that matters.
Re:Golang's network stack attempts to parse HTTP headers as ... by saurik on Thursday January 21, 2021 @07:29AM (#25858176)
Every time someone creates a new programming language, the world builds yet another pile of broken HTTP implementations--clients and servers--because everyone refuses to read the specifications, thinking "none of this is hard". I remember dealing with some early Ruby web servers, 15 years ago, and was just shocked at how broken they were despite being used by everyone in production! In fact, there is a lot of nuance to HTTP, and as a civilization we seemed doomed to reliving the same mistakes over and over again every few years when the entire stack gets rewritten from scratch :/.

So, I thereby was neither surprised to read that the Go HTTP server did this obviously-wrong thing... NOR was I particularly surprised to read the person complaining about it also didn't seem to understand the correct behavior, either (though I do appreciate this is a bit "unfair"--I mean, it's a tweet ;P--but I think still makes for an interesting point here) :/. I have dealt with so many broken HTTP clients and servers over the years written by people who probably also never read the manual that came with their microwave because "documentation is for idiots" or whatever, that I have just become demoralized about it :(.

A few months ago, I was complaining about this, with a short list of stuff everyone tended to get wrong, and this HTTP header field encoding issue was on my shortlist :(.

https://news.ycombinator.com/item?id=24835617

> FWIW, I have encountered tons of issues in peoples' "this is easy, right?" implementations of 100 Continue (including in some at-the-time major Ruby HTTP servers), non-ASCII encoding in HTTP headers (which has a ton of subtle rules, and just throwing around UTF-8 is not correct), and chunked Transfer-Encoding (though I can't remember what was wrong); further, people often don't even realize HTTP trailers exist at all, which is a bit infuriating :/. (Note, though, that I am still 100% on the side of the anti-HTTP/2 camp in this thread.) The core problem is that somehow people think they can implement a spec without even, you know, reading the spec, which is so far past hubris as to have gone through incompetence into obvious negligence.

AFAIK, HTTP header fields are (in their generality) specified to be read as binary octets... but then are restricted to using ISO-8859-1 with the premise you are supposed to run them through a MIME parser? Like, some fields have strict formats, so you can't do the full decoding until you know what you are working with--and so I would argue an HTTP library should give you the raw binary data (though decoding them through ISO-8859-1 first isn't supposed to be "wrong")--but for fields with user-displayable text (including HTTP status messages!) you can seriously embed "Q" encoded strings (of all things) if you want, a la RFC 2047. You can't assume a field is restricted to ASCII too early in the parsing process.

https://tools.ietf.org/html/rfc2047

Re:Golang's network stack attempts to parse HTTP headers as ... by Thaxll on Thursday January 21, 2021 @08:24AM (#25858539)
Your arguments are short sighted, if it was as easy as just reading RFC no language would have problems but:

- RFC are inconsistent

- out of touch with the real world

- contain security issues

- not clear

- contradict themselves

So you can look at three majors HTTP server and sometimes they will implement / allow things differently becauset they have no other choice.

Re:Golang's network stack attempts to parse HTTP headers as ... by benibela on Thursday January 21, 2021 @05:20PM (#25865253)
Or you just want to interact with a few servers, so you implement what the servers do

In my case I wanted to interact with a few servers and not write a HTTP client, so I used an existing HTTP client library. Turns out, the library did not handle those servers, so I had to write the header parsing myself, at least the cookie parsing.

I wonder if there are RFC 2047 cookies and you need to parse them, or just send them back as they came

Re:Golang's network stack attempts to parse HTTP headers as ... by baq on Thursday January 21, 2021 @07:37AM (#25858223)
reading this i'm getting flashbacks from python 2 -> 3 migration and all those people complaining that they're now getting Unicode*Errors thrown at them. the fact is, none of this stuff is simple! you can pretend it is until a corner case bites you in the behind, suddenly the tower of abstraction falls apart and you're left picking up the parts in four different string encoding formats. (see the abysmal C which at least doesn't pretend anything at all, C++ which doesn't require you to know all this stuff and then it just blows up if you make a particularly wrong misstep and most recently Rust, which will call you names for using the wrong type in the wrong place, in turn greatly confusing people who say strings are easy).
Re:Golang's network stack attempts to parse HTTP headers as ... by wizzwizz4 on Thursday January 21, 2021 @08:04AM (#25858397)
In this case, Python and Rust's behaviours are correct. You only get type errors / Unicode* exceptions when you're making wrong assumptions; if you get them, and don't understand why, you're writing buggy code to do things you don't understand and should be thankful for the errors letting you know there's a problem.
Re:Golang's network stack attempts to parse HTTP headers as ... by pdimitar on Thursday January 21, 2021 @08:14AM (#25858460)
I quite like what languages with proper sum types -- Rust, OCaml, Haskell -- do in these situations.

We cannot tolerate half-written buggy clients for an eternity.

Let us have more errors. Computers aren't humans. You have to specify things precisely, not throw a blob at them and let them figure it out. They can't. The attempts at that have only led to countless bugs and costly security breaches.

Re:Golang's network stack attempts to parse HTTP headers as ... by saurik on Thursday January 21, 2021 @08:13AM (#25858453)
So, my favorite thought process from the Python 2 to 3 transition comes from staring deeply into the hubris of the Python 3000 effort itself as wanting to insist "everything is Unicode"; on the one hand they were trying to shame developers using Python 2 for using it incorrectly (even though it was totally possible to handle Unicode correctly with that stack), yet on the other they were pumping out ridiculous unavoidable security SNAFUs in their API surface by assuming that filenames on disk are UTF-8 :/.

To tell another story of the importance of reading the--in this case, not even long!--specification: on Windows, filenames are stored as UCS-2 (as it all predates UTF-16), and is most likely most correctly modeled as an arbitrary sequence of non-0 16-bit values; meanwhile, on Unix systems, a filename is pretty much just an arbitrary sequence of non-0 8-bit values (with some weirdness around the slash character... the API surface doesn't support it really but sometimes the filesystem does). The premise that filenames have a display encoding mostly comes from the software rendering the strings and is then defined in terms of a bunch of hacks involving "locales", and isn't a property of the underlying API or storage semantics.

You can't assume in either case that they are canonical Unicode representations, and so storing them as "strings" in your program is super wrong and can lead to anything from data storage mistakes to security vulnerabilities. The Python 3 API filesystem API was thereby so bad that it would do things like just skip over files during directory traversal that didn't map to its attempt to decode stuff as Unicode. Like: if you wanted to hide a file from a Python 3 program, you could just make it invalid Unicode :/.

(I realized people reading this comment might find a reference on this fun, and so found this seemingly-comprehensive postmortem. I haven't read it, but it at least looks really good ;P.)

https://vstinner.github.io/python30-listdir-undecodable-file...

I thereby find the whole Python 3 debacle extra demoralizing as it felt like a bunch of people who didn't truly understand how hard the problem they were dealing with was--and how much of the world actually is binary, and how often (when dealing with things like filesystems and network protocols) parsing binary sequences as pseudo-strings is "correct"--both sometimes-correctly insisting everyone was wrong while themselves failing to build something even minimally useful until like, Python 3.4 or so; and, in the end, I feel like they just ended up creating a "new mess" by having filenames now encoded as "Unicode strings"... but with weird exceptions for allowing round-trip behaviors of strings that couldn't be decoded :(.

The reality is that there are a number of things that developers work with which are pseudo-strings, and languages should provide good tooling for working with them. We see the same issue come up with HTTP headers and filenames: things that are actually binary but have string-like structure. There is a reason why both C++ and Python 2 are often looked at fondly by low-level developers for providing low-level string representations, even while they end up encouraging broken behavior higher up the stack. I haven't looked enough at Rust's standard library to see if they truly nailed this yet, but maybe one day we will have a language that encourages the right behavior for these mechanisms that are neither strictly string not strictly binary.

Re:Golang's network stack attempts to parse HTTP headers as ... by pansa2 on Thursday January 21, 2021 @08:21AM (#25858518)
> Python 3000 effort itself as wanting to insist "everything is Unicode"

How does Python 3 handle these HTTP headers and other things that are purely ASCII? Does it treat them as type `bytes` instead of (Unicode) `str`?

Re:Golang's network stack attempts to parse HTTP headers as ... by saurik on Thursday January 21, 2021 @08:36AM (#25858629)
I just did some digging, and the Python 3 built-in HTTP stuff seems to represent HTTP headers using a reasonably-comprehensive mechanism that is shared with MIME parsing and the various IMF semantics, and so I am pretty sure you end up getting back an object of type email.header.Header (but I might have misunderstood the documentation just now... I mostly gave up on Python when they moved to Python 3 so I haven't experienced this first hand).

https://docs.python.org/3/library/email.header.html#email.he...

This actually looks like a really solid implementation--like, I am super impressed by this (though watch it suck... everything sucks ;P)--as it doesn't assume any ability to flatten the result! That said, I am also deeply concerned that this might pre-decode something that should actually be in ASCII all the way down from MIME (which, notably, is complex enough of a format to include support for embedded comments). I kind of want to do some auditing here, to make sure there aren't some parser discrepancy vulnerabilities in common Python HTTP frameworks ;P.

But like, I think this object represents a sequence of strings each with its own encoding, allowing it to "Q" encode the result exactly as specified (and correctly round trip something it parsed, which still does feel a tad dangerous, and maybe the API lets you avoid it in those cases). It then has an __str__ implementation (to let you use it as a string) that explicitly claims to return an "approximation" of the header (which may or may not be a safe thing to use in different circumstances; but like, that in some sense isn't the API's fault?...).

Re:Golang's network stack attempts to parse HTTP headers as ... by Measter on Thursday January 21, 2021 @11:46AM (#25860895)
Rust is a bit notorious for its string-related complexity, and that's for the reasons you talked about.

The most common form of string you'll see is `String`[0], or its view type `str`[1], which are UTF-8 encoded. These have most of the string functions you'd expect to have.

Of course, as you mentioned, operating systems don't all use UTF-8, which brings us to `OsString`[2], and its view type `OsStr`[3]. This is the data type that string data from the OS is represented as. Both are basically bundles of bytes, with no specific encoding. You'll note that these are in the FFI namespace.

You then have `PathBuf`[4] and its view type `Path`[5], which are wrappers around `OsString` and `OsStr` respectively, and provide path-specific functionality.

All the file system related APIs take anything that implements `AsRef<Path>`, which is runtime-cost-free for all of these types. There's not many functions that take an `OsStr`, but those that do take it in the form `AsRef<OsStr>`, which like `Path` is runtime-cost-free for all these types. None of the APIs take `PathBuf` or `OsString`.

Return values are typically `PathBuf` for file system APIs, and `OsString` for other APIs. The only exceptions I know of are the functions for getting environment variables and command line arguments, which also provide functions that return `String` because a common use case would be converting them anyway.

[0] https://doc.rust-lang.org/std/string/struct.String.html

[1] https://doc.rust-lang.org/std/primitive.str.html

[2] https://doc.rust-lang.org/std/ffi/struct.OsString.html

[3] https://doc.rust-lang.org/std/ffi/struct.OsStr.html

[4] https://doc.rust-lang.org/std/path/struct.PathBuf.html

[5] https://doc.rust-lang.org/std/path/struct.Path.html

Re:Golang's network stack attempts to parse HTTP headers as ... by zvrba on Thursday January 21, 2021 @09:19AM (#25859034)
> Every time someone creates a new programming language, the world builds yet another pile of broken HTTP implementations

This tells a lot about HTTP as well.

Re:Golang's network stack attempts to parse HTTP headers as ... by kenniskrag on Thursday January 21, 2021 @08:12AM (#25858446)
is there only one rfc for http? How can I find the related one's? Tried that during studying how a GET/POST etc. works and what the diff to HTTP/2 was. But I always got a bunch of rfc's where I didn't know if they are related or already obsolete.
Re:Golang's network stack attempts to parse HTTP headers as ... by dgellow on Thursday January 21, 2021 @09:59AM (#25859459)
So, it's a mess to navigate, but in general at the beginning of the RFC you have a list of other documents that update or obsolete it, and also list of documents updated or obsoleted by it.

As an example: https://tools.ietf.org/html/rfc6126. The document starts with "Updated by: 7298, 7557".

Regarding HTTP, if you pass by the wikipedia article, you have the list of RFC on the menu on the right: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol.

RFC 1945 HTTP/1.0 (1996)

RFC 2616 HTTP/1.1 (1999)

RFC 7540 HTTP/2 (2015)

RFC 7541 Header Compression (2, 2015)

RFC 7230 Message Syntax and Routing (1.1, 2014)

RFC 7231 Semantics and Content (1.1, 2014)

RFC 7232 Conditional Requests (1.1, 2014)

RFC 7233 Range Requests (1.1, 2014)

RFC 7234 Caching (1.1, 2014)

RFC 7235 Authentication (1.1, 2014)

Re:Golang's network stack attempts to parse HTTP headers as ... by kenniskrag on Thursday January 21, 2021 @10:38AM (#25859955)
Great! I didn't saw the information on top left. For example for the header compression there isn't a two way connection? I see only a reference to the http rfc.