formaui-net.Framework.Sanitization 1.0.0-preview.27

Prefix Reserved
This is a prerelease version of formaui-net.Framework.Sanitization.
dotnet add package formaui-net.Framework.Sanitization --version 1.0.0-preview.27
                    
NuGet\Install-Package formaui-net.Framework.Sanitization -Version 1.0.0-preview.27
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="formaui-net.Framework.Sanitization" Version="1.0.0-preview.27" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="formaui-net.Framework.Sanitization" Version="1.0.0-preview.27" />
                    
Directory.Packages.props
<PackageReference Include="formaui-net.Framework.Sanitization" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add formaui-net.Framework.Sanitization --version 1.0.0-preview.27
                    
#r "nuget: formaui-net.Framework.Sanitization, 1.0.0-preview.27"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package formaui-net.Framework.Sanitization@1.0.0-preview.27
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=formaui-net.Framework.Sanitization&version=1.0.0-preview.27&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=formaui-net.Framework.Sanitization&version=1.0.0-preview.27&prerelease
                    
Install as a Cake Tool

Forma.Framework.Sanitization

An allowlist HTML sanitizer for content people author, such as a rich text editor's output or a pasted document, so the application can render it as markup. It keeps what a markup policy permits and removes the rest, and it depends on nothing beyond .NET itself. Beside it stands AddressPolicy, the rule for the addresses written into a page, which Forma's components and the default policy apply.

Install and register

dotnet add package formaui-net.Framework.Sanitization --prerelease
using Forma.Framework.Sanitization;

builder.Services.AddFormaSanitizationServices();

AddFormaSanitizationServices registers IMarkupPolicy as RichContentMarkupPolicy.Default and IHtmlSanitizer as AllowlistHtmlSanitizer, both singletons, each with TryAdd, so a policy the host registered first is the one the sanitizer applies.

Member What it is
IHtmlSanitizer.Sanitize(html) The HTML with everything its policy refuses removed; an empty string for blank input.
IHtmlSanitizer.MarkupPolicy The policy the sanitizer applies.
IMarkupPolicy.Allows(subject, value, owner, out canonical) Whether a subject is permitted, and the spelling to write it in, or null to keep the one given. The owner is the element an attribute or an address belongs to.
HtmlSubject What a policy is asked about: Element, ElementContent, Attribute, StyleProperty, StyleDeclaration or Url.
AddressPolicy.Allows(address, kind) Whether an address of an AddressKind may be the given address.
AddressPolicy.CanRunScript(address) Whether an address can run script where a page writes it: javascript:, vbscript:, and data: of anything but a raster image, a video, audio or captions.
AddressPolicy.GetScheme(address) The address's scheme as the browser reads it, in lowercase, or null for a relative address.

AllowlistHtmlSanitizer can also be created directly: with no argument it applies RichContentMarkupPolicy.Default, and with an IMarkupPolicy it applies that one.

Sanitizing

using Forma.Framework.Sanitization;

public sealed class ArticleService(IHtmlSanitizer sanitizer)
{
    public string Prepare(string authoredHtml)
        => sanitizer.Sanitize(authoredHtml);
}
var sanitizer = new AllowlistHtmlSanitizer();

sanitizer.Sanitize("<p onclick=\"steal()\">Safe <b>text</b></p><script>alert(1)</script>");
// "<p>Safe <b>text</b></p>"

sanitizer.Sanitize("<a href=\"javascript:alert(1)\" title=\"a > b\">Click</a>");
// "<a title=\"a &gt; b\">Click</a>"

sanitizer.Sanitize("<P CLASS=\"lead\">Text<br></br></P>");
// "<p class=\"lead\">Text<br /></p>"

sanitizer.Sanitize("<p>2 < 3</p><meta http-equiv=\"refresh\"");
// "<p>2 &lt; 3</p>&lt;meta http-equiv=\"refresh\""

Under any policy, the sanitizer drops event handler attributes, those whose name starts with on, and every address that can run script, which AddressPolicy.CanRunScript tells, along with comments, doctypes and processing instructions; removes an element whose content the policy refuses together with that content; writes void elements such as br and img self-closed; re-escapes every attribute value it keeps; and writes a < that opens no complete tag as &lt;, so the < of 2 < 3, or a tag left open at the end of the content, stays text. Every tag in its output is one it wrote from what the policy kept. A style attribute is kept while at least one of its declarations is permitted.

The default policy

