C#


CSHARP.IO.TAINT.TRUSTED : Tainted @Trusted Value (C#)

要旨

Tainted data flows into a field or parameter annotated as [Trusted].

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:

プロパティ

クラス名 Tainted @Trusted Value (C#)
日本語クラス名 Tainted @Trusted Value (C#)
クラス分類 セキュリティ (security)
ニーモニック CSHARP.IO.TAINT.TRUSTED
カテゴリー
CWE CWE:74 Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
OWASP-2017 OWASP-2017:A1 Injection
OWASP-2021 OWASP-2021:A3 Injection
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Tainted @Trusted Value (C#)"

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
                }
            }
        }
    }
}

解決法

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 prepared statements for SQL queries rather than free queries built from string concatenation.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。