Skip to content

Commit 2637fb4

Browse files
committed
Added mitigating security section to all previous versions of article
1 parent 3ac8957 commit 2637fb4

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

aspnetcore/web-api/jsonpatch/includes/jsonpatch9.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
This article explains how to handle JSON Patch requests in an ASP.NET Core web API.
44

5+
> [!IMPORTANT]
6+
> 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.
7+
58
## Package installation
69

710
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.
811

9-
> [!IMPORTANT]
10-
> 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.
11-
1212
To enable JSON Patch support:
1313

1414
* Install the [`Microsoft.AspNetCore.Mvc.NewtonsoftJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson/) NuGet package.
@@ -239,6 +239,53 @@ To test the sample, run the app and send HTTP requests with the following settin
239239
* Header: `Content-Type: application/json-patch+json`
240240
* Body: Copy and paste one of the JSON patch document samples from the *JSON* project folder.
241241

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+
public void Validate(JsonPatchDocument patch)
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+
throw new InvalidOperationException();
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.
288+
242289
## Additional resources
243290

244291
* [IETF RFC 5789 PATCH method specification](https://tools.ietf.org/html/rfc5789)
@@ -252,6 +299,9 @@ To test the sample, run the app and send HTTP requests with the following settin
252299

253300
This article explains how to handle JSON Patch requests in an ASP.NET Core web API.
254301

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+
255305
## Package installation
256306

257307
To enable JSON Patch support in your app, complete the following steps:

0 commit comments

Comments
 (0)