Nee*_*ack 6 c# veracode .net-core
这段代码有什么问题?此外我该如何解决它?
public class BodyStreamMiddleware
{
private readonly RequestDelegate _next;
public BodyStreamMiddleware(RequestDelegate next) { _next = next; }
public async Task Invoke(HttpContext context)
{
// Replace the FrameRequestStream with a MemoryStream.
// This is because the MemoryStream is rewindable, the FrameRequestStream is not.
// This allows ExceptionFilters to read the body for logging purposes
string bodyAsText;
using (var bodyReader = new StreamReader(context.Request.Body))
{
bodyAsText = bodyReader.ReadToEnd();
}
var bytesToWrite = Encoding.UTF8.GetBytes(bodyAsText);
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(bytesToWrite, 0, bytesToWrite.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
context.Request.Body = memoryStream;
// Tell ASP.NET core to dispose the memory stream when the request ends
// (only added in desperation)
context.Response.RegisterForDispose(memoryStream);
await _next.Invoke(context);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我对上面的内容运行 Veracode 扫描时,它给了我
404资源关闭或释放不当
我知道下游进程可以获取对内存流的引用并挂起它,但无法看出这与默认的 asp.net 行为有何不同(即某些东西可以获取 FrameRequestStream)。
回答我自己的问题是为了他人的利益......
我通过以不同的方式使请求流重新可读来设法解决/回避这个问题。这让 Veracode 很高兴,尽管我根本不相信以前的代码有什么问题。
把这个放在你的启动课程中......
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.Use(async (context, next) => {
context.Request.EnableRewind();
await next();
}
...
});
Run Code Online (Sandbox Code Playgroud)
我在这里写了关于此的博客https://needhack.wordpress.com/2018/01/17/make-the-request-stream-re-read-in-asp-net-core-with-c/,因为我们是所有人都被告知要写博客,因为如果我们这样做,我们将成为下一个斯科特汉塞尔曼,提高我们的费率并放弃日常通勤,对吗?
| 归档时间: |
|
| 查看次数: |
4670 次 |
| 最近记录: |