Browse Source

Fixed disposal of encrypted streams

master
Teknikode 1 month ago
parent
commit
7a52965300
  1. 4
      Teknik/Areas/Paste/Controllers/PasteController.cs
  2. 6
      Teknik/Areas/Upload/Controllers/UploadController.cs
  3. 2
      Teknik/Areas/Vault/Controllers/VaultController.cs
  4. 27
      Utilities/Cryptography/AesCounterMode.cs
  5. 6
      Utilities/Cryptography/AesCounterStream.cs
  6. 20
      Utilities/PooledArray.cs
  7. 3
      Utilities/ResponseHelper.cs

4
Teknik/Areas/Paste/Controllers/PasteController.cs

@ -146,7 +146,7 @@ namespace Teknik.Areas.Paste.Controllers @@ -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 @@ -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);

6
Teknik/Areas/Upload/Controllers/UploadController.cs

@ -310,6 +310,8 @@ namespace Teknik.Areas.Upload.Controllers @@ -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 @@ -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 @@ -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);
}

2
Teknik/Areas/Vault/Controllers/VaultController.cs

@ -110,7 +110,7 @@ namespace Teknik.Areas.Vault.Controllers @@ -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;

27
Utilities/Cryptography/AesCounterMode.cs

@ -26,7 +26,7 @@ namespace Teknik.Utilities.Cryptography @@ -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 @@ -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 @@ -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();
}
}
}
}

6
Utilities/Cryptography/AesCounterStream.cs

@ -251,8 +251,10 @@ namespace Teknik.Utilities.Cryptography @@ -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);
}

20
Utilities/PooledArray.cs

@ -23,9 +23,16 @@ namespace Teknik.Utilities @@ -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 @@ -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);
}
}
}
}

3
Utilities/ResponseHelper.cs

@ -18,14 +18,13 @@ namespace Teknik.Utilities @@ -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);

Loading…
Cancel
Save