Skip to content

Commit

Permalink
Create exception type for capacity exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
CrspyAu committed Dec 19, 2024
1 parent 88d4a56 commit 8cf59d9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
4 changes: 1 addition & 3 deletions UnitTests/SecurityVulnerabilitiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public void DeflateBomb()
string bomb = Jose.JWT.Encode(payload, publicKey, JweAlgorithm.RSA_OAEP, JweEncryption.A256GCM, JweCompression.DEF);

// when
Exception thrownException = Assert.Throws<JoseException>(() => Jose.JWT.Decode(bomb, privateKey));
Assert.IsType<NotSupportedException>(thrownException.InnerException);
Exception thrownException = Assert.Throws<CapacityExceededException>(() => Jose.JWT.Decode(bomb, privateKey));
}

[Fact]
Expand All @@ -219,6 +218,5 @@ public void TruncatedGcmAuthTag()
Console.Out.WriteLine(e.ToString());
}
}

}
}
12 changes: 8 additions & 4 deletions jose-jwt/JWT.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Jose;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jose
Expand Down Expand Up @@ -499,7 +497,7 @@ public static byte[] DecryptBytes(string token, object key, JweAlgorithm? alg =

private static byte[] DecodeBytes(Compact.Iterator parts, object key = null, JwsAlgorithm? expectedJwsAlg = null, JweAlgorithm? expectedJweAlg = null, JweEncryption? expectedJweEnc = null, JwtSettings settings = null, byte[] payload = null)
{
Ensure.IsNotEmpty(parts.Token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null.");
Ensure.IsNotEmpty(parts.Token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null.");

if (parts.Count == 5) //encrypted JWT
{
Expand All @@ -512,7 +510,7 @@ private static byte[] DecodeBytes(Compact.Iterator parts, object key = null, Jws
}
else if (parts.Count == 3) // signed JWT
{
if (expectedJweAlg != null || expectedJweEnc !=null)
if (expectedJweAlg != null || expectedJweEnc != null)
{
throw new InvalidAlgorithmException("Signed tokens can't assert encryption type.");
}
Expand Down Expand Up @@ -610,4 +608,10 @@ public class InvalidAlgorithmException : JoseException
public InvalidAlgorithmException(string message) : base(message) { }
public InvalidAlgorithmException(string message, Exception innerException) : base(message, innerException) { }
}

public class CapacityExceededException : JoseException
{
public CapacityExceededException(string message) : base(message) { }
public CapacityExceededException(string message, Exception innerException) : base(message, innerException) { }
}
}
4 changes: 2 additions & 2 deletions jose-jwt/compression/CappedMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override void Write(byte[] buffer, int offset, int count)
{
if (Length + Math.Min(count, buffer.Length - offset) > maxCapacity)
{
throw new NotSupportedException("Exceeding maximum memory stream size.");
throw new CapacityExceededException("Exceeding maximum memory stream size.");
}

base.Write(buffer, offset, count);
Expand All @@ -26,7 +26,7 @@ public override void WriteByte(byte value)
{
if (Length + 1 > maxCapacity)
{
throw new NotSupportedException("Exceeding maximum memory stream size.");
throw new CapacityExceededException("Exceeding maximum memory stream size.");
}

base.WriteByte(value);
Expand Down
28 changes: 10 additions & 18 deletions jose-jwt/compression/DeflateCompression.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.IO;
using System.IO.Compression;

Expand Down Expand Up @@ -27,26 +26,19 @@ public byte[] Compress(byte[] plainText)
}

public byte[] Decompress(byte[] compressedText)
{
try
{
using (MemoryStream ms = new CappedMemoryStream(maxBufferSizeBytes))
{
using (MemoryStream ms = new CappedMemoryStream(maxBufferSizeBytes))
using (MemoryStream compressedStream = new MemoryStream(compressedText))
{
using (MemoryStream compressedStream = new MemoryStream(compressedText))
using (DeflateStream deflater = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
using (DeflateStream deflater = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
deflater.CopyTo(ms);
}
}

return ms.ToArray();
}
}
catch (NotSupportedException e)
{
throw new JoseException("Unable to deflate compressed payload, most likely exceeded decompression buffer size.", e);
}
deflater.CopyTo(ms);
}
}

return ms.ToArray();
}
}
}
}

0 comments on commit 8cf59d9

Please sign in to comment.