Skip to content
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

[BUG] FileInfo.Create wraps file handle with wrong FileSystemRights. #111540

Open
mikelatcs opened this issue Jan 17, 2025 · 1 comment
Open

[BUG] FileInfo.Create wraps file handle with wrong FileSystemRights. #111540

mikelatcs opened this issue Jan 17, 2025 · 1 comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@mikelatcs
Copy link

mikelatcs commented Jan 17, 2025

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 CanWrite property should read to false.

Actual behavior

  • A read-only file handle is opened and wrapped it into a FileStream (so far so good).
  • The FileStream file access is wrongly set internally to FileAccess.ReadWrite.
  • As a result, CanWrite is true for 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.
	}
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 17, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

1 participant