// it's unfortunately common for developers to
// forget to HTML-encode a string once it has been JS-encoded,
// so this offers extra protection.
- DefaultHtmlEncoder.ForbidHtmlCharacters(_allowedCharacters);
+ HtmlEncoderHelper.ForbidHtmlCharacters(_allowedCharacters);
// '\' (U+005C REVERSE SOLIDUS) must always be escaped in Javascript / ECMAScript / JSON.
// '/' (U+002F SOLIDUS) is not Javascript / ECMAScript / JSON-sensitive so doesn't need to be escaped.
// it's unfortunately common for developers to
// forget to HTML-encode a string once it has been JS-encoded,
// so this offers extra protection.
- DefaultHtmlEncoder.ForbidHtmlCharacters(allowedCharacters);
+ HtmlEncoderHelper.ForbidHtmlCharacters(allowedCharacters);
// '\' (U+005C REVERSE SOLIDUS) must always be escaped in Javascript / ECMAScript / JSON.
// '/' (U+002F SOLIDUS) is not Javascript / ECMAScript / JSON-sensitive so doesn't need to be escaped.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Internal;
// (includes categories Cc, Cs, Co, Cn, Zs [except U+0020 SPACE], Zl, Zp)
_allowedCharacters.ForbidUndefinedCharacters();
- ForbidHtmlCharacters(_allowedCharacters);
- }
-
- internal static void ForbidHtmlCharacters(AllowedCharactersBitmap allowedCharacters)
- {
- allowedCharacters.ForbidCharacter('<');
- allowedCharacters.ForbidCharacter('>');
- allowedCharacters.ForbidCharacter('&');
- allowedCharacters.ForbidCharacter('\''); // can be used to escape attributes
- allowedCharacters.ForbidCharacter('\"'); // can be used to escape attributes
- allowedCharacters.ForbidCharacter('+'); // technically not HTML-specific, but can be used to perform UTF7-based attacks
+ HtmlEncoderHelper.ForbidHtmlCharacters(_allowedCharacters);
}
public DefaultHtmlEncoder(params UnicodeRange[] allowedRanges) : this(new TextEncoderSettings(allowedRanges))
return true;
}
}
+
+ /// <summary>
+ /// Separates static methods from HtmlEncoder and DefaultHtmlEncoder so those classes can be trimmed
+ /// when only these static methods are needed.
+ /// </summary>
+ internal static class HtmlEncoderHelper
+ {
+ internal static void ForbidHtmlCharacters(AllowedCharactersBitmap allowedCharacters)
+ {
+ allowedCharacters.ForbidCharacter('<');
+ allowedCharacters.ForbidCharacter('>');
+ allowedCharacters.ForbidCharacter('&');
+ allowedCharacters.ForbidCharacter('\''); // can be used to escape attributes
+ allowedCharacters.ForbidCharacter('\"'); // can be used to escape attributes
+ allowedCharacters.ForbidCharacter('+'); // technically not HTML-specific, but can be used to perform UTF7-based attacks
+ }
+ }
}
// it's unfortunately common for developers to
// forget to HTML-encode a string once it has been URL-encoded,
// so this offers extra protection.
- DefaultHtmlEncoder.ForbidHtmlCharacters(_allowedCharacters);
+ HtmlEncoderHelper.ForbidHtmlCharacters(_allowedCharacters);
// Per RFC 3987, Sec. 2.2, we want encodings that are safe for
// four particular components: 'isegment', 'ipath-noscheme',