ASP.NET and FxCop
Does anybody have a guide on what FxCop rules should be enabled/disabled when analyzing an ASP.NET Application?
Thanks!
Does anybody have a guide on what FxCop rules should be enabled/disabled when analyzing an ASP.NET Application?
Thanks!
I'm not aware of any guides, but in general you may encounter violations that simply cannot be fixed because of ASP.NET's bizarre design. Newer versions of FxCop are better about special-casing ASP.NET situations.
For now, when you run into unfixable issues, you can either exclude them, or, if it's particularly annoying, disable the rule. It might be best to move all your business logic into a Class Library project so you can leave the full FxCop suite enabled at least against that part.
-Ryan / Kardax
FxCop was originally designed for reusable libraries and has tended to fire many, many false positives against ASP.NET binaries. We've enabled code analysis for web apps in VS2005 Developer, however, and as part of that work we've made a significant pass through the rule set in order to eliminate noise. We completed a few outstanding work items just yesterday. The result is that the next version of FxCop (1.34) will return a much more focused result set when targeting Web Apps. This noise reduction will occur only on v2.0 of the framework, btw, due to our dependency on a new CLR attribute (GeneratedCodeAttribute) that helps identify wizard or otherwise auto-generated code.
There is only one rule in FxCop, currently, that was written with web apps in mind and it is the security check 'Review Sql queries for security vulnerabilities'. We hope to greatly increase our web app analysis capabilities in the next version of VS.
Michael Fanning
MS Development: VSTS Code Analysis/FxCop
Hello!
I'm working on presentation for DevDays about Security.
I want to show how senior developers can minimize attack surface with tools such fxCop. (I have last version installed and ASP.NET 2.0)
I copied a code from ''Review Sql queries for security vulnerabilities'' Rule and make small changes. Exception didn't fire up, but vulnerability exists!
What should I do for firing this rule up?
public
partial class _Default : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}
protected void Button1_Click(object sender, EventArgs e){
labelResult.Text = tbQuery.Text;
DoQuery(tbQuery.Text);
}
public void DoQuery(string queryString){
SqlConnection someConnection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Werp;Integrated Security=True;Pooling=False"); SqlCommand someCommand = new SqlCommand();someCommand.Connection = someConnection;
someCommand.CommandText =
"SELECT LastName FROM Employees " + "WHERE FirstName='" + queryString + "'";someConnection.Open();
SqlDataReader reader = someCommand.ExecuteReader(); while (reader.Read()){
Response.Write(
"<li>" + reader[0]);}
someConnection.Close();
}
}
I simplified example and move method to the Class Library.
Rule dosn't fire up too for such simple method.
public List<String> DoQuery(string query){
SqlConnection someConnection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Werp;Integrated Security=True;Pooling=False"); SqlCommand someCommand = new SqlCommand();someCommand.Connection = someConnection;
someCommand.CommandText =
"SELECT LastName FROM Employees " + "WHERE FirstName='" + query + "' OR LastName ='" + query + '"';someConnection.Open();
SqlDataReader reader = someCommand.ExecuteReader(); List<String> result = new List<string>(); while (reader.Read()){
result.Add(reader[0].ToString());
}
someConnection.Close();
return result;}
But method _WITHOUT_ generic collection is performed well. Does VisitCall work for methods with generic types?
Actually, I have installed fxCop from Windows Vista SDK February CTP.
I reinstalled fxCop taken from gotdotnet (RC1) and no, it doesn't catch vulnerability with generics inside.
Here the sample. SQL Injection rule fires up only for DoQuery method
public static class MyService{
private static string connection = @"Data Source=.\sqlexpress;Initial Catalog=Werp;Integrated Security=True;Pooling=False"; public static string[] DoQuery(string query){
SqlConnection someConnection = new SqlConnection(connection); SqlCommand someCommand = new SqlCommand();someCommand.Connection = someConnection;
someCommand.CommandText =
"SELECT LastName FROM Employees " + "WHERE FirstName='" + query + "'";someConnection.Open();
SqlDataReader reader = someCommand.ExecuteReader(); ArrayList result = new ArrayList(); while (reader.Read()){
result.Add(reader[0]);
}
someConnection.Close();
return (string[])result.ToArray(typeof(String));}
public static List<String> DoQuery1(string query){
SqlConnection someConnection = new SqlConnection(connection); SqlCommand someCommand = new SqlCommand();someCommand.Connection = someConnection;
someCommand.CommandText =
"SELECT LastName FROM Employees " + "WHERE FirstName='" + query + "'";someConnection.Open();
SqlDataReader reader = someCommand.ExecuteReader(); List<String> result = new List<string>(); while (reader.Read()){
result.Add(reader[0].ToString());
}
someConnection.Close();
return result;}
}