views:

2600

answers:

5

Is there a library or acceptable method for sanitizing the input to an html page?

In this case I have a form with just a name, phone number, and email address.

Code must be C#.

For example:

"<script src='bobs.js'>John Doe</script>" should become "John Doe"
+16  A: 

You could use the StackOverflow santization method from here.

Bryant
That allows way too much in.
Chris Lively
The approach takes a whitelist approach, so just use the same code but don't include any tags in your whitelist. It will strip everything out.
Bryant
A: 

You are looking for RegEx class and for pattern like this <(.|\n)*?>.

You can find a lot of examles on google.

Jakub Šturc
Using RegEx for this is not very wise, this will give you a false sense of security since there are always corner cases that gives a hacker a way to still inject script tags or other things into your fields. RegEx is not made to sanitize input...
Redbeard 0x0A
+5  A: 

If by sanitize you mean REMOVE the tags entirely, the RegEx example referenced by Bryant is the type of solution you want.

If you just want to ensure that the code DOESN'T mess with your design and render to the user. You can use the HttpUtility.HtmlEncode method to prevent against that!

Mitchel Sellers
Is there a reason to do that instead of the simpler regex by Jakub?
Chris Lively
The regex solution will remove the code, it works....but takes time. HtmlEncode, just formats it in a safe manner for web display.
Mitchel Sellers
+4  A: 

Based on the comment you made to this answer, you might find some useful info in this question:
http://stackoverflow.com/questions/72394/what-should-a-developer-know-before-building-a-public-web-site

Here's a parameterized query example. Instead of this:

string sql = "UPDATE UserRecord SET FirstName='" + txtFirstName.Text + "' WHERE UserID=" + UserID;

Do this:

SqlCommand cmd = new SqlCommand("UPDATE UserRecord SET FirstName= @FirstName WHERE UserID= @UserID");
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = txtFirstName.Text;
cmd.Parameters.Add("@UserID", SqlDbType.Integer).Value = UserID;


Edit: Since there was no injection, I removed the portion of the answer dealing with that. I left the basic parameterized query example, since that may still be useful to anyone else reading the question.
--Joel

Joel Coehoorn
Actually, no. I was just trying to be proactive with some new development. Great info though.
Chris Lively
Make sure you've seen the latest edit: I added a very useful link at the bottom.
Joel Coehoorn
BTW, I'm already using s'procs anyway. I just want to make sure that systems downstream (which I have absolutely no control over) don't incorrectly deal with the input.
Chris Lively
+3  A: 

What about using Microsoft Anti-Cross Site Scripting Library?

stian.net
Interesting. When I have time I'll play with it. Looks promising though.
Chris Lively