Skip to content

Commit e4a858f

Browse files
committed
Ensure content is HTML encoded
1 parent af16da4 commit e4a858f

12 files changed

+30
-17
lines changed

src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.CharacterCount.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Encodings.Web;
12
using Microsoft.AspNetCore.Html;
23
using Microsoft.AspNetCore.Mvc.Rendering;
34
using Microsoft.AspNetCore.Mvc.ViewFeatures;
@@ -50,7 +51,7 @@ IHtmlContent GenerateHint()
5051
var content = maxWords.HasValue ?
5152
$"You can enter up to {maxWords} words" :
5253
$"You can enter up to {maxLength} characters";
53-
var hintContent = new HtmlString(content);
54+
var hintContent = new HtmlString(HtmlEncoder.Default.Encode(content));
5455

5556
var attributes = countMessageAttributes.ToAttributeDictionary();
5657
attributes.MergeCssClass("govuk-character-count__message");

src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.NotificationBanner.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.Encodings.Web;
23
using Microsoft.AspNetCore.Html;
34
using Microsoft.AspNetCore.Mvc.Rendering;
45
using Microsoft.AspNetCore.Mvc.ViewFeatures;
@@ -43,10 +44,10 @@ public TagBuilder GenerateNotificationBanner(
4344
NotificationBannerDefaultSuccessRole :
4445
NotificationBannerDefaultRole;
4546

46-
titleContent ??= new HtmlString(
47+
titleContent ??= new HtmlString(HtmlEncoder.Default.Encode(
4748
type == NotificationBannerType.Success ?
4849
NotificationBannerDefaultSuccessTitle :
49-
NotificationBannerDefaultTitle);
50+
NotificationBannerDefaultTitle));
5051

5152
titleId ??= NotificationBannerDefaultTitleId;
5253

src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Pagination.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.Encodings.Web;
45
using Microsoft.AspNetCore.Html;
56
using Microsoft.AspNetCore.Mvc.Rendering;
67
using Microsoft.AspNetCore.Mvc.ViewFeatures;
@@ -67,7 +68,7 @@ public TagBuilder GeneratePagination(
6768
title.AddCssClass("govuk-pagination__link-title--decorated");
6869
}
6970

70-
title.InnerHtml.AppendHtml(previous.Text ?? new HtmlString(PaginationDefaultPreviousText));
71+
title.InnerHtml.AppendHtml(previous.Text ?? new HtmlString(HtmlEncoder.Default.Encode(PaginationDefaultPreviousText)));
7172

7273
link.InnerHtml.AppendHtml(title);
7374

@@ -187,7 +188,7 @@ public TagBuilder GeneratePagination(
187188
title.AddCssClass("govuk-pagination__link-title--decorated");
188189
}
189190

190-
title.InnerHtml.AppendHtml(next.Text ?? new HtmlString(PaginationDefaultNextText));
191+
title.InnerHtml.AppendHtml(next.Text ?? new HtmlString(HtmlEncoder.Default.Encode(PaginationDefaultNextText)));
191192

192193
link.InnerHtml.AppendHtml(title);
193194

src/GovUk.Frontend.AspNetCore/TagHelpers/BackLinkTagHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Encodings.Web;
12
using System.Threading.Tasks;
23
using GovUk.Frontend.AspNetCore.HtmlGeneration;
34
using Microsoft.AspNetCore.Html;
@@ -15,7 +16,7 @@ public class BackLinkTagHelper : TagHelper
1516
{
1617
internal const string TagName = "govuk-back-link";
1718

18-
private static readonly HtmlString _defaultContent = new HtmlString(ComponentGenerator.BackLinkDefaultContent);
19+
private static readonly HtmlString _defaultContent = new HtmlString(HtmlEncoder.Default.Encode(ComponentGenerator.BackLinkDefaultContent));
1920

2021
private readonly IGovUkHtmlGenerator _htmlGenerator;
2122

src/GovUk.Frontend.AspNetCore/TagHelpers/CharacterCountTagHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text.Encodings.Web;
34
using GovUk.Frontend.AspNetCore.HtmlGeneration;
45
using Microsoft.AspNetCore.Html;
56
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -246,7 +247,8 @@ TagBuilder GenerateTextArea(bool haveError)
246247
var resolvedName = ResolveName();
247248

248249
var resolvedContent = characterCountContext.Value ??
249-
new HtmlString(AspFor != null ? ModelHelper.GetModelValue(ViewContext!, AspFor.ModelExplorer, AspFor.Name) : string.Empty);
250+
new HtmlString(HtmlEncoder.Default.Encode(
251+
AspFor != null ? ModelHelper.GetModelValue(ViewContext!, AspFor.ModelExplorer, AspFor.Name) ?? string.Empty : string.Empty));
250252

251253
var resolvedTextAreaAttributes = TextAreaAttributes.ToAttributeDictionary();
252254
resolvedTextAreaAttributes.MergeCssClass("govuk-js-character-count");

src/GovUk.Frontend.AspNetCore/TagHelpers/DateInputTagHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Text.Encodings.Web;
45
using GovUk.Frontend.AspNetCore.HtmlGeneration;
56
using GovUk.Frontend.AspNetCore.ModelBinding;
67
using Microsoft.AspNetCore.Html;
@@ -268,7 +269,7 @@ DateInputItem CreateDateInputItem(
268269

269270
var resolvedItemId = contextItem?.Id ?? $"{resolvedId}.{contextItem?.Name ?? defaultName}";
270271

271-
var resolvedItemLabel = contextItem?.LabelContent ?? new HtmlString(defaultLabel);
272+
var resolvedItemLabel = contextItem?.LabelContent ?? new HtmlString(HtmlEncoder.Default.Encode(defaultLabel));
272273

273274
var resolvedItemHaveError = haveError && (errorItems & errorSource) != 0;
274275

src/GovUk.Frontend.AspNetCore/TagHelpers/ErrorMessageTagHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Text.Encodings.Web;
34
using System.Threading.Tasks;
45
using GovUk.Frontend.AspNetCore.HtmlGeneration;
56
using Microsoft.AspNetCore.Html;
@@ -92,7 +93,7 @@ await output.GetChildContentAsync() :
9293

9394
if (validationMessage != null)
9495
{
95-
resolvedContent = new HtmlString(validationMessage);
96+
resolvedContent = new HtmlString(HtmlEncoder.Default.Encode(validationMessage));
9697
}
9798
}
9899

src/GovUk.Frontend.AspNetCore/TagHelpers/ErrorSummaryItemTagHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Text.Encodings.Web;
56
using System.Threading.Tasks;
67
using GovUk.Frontend.AspNetCore.HtmlGeneration;
78
using GovUk.Frontend.AspNetCore.ModelBinding;
@@ -100,7 +101,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
100101
return;
101102
}
102103

103-
itemContent = new HtmlString(validationMessage);
104+
itemContent = new HtmlString(HtmlEncoder.Default.Encode(validationMessage));
104105
}
105106

