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

Validate JavaScript condition in Layer Rule on save (#12901) #12968

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,20 +1,41 @@
using System;
using System.Threading.Tasks;
using Esprima;
using Jint.Runtime;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Mvc.ModelBinding;
using OrchardCore.Rules.Models;
using OrchardCore.Rules.Services;
using OrchardCore.Rules.ViewModels;

namespace OrchardCore.Rules.Drivers
{
public class JavascriptConditionDisplayDriver : DisplayDriver<Condition, JavascriptCondition>
{
private readonly ConditionOperatorOptions _options;
private readonly IHtmlLocalizer H;
private readonly IStringLocalizer S;
private readonly INotifier _notifier;
private readonly JavascriptConditionEvaluator _evaluator;

public JavascriptConditionDisplayDriver(IOptions<ConditionOperatorOptions> options)
public JavascriptConditionDisplayDriver(
IOptions<ConditionOperatorOptions> options,
IHtmlLocalizer<JavascriptConditionDisplayDriver> htmlLocalizer,
IStringLocalizer<JavascriptConditionDisplayDriver> stringLocalizer,
JavascriptConditionEvaluator evaluator,
INotifier notifier)
{
_options = options.Value;
H = htmlLocalizer;
S = stringLocalizer;
_evaluator = evaluator;
_notifier = notifier;
}

public override IDisplayResult Display(JavascriptCondition condition)
Expand All @@ -40,8 +61,39 @@ public override async Task<IDisplayResult> UpdateAsync(JavascriptCondition condi
var model = new JavascriptConditionViewModel();
if (await updater.TryUpdateModelAsync(model, Prefix))
{
// TODO is empty.
condition.Script = model.Script;
if (string.IsNullOrWhiteSpace(model.Script))
{
updater.ModelState.AddModelError(Prefix, nameof(model.Script), S["The script is required"]);
//Codemirror hides the textarea which displays the error when updater.ModelState.AddModelError is used, that's why a notifier is used to show the error to user
await _notifier.ErrorAsync(H["The script is required"]);
return Edit(condition);
}

try
{
_ = await _evaluator.EvaluateAsync(new()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ParseScript instead of evaluate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I parse it first and then evaluate? Sébastien Roses second point in the issue is "ensure that evaluating the script is not breaking the full page..." so I have to evaluate and I thought that since EvaluateAsync parses it with ParseScript before evaluation then I only need to catch the exception, otherwise I would be parsing it twice.

{
ConditionId = condition.ConditionId,
Name = condition.Name,
Script = model.Script
});
condition.Script = model.Script;
}
catch (ParserException ex) //Invalid syntax
{
updater.ModelState.AddModelError(Prefix, nameof(model.Script), S["The script couldn't be parsed. Details: {0}", ex.Message]);
await _notifier.ErrorAsync(H["The script couldn't be parsed. Details: {0}", ex.Message]);
}
catch (JavaScriptException ex) //Evaluation threw an Error
{
updater.ModelState.AddModelError(Prefix, nameof(model.Script), S["JavaScript evaluation resulted in an exception. Details: {0}", ex.Message]);
await _notifier.ErrorAsync(H["JavaScript evaluation resulted in an exception. Details: {0}", ex.Message]);
}
catch (Exception ex) when (ex is InvalidCastException or FormatException) //Evaluation completes successfully, but the result cannot be converted to Boolean
{
updater.ModelState.AddModelError(Prefix, nameof(model.Script), S["The script evaluation failed. Details: {0}", ex.Message]);
await _notifier.ErrorAsync(H["The script evaluation failed. Details: {0}", ex.Message]);
}
}

return Edit(condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Jint" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ContentManagement.Display\OrchardCore.ContentManagement.Display.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.DisplayManagement\OrchardCore.DisplayManagement.csproj" />
Expand Down