1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Fixed disposal of encrypted streams

This commit is contained in:
Uncled1023 2022-05-29 14:21:24 -07:00
parent 6edc67ab78
commit 7a52965300
7 changed files with 54 additions and 14 deletions

View File

@ -146,7 +146,7 @@ namespace Teknik.Areas.Paste.Controllers
if (string.IsNullOrEmpty(fileName))
return new StatusCodeResult(StatusCodes.Status404NotFound);
var storageService = StorageServiceFactory.GetStorageService(_config.PasteConfig.StorageConfig);
var fileStream = storageService.GetFile(fileName);
using var fileStream = storageService.GetFile(fileName);
if (fileStream == null)
return new StatusCodeResult(StatusCodes.Status404NotFound);
@ -317,7 +317,7 @@ namespace Teknik.Areas.Paste.Controllers
if (string.IsNullOrEmpty(paste.FileName))
return new StatusCodeResult(StatusCodes.Status404NotFound);
var storageService = StorageServiceFactory.GetStorageService(_config.PasteConfig.StorageConfig);
var fileStream = storageService.GetFile(paste.FileName);
using var fileStream = storageService.GetFile(paste.FileName);
if (fileStream == null)
return new StatusCodeResult(StatusCodes.Status404NotFound);

View File

@ -310,6 +310,8 @@ namespace Teknik.Areas.Upload.Controllers
long length = contentLength;
if (fileStream != null)
{
Response.RegisterForDispose(fileStream);
#region Range Calculation
// Are they downloading it by range?
bool byRange = !string.IsNullOrEmpty(Request.Headers["Range"]); // We do not support ranges
@ -428,6 +430,8 @@ namespace Teknik.Areas.Upload.Controllers
var fileStream = storageService.GetFile(upload.FileName);
if (fileStream != null)
{
Response.RegisterForDispose(fileStream);
// Notify the client the content length we'll be outputting
Response.Headers.Add("Content-Length", upload.ContentLength.ToString());
@ -527,6 +531,8 @@ namespace Teknik.Areas.Upload.Controllers
Response.RegisterForDispose(ivArray);
var aesStream = new AesCounterStream(fileStream, false, keyArray, ivArray);
Response.RegisterForDispose(aesStream);
//return File(aesStream, contentType, true);
return new BufferedFileStreamResult(contentType, async (response) => await ResponseHelper.StreamToOutput(response, aesStream, length, _config.UploadConfig.ChunkSize), false);
}

View File

@ -110,7 +110,7 @@ namespace Teknik.Areas.Vault.Controllers
{
// Read in the file
var storageService = StorageServiceFactory.GetStorageService(_config.PasteConfig.StorageConfig);
var fileStream = storageService.GetFile(paste.Paste.FileName);
using var fileStream = storageService.GetFile(paste.Paste.FileName);
if (fileStream == null)
continue;

View File

@ -26,7 +26,7 @@ namespace Teknik.Utilities.Cryptography
_Algo.Padding = PaddingMode.None;
// Set the internal variables
_InitialCounter = initialCounter;
_InitialCounter = new PooledArray(initialCounter);
}
public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv)
@ -51,7 +51,11 @@ namespace Teknik.Utilities.Cryptography
protected override void Dispose(bool disposed)
{
_Algo.Dispose();
if (!disposed)
{
_Algo.Dispose();
_InitialCounter.Dispose();
}
}
}
@ -229,10 +233,21 @@ namespace Teknik.Utilities.Cryptography
public void Dispose()
{
_CounterEncryptor.Dispose();
_IV.Dispose();
_Counter.Dispose();
_EncryptedCounter.Dispose();
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (disposing)
{
_CounterEncryptor.Dispose();
_IV.Dispose();
_Counter.Dispose();
_EncryptedCounter.Dispose();
}
}
}
}

View File

@ -251,8 +251,10 @@ namespace Teknik.Utilities.Cryptography
protected override void Dispose(bool disposing)
{
_Inner.Dispose();
_Cipher.Dispose();
if (disposing)
{
_Cipher.Dispose();
}
base.Dispose(disposing);
}

View File

@ -23,9 +23,16 @@ namespace Teknik.Utilities
public PooledArray(byte[] array)
{
Length = array.Length;
Array = _arrayPool.Rent(array.Length);
array.CopyTo(Array, 0);
}
public PooledArray(PooledArray array)
{
Length = array.Length;
Array = _arrayPool.Rent(array.Length);
array.CopyTo(Array);
}
public void CopyTo(byte[] destination)
@ -40,7 +47,18 @@ namespace Teknik.Utilities
public void Dispose()
{
_arrayPool.Return(Array);
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (disposing)
{
_arrayPool.Return(Array);
}
}
}
}

View File

@ -18,14 +18,13 @@ namespace Teknik.Utilities
bufferSize = length;
var pooledArray = new PooledArray(bufferSize);
response.RegisterForDispose(stream);
response.RegisterForDispose(pooledArray);
try
{
int processedBytes;
do
{
processedBytes = await stream.ReadAsync(pooledArray.Array, 0, pooledArray.Length);
processedBytes = stream.Read(pooledArray.Array, 0, pooledArray.Length);
if (processedBytes > 0)
{
await response.Body.WriteAsync(pooledArray.Array, 0, processedBytes);