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

Add GetEncodedStringValue to SubresourceIntegrityTagHelper #108

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,6 @@
namespace Boxed.AspNetCore.TagHelpers
{
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
Expand All @@ -21,12 +22,14 @@ public class HrefSubresourceIntegrityTagHelper : SubresourceIntegrityTagHelper
/// <param name="hostingEnvironment">The hosting environment.</param>
/// <param name="actionContextAccessor">The MVC action context accessor.</param>
/// <param name="urlHelperFactory">The URL helper factory.</param>
/// <param name="htmlEncoder">The <see cref="HtmlEncoder"/>.</param>
public HrefSubresourceIntegrityTagHelper(
IDistributedCache distributedCache,
IHostingEnvironment hostingEnvironment,
IActionContextAccessor actionContextAccessor,
IUrlHelperFactory urlHelperFactory)
: base(distributedCache, hostingEnvironment, actionContextAccessor, urlHelperFactory)
IUrlHelperFactory urlHelperFactory,
HtmlEncoder htmlEncoder)
: base(distributedCache, hostingEnvironment, actionContextAccessor, urlHelperFactory, htmlEncoder)
{
}

Expand Down
@@ -1,5 +1,6 @@
namespace Boxed.AspNetCore.TagHelpers
{
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
Expand All @@ -21,12 +22,14 @@ public class SrcSubresourceIntegrityTagHelper : SubresourceIntegrityTagHelper
/// <param name="hostingEnvironment">The hosting environment.</param>
/// <param name="actionContextAccessor">The MVC action context accessor.</param>
/// <param name="urlHelperFactory">The URL helper factory.</param>
/// <param name="htmlEncoder">The <see cref="HtmlEncoder"/>.</param>
public SrcSubresourceIntegrityTagHelper(
IDistributedCache distributedCache,
IHostingEnvironment hostingEnvironment,
IActionContextAccessor actionContextAccessor,
IUrlHelperFactory urlHelperFactory)
: base(distributedCache, hostingEnvironment, actionContextAccessor, urlHelperFactory)
IUrlHelperFactory urlHelperFactory,
HtmlEncoder htmlEncoder)
: base(distributedCache, hostingEnvironment, actionContextAccessor, urlHelperFactory, htmlEncoder)
{
}

Expand Down
Expand Up @@ -5,6 +5,7 @@ namespace Boxed.AspNetCore.TagHelpers
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Html;
Expand Down Expand Up @@ -32,6 +33,7 @@ public abstract class SubresourceIntegrityTagHelper : TagHelper
private readonly IDistributedCache distributedCache;
private readonly IHostingEnvironment hostingEnvironment;
private readonly IUrlHelper urlHelper;
private readonly HtmlEncoder htmlEncoder;

/// <summary>
/// Initializes a new instance of the <see cref="SubresourceIntegrityTagHelper"/> class.
Expand All @@ -40,14 +42,17 @@ public abstract class SubresourceIntegrityTagHelper : TagHelper
/// <param name="hostingEnvironment">The hosting environment.</param>
/// <param name="actionContextAccessor">The MVC action context accessor.</param>
/// <param name="urlHelperFactory">The URL helper factory.</param>
/// <param name="htmlEncoder">The <see cref="HtmlEncoder"/>.</param>
public SubresourceIntegrityTagHelper(
IDistributedCache distributedCache,
IHostingEnvironment hostingEnvironment,
IActionContextAccessor actionContextAccessor,
IUrlHelperFactory urlHelperFactory)
IUrlHelperFactory urlHelperFactory,
HtmlEncoder htmlEncoder)
{
this.distributedCache = distributedCache ?? throw new ArgumentNullException(nameof(distributedCache));
this.hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
this.htmlEncoder = htmlEncoder ?? throw new ArgumentNullException(nameof(htmlEncoder));

if (actionContextAccessor == null)
{
Expand Down Expand Up @@ -105,7 +110,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
throw new ArgumentNullException(nameof(output));
}

var url = output.Attributes[this.UrlAttributeName].Value.ToString();
var url = this.GetEncodedStringValue(output.Attributes[this.UrlAttributeName].Value);

if (!string.IsNullOrWhiteSpace(url) && !string.IsNullOrWhiteSpace(this.Source))
{
Expand Down Expand Up @@ -245,5 +250,37 @@ private Task SetCachedSriAsync(string url, string value)
var bytes = this.ReadAllBytes(filePath);
return GetSpaceDelimetedSri(bytes, hashAlgorithms);
}

private string GetEncodedStringValue(object attributeValue)
RehanSaeed marked this conversation as resolved.
Show resolved Hide resolved
{
if (attributeValue is string stringValue)
{
var encodedStringValue = this.htmlEncoder.Encode(stringValue);
return encodedStringValue;
}
else
{
if (attributeValue is IHtmlContent htmlContent)
{
if (htmlContent is HtmlString htmlString)
{
// No need for a StringWriter in this case.
stringValue = htmlString.ToString();
}
else
{
using (var writer = new StringWriter())
{
htmlContent.WriteTo(writer, this.htmlEncoder);
RehanSaeed marked this conversation as resolved.
Show resolved Hide resolved
stringValue = writer.ToString();
}
}

return stringValue;
}
}

return attributeValue.ToString();
}
}
}
Expand Up @@ -3,6 +3,7 @@ namespace Boxed.AspNetCore.TagHelpers.Test
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -23,6 +24,7 @@ public class SubresourceIntegrityTagHelperTest
private readonly Mock<IUrlHelperFactory> urlHelperFactoryMock;
private readonly Mock<IUrlHelper> urlHelperMock;
private readonly SubresourceIntegrityTagHelper tagHelper;
private readonly Mock<HtmlEncoder> htmlEncoderMock;

public SubresourceIntegrityTagHelperTest()
{
Expand All @@ -31,15 +33,18 @@ public SubresourceIntegrityTagHelperTest()
this.actionContextAccessor = new Mock<IActionContextAccessor>(MockBehavior.Strict);
this.urlHelperFactoryMock = new Mock<IUrlHelperFactory>(MockBehavior.Strict);
this.urlHelperMock = new Mock<IUrlHelper>(MockBehavior.Strict);
this.htmlEncoderMock = new Mock<HtmlEncoder>(MockBehavior.Strict);

this.actionContextAccessor.SetupGet(x => x.ActionContext).Returns((ActionContext)null);
this.urlHelperFactoryMock.Setup(x => x.GetUrlHelper(this.actionContextAccessor.Object.ActionContext)).Returns(this.urlHelperMock.Object);
this.htmlEncoderMock.Setup(x => x.Encode(It.IsAny<string>())).Returns((string s) => s);

this.tagHelper = new TestSubresourceIntegrityTagHelper(
this.distributedCacheMock.Object,
this.hostingEnvironmentMock.Object,
this.actionContextAccessor.Object,
this.urlHelperFactoryMock.Object);
this.urlHelperFactoryMock.Object,
this.htmlEncoderMock.Object);
}

[Fact]
Expand Down Expand Up @@ -143,8 +148,9 @@ public class TestSubresourceIntegrityTagHelper : SubresourceIntegrityTagHelper
IDistributedCache distributedCache,
IHostingEnvironment hostingEnvironment,
IActionContextAccessor actionContextAccessor,
IUrlHelperFactory urlHelperFactory)
: base(distributedCache, hostingEnvironment, actionContextAccessor, urlHelperFactory)
IUrlHelperFactory urlHelperFactory,
HtmlEncoder htmlEncoder)
: base(distributedCache, hostingEnvironment, actionContextAccessor, urlHelperFactory, htmlEncoder)
{
}

Expand Down