Skip to content

Commit 9e7c001

Browse files
committed
bug fix in HmacAuthenticationHandler
1 parent 2db749b commit 9e7c001

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ A HMAC (Hash-based Message Authentication Code) authentication system for ASP.NE
66
[![License](https://img.shields.io/github/license/loresoft/HashGate.svg)](https://github.com/loresoft/HashGate/blob/main/LICENSE)
77
[![Coverage Status](https://coveralls.io/repos/github/loresoft/HashGate/badge.svg?branch=main)](https://coveralls.io/github/loresoft/HashGate?branch=main)
88

9-
109
| Package | Version | Description |
1110
| -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
1211
| [HashGate.AspNetCore](https://www.nuget.org/packages/HashGate.AspNetCore/) | [![NuGet](https://img.shields.io/nuget/v/HashGate.AspNetCore.svg)](https://www.nuget.org/packages/HashGate.AspNetCore/) | Server-side HMAC authentication for ASP.NET Core applications |

src/HashGate.AspNetCore/HmacAuthenticationHandler.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,21 @@ private async Task<string> GenerateContentHash()
139139
// Ensure the request body can be read multiple times
140140
Request.EnableBuffering();
141141

142+
// Return empty content hash if there is no body
143+
if (Request.ContentLength == 0 || Request.Body == Stream.Null)
144+
return HmacAuthenticationShared.EmptyContentHash;
142145

143146
await using var memoryStream = new MemoryStream();
144-
await Request.Body.CopyToAsync(memoryStream).ConfigureAwait(false);
147+
await Request.BodyReader.CopyToAsync(memoryStream).ConfigureAwait(false);
145148

146149
// Reset position after reading
147150
Request.Body.Position = 0;
148151

149-
var hashBytes = SHA256.HashData(memoryStream.ToArray());
152+
// If the body is empty after reading, return empty content hash
153+
if (memoryStream.Length == 0)
154+
return HmacAuthenticationShared.EmptyContentHash;
150155

151-
var json = Encoding.UTF8.GetString(memoryStream.ToArray());
156+
var hashBytes = SHA256.HashData(memoryStream.ToArray());
152157

153158
// 32 bytes SHA256 -> 44 chars base64
154159
Span<char> base64 = stackalloc char[44];

0 commit comments

Comments
 (0)