2017-11-13 11 views
2

このコードで何が問題になっていますか?さらに、私はそれをどのように修正するのですか?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不適切なリソースのシャットダウンまたはリリースを与える上で、私はのVeraCodeスキャンを実行すると、私は下流のプロセスがメモリへの参照をつかむことができることを理解し

ストリームとそれにぶら下がって、どのようにデフォルトのasp.netの動作(つまり、何かがFrameRequestStreamをつかむことができる)とは異なることを確認することができません。 ...

を他人の利益のために自分の質問に答える

+0

'RegisterForDispose'と' using'ブロックを使い捨てにしているようです。そして再び、 'IDisposable.Dispose'は冪等であると考えられます。 'RegisterForDispose'を呼び出す場合は – Ryan

+0

- ストリームを手動で廃棄するのはなぜですか? – Evk

+0

... Veracodeを幸せにしようとする絶望。以前は「使用している」だけでした。私は質問を編集します。 – NeedHack

答えて

1

は、私は別の方法で再読み込み可能な要求ストリームを作ることによって、この問題を回避/修正することができました。これはVeracodeを幸せに保ちますが、前のコードに何か間違いがあると私は確信していません。ヤー起動クラスで

スティックは、この...

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    ... 
    app.Use(async (context, next) => { 
     context.Request.EnableRewind(); 
     await next(); 
    } 
    ... 
}); 

私たちがしなければ、我々は次のスコットHanselman氏、増加となりますので、我々はすべてのブログに言われているので、私は、ここではこれについてhttps://needhack.wordpress.com/2018/01/17/make-the-request-stream-re-readable-in-asp-net-core-with-c/をブログに書いています私たちの料金は、毎日の通勤をあきらめますか?

+0

このNeedHackをお寄せいただきありがとうございますが、これは私が探していたものではありません。私のコードのMemoryStreamがこのCWE 404エラーを引き起こしているようです。私はそれが誰かを助けると確信していますが、投稿していただきありがとうございます。 – VinnyGuitara

関連する問題