New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArgumentOutOfRangeException in ViewBufferTextWriter with latest servicing release #31299
Comments
I'm aware. Looks like our March servicing release exposed this latent bug in MVC. Will spin up a conversation with shiproom about how we should address this. |
BTW, I suggest that when you fix this, you also audit your existing |
Thanks for contacting us. |
* Stop throwing exception when writing 0 length buffers Fixes: #31299 * Leverage runtime checks & add test https://github.com/dotnet/runtime/blob/7938f9d9fadc3952d792329be602668bef124f23/src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs#L159
@TanayParikh @mkArtakMSFT - Is there an estimate of when this fix will go out? Some context:
We have spot fixes to unblock updates, but would be very excited to have this reliably fixed. |
This was fixed via #32277 & will be part of the .net 6 Preview 5 release. |
@Lizzy-Gallagher this is scheduled for 6.0. Can you provide some code that shows how you are hitting this issue? Orchard is using a custom view engine, and we couldn't find a way to trigger the behavior otherwise. |
Thanks for the quick response @sebastienros and @TanayParikh, I have put together a minimal repro in .NET Core 3.1: Steps to repro
Gif of the repro:Fail.cshtml:
Pass.cshtml:
To be honest, I am not positive of the link between this error and System.Text.Encodings.Web update we did. This is mostly because I don't completely understand how this .NET Core 3.1 app gets its copy of these classes. Our apps hitting this in production get it from installed NuGet packages as they are .NET Framework. Perhaps this is what you meant by this being a "latent bug in MVC"? I don't fully understand the dependency tree here, but we would be very interested in a fix that is not just in .NET 6 as we have .NET Framework apps affected by this. 😬 |
Here is the interesting part public void CreateRepro(IHtmlContent text)
{
// In production code, we are doing something more complicated with this
// ViewContext.Writer and would like to continue using it if possible.
var writer = this._htmlHelper.ViewContext.Writer;
var headerTextDiv = new TagBuilder("div");
writer.Write(headerTextDiv.RenderStartTag());
headerTextDiv.InnerHtml.AppendHtml(text);
headerTextDiv.WriteTo(writer, HtmlEncoder.Default);
writer.Write(headerTextDiv.RenderEndTag());
// NOTE: This issue is only present when the text is inside
// a TagBuilder. The following does not throw the ArgumentOutOfRangeException:
// writer.Write(text);
} Thanks. As a mitigation you can deploy the previous .NET version as self-contained to prevent the system from using a more recent version, until this is fixed, probably in next service release (though I don't want to promise anything). |
Another mitigation that I haven't tried but might just work as well is to use a zero-width space (http://www.unicode-symbol.com/u/200B.html) as the first char, since the issue only occurs when the first char needs to be encoded, hoping it doesn't have to. |
* Stop throwing exception when writing 0 length buffers Fixes: #31299 * Leverage runtime checks & add test https://github.com/dotnet/runtime/blob/7938f9d9fadc3952d792329be602668bef124f23/src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs#L159
* Stop throwing exception when writing 0 length buffers Fixes: #31299 * Leverage runtime checks & add test https://github.com/dotnet/runtime/blob/7938f9d9fadc3952d792329be602668bef124f23/src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs#L159
## Description Update guard logic to permit writing 0 length buffers. This can happen in certain globalized scenarios (ex. `¿` with Portugese) where the first char needs to be encoded. Patch #31299 for 5.0 by cherry-picking the #32277 squash commit. ## Customer Impact Writing 0-length char buffers leads to an ArgumentOutOfRangeException. This may occur in a globalization context with certain chars such as `¿`. #31299 (comment) Also impacts Orchard: #31299 (comment) ## Regression? - [ ] Yes - [x] No [If yes, specify the version the behavior has regressed from] ## Risk - [ ] High - [ ] Medium - [x] Low Low risk as we've just updated the logic slightly to permit 0 length buffers. ## Verification - [x] Manual (required) - [x] Automated ## Packaging changes reviewed? - [ ] Yes - [ ] No - [x] N/A Addresses #31299
## Description Update guard logic to permit writing 0 length buffers. This can happen in certain globalized scenarios (ex. `¿` with Portugese) where the first char needs to be encoded. Patch #31299 for 3.1 by cherry-picking the #32277 squash commit. ## Customer Impact Writing 0-length char buffers leads to an ArgumentOutOfRangeException. This may occur in a globalization context with certain chars such as `¿`. #31299 (comment) Also impacts Orchard: #31299 (comment) ## Regression? - [ ] Yes - [x] No [If yes, specify the version the behavior has regressed from] ## Risk - [ ] High - [ ] Medium - [x] Low Low risk as we've just updated the logic slightly to permit 0 length buffers. ## Verification - [x] Manual (required) - [x] Automated ## Packaging changes reviewed? - [ ] Yes - [ ] No - [x] N/A Addresses #31299
This line
https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.ViewFeatures/src/Buffers/ViewBufferTextWriter.cs#L113
throws when invoked with
index: 0, count: 0
which is the case when using the new html encoderTextWriterExtensions
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Encodings.Web/src/System/IO/TextWriterExtensions.cs#L33
when invoking this method with a string that starts with a chars that needs to be encoded:
https://github.com/dotnet/runtime/blob/1a35f1db908f96f2bddd9d23c780c515d14f16e0/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/TextEncoder.cs#L204
I have multiple reports of Orchard users with applications on Azure that stopped working when the release was pushed.
OrchardCMS/OrchardCore#8887
The text was updated successfully, but these errors were encountered: