ASP.NET Data Input Validation

by admin 1. November 2008 18:18

Data Input I'm no security expert, and as such, I think I'm a member of an ever-growing group of Web developers who fly by the seat of their pants when it comes to the security of their Web forms. As an ASP.NET developer I have had a tendency to presume that the framework is going to insulate me from most of the "nuts and bolts stuff". Of course this is not the kind of beneficial abstraction that frameworks were meant to provide us with. It is up to each of us to take responsibility for the code we create. Testing, even when it is carried out, shouldn't stop as soon as we find out that our code "is working". There needs to be some baseline better practise for creating everyday Web forms other than relying on ValidateRequest being set to true.

Recently I published a post entitled 500,000 SQL Injection Attacks. The half a million attacks actually occurred in a single week. This is mind boggling. A lot of the attacks targeted older ASP sites and I was surprised at how much of this old code was still out there. What surprised me most was how many idiots were out there calling themselves developers, not to mention the bottom-line execs who hired them in the first place. That 500,000 sites were attacked in a single week should be telling us something. We need more qualified programmers in the industry and we need the education system to introduce students to the World of IT that they are going to be living in.

Most security holes are created by developers with little understanding of security issues. These security holes are then exploited by hackers who understand these security issues only too well. It's a lethal recipe. Developers need to understand what it is they have to protect against and how to go about doing it. The tools and guidance for creating safer Web forms are available to us right now if we know where to look.

From a security point of view, our Web forms are naked and 100% vulnerable. We need to look at all the ways data is passed to them and test as appropriate:

    * Form Fields
    * URL Query Strings
    * Cookies
    * Database
    * ViewState

The most prevalent forms of attack seem to be Script Injection, Cross Site Scripting and SQL Injection. As for SQL Injection, this can be mitigated against by using parameterized queries. It is the act of parameterizing the database queries that make stored procedures so resilient to attack. The other forms of script attack can be handled by downloading and using the Microsoft Anti-Cross Site Scripting Library in your Web application projects.

A best practise would consist of the use of this library in conjunction with proper data validation (validators) and filtering (regular expressions). If you have existing code which you know is vulnerable you can still use tools to inspect your code and then you can implement the necessary protection measures where needed.

On the MSDN Code Gallery, there is a complete ASP.NET 2.0 Reference Security Implementation which you can download and explore. It was created in VS 2005 and includes an installer. This is a very helpful resource to answer any questions you may have. Note: you must have VS 2005 installed for this to work.

I've saved the best news for last :-) The Microsoft CIGS (Connected Information Security Group) are working on the new Security Runtime Engine. It is a HTTP module which will provide protection against the most common Web application security vulnerabilities, including Cross Site Scripting. The CIGS group are currently testing it and the beta should be available shortly.

kick it on   PHP, ASP, .NET, JSP Resources, Reviews

If anything can drive this Irishman into a fit of cursing, it's the need to create a regular expression quickly. With a little application, I know I could learn to create them myself but I figured some time back that the frequency with which I use them would not equate to the time invested in the study. So, I either have to rely on ones that I have, and can never locate, or turn to a good tool.

Well, I was reading Professional ASP.NET 3.5 in C# and VB by Scott Hansleman and others recently when I saw mention of a Regular Expression Editor in VS 2008. This was news to me! Then I noticed a regular expression facility in the Find and Replace tool (CTRL-F); how long has that one been around? Anyway, it was just what I needed for some basic regex's for email and phone numbers.

VS 2008 Regular Expression Editor


The only problem is trying to find the thing... they couldn't have hid it better if they tried. First, you have to be in design view to begin to locate it. Do people actually use design view? Then you have to refer to the control's properties window and click on the button next to the ValidationExpression property to launch the editor. Obvious, huh? :-|

VS 2008 Regular Expression Editor


Granted, it's pretty basic but if you're in a hurry this will save you some Googling time! I wonder is it possible to extend this tool to add in one's own custom regex's for reuse?

One more thing: Scott's book is a great reference with over 1600 pages of info. I have only two gripes about it: 1) If you read it in bed you'll need a hoist to avoid the hernia, and 2) The entire book is based on the Web Site template. What were they thinking of?

kick it on   PHP, ASP, .NET, JSP Resources, Reviews

Here's another new toy I stumbled upon - the new AutoCollage tool from Microsoft Research. This is a very easy-to-use image processing program which will automatically create a polished collage from a folder of your favorite pics.

AutoCollage 2008


This is a really handy way to create personalized Christmas cards on the fly and avoid the shopping lines at the mall! All you have to do is pick a folder and click a button. Download the one month trial here.

AutoCollage 2008


Here's a short video presentation of AutoCollage in action :-)

kick it on