This rule is designed to prevent Cross-Site Scripting (XSS) attacks, which occur when an application includes untrusted data in a new web page without proper validation or escaping. XSS attacks allow attackers to execute scripts in the victim’s browser, which can lead to a variety of malicious outcomes such as stealing sensitive data or performing actions on behalf of the user.
The importance of this rule lies in the potential for significant security breaches. XSS attacks can lead to unauthorized access, data theft, and other serious consequences. Therefore, it is crucial to ensure that your C# code is not susceptible to such vulnerabilities.
Good coding practices to avoid XSS attacks include always encoding user input before including it in HTML content, using functions like HtmlEncoder.Default.Encode or HttpUtility.HtmlEncode. Avoid using methods that might introduce vulnerabilities, such as Html.Raw or direct Response.Write with user input. Even when the input comes from a seemingly trusted source, it’s still a good idea to encode it, as it might contain dangerous payloads that were injected earlier.
Non-Compliant Code Examples
usingMicrosoft.AspNetCore.Mvc;usingSystem.Web;namespaceVulnerableApp{publicclassVulnerableController:Controller{ [HttpGet("/profile")]publicIActionResultShowProfile(stringusername){// Non-compliant: Unencoded user input in ContentreturnContent("<div>Hello, "+username+"</div>","text/html");} [HttpGet("/comment")]publicIActionResultShowComment(stringcomment){// Non-compliant: Html.Raw with user inputViewBag.UserComment=Html.Raw(comment);returnView();} [HttpGet("/search")]publicIActionResultSearch(stringquery){// Non-compliant: Direct Response.Write with user inputResponse.ContentType="text/html";Response.Write("<h2>Search results for: "+query+"</h2>");returnnewEmptyResult();}}}
Compliant Code Examples
usingMicrosoft.AspNetCore.Mvc;usingSystem.Web;usingSystem.Text.Encodings.Web;namespaceSecureApp{publicclassSecureController:Controller{ [HttpGet("/user-profile")]publicIActionResultShowUserProfile(stringusername){// Compliant: Using HTML encodingreturnContent("<div>Hello, "+HtmlEncoder.Default.Encode(username)+"</div>","text/html");// Also compliant: Using HttpUtility// return Content("<div>Hello, " + HttpUtility.HtmlEncode(username) + "</div>", "text/html");} [HttpGet("/welcome")]publicIActionResultWelcome(stringname){// Compliant: Static string without user inputreturnContent("<h1>Welcome to our site!</h1>","text/html");} [HttpGet("/product")]publicIActionResultShowProduct(intid){stringproductName=GetProductName(id);// From database, not user input// Compliant: Values from trusted sourcesViewBag.ProductName=productName;returnView();}}}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- csharp-security # Rules to enforce C# security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines