Files
btcpayserver/BTCPayServer.Abstractions/Services/Safe.cs
Nisaba 2250853b3e HTML lang setting and Head tags for POS and Crowdfund public pages (#6229)
* HTML lang setting and Head tags for POS and Crowdfund public pages

* updates #6229

* updates 6229

* resolve conflict

* updated according to Nicolas' recommendations

* updates #6229

* Add RawMeta method in safe.cs

* ...

* resolve conflicts

* resolve conflict

* resolve conflicts

* Updates as Nicolas request

* updates

---------

Co-authored-by: d11n <mail@dennisreimann.de>
2025-01-15 14:49:25 +09:00

71 lines
2.4 KiB
C#

using System.Web;
using Ganss.Xss;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace BTCPayServer.Abstractions.Services
{
public class Safe
{
private readonly IHtmlHelper _htmlHelper;
private readonly IJsonHelper _jsonHelper;
private readonly HtmlSanitizer _htmlSanitizer;
public Safe(IHtmlHelper htmlHelper, IJsonHelper jsonHelper, HtmlSanitizer htmlSanitizer)
{
_htmlHelper = htmlHelper;
_jsonHelper = jsonHelper;
_htmlSanitizer = htmlSanitizer;
}
public IHtmlContent Raw(string value)
{
return _htmlHelper.Raw(_htmlSanitizer.Sanitize(value));
}
public IHtmlContent RawEncode(string value)
{
return _htmlHelper.Raw(HttpUtility.HtmlEncode(_htmlSanitizer.Sanitize(value)));
}
public IHtmlContent Json(object model)
{
return _htmlHelper.Raw(_jsonHelper.Serialize(model));
}
public string RawMeta(string inputHtml, out bool isHtmlModified)
{
bool bHtmlModified;
HtmlSanitizer _metaSanitizer = new HtmlSanitizer();
_metaSanitizer.AllowedTags.Clear();
_metaSanitizer.AllowedTags.Add("meta");
_metaSanitizer.AllowedAttributes.Clear();
_metaSanitizer.AllowedAttributes.Add("name");
_metaSanitizer.AllowedAttributes.Add("http-equiv");
_metaSanitizer.AllowedAttributes.Add("content");
_metaSanitizer.AllowedAttributes.Add("value");
_metaSanitizer.AllowedAttributes.Add("property");
_metaSanitizer.AllowDataAttributes = false;
_metaSanitizer.RemovingTag += (sender, e) => bHtmlModified = true;
_metaSanitizer.RemovingAtRule += (sender, e) => bHtmlModified = true;
_metaSanitizer.RemovingAttribute += (sender, e) => bHtmlModified = true;
_metaSanitizer.RemovingComment += (sender, e) => bHtmlModified = true;
_metaSanitizer.RemovingCssClass += (sender, e) => bHtmlModified = true;
_metaSanitizer.RemovingStyle += (sender, e) => bHtmlModified = true;
bHtmlModified = false;
var sRet = _metaSanitizer.Sanitize(inputHtml);
isHtmlModified = bHtmlModified;
return sRet;
}
}
}