Skip to content

Commit b86ae56

Browse files
Merge pull request #265 from SixLabors/js/fix-hmac-cache-token
Use full URl as HMAC cache key
2 parents 80d1578 + 414abfe commit b86ae56

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private async Task Invoke(HttpContext httpContext, bool retry)
246246

247247
ImageCommandContext imageCommandContext = new(httpContext, commands, this.commandParser, this.parserCulture);
248248

249-
// At this point we know that this is an image request so should attempt to compute a validating HMAC..
249+
// At this point we know that this is an image request so should attempt to compute a validating HMAC.
250250
string hmac = null;
251251
if (checkHMAC && token != null)
252252
{
@@ -256,7 +256,8 @@ private async Task Invoke(HttpContext httpContext, bool retry)
256256
// the token will not match our validating HMAC, however, this would be indicative of an attack and should be treated as such.
257257
//
258258
// As a rule all image requests should contain valid commands only.
259-
hmac = await HMACTokenLru.GetOrAddAsync(token, _ => this.options.OnComputeHMACAsync(imageCommandContext, secret));
259+
// Key generation uses string.Create under the hood with very low allocation so should be good enough as a cache key.
260+
hmac = await HMACTokenLru.GetOrAddAsync(httpContext.Request.GetEncodedUrl(), _ => this.options.OnComputeHMACAsync(imageCommandContext, secret));
260261
}
261262

262263
await this.options.OnParseCommandsAsync.Invoke(imageCommandContext);

0 commit comments

Comments
 (0)