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

View File

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

View File

@ -26,7 +26,7 @@ namespace Teknik.Utilities.Cryptography
_Algo.Padding = PaddingMode.None; _Algo.Padding = PaddingMode.None;
// Set the internal variables // Set the internal variables
_InitialCounter = initialCounter; _InitialCounter = new PooledArray(initialCounter);
} }
public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv) public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv)
@ -51,7 +51,11 @@ namespace Teknik.Utilities.Cryptography
protected override void Dispose(bool disposed) 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() public void Dispose()
{ {
_CounterEncryptor.Dispose(); Dispose(true);
_IV.Dispose();
_Counter.Dispose(); // Suppress finalization.
_EncryptedCounter.Dispose(); 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) protected override void Dispose(bool disposing)
{ {
_Inner.Dispose(); if (disposing)
_Cipher.Dispose(); {
_Cipher.Dispose();
}
base.Dispose(disposing); base.Dispose(disposing);
} }

View File

@ -23,9 +23,16 @@ namespace Teknik.Utilities
public PooledArray(byte[] array) public PooledArray(byte[] array)
{ {
Length = array.Length;
Array = _arrayPool.Rent(array.Length); Array = _arrayPool.Rent(array.Length);
array.CopyTo(Array, 0); array.CopyTo(Array, 0);
}
public PooledArray(PooledArray array)
{
Length = array.Length; Length = array.Length;
Array = _arrayPool.Rent(array.Length);
array.CopyTo(Array);
} }
public void CopyTo(byte[] destination) public void CopyTo(byte[] destination)
@ -40,7 +47,18 @@ namespace Teknik.Utilities
public void Dispose() 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; bufferSize = length;
var pooledArray = new PooledArray(bufferSize); var pooledArray = new PooledArray(bufferSize);
response.RegisterForDispose(stream);
response.RegisterForDispose(pooledArray); response.RegisterForDispose(pooledArray);
try try
{ {
int processedBytes; int processedBytes;
do do
{ {
processedBytes = await stream.ReadAsync(pooledArray.Array, 0, pooledArray.Length); processedBytes = stream.Read(pooledArray.Array, 0, pooledArray.Length);
if (processedBytes > 0) if (processedBytes > 0)
{ {
await response.Body.WriteAsync(pooledArray.Array, 0, processedBytes); await response.Body.WriteAsync(pooledArray.Array, 0, processedBytes);