-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add deny access functionality #34
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
298e378
Added logic to stop JsonPatch from using accessing specific properties
AnderssonPeter 7b2ff47
Improved JsonPatchAccessDeniedException
AnderssonPeter a00db29
Added documentation for DenyPatch
AnderssonPeter 2e4bb61
Added editorconfig file and ensure that we use tabs instead of spaces
AnderssonPeter 10fe7ee
Added unit tests
AnderssonPeter cdcdb56
Change from spaces to tabs in PropertyProxyCache
AnderssonPeter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[*.cs] | ||
indent_style = tab |
This file contains 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
94 changes: 94 additions & 0 deletions
94
SystemTextJsonPatch.Tests/IntegrationTests/DenyIntegrationTest.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Text.Json; | ||
using SystemTextJsonPatch.Exceptions; | ||
using Xunit; | ||
|
||
namespace SystemTextJsonPatch.IntegrationTests; | ||
|
||
public class DenyIntegrationTest | ||
{ | ||
[Fact] | ||
public void TestInList() | ||
{ | ||
// Arrange | ||
var targetObject = new SimpleObjectWithNestedObjectAndDeny() | ||
{ | ||
SimpleObject = new SimpleObject() | ||
{ | ||
IntegerList = new List<int>() { 1, 2, 3 } | ||
} | ||
}; | ||
|
||
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObjectAndDeny>(); | ||
patchDocument.Test(o => o.SimpleObject.IntegerList, 3, 2); | ||
|
||
// Act & Assert | ||
var exception = Assert.Throws<JsonPatchAccessDeniedException>(() => patchDocument.ApplyTo(targetObject)); | ||
Assert.Equal(nameof(SimpleObjectWithNestedObjectAndDeny.SimpleObject), exception.Property); | ||
Assert.Equal(nameof(SimpleObjectWithNestedObjectAndDeny), exception.Type); | ||
} | ||
|
||
[Fact] | ||
public void AddToComplextTypeListSpecifyIndex() | ||
{ | ||
// Arrange | ||
var targetObject = new SimpleObjectWithNestedObjectAndDeny() | ||
{ | ||
SimpleObjectList = new List<SimpleObject>() | ||
{ | ||
new SimpleObject | ||
{ | ||
StringProperty = "String1" | ||
}, | ||
new SimpleObject | ||
{ | ||
StringProperty = "String2" | ||
} | ||
} | ||
}; | ||
|
||
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObjectAndDeny>(); | ||
patchDocument.Add(o => o.SimpleObjectList[0].StringProperty, "ChangedString1"); | ||
|
||
// Act & Assert | ||
var exception = Assert.Throws<JsonPatchAccessDeniedException>(() => patchDocument.ApplyTo(targetObject)); | ||
Assert.Equal(nameof(SimpleObjectWithNestedObjectAndDeny.SimpleObjectList), exception.Property); | ||
Assert.Equal(nameof(SimpleObjectWithNestedObjectAndDeny), exception.Type); | ||
} | ||
|
||
[Fact] | ||
public void RemoveFromList() | ||
{ | ||
// Arrange | ||
var targetObject = new SimpleObjectWithDeny() | ||
{ | ||
IntegerList = new List<int>() { 1, 2, 3 } | ||
}; | ||
|
||
var patchDocument = new JsonPatchDocument(); | ||
patchDocument.Remove("IntegerList/2"); | ||
|
||
// Act & Assert | ||
var exception = Assert.Throws<JsonPatchAccessDeniedException>(() => patchDocument.ApplyTo(targetObject)); | ||
Assert.Equal(nameof(SimpleObjectWithDeny.IntegerList), exception.Property); | ||
Assert.Equal(nameof(SimpleObjectWithDeny), exception.Type); | ||
} | ||
|
||
|
||
[Fact] | ||
public void InnerObjectDeny() | ||
{ | ||
// Arrange | ||
var targetObject = new SimpleObjectWithNestedObjectAndDeny(); | ||
|
||
var patchDocument = new JsonPatchDocument(); | ||
patchDocument.Add("Allow/IntegerValue", 1); | ||
|
||
// Act & Assert | ||
var exception = Assert.Throws<JsonPatchAccessDeniedException>(() => patchDocument.ApplyTo(targetObject)); | ||
Assert.Equal(nameof(SimpleObjectWithDeny.IntegerValue), exception.Property); | ||
Assert.Equal(nameof(SimpleObjectWithDeny), exception.Type); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
SystemTextJsonPatch.Tests/TestObjectModels/SimpleObjectWithDeny.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SystemTextJsonPatch; | ||
|
||
public class SimpleObjectWithDeny | ||
{ | ||
[DenyPatch] | ||
public List<int> IntegerList { get; set; } | ||
[DenyPatch] | ||
public IList<int> IntegerIList { get; set; } | ||
[DenyPatch] | ||
public int IntegerValue { get; set; } | ||
[DenyPatch] | ||
public int AnotherIntegerValue { get; set; } | ||
[DenyPatch] | ||
public string StringProperty { get; set; } | ||
[DenyPatch] | ||
public string AnotherStringProperty { get; set; } | ||
[DenyPatch] | ||
public decimal DecimalValue { get; set; } | ||
[DenyPatch] | ||
public double DoubleValue { get; set; } | ||
[DenyPatch] | ||
public float FloatValue { get; set; } | ||
[DenyPatch] | ||
public Guid GuidValue { get; set; } | ||
} |
28 changes: 28 additions & 0 deletions
28
SystemTextJsonPatch.Tests/TestObjectModels/SimpleObjectWithNestedObjectAndDeny.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace SystemTextJsonPatch; | ||
|
||
public class SimpleObjectWithNestedObjectAndDeny | ||
{ | ||
public int IntegerValue { get; set; } | ||
[DenyPatch] | ||
public NestedObject NestedObject { get; set; } | ||
[DenyPatch] | ||
public SimpleObject SimpleObject { get; set; } | ||
[DenyPatch] | ||
public InheritedObject InheritedObject { get; set; } | ||
[DenyPatch] | ||
public List<SimpleObject> SimpleObjectList { get; set; } | ||
[DenyPatch] | ||
public IList<SimpleObject> SimpleObjectIList { get; set; } | ||
|
||
public SimpleObjectWithDeny Allow { get; set; } | ||
public SimpleObjectWithNestedObjectAndDeny() | ||
{ | ||
NestedObject = new NestedObject(); | ||
SimpleObject = new SimpleObject(); | ||
InheritedObject = new InheritedObject(); | ||
SimpleObjectList = new List<SimpleObject>(); | ||
Allow = new SimpleObjectWithDeny(); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace SystemTextJsonPatch | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | ||
public sealed class DenyPatchAttribute : Attribute | ||
{ | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
SystemTextJsonPatch/Exceptions/JsonPatchAccessDeniedException.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace SystemTextJsonPatch.Exceptions | ||
{ | ||
|
||
#pragma warning disable CA1032 // Implement standard exception constructors | ||
[Serializable] | ||
public class JsonPatchAccessDeniedException : JsonPatchException | ||
#pragma warning restore CA1032 // Implement standard exception constructors | ||
{ | ||
public string Type { get; } = "N/A"; | ||
public string Property { get; } = "N/A"; | ||
public JsonPatchAccessDeniedException(string message, Exception? innerException) : base(message, innerException) | ||
{ | ||
} | ||
|
||
public JsonPatchAccessDeniedException(string type, string property) : this($"Patch is not allowed to access the property {property} on the type {type}", (Exception?)null) | ||
{ | ||
this.Type = type; | ||
this.Property = property; | ||
} | ||
|
||
public JsonPatchAccessDeniedException(PropertyInfo propertyInfo) : this(propertyInfo?.DeclaringType?.Name ?? "N/A", propertyInfo?.Name ?? "N/A") | ||
{ | ||
|
||
} | ||
} | ||
} | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't there also generic overload
propertyInfo.GetCustomAttribute<DenyPatchAttribute>(...)
`?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not a big deal anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes there is, but i don't think it exists in
netstandard2.0
.