- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Open
Labels
Milestone
Description
Description
Calling FileInfo.Create extension method with FileSystemRights.Read as access rights to create a FileStream.
Reproduction Steps
FileInfo fi = new(path);
FileStream stream = fi.Create(
    FileMode.Open,
    FileSystemRights.Read, // <---- Open with read-only access.
    FileShare.ReadWrite | FileShare.Delete,
    4 * KB,
    FileOptions.None,
    null);
// Check stream.CanWrite!
Expected behavior
- A read-only file handle is opened and wrapped it into a FileStream.
- The stream file access should be set to FileAccess.Read.
- The stream CanWriteproperty should read tofalse.
Actual behavior
- A read-only file handle is opened and wrapped it into a FileStream(so far so good).
- The FileStreamfile access is wrongly set internally toFileAccess.ReadWrite.
- As a result, CanWriteistruefor the stream. The underlying handle with throw an exception if we try to write.
Regression?
No response
Known Workarounds
No response
Configuration
Windows 10, .NET SDK 7.0.203, using System.IO.FileSystem.AccessControl package.
Other information
The issue is most probably within the FileSystemAclExtensions.GetFileAccessFromRights method.
Almost any value for the FileSystemRights will match the first condition, and most of them will also match the second.
See:
// rights = FileSystemRights.Read;
private static FileAccess GetFileAccessFromRights(FileSystemRights rights)
{
	FileAccess fileAccess = (FileAccess)0;
	if ((rights & FileSystemRights.FullControl) != 0 || (rights & FileSystemRights.Modify) != 0)
	{
		return FileAccess.ReadWrite; // Returns here.
	}