Orchard Core Sanitizer - Prompt Templates
Configure HTML Sanitization
You are an Orchard Core expert. Generate HTML sanitization configurations and custom sanitizer settings for Orchard Core.
Guidelines
- The HTML Sanitizer is part of Orchard Core's infrastructure — no additional feature needs to be enabled.
- The sanitizer prevents XSS attacks by cleaning the final rendered HTML. Authored HTML and Markdown source is stored unchanged; nothing is sanitized on save.
- It is used by default for
HtmlBodyPart,HtmlField,HtmlMenuItemPart,MarkdownBodyPart, andMarkdownField. - Sanitization can be disabled per field/part via the
Sanitize Htmloption (SanitizeHtml = false) for trusted output. - Use the
@Orchard.SanitizeHtml()Razor helper, thesanitize_htmlLiquid filter, orIHtmlSanitizerService.Sanitize()in code. - Orchard Core customizes the default HtmlSanitizer by allowing the
classattribute, allowingmailtoandtelschemes, and removing theformtag. - Configure the sanitizer using
ConfigureHtmlSanitizerduring service registration; multiple calls are combined. - Sanitizer configuration actions must be deterministic, because results are cached and reused by the same tenant (see below).
- The sanitizer is based on the HtmlSanitizer library.
- Always seal classes.
Parts and Fields Using the Sanitizer
| Part / Field | Pipeline before sanitization |
|-------------|-------------|
| HtmlBodyPart | Authored HTML, then shortcodes. |
| HtmlField | Authored HTML, then shortcodes. |
| HtmlMenuItemPart | Sanitized on a cloned content item when the menu renders; unsafe URL schemes are rejected. |
| MarkdownBodyPart | Markdown converted to HTML, then shortcodes. |
| MarkdownField | Markdown converted to HTML, then shortcodes. |
This applies consistently to Detail, Summary, Preview, BodyAspect, GraphQL
rendered HTML, and the MarkdownToHtmlAsync Razor helper. LiquidPart and
LiquidField have no sanitize setting because their output may not be HTML.
Because the stored source is unsanitized, render the part or field shape (or
sanitize explicitly) instead of writing stored Html with raw:
{{ Model.Content.HtmlBodyPart | shape_render }}
{{ Model.ContentItem.Content.Article.Summary.Html | sanitize_html | raw }}
Orchard Core Default Sanitizer Customizations
Orchard Core modifies the base HtmlSanitizer defaults:
| Change | Description |
|--------|-------------|
| Allow class attribute | The class attribute is added to the allowed attributes list. |
| Allow mailto scheme | The mailto: URI scheme is allowed in links. |
| Allow tel scheme | The tel: URI scheme is allowed in links. |
| Remove form tag | The <form> tag is removed from the allowed tags list. |
Sanitized Result Cache
Content is sanitized each time it is displayed. HtmlSanitizerService caches
the sanitized result of fragments up to 64K characters, reused only by the same
tenant, so repeated displays skip parsing. The cache is shared by all tenants,
so its size (about 8 MB at most) doesn't grow with the number of tenants, and
entries not read for 10 minutes are evicted. Consequently, actions
registered with ConfigureHtmlSanitizer(), including sanitizer event handlers
such as PostProcessNode, must return the same result for the same input;
do not make them depend on the current user, request, or time.
Sanitizing HTML in Razor Views
@Orchard.SanitizeHtml((string)Model.ContentItem.Content.HtmlBodyPart.Html)
Sanitizing HTML in Code
public sealed class CommentFormatter
{
private readonly IHtmlSanitizerService _htmlSanitizerService;
public CommentFormatter(IHtmlSanitizerService htmlSanitizerService)
{
_htmlSanitizerService = htmlSanitizerService;
}
public string Format(string html)
=> _htmlSanitizerService.Sanitize(html);
}
IHtmlSanitizerService lives in OrchardCore.Infrastructure.Html.
Configuring the Sanitizer in Program.cs
builder.Services
.AddOrchardCms()
.ConfigureServices(tenantServices =>
tenantServices.ConfigureHtmlSanitizer((sanitizer) =>
{
// Allow additional URI schemes.
sanitizer.AllowedSchemes.Add("ftp");
sanitizer.AllowedSchemes.Add("data");
}));
Allowing Additional Tags
builder.Services
.AddOrchardCms()
.ConfigureServices(tenantServices =>
tenantServices.ConfigureHtmlSanitizer((sanitizer) =>
{
// Allow <iframe> tags for embedded content.
sanitizer.AllowedTags.Add("iframe");
}));
Allowing Additional Attributes
builder.Services
.AddOrchardCms()
.ConfigureServices(tenantServices =>
tenantServices.ConfigureHtmlSanitizer((sanitizer) =>
{
// Allow data attributes and srcset.
sanitizer.AllowDataAttributes = true;
sanitizer.AllowedAttributes.Add("srcset");
}));
Removing Tags from the Allow List
builder.Services
.AddOrchardCms()
.ConfigureServices(tenantServices =>
tenantServices.ConfigureHtmlSanitizer((sanitizer) =>
{
// Disallow inline styles and embedded media.
sanitizer.AllowedAttributes.Remove("style");
sanitizer.AllowedTags.Remove("video");
}));
Combining Multiple Sanitizer Configurations
builder.Services
.AddOrchardCms()
.ConfigureServices(tenantServices =>
tenantServices.ConfigureHtmlSanitizer((sanitizer) =>
{
// Allow iframes for video embeds.
sanitizer.AllowedTags.Add("iframe");
// Allow additional URI schemes.
sanitizer.AllowedSchemes.Add("ftp");
// Allow the target attribute on links.
sanitizer.AllowedAttributes.Add("target");
}));
Disabling Sanitization on a Field or Part
To disable sanitization for a specific field or part:
- Navigate to the content type definition in the admin UI.
- Edit the field or part settings (e.g.,
HtmlBodyPart). - Uncheck the Sanitize Html option.
Or in a migration: .WithSettings(new HtmlBodyPartSettings { SanitizeHtml = false }).
Only do this when every editor of the content type is trusted.
Common Sanitizer Configuration Scenarios
| Scenario | Configuration |
|----------|---------------|
| Allow YouTube/Vimeo embeds | AllowedTags.Add("iframe") |
| Allow FTP links | AllowedSchemes.Add("ftp") |
| Allow data URIs for inline images | AllowedSchemes.Add("data") |
| Allow custom data attributes | AllowDataAttributes = true |
| Allow heading ids from Markdown autoidentifiers | AllowedAttributes.Add("id") |
| Allow target attribute on links | AllowedAttributes.Add("target") |
| Block all inline styles | AllowedAttributes.Remove("style") |
Upgrading from 3.x
- Before: HTML and Markdown parts and fields sanitized the source when saving.
After: the source is stored as authored and only the final rendered HTML is
sanitized. Templates or APIs that output the stored Html directly must now
sanitize it themselves.
- Sanitizer configuration actions must be deterministic because of the new
sanitized result cache.