이 코드에 어떤 문제가 있습니까? 또한 어떻게 고칠 수 있습니까?Veracode에서 "부적절한 리소스 종료 또는 릴리스"오류가 발생하는 이유는 무엇입니까?
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);
}
}
}
위의 나에게
404 부적절한 자원 종료 또는 해제를 제공 동안 나는 베라 코드 스캔을 실행하면 내가 다운 스트림 프로세스가 메모리에 대한 참조를 잡을 수 있다는 것을 이해
스트림 및 매달려 있지만 기본 asp.net 동작 (예 : 무언가가 FrameRequestStream 잡고 잡을 수) 다른 모든 것을 볼 실패합니다. 다른 사람의 이익을 위해 내 자신의 질문에 대답
'RegisterForDispose'와 disabling하는 'using'블록을 모두 사용하는 것 같습니다. 그리고 다시, 'IDisposable.Dispose'는 멱등수 (idempotent)로되어 있습니다. 'RegisterForDispose'를 호출하면 – Ryan
- 스트림을 수동으로 처리하는 이유는 무엇입니까? – Evk
... Veracode를 행복하게하려고 노력하는 절망. 이전에는 '사용 중'이었습니다. 나는 그 질문을 편집 할 것이다. – NeedHack