Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you validate your URL variables?

When you're passing variables through your site using GET requests, do you validate (regular expressions, filters, etc.) them before you use them?

Say you have the URL http://www.example.com/i=45&p=custform. You know that "i" will always be an integer and "p" will always contain only letters and/or numbers. Is it worth the time to make sure that no one has attempted to manipulate the values and then resubmit the page?

like image 779
Ryan Rodemoyer Avatar asked Oct 07 '08 13:10

Ryan Rodemoyer


People also ask

How do you validate a URL?

When you create a URL input with the proper type value, url , you get automatic validation that the entered text is at least in the correct form to potentially be a legitimate URL. This can help avoid cases in which the user mis-types their web site's address, or provides an invalid one.

How do I send a variable in a URL?

To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.

How do you validate a query string?

Query string values can be checked using regular expressions. You can select regular expressions from the global White list or enter them manually. For example, if you know that a query string must have a value of ABCD , a regular expression of ^ABCD$ is an exact match test.


6 Answers

Yes. Without a doubt. Never trust user input.

To improve the user experience, input fields can (and IMHO should) be validated on the client. This can pre-empt a round trip to the server that only leads to the same form and an error message.

However, input must always be validated on the server side since the user can just change the input data manually in the GET url or send crafted POST data.

In a worst case scenario you can end up with an SQL injection, or even worse, a XSS vulnerability.

Most frameworks already have some builtin way to clean the input, but even without this it's usually very easy to clean the input using a combination of regular exceptions and lookup tables.

  • Say you know it's an integer, use int.Parse or match it against the regex "^\d+$".
  • If it's a string and the choices are limited, make a dictionary and run the string through it. If you don't get a match change the string to a default.
  • If it's a user specified string, match it against a strict regex like "^\w+$"
like image 110
Alf Avatar answered Oct 23 '22 15:10

Alf


As with any user input it is extremely important to check to make sure it is what you expect it is. So yes!

like image 32
Dan Avatar answered Oct 23 '22 15:10

Dan


Yes, yes, and thrice yes.

Many web frameworks will do this for you of course, e.g., Struts 2.

like image 32
JeeBee Avatar answered Oct 23 '22 17:10

JeeBee


One important reason is to check for sql injection. So yes, always sanitize user input.

like image 23
Alonso Avatar answered Oct 23 '22 17:10

Alonso


not just what the others are saying. Imagine a querystring variable called nc, which can be seen to have values of 10, 50 and 100 when the user selects 10, 50 and 100 results per page respectively. Now imagine someone changing this to 50000. If you are just checking that to be an integer, you will be showing 50000 results per page, affecting your pageviews, server loads, script times and so on. Plus this could be your entire database. When you have such rules (10, 50 or 100 results per page), you should additionaly check to see if the value of nr is 10, 50 or 100 only, and if not, set it to a default. This can simply be a min(nc, 100), so it will work if nc is changed to 25, 75 and so on, but will default to 100 if it sees anything above 100.

like image 28
Kinjal Dixit Avatar answered Oct 23 '22 16:10

Kinjal Dixit


I want to stress how important this is. I know the first answer discussed SQL Injection and XSS Vulnerabilities. The latest rave in SQL Injection is passing a binary encoded SQL statement in query strings, which if it finds a SQL injection hole, it will add a http://reallybadsite.com'/> to every text field in your database.

As web developers we have to validate all input, and clean all the output.

Remember a hacker isn't going to use IE to compromise your site, so you can't rely on any validation in the web.

like image 20
JoshBerke Avatar answered Oct 23 '22 16:10

JoshBerke