views:

2080

answers:

3

We use jsp, servlets, beans with mysql database. We don't want to restrict the characters entered by users on form fields. So how do I sanitize the input and how to make sure the output is not changed for malicious activities. Is there way while sending the output I could check if extra code has been sent. Like suppose there is search input field -- the user gives something like <script>alert("I am here")</script>. Is there anway I could know this is a html tag. If the user appends an extra parameter to a link field, is there like a before and after check I could do for the document to realize there has been a extra link field.

+2  A: 

You should always do basic HTML-escaping of data taken from sources like user input or the database that might contain invalid characters. The <c:out> JSP tag does this, for example. That way if the user enters "<script> ..." in a field and you are printing it back again, it will be printed to the HTML as "&lt;script&gt; ...".

araqnid
A: 

Are you trying to protect the database from sql injection attacks?

If so then just use stored procedures or prepared statements to protect the db.

If you know what parameters are allowed to be passed in from the form then just check for those parameters, don't use every parameter that was passed in.

If you use POST instead of GET on the form, and don't react to GET at all, then that will also help, as it will be harder for someone to add parameters.

Also, do a sanity check on every value that you get from a form. Assume that all data that comes from outside your control is suspect until you validate it.

James Black
We are well protected against the sql injection as we use prepared statements but I am worried about the input form fields when they get displayed back to the browser or any kind of malicious code that can be entered in the form fields and cause a xss scripting bug or other types of security violations. I would like to know how to sanitize the input.Apart from <c:out> mentioned above are there are any other methods to weed out those input like escaping etc...Thanks again.
+3  A: 

You really should allow users to input as little HTML and/or javascript as possible. One good solution to validating and sanitizing this stuff is to use a ready-made library like OWASP AntiSamy.

Also, take a look at OWASP Enterprise Security API for a collection of security methods that a developer needs to build a secure web application.

Kaitsu