0

I have an ASP.NET form which has textbox. The user can enter any characters,numbers etc. I should not restrict the user to filter hazardous characters. But I need to prevent cross site scripting.

The user can enter any text like alert('hi') like this.

The data should be saved as its in DB. Also it should be return back and display in label in form as it is.

How can acheive this without cross site scripting

1 Answer 1

1

Well, I think you should consider some restriction on what users are allowed to enter. You don't want null bytes or non-printable characters do you? Even if you accept more than alphanumeric values, you should decide which characters are allowed and exclude the rest using a simple regular expression (with start and end anchors of course).

Then, the way to prevent XSS is to encode the value whenever you display it. There are a whole host of ways to do this, but using the AntiXSS class of the Microsoft Web Protection Library is the best if you ask me. You can encode the output based on whether you're rendering it within HTML elements, attributes, JavaScript, and so on.

Sign up to request clarification or add additional context in comments.

3 Comments

I dont want to go for first option. I am good with second. if encode "<" this is saving as &lt in DB. I want to display as "<" while retriving. how can I?
@web dunia, where are you displaying the result? Browsers will display $lt; as < unless it's in a textbox or something.
Encoding before you store in the database is a bad idea, as it doesn't take into account where you're going to output the value. You need different encodings based on whether you output a value in an HTML element or HTML attribute, for instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.