5

What is the best practice? I am making a ASP.NET site where the user can input text data to be stored at a SQL database. I am using HttpUtility.HTNLEncode() to store the data and HTMLDecode to display it.

This works well, but it does searching (selecting or free text) a lot more difficult. The user should be able to enter text containing <, ", ' and any other problematic character.

What is the best practice? To store the data un-encoded? How can I mitigate the risks of injection then?

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Lobuno
  • 1,405
  • 1
  • 18
  • 28

4 Answers4

6

Always store user input in the database unencoded, and always encode user input from database before outputting it.

You also should filter/validate user input before persisting.

  • Input: User input -> Validate/filter -> Persist to database
  • Output: Content from database -> Encode -> Output to client

This is the only sane way to use and reuse user data.

See also http://msdn.microsoft.com/en-us/library/t4ahd590%28v=vs.80%29.aspx#cpconbestsecuritypracticesforwebapplicationsanchor4 as well as Should HTML be encoded before being persisted?

Community
  • 1
  • 1
marapet
  • 54,856
  • 12
  • 170
  • 184
  • i did not dwonvote, but the goal here is to have the data safe even in case of injection, if i store the data unencoded and encode it on output , what's the sense? – Freeman Sep 05 '12 at 12:02
  • 1
    If you encode all data coming from database before outputting it, you're perfectly safe. That's best practice, in razor even default (the `@` syntax). There is no point in encoding all the content of your database - data itself is not dangerous, it's when you accept and output unfiltered and unencoded data where danger lies. – marapet Sep 05 '12 at 12:11
  • 1
    i am talking about sql injection here, its not about the data being dangerous, its in the event an attacker does succed with an injection and steals your data, in that event he can read it because you did not encode it before storing it in the database. Thats what i am reffering to. – Freeman Sep 05 '12 at 12:14
  • 2
    I don't think this question is about SQL injection - to prevent that, you should use parametrized queries, not HTMLEncode/HTMLDecode. – marapet Sep 05 '12 at 12:18
  • 3
    *Escape* the data when storing to prevent SQL injection, do not *encode* it. –  Sep 05 '12 at 12:20
2

There are a few areas to cover here, so I'll do my best to cover the points. The points are:

  • Submitting potentially unsafe text
  • Storing unsafe text
  • Displaying unsafe text

ASP.NET has a validation mechanism (as pointed out by @Candie) to be a first line defence against an attack. If you have an app that needs to submit HTML, XML, JS etc, you'll have to override the validation to allow it through.

Once the data is through, I would say it is safe to store. The best way to store this data is through the use of Stored Procedures, and not dynamic T-SQL, as it can lead to SQL Injection attacks.

The only problem left now comes from displaying that data verbatim in HTML. If you literally dump content onto the screen, this is where your problems may begin to become more apparent. So this is where your HTMLEncode comes into play. Characters are converted to HTML equivelant codes, so as not to be a danger. The Literal control offers a .Mode property so the control can handle this for you.

Finally, there is an article on MSDN around How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, which may also be of use.

Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
1

The question you need to ask yourself is, why are you encoding the data?

If you are trying to avoid SQL injection, then you should validate the data before putting it into the database. For example, if you only want alphanumeric characters in the input, then you would check that before inserting it into the database.

If they are entering HTML, which makes it harder to check the text, then I would recommend using stored procedures.

If you're using MsSQL then this may help..

If you're using MySQL then I think this may refer to them.

Remember, always sanitize your inputs!

Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
0

You adjust ValidateRequestMode in your page aspx

ValidateRequestMode="ValidateRequestMode.Disabled"

Link : http://msdn.microsoft.com/en-us/library/system.web.ui.page.validaterequestmode.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51