HttpAbstractions icon indicating copy to clipboard operation
HttpAbstractions copied to clipboard

Non-english characters are encoded

Open danroth27 opened this issue 11 years ago • 19 comments

Reported as https://github.com/aspnet/Razor/issues/409 by @anfomin

Every non-english character is encoded. For example:

@{
    string s = "Добрый день"; // this is Russian text
}
@s

The result is something like: 2015-05-27 9 55 55


Characters displayed correct, but page size increased significantly :-1: Also MVC 5 rendered unicode symbol correctly in HTML source.

danroth27 avatar May 28 '15 19:05 danroth27

Actually MVC 5 would have done the same if you'd switched to AntiXSS as the encoder.

By design.

blowdart avatar May 28 '15 19:05 blowdart

What API are you complaining about?

Tratcher avatar May 28 '15 19:05 Tratcher

Ah, IHtmlEncoder

Tratcher avatar May 28 '15 19:05 Tratcher

@blowdart however in MVC 5 it was a choice and the default HTML encoder provided readable source of a reasonable size.

dougbu avatar May 28 '15 19:05 dougbu

The standard Russian characters should have been safe listed with the new encoder. You'll need to examine the whitelist generation to see why they're not.

blowdart avatar May 28 '15 19:05 blowdart

@blowdart from the HtmlEncoder source, the default escaped range is UnicodeRanges.BasicLatin which has this convenient comment:

/// A <see cref="UnicodeRange"/> corresponding to the 'Basic Latin' Unicode block (U+0000..U+007F).

Are you suggesting a different default?

dougbu avatar May 28 '15 21:05 dougbu

I though it was changed, but ok. And no, let's be honest, I'd encode everything by default to avoid certain utf7 badness. Customer can use the Default property to widen their safe list as needed.

blowdart avatar May 28 '15 21:05 blowdart

Every character encoding brings another problems. Sometimes we use <script> generation in razor pages like:

<script>
    $(function() {
        var someVariable = "@thisIsDotNetVariable";
        // other code goes here
    });
</script>

So JavaScript will not decode this value and will display unicode numbers.

anfomin avatar May 29 '15 04:05 anfomin

Don't do that for javascript. It's simply wrong.

@ encoding is HTML encoding. It's not JavaScript encoding. Using HTML encoding inside javascript means some of the characters that can cause javascript injection are not encoded.

The most foolproof way to inject variable values into javascript is to have a div or span or other tag, and use a data attribute - using @ encoding there is correct, and you're protected. Then you'd retrieve it within the javascript.

blowdart avatar May 29 '15 12:05 blowdart

@blowdart OK, this it good explanation not to use @ with JS. But encoded-text increase page size 7 times (each character converted into 7). I do not think this is good behavior by default.

Also there is no attack can be done using Russian or any other language characters so they do not require HTML encoding. This characters do not have any "special" behavior compared to English ones.

anfomin avatar May 29 '15 14:05 anfomin

Actually you can attack via some characters depending on the underlying page encoding and other badness. Utf7 is a great example of this - http://openmya.hacker.jp/hasegawa/public/20071107/s6/h6.html?file=datae.txt has some details.

We're discussing internally what we want to do - but the encoders are used by things other than razor, and defaulting to encoding everything over and above ASCII is what makes it 100% safe for the other things to consume, so I'm not minded to change the default encoder configuration. With gzip compression which most servers support these days the actually transport size increase shouldn't be that painful, but I realise if you're not in a default English speaking country it's probably not a cost you want to take (being a Brit, it annoys me when we end up assuming everything speaks US english).

You could just whitelist everything yourself, either by

HtmlEncoder.Default = new HtmlEncoder(UnicodeRanges.All);

before the encoder gets registered in DI, or via

serviceColl.AddWebEncoders(o => { 
    o.CodePointFilter = new CodePointFilter(UnicodeRanges.All);
});

If you wanted to narrow it down, the you can create ranges and pass them into the encoder which match the Unicode code charts which contain the characters you need (Unicode doesn't do languages, so you will need to mix and match - you can find the code charts at http://www.unicode.org/charts/)

blowdart avatar May 29 '15 14:05 blowdart

Note setting HtmlEncoder.Default is likely not what you want. That changes only the HTML encoder, leaving the JavaScript and URL encoders with their (ASCII) defaults.

Recommend

  services.Configure<WebEncoderOptions>(options => new CodePointFilter(UnicodeRanges.All));

because (today) nothing configures WebEncoderOptions but much of the system adds the web encoders.

dougbu avatar May 29 '15 16:05 dougbu

@blowdart Is there any action required here? This sounds all by design.

Tratcher avatar Sep 09 '15 21:09 Tratcher

By design. The MVC team wanted to change it somewhat, but never came up with a suitable suggestion,

blowdart avatar Sep 09 '15 21:09 blowdart

I have the same problem with accents (pt-BR). In previous versions this was not a problem.

I think to a final version, native, pure and simple MVC should accept this kind of character without that develop change any settings.

More than a technical issue, for me it is a matter of attention to non-English developers

penihel avatar Nov 23 '15 13:11 penihel

The api was changed, the issue can be walked abound by the code:

services.Configure<WebEncoderOptions>(options => 
{
      options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});

kerryjiang avatar Jun 04 '16 06:06 kerryjiang

I agree that BasicLatin is a horrible default. Not only it increases the file size and overhead (don't we expect the core to run on small devices too?) but also makes the page illegible to humans.

miloush avatar Jun 07 '16 17:06 miloush

where is the WebEncoderOptions? @kerryjiang

Cuiqs76 avatar Feb 24 '17 15:02 Cuiqs76

This default is absurd. In addition it is not even clear to me if using UnicodeRanges.All is safe or I'd better list a couple of more popular Unicode ranges like Cyrillic, GreekandCoptic, Hebrew and Arabic and leave the rest encoded

Eirenarch avatar Jun 21 '18 12:06 Eirenarch