Fix GetFileAccessFromRights bitwise logic for compound FileSystemRights flags#122406
Merged
Conversation
…Access Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where FileInfo.Create() with FileSystemRights.Read incorrectly returned a FileStream with CanWrite = true. The root cause was faulty bitwise logic in GetFileAccessFromRights that checked for ANY overlapping bits instead of checking that ALL bits of compound flags like FullControl and Modify were present.
Key changes:
- Fixed bitwise comparison from
(rights & flag) != 0to(rights & flag) == flagfor compoundFileSystemRightsflags (FullControlandModify) - Added regression test validating that
FileStream.CanWriteis false when opened withFileSystemRights.Read
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.IO.FileSystem.AccessControl/src/System/IO/FileSystemAclExtensions.cs | Corrected bitwise logic in GetFileAccessFromRights to check for exact match of compound flags instead of partial overlap |
| src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs | Added regression test validating FileStream properties when opened with FileSystemRights.Read |
stephentoub
approved these changes
Dec 12, 2025
jozkee
approved these changes
Jan 13, 2026
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FileInfo.Create()withFileSystemRights.Readincorrectly returned aFileStreamwithCanWrite = truebecause the underlyingGetFileAccessFromRightsmethod used faulty bitwise logic to check forFullControlandModifyflags.Root Cause
The method checked if ANY bits overlapped:
Since
FileSystemRights.Read(0x20089) shares bits withFileSystemRights.FullControl(0x1F01FF), this incorrectly mappedRead→ReadWrite.Changes
Fixed bitwise comparison to check if ALL bits are present:
Added regression test validating that
FileStream.CanWriteis false when opened withFileSystemRights.ReadImpact
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.