Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When authenticating what is the point of JavaScript validation if the server must always check the credentials?

I have a form users fill out and JavaScript is used to validate the input (e.g. makes sure the password field isn't left blank). Since JavaScript is client side and not compiled anyone can easily mess around with it. Does this mean it's necessary to validate data from the user again on the server? If yes, is there anyways it can be made more efficient since JavaScript (theoretically) already did it?

like image 964
Celeritas Avatar asked Dec 12 '22 15:12

Celeritas


2 Answers

Yes, it is necessary to validate data on the server because it can be messed with by end users client-side.

If yes, is there anyways it can be made more efficient since JavaScript (theoretically) already did it?

It is already more efficient than having only server-side validation, because you avoid a lot of round-trips for validation by having client-side validation (you only need to submit the data once, and unless validation was incomplete or disabled, it will go through straightaway). Provides a better user experience, too.

You cannot do away with server-side validation (if you care about the data). If the data only ever goes back to the same user and is not shown or used anywhere else (and has no potential to break anything on your system), you could relax this a little. As as extreme example, Dropbox probably does not care what files you upload, so they don't validate if the HTML you upload contains malicious Javascript.

like image 65
Thilo Avatar answered Dec 14 '22 04:12

Thilo


I can disable any javascript on your page just with a click of the mouse. I can even totally bypass an HTML form and send data directly to your server.

For example, if you retrieve data with $_GET I can bypass your form (and the javascript validation) just by messing with the address bar. Don't think that using $_POST would change this: it just a matter of writing an HTTP request.

So, yes... Never trust user input, even if sanitized with javascript.

As somebody posted above, javascript validation can prevent legitimate user errors (thus save the trip the wrong data would have done to your server and then back to the user) but malicious users will still be able to bypass it VERY easily.

like image 23
Saturnix Avatar answered Dec 14 '22 03:12

Saturnix