RichContentMarkupPolicy permits the markup a rich text editor produces:

  • Elements: headings, paragraphs, quotes and code; inline formatting; line breaks and rules; lists; links; images, video and audio; tables; span, div, details, summary, figure and figcaption. script, style, iframe, object, svg, math, noscript, template, applet and frameset are removed with their content.
  • Attributes: class, id, style, dir, title and lang on any element; the address, target and relation of a link; the source and dimensions of media; the span and scope of a table cell; a list's numbering; and any data- attribute.
  • Inline styles: color, font, text alignment and decoration, line height, margin, padding, border and size, and any custom property starting with --forma-. A declaration containing url(, javascript:, expression(, @import or behavior: is dropped.
  • URLs: what the address rule below allows for the element the address belongs to, judged as a link on an a, as an image's source on an img, as a video's source or its poster on a video, as an audio's source on an audio, and as a video's or an audio's on a source. The address also has to name something: a path within the site, rooted at / or relative to the document, whose first segment holds no colon and which holds no whitespace; a fragment past its #; or something past a scheme's colon and the slashes before its host.

Elements, attributes and style properties are written in lowercase; a data- attribute and a --forma- property keep the spelling they were written in.

Addresses

AddressPolicy decides which addresses may be written where, so that none runs script where it stands. What an address is to its element is its AddressKind, and each kind allows the schemes in its row. A relative address takes the page's own scheme, and every kind allows it.

AddressKind Written as Allows
Link A link a reader follows, an anchor's href http, https, mailto, tel
DownloadLink A link that downloads, an anchor carrying download A link's schemes, blob: and any data:
ImageSource An image's src, a video's poster http, https, blob:, and data: of a PNG, APNG, JPEG, GIF, WebP, AVIF, BMP or icon image
VideoSource A video's src http, https, blob:, and data: of a video
AudioSource An audio's src http, https, blob:, and data: of an audio
TrackSource A text track's src, such as a video's captions http, https, blob:, and data: of WebVTT
FrameSource A frame's src, an object's data http, https, blob:
using Forma.Framework.Sanitization;

AddressPolicy.Allows("https://example.com/stage-12", AddressKind.Link);        // true
AddressPolicy.Allows("JaVaScRiPt:alert(1)", AddressKind.Link);                 // false
AddressPolicy.Allows("data:image/png;base64,iVBORw0KGgo=", AddressKind.ImageSource); // true
AddressPolicy.Allows("data:image/png;base64,iVBORw0KGgo=", AddressKind.Link);  // false
AddressPolicy.GetScheme("\u0001 java\tscript:alert(1)");                       // "javascript"

An address is judged as the browser reads it. The browser drops the control characters and spaces at either end of an address and every tab and line break within it, so java&#9;script: and a javascript: behind a control character are refused as javascript: is. Its scheme is what stands before the first colon where that is a letter followed by letters, digits, +, - or ., in any case. A data: address is judged by the media type before its comma, and one without a comma carries nothing. An SVG image is left out of the image kind, since an SVG document can carry script.

Forma's components write an address its kind doesn't allow inert: the element keeps its text and takes no address, and the refusal is logged at Debug under the component, naming the scheme rather than the address.

Writing a policy

A host whose documents carry different markup implements IMarkupPolicy. This one permits only inline elements and defers everything else to the default policy:

using Forma.Framework.Sanitization;

public sealed class CommentMarkupPolicy : IMarkupPolicy
{
    private static readonly HashSet<string> InlineTags = new(StringComparer.OrdinalIgnoreCase)
    {
        "p", "b", "strong", "i", "em", "code", "a", "br"
    };

    public bool Allows(HtmlSubject subject, ReadOnlySpan<char> value, ReadOnlySpan<char> owner, out string? canonical)
    {
        if (subject == HtmlSubject.Element && !InlineTags.Contains(value.ToString()))
        {
            canonical = null;
            return false;
        }

        return RichContentMarkupPolicy.Default.Allows(subject, value, owner, out canonical);
    }
}
builder.Services.AddSingleton<IMarkupPolicy, CommentMarkupPolicy>();
builder.Services.AddFormaSanitizationServices();

License

Apache License 2.0, under the repository's LICENSE.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net11.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on formaui-net.Framework.Sanitization:

Package Downloads
formaui-net.Blazor

A Blazor component library for .NET 11: form inputs and pickers, dialogs, popovers and toasts, toolbars and menus, tabs and layout, file pickers, uploaders and viewers, icons, emoji and windows.

formaui-net.Framework.Media

The provider contract FormaVideoPlayer plays through.

formaui-net.Framework.Composer.Extensibility

The contracts a FormaComposer plugin is written against: what a plugin contributes, what it reads of the composer while it runs, and what it can ask the composer to do.

formaui-net.Blazor.Explorer

FormaExplorer, a file manager for the Forma component library over items and folders the host already holds: a folder tree, an address bar with history and breadcrumbs, thumbnail, details and list views, search, sorting and grouping, selection, drag and drop between folders, upload, and a preview of any item.

formaui-net.Blazor.Composer

FormaComposer, a rich text authoring surface for Blazor: an editor with the toolbar, dialogs, views and attachments an article or a page body needs, assembled from plugins the host picks.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-preview.27 61 12/3/2026
1.0.0-preview.26 73 12/3/2026
1.0.0-preview.25 75 12/2/2026
1.0.0-preview.24 95 12/1/2026
1.0.0-preview.23 145 11/30/2026
1.0.0-preview.22 137 11/30/2026
1.0.0-preview.21 145 11/29/2026
1.0.0-preview.20 141 11/26/2026
1.0.0-preview.19 144 11/26/2026
1.0.0-preview.18 133 11/26/2026
1.0.0-preview.17 124 11/26/2026
1.0.0-preview.16 118 11/24/2026
1.0.0-preview.15 116 11/23/2026
1.0.0-preview.14 116 11/23/2026
1.0.0-preview.13 109 11/21/2026
1.0.0-preview.12 113 11/20/2026
1.0.0-preview.11 113 11/18/2026
1.0.0-preview.10 114 11/17/2026
1.0.0-preview.9 147 11/17/2026
1.0.0-preview.8 127 11/17/2026
Loading failed

What this release changed is in Forma's changelog: https://github.com/FormaUI/Forma/blob/v1.0.0-preview.27/CHANGELOG.md