HTML sanitization

Sitefinity CMS leverages the out-of-the-box HTML sanitizer to ensure HTML content, for example, scripts or tags, is safe and does not cause security vulnerabilities to your system. The reason is that raw HTML data may come from a variety of sources. The HTML sanitizer avoids cross-site scripting (XSS) attacks while in the meantime allowing users to contribute with HTML markup and content. The HTML sanitizer is configured to work out-of-the-box with built-in widgets to ensure elements from untrusted sources are stripped and are not displayed. The sanitizer identifies elements as untrusted if they are not included in the whitelist.

NOTE: If you are upgrading from versions older than Sitefinity CMS 10.2 and you want to leverage the HTML sanitizer whitelist capabilities, you need to manually switch on the sanitizer. To do so, navigate to Administration » Settings » Advanced » Security. Make sure the Disable HTML sanitization checkbox is not selected. We recommend that once you switch on the HTML sanitizer, you test your website for potential issues with HTML content and content editing.

Enable HTML sanitizer for custom widgets

You use the HTML sanitizer API for your custom widgets, widget templates, and views, so that untrusted content is filtered out.

Web Forms Sanitizer API

To use the HTML sanitizer in Web Form widgets, you call the following static methods in the Telerik.Sitefinity.Web.UI.ControlUtilities class:
  • string Sanitize(string), string Sanitize(object)
    You use these methods to remove or encode parts of the HTML content in the provided parameter. This content may include dangerous or potentially dangerous elements, for example, <script> tags or unescaped attribute values, so that the HTML content returned is safe to be displayed.
  • string SanitizeUrl(string)
    You use this method when the content rendered is an URL, provided by a user. The method ensures that no dangerous protocols, for example, javascript:, or unescaped query string parameter values are passed. The method is executed in the context of Sanitize(string) method for the values of the attributes that contain URL values.

Feather Sanitizer API

When working with Feather, you need to use the following helper methods in the Telerik.Sitefinity.Frontend.Mvc.Helpers namespace:

  • @Html.HtmlSanitize(ModelUserProvidedHtml)
  • @Html.HtmlSanitizeUrl(Model.UserProvidedUrl)

Modify the HTML sanitizer configuration

In case your website scenario requires you to change the list with whitelisted elements, you can modify the default configuration of the HTML sanitizer. To do this, you create a new HTML sanitizer that extends the out-of-the-box capabilities of the default sanitizer and adds more elements to the whitelist. The following sample demonstrates how to add a custom_attribute to the HTML sanitizer whitelist:

Modify the HTML sanitizer configuration with the .NET Core application renderer

If you have the .NET Core application setup as your renderer, in addition to the changes made in Sitefinity CMS, changes to the .NET core application must be made as well. This sample shows how to add attributes to the whitelesited elements:

After that you register the dependency injection in the program.cs file:

For more information you can review this sample project in GitHub.

HTML sanitizer allowlist

For a list of whitelisted elements such as attributes, tags, schemes etc. you can visit the HtmlSanitizer GitHub repository.

In addition, Sitefinity CMS includes the following elements in its HtmlSanitizer whitelist:

Attributes

  • controls
  • class 
  • id
  • sfref

Tags

  • iframe
  • video
  • audio
  • source
  • svg
  • use

Schemes

  • mailto

 

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?