Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a098770

Browse files
MarcoRossignoliPaulo Janotti
authored andcommitted
"Don't directly throw Exception" System.IO.FileSystem.AccessControl (#25696)
* Fix throw new Exception(); * Fix throw new Exception() * Fix throw new Exception(); * Fix throw new Exception(); * Fix throw new Exception(); * Fix throw new Exception(); * Fix throw new Exception(); * Don't directly throw Exception * test RemoveAccessRuleAll_AccessControlType_Deny_ThrowException and throw InvalidOperationException * change exception message * add more descriptive exception message
1 parent 328fdcc commit a098770

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/System.IO.FileSystem.AccessControl/src/Resources/Strings.resx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,10 @@
138138
<data name="PlatformNotSupported_AccessControl" xml:space="preserve">
139139
<value>Access Control List (ACL) APIs are part of resource management on Windows and are not supported on this platform.</value>
140140
</data>
141-
</root>
141+
<data name="TypeUnrecognized_AccessControl" xml:space="preserve">
142+
<value>AccessControlType '{0}' unrecognized</value>
143+
</data>
144+
<data name="InvalidOperation_RemoveFail" xml:space="preserve">
145+
<value>Remove access rule failed</value>
146+
</data>
147+
</root>

src/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/DirectoryObjectSecurity.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,8 @@ private bool ModifyAccess(AccessControlModification modification, ObjectAccessRu
347347
case AccessControlModification.RemoveAll:
348348
result = _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Allow, sid, -1, InheritanceFlags.ContainerInherit, 0, ObjectAceFlags.None, Guid.Empty, Guid.Empty);
349349
if (result == false)
350-
{
351-
Debug.Assert(false, "Invalid operation");
352-
throw new Exception();
350+
{
351+
throw new InvalidOperationException(SR.InvalidOperation_RemoveFail);
353352
}
354353

355354
break;
@@ -393,9 +392,8 @@ private bool ModifyAccess(AccessControlModification modification, ObjectAccessRu
393392
case AccessControlModification.RemoveAll:
394393
result = _securityDescriptor.DiscretionaryAcl.RemoveAccess(AccessControlType.Deny, sid, -1, InheritanceFlags.ContainerInherit, 0, ObjectAceFlags.None, Guid.Empty, Guid.Empty);
395394
if (result == false)
396-
{
397-
Debug.Assert(false, "Invalid operation");
398-
throw new Exception();
395+
{
396+
throw new InvalidOperationException(SR.InvalidOperation_RemoveFail);
399397
}
400398

401399
break;
@@ -412,9 +410,8 @@ private bool ModifyAccess(AccessControlModification modification, ObjectAccessRu
412410
}
413411
}
414412
else
415-
{
416-
Debug.Assert(false, "rule.AccessControlType unrecognized");
417-
throw new Exception();
413+
{
414+
throw new ArgumentException(SR.Format(SR.TypeUnrecognized_AccessControl, rule.AccessControlType));
418415
}
419416

420417
modified = result;
@@ -488,9 +485,8 @@ private bool ModifyAudit(AccessControlModification modification, ObjectAuditRule
488485
case AccessControlModification.RemoveAll:
489486
result = _securityDescriptor.SystemAcl.RemoveAudit(AuditFlags.Failure | AuditFlags.Success, sid, -1, InheritanceFlags.ContainerInherit, 0, ObjectAceFlags.None, Guid.Empty, Guid.Empty);
490487
if (result == false)
491-
{
492-
Debug.Assert(false, "Invalid operation");
493-
throw new Exception();
488+
{
489+
throw new InvalidOperationException(SR.InvalidOperation_RemoveFail);
494490
}
495491

496492
break;

src/System.IO.FileSystem.AccessControl/tests/DirectoryObjectSecurityTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,23 @@ public void RemoveAccessRuleAll_AccessControlType_Allow_Succeeds()
493493
Assert.False(existingRules.Contains(customAccessRuleSynchronize));
494494
}
495495

496+
[Fact]
497+
public void RemoveAccessRuleAll_AccessControlType_Deny_ThrowException()
498+
{
499+
var descriptor = new CommonSecurityDescriptor(true, true, string.Empty);
500+
var customObjectSecurity = new CustomDirectoryObjectSecurity(descriptor);
501+
502+
var objectTypeGuid = Guid.NewGuid();
503+
var identityReference = new NTAccount(@"NT AUTHORITY\SYSTEM");
504+
var customAccessRuleReadWrite = new CustomAccessRule(
505+
identityReference, ReadWriteAccessMask, true, InheritanceFlags.ObjectInherit,
506+
PropagationFlags.InheritOnly, objectTypeGuid, Guid.NewGuid(), AccessControlType.Deny
507+
);
508+
509+
customObjectSecurity.AddAccessRule(customAccessRuleReadWrite);
510+
AssertExtensions.Throws<InvalidOperationException, SystemException>(() => customObjectSecurity.RemoveAccessRuleAll(customAccessRuleReadWrite));
511+
}
512+
496513
[Fact]
497514
public void RemoveAccessRuleAll_AccessControlType_Deny_Succeeds()
498515
{

0 commit comments

Comments
 (0)