-
-
Notifications
You must be signed in to change notification settings - Fork 104
Add Authorization Utilitites #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ Coverage Diff @@
## main #281 +/- ##
====================================
Coverage 84% 85%
====================================
Files 75 76 +1
Lines 2047 2169 +122
Branches 299 303 +4
====================================
+ Hits 1739 1857 +118
- Misses 223 227 +4
Partials 85 85
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
ronaldbarendse
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be a great addition and will make (correct) use of the HMAC image processing protection a lot easier! I've added some comments/suggestions and would love to test this when it's ready for review 👍🏻
|
@ronaldbarendse Good feedback, thanks! I've pushed all the fixes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've successfully tested this PR together with a massively simplified version of umbraco/Umbraco-CMS#12376 (replacing the custom ImageSharpImageUrlTokenGenerator abstraction/implementation with ImageSharpRequestAuthorizationUtilities) 🙌🏻
I've only added some final comments about backwards compatibility and naming, but looks good to me otherwise!
| FormatUtilities formatUtilities, | ||
| AsyncKeyReaderWriterLock<string> asyncKeyLock) | ||
| AsyncKeyReaderWriterLock<string> asyncKeyLock, | ||
| ImageSharpRequestAuthorizationUtilities requestAuthorizationUtilities) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this parameter is a (binary) breaking change which can be avoided by re-adding the previous constructor as obsolete and manually constructing the ImageSharpRequestAuthorizationUtilities:
[Obsolete]
public ImageSharpMiddleware(
RequestDelegate next,
IOptions<ImageSharpMiddlewareOptions> options,
ILoggerFactory loggerFactory,
IRequestParser requestParser,
IEnumerable<IImageProvider> resolvers,
IEnumerable<IImageWebProcessor> processors,
IImageCache cache,
ICacheKey cacheKey,
ICacheHash cacheHash,
CommandParser commandParser,
FormatUtilities formatUtilities,
AsyncKeyReaderWriterLock<string> asyncKeyLock)
: this(
next,
options,
loggerFactory,
requestParser,
resolvers,
processors,
cache,
cacheKey,
cacheHash,
commandParser,
formatUtilities,
asyncKeyLock,
new ImageSharpRequestAuthorizationUtilities(options, requestParser, processors, commandParser, null))
{
}This does require ImageSharpRequestAuthorizationUtilities to allow a null value for IServiceProvider, but that shouldn't be an issue, as it isn't required/used by the methods this middleware calls into anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it’s binary breaking but to upgrade you’re gonna have to recompile anyway. Obsolete isn’t actually a workaround due to consumer rulesets which can be violated using the attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While looking into this a bit more, I noticed ImageSharpRequestAuthorizationUtilities is currently only really required to strip unknown commands, as ComputeHMACAsync(ImageCommandContext) just delegates to this.options.OnComputeHMACAsync(ImageCommandContext, byte[]). I also think that stripping unknown commands isn't a request authorization concern, so it's probably best to remove this dependency on ImageSharpRequestAuthorizationUtilities altogether...
| this.logger = loggerFactory.CreateLogger<ImageSharpMiddleware>(); | ||
| this.formatUtilities = formatUtilities; | ||
| this.asyncKeyLock = asyncKeyLock; | ||
| this.authorizationUtilities = requestAuthorizationUtilities; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the field also be named requestAuthorizationUtilities?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, that’s just ocd
| /// <summary> | ||
| /// Contains various helper methods for authorizing image requests. | ||
| /// </summary> | ||
| public sealed class ImageSharpRequestAuthorizationUtilities |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be renamed to RequestAuthorizationUtilities, similar to FormatUtilities, HMACUtilities, etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to make it more explicit. It’s for ImageSharp requests only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but it's already in the SixLabors.ImageSharp.Web namespace and consumers can otherwise alias the class name if it would conflict with something else:
using ImageSharpRequestAuthorizationUtilities = SixLabors.ImageSharp.Web.RequestAuthorizationUtilities;And after mentioning that, I acknowledge that you can also use that to do the reverse 😓 So it's more to align with the naming used by all the other utility classes...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming is about about consumption intent. All the other types are designed to be used conceptionally within other ImageSharp.Web type implementations (processors generally) whereas this type is designed to be consumed from the users code only. It has to be explicit and unfortunately verbose.
|
I've done some further testing, investigation and refactoring to:
You can see all my changes here: js/auth-utils...ronaldbarendse:ImageSharp.Web:js/auth-utils. I can convert this into a PR targeting |
|
Thanks for having a crack but I think you missed the point of this new type when making some changes. We absolutely want to be using the new type from the middleware. It ensures the same type process is used on both sides. Moving operations to various types ( Tests should be cleaned up (I'll copy some of the changes you made across), most definitely but the other random changes (logging, field field order) are unnecessary. Code shouldn't be changed for changes sake. We also never, ever use My changes are only binary breaking if someone is directly calling the middleware constructor which they simply should not be doing. I'll be versioning this release 2.1.0 anyway to send a signal that more fundamental changes have taken place. |
|
This is probably good to merge now. |
|
@JimBobSquarePants Can this also be released in a 2.0.3 patch version? I'd love to finish the HMAC authorization support in Umbraco (see PR umbraco/Umbraco-CMS#12376) 😇 |
|
@ronaldbarendse I'm actually hoping to release a 2.1.0 build very soon. I've been working on a TagHelper which is allowing me to dogfood the utilites a bit better before releasing. |
|
I see the work on the |
|
Let me see what Ican do. No promises though. I've got a lot on just now. |
Prerequisites
Description
Adds a new type
ImageSharpRequestAuthorizationUtilitieswhich completes the HMAC story, allowing developers to create tokens directly using the functions defined in the middleware options.