Skip to content

Commit d9368e1

Browse files
authored
Merge pull request #359 from vanillajonathan/patch-2
Add HtmlSanitizerOptions
2 parents 79c4345 + b21e34a commit d9368e1

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/HtmlSanitizer/HtmlSanitizer.cs

+15
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ public class HtmlSanitizer : IHtmlSanitizer
6666

6767
private static readonly HtmlParser defaultHtmlParser = new(new HtmlParserOptions(), BrowsingContext.New(defaultConfiguration));
6868

69+
/// <summary>
70+
/// Initializes a new instance of the <see cref="HtmlSanitizer"/> class.
71+
/// </summary>
72+
/// <param name="options">Options to control the sanitizing.</param>
73+
public HtmlSanitizer(HtmlSanitizerOptions options)
74+
{
75+
AllowedTags = new HashSet<string>(options.AllowedTags, StringComparer.OrdinalIgnoreCase);
76+
AllowedSchemes = new HashSet<string>(options.AllowedSchemes, StringComparer.OrdinalIgnoreCase);
77+
AllowedAttributes = new HashSet<string>(options.AllowedAttributes, StringComparer.OrdinalIgnoreCase);
78+
UriAttributes = new HashSet<string>(options.UriAttributes, StringComparer.OrdinalIgnoreCase);
79+
AllowedClasses = new HashSet<string>(options.AllowedCssClasses, StringComparer.OrdinalIgnoreCase);
80+
AllowedCssProperties = new HashSet<string>(options.AllowedCssProperties, StringComparer.OrdinalIgnoreCase);
81+
AllowedAtRules = new HashSet<CssRuleType>(options.AllowedAtRules);
82+
}
83+
6984
/// <summary>
7085
/// Initializes a new instance of the <see cref="HtmlSanitizer"/> class.
7186
/// </summary>
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using AngleSharp.Css.Dom;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Ganss.XSS
6+
{
7+
/// <summary>
8+
/// Provides options to be used with <see cref="HtmlSanitizer"/>.
9+
/// </summary>
10+
public class HtmlSanitizerOptions
11+
{
12+
/// <summary>
13+
/// Gets or sets the allowed tag names such as "a" and "div".
14+
/// </summary>
15+
public ISet<string> AllowedTags { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
16+
17+
/// <summary>
18+
/// Gets or sets the allowed HTML attributes such as "href" and "alt".
19+
/// </summary>
20+
public ISet<string> AllowedAttributes { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
21+
22+
/// <summary>
23+
/// Gets or sets the allowed CSS classes.
24+
/// </summary>
25+
public ISet<string> AllowedCssClasses { get; set; } = new HashSet<string>();
26+
27+
/// <summary>
28+
/// Gets or sets the allowed CSS properties such as "font" and "margin".
29+
/// </summary>
30+
public ISet<string> AllowedCssProperties { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
31+
32+
/// <summary>
33+
/// Gets or sets the allowed CSS at-rules such as "@media" and "@font-face".
34+
/// </summary>
35+
public ISet<CssRuleType> AllowedAtRules { get; set; } = new HashSet<CssRuleType>();
36+
37+
/// <summary>
38+
/// Gets or sets the allowed URI schemes such as "http" and "https".
39+
/// </summary>
40+
public ISet<string> AllowedSchemes { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
41+
42+
/// <summary>
43+
/// Gets or sets the HTML attributes that can contain a URI such as "href".
44+
/// </summary>
45+
public ISet<string> UriAttributes { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
46+
}
47+
}

test/HtmlSanitizer.Tests/Tests.cs

+25
Original file line numberDiff line numberDiff line change
@@ -3352,5 +3352,30 @@ public void HexColorTest()
33523352

33533353
Assert.Equal(@"<p style=""color: rgba(0, 0, 0, 1)"">Text</p>", sanitized);
33543354
}
3355+
3356+
[Fact]
3357+
public void WithOptions()
3358+
{
3359+
// Arrange
3360+
var options = new HtmlSanitizerOptions
3361+
{
3362+
AllowedTags = new HashSet<string>() { "strong", "em", "p" },
3363+
AllowedAttributes = new HashSet<string>() { "title" },
3364+
AllowedCssClasses = new HashSet<string>(),
3365+
AllowedCssProperties = new HashSet<string>(),
3366+
AllowedAtRules = new HashSet<CssRuleType>(),
3367+
AllowedSchemes = new HashSet<string>() { "https" },
3368+
UriAttributes = new HashSet<string>()
3369+
};
3370+
var sanitizer = new HtmlSanitizer(options);
3371+
3372+
// Act
3373+
var htmlFragment = "<strong>Lorem ipsum</strong>";
3374+
var actual = sanitizer.Sanitize(htmlFragment);
3375+
3376+
// Assert
3377+
var expected = "<strong>Lorem ipsum</strong>";
3378+
Assert.Equal(expected, actual);
3379+
}
33553380
}
33563381
}

0 commit comments

Comments
 (0)