-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix read/write directory permission checks (#151)
* Fix read/write directory permission checks * Update logic to take specific Deny rules into account which may take precedence * Create abstract base class for file system utils Co-authored-by: Kevin Sigmund <[email protected]>
- Loading branch information
Showing
4 changed files
with
52 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.AccessControl; | ||
using System.Security.Principal; | ||
|
||
namespace Microsoft.Sbom.Common | ||
{ | ||
public class WindowsFileSystemUtils : FileSystemUtils | ||
{ | ||
override public bool DirectoryHasReadPermissions(string directoryPath) => DirectoryHasRights(directoryPath, FileSystemRights.Read); | ||
|
||
override public bool DirectoryHasWritePermissions(string directoryPath) => DirectoryHasRights(directoryPath, FileSystemRights.Write); | ||
|
||
// Get the collection of authorization rules that apply to the directory | ||
private bool DirectoryHasRights(string directoryPath, FileSystemRights fileSystemRights) | ||
{ | ||
try | ||
{ | ||
WindowsIdentity current = WindowsIdentity.GetCurrent(); | ||
var directoryInfo = new DirectoryInfo(directoryPath); | ||
|
||
return HasAccessControlType(AccessControlType.Allow) && !HasAccessControlType(AccessControlType.Deny); | ||
|
||
// Check if the current user has or does not have the specified rights (either Allow or Deny) | ||
bool HasAccessControlType(AccessControlType accessControlType) | ||
{ | ||
var accessRules = directoryInfo.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier)) | ||
.Cast<FileSystemAccessRule>() | ||
.Any(rule => (current.Groups.Contains(rule.IdentityReference) || current.User.Equals(rule.IdentityReference)) | ||
&& ((fileSystemRights & rule.FileSystemRights) == fileSystemRights) | ||
&& (rule.AccessControlType == accessControlType)); | ||
return accessRules; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
// TODO Add logger with debug | ||
return false; | ||
} | ||
} | ||
} | ||
} |