The AntiXss class library is an open-source class library that prevents injection attacks. It uses the whitelist mechanism for content encoding. Currently, it supports these input types: XML, HTML, QueryString, HTMLFormURLEncode, Ldap, and JavaScript. In daily development, we do not securely encode input types such as Ldap or JavaScript. Most of them are encoded securely for XML, QueryString, or Form URL. The following is a small example of a secure encoded XML file:
Encoding XML
Static void EncodeXML ()
{
String attachedXML = @"
1
Book
80
10
1
& 80
10
";
Regex extractRegex = new Regex (@" \ S * (.*?) \ S * (.*?) \ S * (.*?) \ S * (.*?) \ S *");
String xmlNodeFormat = @" {1} {2} {3} {4} ";
StringBuilder safeXml = new StringBuilder ();
MatchCollection matches = extractRegex. Matches (attachedXML );
SafeXml. AppendLine (" ");
Foreach (Match item in matches)
{
SafeXml. AppendLine (string. Format (xmlNodeFormat, AntiXssLibrary. Encoder. XmlEncode (item. Groups [1]. Value)
, AntiXssLibrary. Encoder. XmlEncode (item. Groups [2]. Value)
, AntiXssLibrary. Encoder. XmlEncode (item. Groups [3]. Value)
, AntiXssLibrary. Encoder. XmlEncode (item. Groups [4]. Value)
, AntiXssLibrary. Encoder. XmlEncode (item. Groups [5]. Value )));
}
SafeXml. AppendLine (" ");
Console. WriteLine ("unsafe xml: \ n" + attachedXML );
Console. WriteLine ("safe xml: \ n" + safeXml );
/* OUT PUT
1
Book
80
10
1
& 80
10
*/