Skip to content

Commit cbf9f9b

Browse files
authored
Merge pull request from GHSA-gvpc-3pj6-4m9w
* Add MarkDownPropertyValueEditor with html sanitizer * Implement IMarkdownSanitizer.
1 parent e37cf30 commit cbf9f9b

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

src/Umbraco.Core/DependencyInjection/UmbracoBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ private void AddCoreServices()
318318
Services.AddSingleton<ConflictingPackageData>();
319319
Services.AddSingleton<CompiledPackageXmlParser>();
320320

321-
// Register a noop IHtmlSanitizer to be replaced
321+
// Register a noop IHtmlSanitizer & IMarkdownSanitizer to be replaced
322322
Services.AddUnique<IHtmlSanitizer, NoopHtmlSanitizer>();
323+
Services.AddUnique<IMarkdownSanitizer, NoopMarkdownSanitizer>();
323324

324325
Services.AddUnique<IPropertyTypeUsageService, PropertyTypeUsageService>();
325326
Services.AddUnique<IDataTypeUsageService, DataTypeUsageService>();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Umbraco.Cms.Core.IO;
2+
using Umbraco.Cms.Core.Models.Editors;
3+
using Umbraco.Cms.Core.Security;
4+
using Umbraco.Cms.Core.Serialization;
5+
using Umbraco.Cms.Core.Services;
6+
using Umbraco.Cms.Core.Strings;
7+
using Umbraco.Extensions;
8+
9+
namespace Umbraco.Cms.Core.PropertyEditors;
10+
11+
/// <summary>
12+
/// A custom value editor to ensure that macro syntax is parsed when being persisted and formatted correctly for
13+
/// display in the editor
14+
/// </summary>
15+
internal class MarkDownPropertyValueEditor : DataValueEditor
16+
{
17+
private readonly IMarkdownSanitizer _markdownSanitizer;
18+
19+
public MarkDownPropertyValueEditor(
20+
ILocalizedTextService localizedTextService,
21+
IShortStringHelper shortStringHelper,
22+
IJsonSerializer jsonSerializer,
23+
IIOHelper ioHelper,
24+
DataEditorAttribute attribute,
25+
IMarkdownSanitizer markdownSanitizer)
26+
: base(localizedTextService, shortStringHelper, jsonSerializer, ioHelper, attribute) => _markdownSanitizer = markdownSanitizer;
27+
28+
public override object? FromEditor(ContentPropertyData editorValue, object? currentValue)
29+
{
30+
if (string.IsNullOrWhiteSpace(editorValue.Value?.ToString()))
31+
{
32+
return null;
33+
}
34+
35+
var sanitized = _markdownSanitizer.Sanitize(editorValue.Value.ToString()!);
36+
37+
return sanitized.NullOrWhiteSpaceAsNull();
38+
}
39+
}

src/Umbraco.Core/PropertyEditors/MarkdownPropertyEditor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.Extensions.DependencyInjection;
55
using Umbraco.Cms.Core.IO;
6+
using Umbraco.Cms.Core.Models;
67
using Umbraco.Cms.Core.Services;
78
using Umbraco.Cms.Web.Common.DependencyInjection;
89

@@ -50,4 +51,11 @@ public MarkdownPropertyEditor(
5051
/// <inheritdoc />
5152
protected override IConfigurationEditor CreateConfigurationEditor() =>
5253
new MarkdownConfigurationEditor(_ioHelper, _editorConfigurationParser);
54+
55+
/// <summary>
56+
/// Create a custom value editor
57+
/// </summary>
58+
/// <returns></returns>
59+
protected override IDataValueEditor CreateValueEditor() =>
60+
DataValueEditorFactory.Create<MarkDownPropertyValueEditor>(Attribute!);
5361
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Umbraco.Cms.Core.Security;
2+
3+
/// <summary>
4+
/// Sanitizer service for the markdown editor.
5+
/// </summary>
6+
public interface IMarkdownSanitizer
7+
{
8+
/// <summary>
9+
/// Sanitizes Markdown
10+
/// </summary>
11+
/// <param name="markdown">Markdown to be sanitized</param>
12+
/// <returns>Sanitized Markdown</returns>
13+
string Sanitize(string markdown);
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Umbraco.Cms.Core.Security;
2+
3+
/// <inheritdoc />
4+
public class NoopMarkdownSanitizer : IMarkdownSanitizer
5+
{
6+
/// <inheritdoc />
7+
public string Sanitize(string markdown) => markdown;
8+
}

0 commit comments

Comments
 (0)