Skip to content
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

Limit content item recursions to 20 #7500

Merged
merged 4 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace OrchardCore.Contents.Liquid
{
public class BuildDisplayFilter : ILiquidFilter
{
private const int DefaultMaxContentItemRecursions = 20;

public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx)
{
static async ValueTask<FluidValue> Awaited(Task<IShape> task)
Expand Down Expand Up @@ -49,8 +51,11 @@ static async ValueTask<FluidValue> Awaited(Task<IShape> task)

var buildDisplayRecursionHelper = serviceProvider.GetRequiredService<IContentItemRecursionHelper<BuildDisplayFilter>>();

// When {{ Model.ContentItem | shape_build_display | shape_render }} is called prevent recursion.
if (buildDisplayRecursionHelper.IsRecursive(contentItem))
// When {{ Model.ContentItem | shape_build_display | shape_render }} is called prevent unlimited recursions.
// max_recursions is an optional argument to override the default limit of 20.
var maxRecursions = arguments["max_recursions"];
var recursionLimit = maxRecursions.Type == FluidValues.Number ? Convert.ToInt32(maxRecursions.ToNumberValue()) : DefaultMaxContentItemRecursions;
if (buildDisplayRecursionHelper.IsRecursive(contentItem, recursionLimit))
{
return new ValueTask<FluidValue>(NilValue.Instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,35 @@ public interface IContentItemRecursionHelper<T> where T : ILiquidFilter
/// <summary>
/// Returns <see langword="True"/> when the <see cref="ContentItem"/> has already been evaluated during this request by the particular filter./>
/// </summary>
bool IsRecursive(ContentItem contentItem);
bool IsRecursive(ContentItem contentItem, int maxRecursions = 1);
}

/// <inheritdocs />
public class ContentItemRecursionHelper<T> : IContentItemRecursionHelper<T> where T : ILiquidFilter
{
private HashSet<string> _contentItemIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private Dictionary<ContentItem, int> _recursions = new Dictionary<ContentItem, int>();

/// <inheritdocs />
public bool IsRecursive(ContentItem contentItem)
public bool IsRecursive(ContentItem contentItem, int maxRecursions = 1)
{
if (_contentItemIds.Contains(contentItem.ContentItemId))
if (_recursions.ContainsKey(contentItem))
{
return true;
}
var counter = _recursions[contentItem];
if (maxRecursions < 1)
{
maxRecursions = 1;
}

if (counter > maxRecursions)
{
return true;
}

_contentItemIds.Add(contentItem.ContentItemId);
_recursions[contentItem] = counter + 1;
return false;
}

_recursions[contentItem] = 1;
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/OrchardCore.Modules/OrchardCore.Contents/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ public override void ConfigureServices(IServiceCollection services)
services.AddLiquidFilter<DisplayUrlFilter>("display_url");
services.AddLiquidFilter<FullTextFilter>("full_text");

services.AddScoped<IContentItemRecursionHelper<FullTextFilter>, ContentItemRecursionHelper<FullTextFilter>>();
services.AddScoped<IContentItemRecursionHelper<BuildDisplayFilter>, ContentItemRecursionHelper<BuildDisplayFilter>>();
services.AddScoped(typeof(IContentItemRecursionHelper<>), typeof(ContentItemRecursionHelper<>));
}

public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
Expand Down