Configure header parsing to allow non-compliant headers
Rather than rejecting requests, allow configuration to either ignore UTF8 headers, or, parse them, even if they're illegal.
I suggest an enum Reject, Ignore, Parse.
However on output we should still be strict, UrlEncoding cookie values etc.
See https://github.com/aspnet/KestrelHttpServer/issues/1076 and https://github.com/aspnet/KestrelHttpServer/issues/1125
edit by @muratg: We should also consider request line when we get to this. https://github.com/aspnet/KestrelHttpServer/issues/2647
In practice I expect this to become an FAQ where most people turn on UTF-8, so we should consider making that the default.
Have we tested how these headers forward through IIS? WebListener?
I like how we say "We" Nope, please try it :p
@halter73 please work with @cesarbs, based on timing this may be load balanced to you.
cc @davidfowl
A big change (essentially a rewrite in header processing) so moving to 1.2.0.
@Tratcher could you file a corresponding bug on WebListener repo?
Should also fix: https://github.com/aspnet/KestrelHttpServer/issues/1125
Investigating this. Will check how those headers forward through IIS, and also what other servers do.
Here's what I found so far:
| Server | Behavior |
|---|---|
| IIS | accepts UTF-8 in header value, don't know if immediatelly decoded |
| IIS running ASP.NET 4 app | accepts UTF-8, decodes it as such |
| IIS with ANCM | rejects, not yet sure why, but the request is not forwarded at all |
| WebListener | accepts UTF-8, decodes it as such |
| nginx | accepts non-ASCII, haven't checked yet what it does with it |
| node.js | accepts non-ASCII, it's up to the app to decode it |
| Apache | accepts non-ASCII, it's up to the app to decode it |
A relevant bit from the RFC:
Historically, HTTP has allowed field content with text in the
ISO-8859-1 charset [ISO-8859-1], supporting other charsets only
through use of [RFC2047] encoding. In practice, most HTTP header
field values use only a subset of the US-ASCII charset [USASCII].
Newly defined header fields SHOULD limit their field values to
US-ASCII octets. A recipient SHOULD treat other octets in field
content (obs-text) as opaque data.
I think the most relevant part here is:
A recipient SHOULD treat other octets in field content (obs-text) as opaque data.
So it's not forbidding chars above 0x7F. obs-text is actually defined in the next section as
obs-text = %x80-FF
The most correct behavior seems to be to accept characters in the 0x80 - 0xFF range (which we reject at present), but to let the app decide the encoding. Http.sys appears to deviate from this though, by decoding as UTF-8.
For reference, which header did you test? You may get different results for a common header vs a custom header. E.g. Host and Location are often special cased.
How does Apache leave it up to the app to decode it? Does it expose the raw header bytes?
@Tratcher I was testing with Referer, as in #1125.
I tested Apache with a PHP app, which saw the header as raw bytes. You get a "string" for it, but I'd have to manually decode it as UTF-8 to get the right chars.
@blowdart We're currently rejecting if the header is UTF8, so I don't think this has security implications. Moving to 2.1.0
Won't this still reject UTF8 cookies? if so that's a problem
FYI if you care SNI says the host is utf8
https://tools.ietf.org/html/rfc3546#section-3.1
The hostname is represented as a byte string using UTF-8 encoding [UTF8], without a trailing dot.
Now a good server should probably check the host header and SNI match, might be impossible if you can't have a utf8 header
There is even a spec on how to deal with hostnames
https://tools.ietf.org/html/rfc3490#section-4
But a lot is left as an exercise for the reader.
Won't this still reject UTF8 cookies? if so that's a problem
Why is that a problem? We've had 2 complaints overall so far.
If you provide service for customers, and they allowed to insert javascript, and If it accidentally inserts a unicode character. then their customers will have problems forever.
critical problem. please hot fix
IE doesn't encode the referer header. As a result, this breaks our .NET Core APIs when requests are coming from IE users on some of our sites with e.g. Cyrillic characters in the URL. Would be good to have support for this.
@davidfowl I think you are not getting many complaints because it takes so long to track down what the issue is. Because many different applications might set cookies with utf-8 characters on the root of the domain (.example.com) and thus some developers might blame it on user error because that encoding error is not popping up in the logs and may be hard to reproduce.
We could really use this, we have a site running on a subdomain (site.bigcompany.com) and user navigate to our site through our company site (bigcompany.com). Our company site uses cookies with non-ascii characters that are also sent to the subdomains and causes users that access our site through our company site to receive bad requests. We're now working with the team from our company site to have them fix their cookies.
I believe this is something that can also be misused for a DOS-attacks. If you manage to set a cookie in the users browser then your site (running ANC) will not function anymore.
As other mentions, the error might originate from javascript cookies and not your own code, so you might not able to correct the error.
In our situation Google Analytics sat the malformed/utf-8 cookie.
Our marketing department used the querystring utm_campaign to name their campaigns (containing danish characters), so when they made an url to our site, and posted it on facebook or other places, all users that clicked that link was no longer able to view our website after the first page view. Only solution was to clear cookies (or wait the 6 months until cookie expiration) - but not many users told us we had a problem, they just went on to our competitor instead.
- User clicks link http://website/page?utm_campaign=MinKampagneUndersøgelse
- Page loads and user sees it fine
- Google Analytics kicks in on the client side and sees the utm_campaign querystring and stores a __utmz cookie on our domain containing the utf-8 string
- Next pageload by the user is unable to process because of the utf-8 cookie and the user sees a blank page with no error.
It took us ages to track down, because the cookie was actually sat on a parent domain and not the kestrel site (which, at the time, was running on a subdomain). And it was not even sat by our own code, but third party javascript, so searching our codebase for the cookie name came out with 0 results.
Since we discovered the error, a newer version of Google Analytics has changed behaviour of storing all their data at their side and only store a user-id cookie on the client side, so the malformed cookie is no longer set. See this for explanation: https://stackoverflow.com/questions/18604715/google-analytics-missing-utmz-cookie
I think you might be able to reproduce the error if you are using the old Google Analytics scripts (ga.js) on your kestrel website (instead of the newer analytics.js), by simply loading a url with a utm_campaign querystring containing international characters (we have switched to the newer version, so can't test it on my own anymore).
It would be great to at least have some kind of option to ignore utf8 cookies, so we at least can get around it by programming instead of loosing users forever.
We also had this problem, which was extremely difficult to narrow down to being this. We use shibboleth which is injecting request headers into our application. One of our staff had í in their name, which just resulted in them getting the 400 error and us being confused for a long time.