JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C#


CSHARP.IO.INJ.XSS : Cross Site Scripting (C#)

Summary

Tainted data flows into a field annotated as [CrossSiteTrusted], or into a script execution routine.

Checks for this class use the CodeSonar C# taint analysis. See Taint Tracking for CodeSonar C# Warning Classes for more information on this analysis, including lists of the methods that CodeSonar automatically recognizes as relevant and information about specifying additional methods that CodeSonar should recognize as taint sources, sinks, or sanitizers.

Use configuration parameter CSHARP_ANALYSIS_ADVANCED_INJECTION to specify the level of checking to perform for this and other taint-related warning classes:

Properties

Class Name Cross Site Scripting (C#)
Significance security
Mnemonic CSHARP.IO.INJ.XSS
Categories
CWE CWE:79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
  CWE:81 Improper Neutralization of Script in an Error Message Web Page
DISA-6r1 DISA-6r1:V-222602 The application must protect from Cross-Site Scripting (XSS) vulnerabilities.
DISA-5r3 DISA-5r3:V-70257 The application must protect from Cross-Site Scripting (XSS) vulnerabilities.
DISA-4r3 DISA-4r3:V-70257 The application must protect from Cross-Site Scripting (XSS) vulnerabilities.
OWASP-2017 OWASP-2017:A1 Injection
  OWASP-2017:A7 Cross site scripting (XSS)
OWASP-2021 OWASP-2021:A3 Injection
OWASP-2025 OWASP-2025:A05 Injection
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Cross Site Scripting (C#)"

Examples

Example 1
using com.juliasoft.julia.checkers.injection;
using System;

using System.IO;
using System.Web;
using System.Web.UI;


namespace DocumentationExamples
{

    public class BasicInjection : Page
    {
        public static void Main(string[] args)
        { }

        [SqlTrusted]
        private object sqlTrusted;
        [Trusted]
        private object trusted;
        public string a = "my friend";
        protected void Page_Load(object sender, EventArgs e)
        {
            ProcessRequest(Request, Response);
        }
        private void Test2([SqlTrusted] string s) { }
        private void Test3([Trusted] string s) { }
        private void ProcessRequest(HttpRequest request, HttpResponse response)
        {
            response.ContentType = "text/html;charset=UTF-8";
            string user = request.Params["user"];
            using (TextWriter p = response.Output)
            {
                p.WriteLine("User : " + user);  // Cross Site Scripting (C#) warning issued here
                p.WriteLine(user);              // Cross Site Scripting (C#) warning issued here
                p.WriteLine("hello");
                p.WriteLine("hello " + a);
            }
            if (user != null)
                Test2(user);                    // SQL Injection (C#) warning issued here 
            Test2("User: " + user);             //[SQL Injection (C#) warning issued here 
            Test2("hello");
            Test2("hello " + a);
            Test3(user);                        // Tainted @Trusted Value (C#) warning issued here 
            Test3("User: " + user);             // Tainted @Trusted Value (C#) warning issued here 
            Test3("hello");
            Test3("hello " + a);
            sqlTrusted = user;                  // SQL Injection (C#) warning issued here 
            trusted = user;                     // Tainted @Trusted Value (C#) warning issued here 
            sqlTrusted = "User: " + user;       // SQL Injection (C#) warning issued here 
            trusted = "User: " + user;          // Tainted @Trusted Value (C#) warning issued here 
            sqlTrusted = "hello";
            trusted = "hello";
            sqlTrusted = "hello " + a;
            trusted = "hello " + a;
        }
    }
}
Example 2
using com.juliasoft.julia.checkers.flows;
using com.juliasoft.julia.checkers.injection;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Web.UI;

namespace DocumentationExamples
{

    public class Injection : Page
    {
        public static void Main(string[] args)
        { }

        [SqlTrusted]
        private object sqlTrusted;
        [Trusted]
        private object trusted;

        public void Page_Load(object source, EventArgs e)
        {
            ProcessRequest(Request, Response);
        }

        public void Test1([UntrustedUserInput] string user)
        {
            SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            string query = "SELECT * FROM  User where userId='" + user + "'";


            cmd.CommandText = query;             // SQL Injection (C#) warning issued here
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

            reader = cmd.ExecuteReader();
            Console.WriteLine(cmd.CommandText);

            // the analyzer does not like these
            Test2(query + " extra characters");  // SQL Injection (C#) warning issued here 
            Test3(query + " hello");             // Tainted @Trusted Value (C#) warning issued here

            // while the analyzer likes this!
            Test2("driver".Substring(3, 5));

            sqlConnection1.Close();
        }

        private void Test2([SqlTrusted] string s) { }

        private void Test3([Trusted] string s) { }

        private void ProcessRequest(HttpRequest request, HttpResponse response)
        {
            response.ContentType = "text/html;charset=UTF-8";

            string user = request.Params["user"];

            using (SqlConnection conn = new SqlConnection("Connection String"))
            using (TextWriter p = response.Output)
            {
                SqlCommand st = conn.CreateCommand();
                string query = "SELECT * FROM  User where userId='" + user + "'";
                p.WriteLine(query);              // Cross Site Scripting (C#) warning issued here
                st.CommandText = query;          // SQL Injection (C#) warning issued here
                st.CommandType = CommandType.Text;
                SqlDataReader reader = st.ExecuteReader();
                trusted = query.Substring(5, 7); // Tainted @Trusted Value (C#) warning issued here
                sqlTrusted = query;              // SQL Injection (C#) warning issued here

                p.WriteLine("Results");
                while (reader.NextResult())
                {
                    string s = reader["address"] as string;
                    p.WriteLine("\t\t" + s);     // Cross Site Scripting (C#) warning issued here
                }
            }
        }
    }
}

Resolution

Verify that a security error actually corresponds to unconstrained information flow from source locations into sink locations. If this is the case, add a sanitizing algorithm to clean data before it flows into sinks, or use safe statements to propagate data inside the program.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.