106107
string? resolvedHref = null;

src/GovUk.Frontend.AspNetCore/TagHelpers/ErrorSummaryTagHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Encodings.Web;
12
using System.Threading.Tasks;
23
using GovUk.Frontend.AspNetCore.HtmlGeneration;
34
using Microsoft.AspNetCore.Html;
@@ -62,7 +63,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
6263

6364
var tagBuilder = _htmlGenerator.GenerateErrorSummary(
6465
DisableAutoFocus,
65-
errorSummaryContext.Title?.Content ?? new HtmlString(ComponentGenerator.ErrorSummaryDefaultTitle),
66+
errorSummaryContext.Title?.Content ?? new HtmlString(HtmlEncoder.Default.Encode(ComponentGenerator.ErrorSummaryDefaultTitle)),
6667
errorSummaryContext.Title?.Attributes,
6768
errorSummaryContext.Description?.Content,
6869
errorSummaryContext.Description?.Attributes,

src/GovUk.Frontend.AspNetCore/TagHelpers/FormErrorSummaryTagHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using System.Text.Encodings.Web;
23
using System.Threading.Tasks;
34
using GovUk.Frontend.AspNetCore.HtmlGeneration;
45
using Microsoft.AspNetCore.Html;
@@ -75,7 +76,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
7576

7677
var errorSummary = _htmlGenerator.GenerateErrorSummary(
7778
ComponentGenerator.ErrorSummaryDefaultDisableAutoFocus,
78-
titleContent: new HtmlString(ComponentGenerator.ErrorSummaryDefaultTitle),
79+
titleContent: new HtmlString(HtmlEncoder.Default.Encode(ComponentGenerator.ErrorSummaryDefaultTitle)),
7980
titleAttributes: null,
8081
descriptionContent: null,
8182
descriptionAttributes: null,

0 commit comments

Comments
 (0)