Security vulnerabilities are not acceptable in sample code
Earlier this week the ASP.NET article of the day linked to 4-Tier Architecture in ASP.NET with C# which I noticed suffered from both HTML and SQL injection. I promptly informed the author and the ASP.NET site (who pulled the link) but the author was rather unconcerned and wrote (after editing my comment):
Thanks for your feedback Damieng. Sql statement has been used here to make the tutorial simple, it is informed in the DAL description.
The problem is people borrowing this code may not notice the vulnerability or understand how to fix it. This isn’t the first time I’ve seen easily exploited sample code, responded and been buffed off with the it’s just sample code excuse.
Writing secure code isn’t difficult, time consuming or confusing to read.
If you must write your own data-access-layer (DAL) code use parameterised queries and not string concatenation.
When outputting values be 100% sure whether your technique will encode the values for you or not and be aware of what encoding tools are available to you.
ASP & ASP.NET’s Response.Write and <%= %> methods do NOT encode for you and you should be using HttpUtility.HtmlEncode to output data to a HTML stream.
Samples of vulnerable and secure code are in my presentation on Web Security I gave at the Guernsey Software Developer Forum a few months ago.