Skip to content

Commit

Permalink
Place limits on model binding collection size and recursion depth (do…
Browse files Browse the repository at this point in the history
…tnet#7214)

- dotnet#7052
- add MvcOptions.MaxModelBindingCollectionSize` and `MvcOptions.MaxModelBindingRecursionDepth`

nits:
- update syntax of a few `Resources.Designer.cs` files (I ran `/t:resx` on Mvc.sln)
- take VS suggestions in a few test classes
  • Loading branch information
dougbu authored Feb 19, 2019
1 parent 69abefa commit 3e0c751
Show file tree
Hide file tree
Showing 21 changed files with 836 additions and 114 deletions.
24 changes: 8 additions & 16 deletions src/Hosting/Hosting/src/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 12 additions & 24 deletions src/Middleware/Session/src/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ public void GetApiDescription_PopulatesParametersThatAppearOnRouteTemplate_AndHa
var action = CreateActionDescriptor(nameof(FromRouting));
action.AttributeRouteInfo = new AttributeRouteInfo { Template = template };

var parameterDescriptor = action.Parameters[0];

// Act
var descriptions = GetApiDescriptions(action);

Expand Down Expand Up @@ -1511,7 +1509,6 @@ public void GetApiDescription_ParameterDescription_DTOWithCollection()
{
// Arrange
var action = CreateActionDescriptor(nameof(AcceptsHasCollection));
var parameterDescriptor = action.Parameters.Single();

// Act
var descriptions = GetApiDescriptions(action);
Expand All @@ -1531,7 +1528,6 @@ public void GetApiDescription_ParameterDescription_DTOWithCollection_ElementsWit
{
// Arrange
var action = CreateActionDescriptor(nameof(AcceptsHasCollection_Complex));
var parameterDescriptor = action.Parameters.Single();

// Act
var descriptions = GetApiDescriptions(action);
Expand Down
30 changes: 30 additions & 0 deletions src/Mvc/Mvc.Core/src/ModelBinding/Binders/ArrayModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ public ArrayModelBinder(
{
}

/// <summary>
/// Creates a new <see cref="ArrayModelBinder{TElement}"/>.
/// </summary>
/// <param name="elementBinder">
/// The <see cref="IModelBinder"/> for binding <typeparamref name="TElement"/>.
/// </param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="allowValidatingTopLevelNodes">
/// Indication that validation of top-level models is enabled. If <see langword="true"/> and
/// <see cref="ModelMetadata.IsBindingRequired"/> is <see langword="true"/> for a top-level model, the binder
/// adds a <see cref="ModelStateDictionary"/> error when the model is not bound.
/// </param>
/// <param name="mvcOptions">The <see cref="MvcOptions"/>.</param>
/// <remarks>
/// <para>This is the preferred <see cref="ArrayModelBinder{TElement}"/> constructor.</para>
/// <para>
/// The <paramref name="allowValidatingTopLevelNodes"/> parameter is currently ignored.
/// <see cref="CollectionModelBinder{TElement}.AllowValidatingTopLevelNodes"/> is always <see langword="true"/>
/// in <see cref="ArrayModelBinder{TElement}"/>.
/// </para>
/// </remarks>
public ArrayModelBinder(
IModelBinder elementBinder,
ILoggerFactory loggerFactory,
bool allowValidatingTopLevelNodes,
MvcOptions mvcOptions)
: base(elementBinder, loggerFactory, allowValidatingTopLevelNodes: true, mvcOptions)
{
}

/// <inheritdoc />
public override bool CanCreateInstance(Type targetType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
{
Expand All @@ -23,15 +24,17 @@ public IModelBinder GetBinder(ModelBinderProviderContext context)
if (context.Metadata.ModelType.IsArray)
{
var elementType = context.Metadata.ElementMetadata.ModelType;
var binderType = typeof(ArrayModelBinder<>).MakeGenericType(elementType);
var elementBinder = context.CreateBinder(context.Metadata.ElementMetadata);

var binderType = typeof(ArrayModelBinder<>).MakeGenericType(elementType);
var loggerFactory = context.Services.GetRequiredService<ILoggerFactory>();
var mvcOptions = context.Services.GetRequiredService<IOptions<MvcOptions>>().Value;
return (IModelBinder)Activator.CreateInstance(
binderType,
elementBinder,
loggerFactory,
true /* allowValidatingTopLevelNodes */);
true /* allowValidatingTopLevelNodes */,
mvcOptions);
}

return null;
Expand Down
Loading

0 comments on commit 3e0c751

Please sign in to comment.