Skip to content

Commit f71826b

Browse files
Copilotstephentoub
andauthored
Fix GetFileAccessFromRights bitwise logic for compound FileSystemRights flags (#122406)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 820cc63 commit f71826b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/libraries/System.IO.FileSystem.AccessControl/src/System/IO/FileSystemAclExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ private static FileAccess GetFileAccessFromRights(FileSystemRights rights)
218218
{
219219
FileAccess access = 0;
220220

221-
if ((rights & FileSystemRights.FullControl) != 0 ||
222-
(rights & FileSystemRights.Modify) != 0)
221+
if ((rights & FileSystemRights.FullControl) == FileSystemRights.FullControl ||
222+
(rights & FileSystemRights.Modify) == FileSystemRights.Modify)
223223
{
224224
return FileAccess.ReadWrite;
225225
}

src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,42 @@ public void FileInfo_Create_FileSecurity_DenyAccessRule(FileSystemRights rightsT
507507
VerifyAccessSecurity(expectedSecurity, actualSecurity);
508508
}
509509

510+
[Fact]
511+
public void FileInfo_Create_ReadRights_CanWriteIsFalse()
512+
{
513+
// Regression test: When opening a file with FileSystemRights.Read, the FileStream should have CanWrite = false
514+
using var tempRootDir = new TempAclDirectory();
515+
string path = tempRootDir.GenerateSubItemPath();
516+
var fileInfo = new FileInfo(path);
517+
518+
// First create the file
519+
using (FileStream createStream = fileInfo.Create(
520+
FileMode.CreateNew,
521+
FileSystemRights.Write,
522+
FileShare.ReadWrite | FileShare.Delete,
523+
DefaultBufferSize,
524+
FileOptions.None,
525+
null))
526+
{
527+
Assert.True(fileInfo.Exists);
528+
tempRootDir.CreatedSubfiles.Add(fileInfo);
529+
}
530+
531+
// Now open it with read-only rights
532+
using (FileStream readStream = fileInfo.Create(
533+
FileMode.Open,
534+
FileSystemRights.Read,
535+
FileShare.ReadWrite | FileShare.Delete,
536+
DefaultBufferSize,
537+
FileOptions.None,
538+
null))
539+
{
540+
// The stream should be read-only
541+
Assert.False(readStream.CanWrite, "FileStream opened with FileSystemRights.Read should have CanWrite = false");
542+
Assert.True(readStream.CanRead, "FileStream opened with FileSystemRights.Read should have CanRead = true");
543+
}
544+
}
545+
510546
#endregion
511547

512548
#region DirectorySecurity CreateDirectory

0 commit comments

Comments
 (0)