You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/web-api/jsonpatch/includes/jsonpatch9.md
+57-8Lines changed: 57 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,14 @@
2
2
3
3
This article explains how to handle JSON Patch requests in an ASP.NET Core web API.
4
4
5
+
> [!IMPORTANT]
6
+
> The JSON Patch standard has ***inherent security risks***. This implementation ***doesn't attempt to mitigate these inherent security risks***. It's the responsibility of the developer to ensure that the JSON Patch document is safe to apply to the target object. For more information, see the [Mitigating Security Risks](#mitigating-security-risks) section.
7
+
5
8
## Package installation
6
9
7
-
JSON Patch support in ASP.NET Core web API is based on `Newtonsoft.Json` and requires the [`Microsoft.AspNetCore.Mvc.NewtonsoftJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson/) NuGet package. To enable JSON Patch support:
10
+
JSON Patch support in ASP.NET Core web API is based on `Newtonsoft.Json` and requires the [`Microsoft.AspNetCore.Mvc.NewtonsoftJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson/) NuGet package.
11
+
12
+
To enable JSON Patch support:
8
13
9
14
* Install the [`Microsoft.AspNetCore.Mvc.NewtonsoftJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson/) NuGet package.
10
15
* Call <xref:Microsoft.Extensions.DependencyInjection.NewtonsoftJsonMvcBuilderExtensions.AddNewtonsoftJson%2A>. For example:
@@ -234,6 +239,53 @@ To test the sample, run the app and send HTTP requests with the following settin
* Body: Copy and paste one of the JSON patch document samples from the *JSON* project folder.
236
241
242
+
## Mitigating security risks
243
+
244
+
When using the `Microsoft.AspNetCore.JsonPatch` package with the `Newtonsoft.Json`-based implementation, it's critical to understand and mitigate potential security risks. The following sections outline the identified security risks associated with JSON Patch and provide recommended mitigations to ensure secure usage of the package.
245
+
246
+
> [!IMPORTANT]
247
+
> ***This is not an exhaustive list of threats.*** App developers must conduct their own threat model reviews to determine an app-specific comprehensive list and come up with appropriate mitigations as needed. For example, apps which expose collections to patch operations should consider the potential for algorithmic complexity attacks if those operations insert or remove elements at the beginning of the collection.
248
+
249
+
By running comprehensive threat models for their own apps and addressing identified threats while following the recommended mitigations below, consumers of these packages can integrate JSON Patch functionality into their apps while minimizing security risks.
250
+
251
+
### Denial of Service (DoS) via memory amplification
252
+
253
+
***Scenario**: A malicious client submits a `copy` operation that duplicates large object graphs multiple times, leading to excessive memory consumption.
254
+
***Impact**: Potential Out-Of-Memory (OOM) conditions, causing service disruptions.
255
+
***Mitigation**:
256
+
* Validate incoming JSON Patch documents for size and structure before calling `ApplyTo`.
257
+
* The validation needs to be app specific, but an example validation can look similar to the following:
258
+
259
+
```csharp
260
+
publicvoidValidate(JsonPatchDocumentpatch)
261
+
{
262
+
// This is just an example. It's up to the developer to make sure that
263
+
// this case is handled properly, based on the app needs.
264
+
if (patch.Operations.Where(op=>op.OperationType==OperationType.Copy).Count()
265
+
>MaxCopyOperationsCount)
266
+
{
267
+
thrownewInvalidOperationException();
268
+
}
269
+
}
270
+
```
271
+
272
+
### Business Logic Subversion
273
+
274
+
***Scenario**: Patch operations can manipulate fields with implicit invariants (for example, internal flags, IDs, or computed fields), violating business constraints.
275
+
***Impact**: Data integrity issues and unintended app behavior.
276
+
***Mitigation**:
277
+
* Use POCO objects with explicitly defined properties that are safe to modify.
278
+
* Avoid exposing sensitive or security-critical properties in the target object.
279
+
* If no POCO object is used, validate the patched object after applying operations to ensure business rules and invariants aren't violated.
280
+
281
+
### Authentication and authorization
282
+
283
+
***Scenario**: Unauthenticated or unauthorized clients send malicious JSON Patch requests.
284
+
***Impact**: Unauthorized access to modify sensitive data or disrupt app behavior.
285
+
***Mitigation**:
286
+
* Protect endpoints accepting JSON Patch requests with proper authentication and authorization mechanisms.
287
+
* Restrict access to trusted clients or users with appropriate permissions.
@@ -247,6 +299,9 @@ To test the sample, run the app and send HTTP requests with the following settin
247
299
248
300
This article explains how to handle JSON Patch requests in an ASP.NET Core web API.
249
301
302
+
> [!IMPORTANT]
303
+
> The JSON Patch standard has ***inherent security risks***. Since these risks are inherent to the JSON Patch standard, this implementation ***doesn't attempt to mitigate inherent security risks***. It's the responsibility of the developer to ensure that the JSON Patch document is safe to apply to the target object. For more information, see the [Mitigating Security Risks](#mitigating-security-risks) section.
304
+
250
305
## Package installation
251
306
252
307
To enable JSON Patch support in your app, complete the following steps:
@@ -476,11 +531,5 @@ To test the sample, run the app and send HTTP requests with the following settin
0 commit comments