Insecure Processing of Data
This category covers the following issues:
Fixing Cross-Site Scripting
About XSS
Option A: Perform output encoding
Go through the issues that GuardRails identified in the PR/MR.
Look for patterns like this:
public class TestController : Controller
{
[HttpGet(""{myParam}"")]
public string Get(string myParam)
{
return "value " + myParam;
}
}And use safe alternatives like
HttpUtility.HtmlEncode
:public class TestController : Controller
{
[HttpGet(""{myParam}"")]
public string Get(string myParam)
{
return "value " + HttpUtility.HtmlEncode(myParam);
}
}Test it, ship it 🚢 and relax 🌴
Fixing Insecure Deserialization
About Deserialization
Option A: Don't parse untrusted data with XMLDecoder
An attacker may pass specially crafted serialized .NET object of specific class that will execute malicious code during the construction of the object, which can result in arbitrary code execution, full application compromise or denial of service.
Unfortunately, there is no simple fix for this. Ideally, no untrusted data is deserialized, such as user input from request parameters, or cookies.
Go through the issues that GuardRails identified in the PR/MR.
Look for code like this:
private void ConvertData(string json)
{
var mySerializer = new JavaScriptSerializer(new SimpleTypeResolver());
Object mything = mySerializer.Deserialize(json, typeof(SomeDataClass)/* the type doesn't matter */);
}Follow the steps detailed here
Test it, ship it 🚢 and relax 🌴
Fixing Insecure XML Processing
About XML External Entities
Option A: Prevent XML External Entity Attacks
XML External Entity (XXE) attacks can occur when an XML parser supports XML entities while processing XML received from an untrusted source. Note that in .NET Framework versions 4.5.2 and up, XmlTextReader’s internal XmlResolver is set to null by default, making the XmlTextReader ignore DTDs by default. The XmlTextReader can become unsafe if you create your own non-null XmlResolver with default or unsafe settings.
Go through the issues that GuardRails identified in the PR/MR.
Look for code like this:
// DTD expansion is enabled by default
XmlReaderSettings settings = new XmlReaderSettings();
XmlReader reader = XmlReader.Create(inputXml, settings);or:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(pathToXmlFile);
Console.WriteLine(xmlDoc.InnerText);And replace it with this:
var settings = new XmlReaderSettings();
// Prior to .NET 4.0
settings.ProhibitDtd = true; // default is false!
// .NET 4.0 - .NET 4.5.2
settings.DtdProcessing = DtdProcessing.Prohibit; // default is DtdProcessing.Parse!
XmlReader reader = XmlReader.Create(inputXml, settings);or:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null; // Setting this to NULL disables DTDs - Its NOT null by default.
xmlDoc.Load(pathToXmlFile);
Console.WriteLine(xmlDoc.InnerText);Test it, ship it 🚢 and relax 🌴
Fixing XPath Injection
About XPath Injection
Option A: Prevent XPATH Injection
If the user input is not properly filtered, a malicious user could extend the XPath query.
Go through the issues that GuardRails identified in the PR/MR.
Look for code like this:
var doc = new XmlDocument {XmlResolver = null};
doc.Load("/config.xml");
var results = doc.SelectNodes("/Config/Devices/Device[id='" + input + "']");And replace it with this:
Regex rgx = new Regex(@"^[a-zA-Z0-9]+$");
if(rgx.IsMatch(input))
{
XmlDocument doc = new XmlDocument {XmlResolver = null};
doc.Load("/config.xml");
var results = doc.SelectNodes("/Config/Devices/Device[id='" + input + "']");
}Test it, ship it 🚢 and relax 🌴
Fixing LDAP Injection
About LDAP Injection
Option A: Prevent LDAP Injection
Go through the issues that GuardRails identified in the PR/MR.
Look for code like this:
var searcher = new DirectorySearcher();
searcher.Filter = "(cn=" + input + ")";or:
var dir = new DirectoryEntry();
dir.Path = $"GC://DC={input},DC=com";Follow the instructions provided by WASC-29, or leverage the proper encoder (LdapFilterEncode or LdapDistinguishedNameEncode) from the AntiXSS library:
// LdapFilterEncode
var searcher = new DirectorySearcher();
searcher.Filter = "(cn=" + Encoder.LdapFilterEncode(input) + ")";
// LdapDistinguishedNameEncode
var dir = new DirectoryEntry();
dir.Path = $"GC://DC={Encoder.LdapDistinguishedNameEncode(input)},DC=com";Test it
Ship it 🚢 and relax 🌴