-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Added 'view' permissions for media folders. #15173
Conversation
This pull request has merge conflicts. Please resolve those before requesting a review. |
Please fix the merge conflict |
This pull request has merge conflicts. Please resolve those before requesting a review. |
Again we have a merge conflict :( |
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.
Awesome! This is a pretty old feature request: #3590. Also see #6027 (comment) for some implementation ideas.
When addressing review feedback, please adhere to the following:
- Apply code suggestions directly so the reviewer doesn't have to eyeball the changes. These resolve themselves after applying them, that's fine.
- Don't resolve other conversations so it's easier to track for the reviewer. Then, the reviewer will resolve them.
- Feel free to mark conversations that you addressed to keep track of them with an emoji or otherwise, just don't resolve them.
- Please keep conversations happening in line comment in those convos, otherwise communication will be a mess. If you have trouble finding them, see this video.
- Please click "Re-request review" in the top-right corner for each reviewer when you're ready for another round of review, so they know that you're done.
src/OrchardCore.Modules/OrchardCore.Media/Processing/MediaImageSharpConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/MediaOptionsConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/MediaOptionsConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/ViewMediaFolderAuthorizationHandler.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/ViewMediaFolderAuthorizationHandler.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/ViewMediaFolderAuthorizationHandler.cs
Outdated
Show resolved
Hide resolved
...Core.Tests/Modules/OrchardCore.Media/SecureMedia/ViewMediaFolderAuthorizationHandlerTests.cs
Show resolved
Hide resolved
...Core.Tests/Modules/OrchardCore.Media/SecureMedia/ViewMediaFolderAuthorizationHandlerTests.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/ViewMediaFolderAuthorizationHandler.cs
Outdated
Show resolved
Hide resolved
…eSharpConfiguration.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…sConfiguration.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…ns.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…ns.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…ns.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…troller.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…lderAuthorizationHandler.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…lderAuthorizationHandler.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
…lderAuthorizationHandler.cs Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
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.
Great! When this is merged, with 3 issues closing, this will be a combo breaker :). I've asked for feedback under the issues; if nothing else happens, I'll merge this in a week.
This pull request has merge conflicts. Please resolve those before requesting a review. |
Please merge the conflict and I will do a quick review, then we can merge ASAP before another merge conflict shows up |
You just need to rebuild the frontend assets, which is now a more reliable and automatically verified process: #15735. But it's still just a temporary band-aid before we have a proper development story for assets, see #15740. I'll merge this on the 25th, but not before that, waiting for possible feedback under the linked issues. This is quite a large change that many people awaited, so I want to give them a chance to look at it before merge. |
src/OrchardCore.Modules/OrchardCore.Media/Controllers/AdminController.cs
Outdated
Show resolved
Hide resolved
// Note: The ManageMediaFolder permission grants all access, so viewing must be implied by it too. | ||
public static readonly Permission ViewMedia = new("ViewMediaContent", "View media content in all folders", new[] { Permissions.ManageMediaFolder }); | ||
public static readonly Permission ViewRootMedia = new("ViewRootMediaContent", "View media content in the root folder", new[] { ViewMedia }); | ||
public static readonly Permission ViewOthersMedia = new("ViewOthersMediaContent", "View others media content", new[] { Permissions.ManageMediaFolder }); | ||
public static readonly Permission ViewOwnMedia = new("ViewOwnMediaContent", "View own media content", new[] { ViewOthersMedia }); | ||
|
||
private static readonly Permission _viewMediaTemplate = new("ViewMediaContent_{0}", "View media content in folder '{0}'", new[] { ViewMedia }); | ||
|
||
private static Dictionary<ValueTuple<string, string>, Permission> _permissionsByFolder = new(); | ||
private static readonly char[] _trimSecurePathChars = ['/', '\\', ' ']; | ||
public static readonly ReadOnlyDictionary<string, Permission> PermissionTemplates = new(new Dictionary<string, Permission>() | ||
{ | ||
{ ViewMedia.Name, _viewMediaTemplate }, | ||
}); |
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.
// Note: The ManageMediaFolder permission grants all access, so viewing must be implied by it too. | |
public static readonly Permission ViewMedia = new("ViewMediaContent", "View media content in all folders", new[] { Permissions.ManageMediaFolder }); | |
public static readonly Permission ViewRootMedia = new("ViewRootMediaContent", "View media content in the root folder", new[] { ViewMedia }); | |
public static readonly Permission ViewOthersMedia = new("ViewOthersMediaContent", "View others media content", new[] { Permissions.ManageMediaFolder }); | |
public static readonly Permission ViewOwnMedia = new("ViewOwnMediaContent", "View own media content", new[] { ViewOthersMedia }); | |
private static readonly Permission _viewMediaTemplate = new("ViewMediaContent_{0}", "View media content in folder '{0}'", new[] { ViewMedia }); | |
private static Dictionary<ValueTuple<string, string>, Permission> _permissionsByFolder = new(); | |
private static readonly char[] _trimSecurePathChars = ['/', '\\', ' ']; | |
public static readonly ReadOnlyDictionary<string, Permission> PermissionTemplates = new(new Dictionary<string, Permission>() | |
{ | |
{ ViewMedia.Name, _viewMediaTemplate }, | |
}); | |
// Note: The ManageMediaFolder permission grants all access, so viewing must be implied by it too. | |
public static readonly Permission ViewMedia = new("ViewMediaContent", "View media content in all folders", new[] { Permissions.ManageMediaFolder }); | |
public static readonly Permission ViewRootMedia = new("ViewRootMediaContent", "View media content in the root folder", new[] { ViewMedia }); | |
public static readonly Permission ViewOthersMedia = new("ViewOthersMediaContent", "View others media content", new[] { Permissions.ManageMediaFolder }); | |
public static readonly Permission ViewOwnMedia = new("ViewOwnMediaContent", "View own media content", new[] { ViewOthersMedia }); | |
public static readonly ReadOnlyDictionary<string, Permission> PermissionTemplates = new(new Dictionary<string, Permission>() | |
{ | |
{ ViewMedia.Name, _viewMediaTemplate }, | |
}); | |
private static readonly Permission _viewMediaTemplate = new("ViewMediaContent_{0}", "View media content in folder '{0}'", new[] { ViewMedia }); | |
private static readonly char[] _trimSecurePathChars = ['/', '\\', ' ']; | |
private static Dictionary<ValueTuple<string, string>, Permission> _permissionsByFolder = new(); |
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.
Not possible due to initialization order...
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 will check
src/OrchardCore.Modules/OrchardCore.Media/SecureMediaPermissions.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/AttachedMediaFieldFileService.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/MediaOptionsConfiguration.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/Services/ViewMediaFolderAuthorizationHandler.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/ViewModels/MediaApplicationViewModel.cs
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Media/ViewModels/MediaFolderViewModel.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Media.Core/Events/MediaEventHandlerBase.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
public static readonly Permission ViewOthersMedia = new("ViewOthersMediaContent", "View others media content", new[] { Permissions.ManageMediaFolder }); | ||
public static readonly Permission ViewOwnMedia = new("ViewOwnMediaContent", "View own media content", new[] { ViewOthersMedia }); | ||
|
||
private static readonly Permission _viewMediaTemplate = new("ViewMediaContent_{0}", "View media content in folder '{0}'", new[] { ViewMedia }); |
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 think you need to make this public like the others, that's why the order is matter :)
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 don't think so. One can argue that PermissionTemplates
can be private though...
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.
Agree if you are not using it as permission, but you declare it as Permission
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.
Really everything should be private except what you need to do authorization calls outside.
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 changed the visibility, but note that this may have to be reversed in future. Some other code may want to reuse the dynamic permissions to check a requirement of a folder for a different purpose.
…rBase.cs Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
@coderabbitai review |
WalkthroughThis update introduces the "Secure Media" feature in OrchardCore, enhancing security and access control over media files. Changes include new configurations for caching secure files, permissions for media access, and event handling for media directory operations. The update also refines UI elements and backend services to support these security enhancements, ensuring that media files are handled securely based on user roles and permissions. Changes
This table summarizes the key changes across the OrchardCore project, focusing on enhancing media file security and access control. Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
@coderabbitai review |
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.
Actionable comments posted: 1
Out of diff range and nitpick comments (1)
src/docs/reference/modules/Media/README.md (1)
Line range hint
1-1
: Add a space after the period in the first sentence for better readability.
...Core.Tests/Modules/OrchardCore.Media/SecureMedia/ViewMediaFolderAuthorizationHandlerTests.cs
Show resolved
Hide resolved
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.
Final approval and merge up to you, @hishamco.
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.
Just a quick comment, then we can merge
src/OrchardCore.Modules/OrchardCore.Media/Controllers/AdminController.cs
Show resolved
Hide resolved
Thanks for your contribution @gvkries |
This pull request adds a "Secure Media" feature to the Media module, enhancing security and control over media files. The key features include:
Restricted Access to Media Folders: A view permission is created for the root media folder and for each first-level folder within the media root, allowing administrators to restrict access based on user roles.
Enhanced Viewing Permissions: Introduces permissions to view one's own media files and/or those of others, expanding upon the existing
ManageOwnMedia
permission.Consistent Access Rules for Media and Content Items: Media attached to content items will adhere to the
ViewContent
permission of the respective content item. This alignment ensures consistent access rules between media and content items.Protection for Temporary Attached Media Files: Secures temporary attached media files in a manner similar to personal user files.
Improved Management Permissions in Admin: Refines the
manage media
permissions to allow media management only when viewing permissions are also granted. This prevents users from managing media they cannot view. Additionally, the creation and deletion buttons in the admin interface are disabled for folders that are not accessible post-creation or for special folders like "_Users" and "mediafields".Handling Unauthorized Access: Introduces a middleware that returns a
404 NotFound
response for unauthenticated access attempts to secured media files. This approach not only restricts access but also conceals the existence of the file.Configurable Cache-Control for Secured Files: Sets the
Cache-Control
header of secured files tono-store
by default, preventing their caching. This setting is configurable to suit different needs.Bearer Token Authentication for API Access: Enables bearer token authentication for media files, aligning with OrchardCore's API capabilities. This feature is particularly useful for headless CMS scenarios and external application integrations.
This implementation addresses most ideas outlined in issue #9369. It focuses on introducing simple view permissions for media files, avoiding an excessive number of permissions in the interface. This approach also resolves issues like #12057 by enabling folder access restrictions for users without viewing permissions.
Fixes #3590, fixes #9369, fixes #12057.