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 |
Tainted data might flow into a field annotated as [SqlTrusted], or into an SQL query.
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:
| Class Name | SQL Injection (C#) | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | security | |||||||||||||||||||||
| Mnemonic | CSHARP.IO.INJ.SQL | |||||||||||||||||||||
| Categories |
|
|||||||||||||||||||||
| 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="SQL Injection (C#)" |
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;
}
}
}
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 safe statements to propagate data inside the program.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.