Input Validation on Client-Side or Server-Side? (2024)

The question of whether input validation should be performed on the client side or server side is one that has been debated time and time again. There are pros and cons to both approaches, and ultimately the decision of which one to use depends on the specific situation. As web applications are developed, the input received from the client is often stored or reflected back to the user. Then, as security implications were discovered, it was understood that the input needed to be validated. Developers were then advised to restrict or sanitize the user's input.

While some developers only validate on the client-side (e.g., JavaScript), some validate on only the server-side, while others validate on both. Below, each type will be broken down to understand its importance.

Input Validation on Client-Side

Client-side validation is visible to the user. It involves having validation on input forms through JavaScript. For example, if the input is submitted for a phone number or email, a JavaScript validator would provide an error if anything is submitted that does not conform to a phone number or email. For example, below we will see how client-side restrictions could be easily changed.

The following form will only submit if the phrase “success” is submitted.

Input Validation on Client-Side or Server-Side? (1)

Figure 1: Client-side Input Validation, Stage 1

However, a token error is presented when “success” is submitted.

Input Validation on Client-Side or Server-Side? (2)

Figure 2: Client-side Input Validation, Stage 2

This means that the request is submitting a token in addition to the phrase. In order to see what is being done, we can take a look at the request using an interception proxy (such as Burp Proxy).

Input Validation on Client-Side or Server-Side? (3)

Figure 3: Client-side Input Validation, Stage 3

Now that we have confirmed our suspicions, we need to understand what is being used to generate the token. Given the JavaScript is provided to us to populate the page, we can easily view the code.

function rot13(inp) {return inp.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});}function generate_token() {var phrase = document.getElementById("phrase").value;document.getElementById("token").value = md5(rot13(phrase));}generate_token();

The code above will take the phrase from the input box, run it through a simple substitution cipher (rot13) and then md5 it. We can confirm this by taking the phrase “ChangeMe” and running it through a rot13 substitution which would present us with “PunatrZr”. The hash of “PunatrZr” is 8b479aefbd90795395b3e7089ae0dc09, which matches the token in the request above.

If the phrase “success” is to be successfully submitted, we will need to do the same steps as we did for “ChangeMe”.

  • The rot13 of “success” becomes “fhpprff”

  • The md5 of “fhpprff” is 38581812b435834ebf84ebcc2c6424d6

The next step would be to intercept the request before it is sent to the server and modify the token to be that of 38581812b435834ebf84ebcc2c6424d6.

Input Validation on Client-Side or Server-Side? (4)

Figure 4: Client-side Input Validation, Stage 4

Once submitted, we’ll receive confirmation that it succeeded.

Input Validation on Client-Side or Server-Side? (5)

Figure 5: Client-side Input Validation, Stage 5 – Bypassed

JavaScript has to be accessible by the browser (and therefore the user) to ensure the web application functions as it should. As seen above, any restrictions can be quickly reverse engineered or even bypassed entirely.

Input Validation on Server-Side

Server-side input validation will take whatever is sent by the client and conduct additional checks. Using server-side validation indicates that any input sent by the user (or client) cannot be trusted.

In order to show how effective this is, the following form is vulnerable to Cross-Site Scripting.

Input Validation on Client-Side or Server-Side? (6)

Figure 6: Server-side Input Validation, Stage 1

If a JavaScript payload is submitted, the alert box will appear.

Input Validation on Client-Side or Server-Side? (7)

Figure 7: Server-side Input Validation, Stage 2

This implies that the request was sent to the server and reflected back to the client without any validation. In these examples, we have access to the underlying source code to validate our claim.

  • echo ‘<pre>Hello ‘ . $_GET[ ‘name’ ] . ‘</pre’;

There are many approaches that can be taken to validate from the server-side, and one can simply be to remove JavaScript tags when the name is sent back to the client.

The same payload was sent but now the alert box was not populated.

Input Validation on Client-Side or Server-Side? (8)

Figure 8: Server-side Input Validation, Stage 3 – Fail

Taking a look at the source, we notice that any presence of a script tag within the name variable will be replaced.

  • $name = str_replace( ‘<script>’, ”, $_GET[ ‘name’ ] );

These simple replacements can be by-passed using other Cross-Site Scripting payloads. The recommended solution is to use the PHP htmlspecialchars() function that will encode user input to completely prevent Cross-Site Scripting.

  • $name = htmlspecialchars( $_GET[ ‘name’ ] );

Which Input Validation Should You Use?

In general, it is best to perform input validation on both the client side and server side. Client-side input validation can help reduce server load and can prevent malicious users from submitting invalid data. However, client-side input validation is not a substitute for server-side input validation. Server-side input validation is essential to ensure that only valid data is processed by the application.

While running a dynamic application security test, tools used will bypass the client-side restrictions to attempt injection attacks (e.g., Cross-Site Scripting, SQL injection). Each piece of input must be validated on both ends to ensure injection attacks are mitigated.

Doing input validation on both the client side and server side has a number of benefits:

  • It helps reduce server load since invalid data is never submitted in the first place.

  • It helps prevent malicious users from submitting invalid data.

  • It can help improve application performance since the application doesn't have to waste time processing invalid data.

Ultimately, the decision of whether to perform input validation on the client side or server side depends on the specific situation.

Input Validation on Client-Side or Server-Side? (2024)
Top Articles
Aptitude test | IQ, Cognitive Ability & Problem-Solving
Why candidate aptitude assessments matter - TG
Compare Foods Wilson Nc
Metallica - Blackened Lyrics Meaning
Research Tome Neltharus
Occupational therapist
Umn Pay Calendar
Steve Strange - From Punk To New Romantic
Fcs Teamehub
Jesus Revolution Showtimes Near Chisholm Trail 8
Amateur Lesbian Spanking
Housing Intranet Unt
U.S. Nuclear Weapons Complex: Y-12 and Oak Ridge National Laboratory…
Tracking Your Shipments with Maher Terminal
Dallas Cowboys On Sirius Xm Radio
Nhl Wikia
Air Force Chief Results
Craigslist Lakeville Ma
Selfservice Bright Lending
Military life insurance and survivor benefits | USAGov
Sef2 Lewis Structure
Shreveport City Warrants Lookup
Yosemite Sam Hood Ornament
D2L Brightspace Clc
Www.1Tamilmv.con
Dl.high Stakes Sweeps Download
Lininii
Wells Fargo Bank Florida Locations
Redding Activity Partners
The Ultimate Guide to Obtaining Bark in Conan Exiles: Tips and Tricks for the Best Results
Plato's Closet Mansfield Ohio
T&J Agnes Theaters
In Polen und Tschechien droht Hochwasser - Brandenburg beobachtet Lage
Midsouthshooters Supply
Pinellas Fire Active Calls
What Is Kik and Why Do Teenagers Love It?
Craigslist Pa Altoona
Dcilottery Login
Conan Exiles Armor Flexibility Kit
Smite Builds Season 9
Kenner And Stevens Funeral Home
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Brother Bear Tattoo Ideas
Caphras Calculator
Zipformsonline Plus Login
El Patron Menu Bardstown Ky
Diamond Spikes Worth Aj
Verilife Williamsport Reviews
Suzanne Olsen Swift River
Overstock Comenity Login
Electronics coupons, offers & promotions | The Los Angeles Times
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5830

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.