Input Validation: Whitelisting, Sanitization, and Error Feedback
Input Validation: Whitelisting, Sanitization, and Error Feedback
Introduction
जब हम web applications या software बनाते हैं, तो Input Validation सबसे जरूरी security step होता है। इसका simple मतलब है — user से मिलने वाले data को check करना कि वो सही format में है या नहीं। अगर validation ठीक से नहीं हुआ, तो hacker गलत data डालकर system को damage कर सकता है। इसलिए हर developer को ये concept अच्छे से समझना चाहिए, खासकर exam point of view से।
Why Input Validation is Important
Input Validation system को कई खतरों से बचाता है जैसे कि SQL Injection, Cross-Site Scripting (XSS), और Command Injection। अगर data validate नहीं किया गया, तो attacker malicious code डालकर database या server पर control पा सकता है। Validation से हम यह ensure करते हैं कि user सिर्फ expected format में ही input दे सके।
Examples of Unsafe Input
- Username में script tags डालना जैसे
<script>alert('hack')</script> - Form fields में SQL query डालना जैसे
' OR 1=1 -- - Command injection जैसे
; rm -rf /
ऐसे attacks को रोकने के लिए हमें तीन main techniques का use करना चाहिए — Whitelisting, Sanitization, और Error Feedback।
1. Whitelisting
Whitelisting का मतलब है कि आप सिर्फ वही input allow करें जो predefined list में allowed है। यानी valid values पहले से define होती हैं और बाकी सब reject कर दिए जाते हैं। इसे सबसे safe validation method माना जाता है क्योंकि यह "allow known good" approach पर काम करता है।
Example of Whitelisting
मान लो किसी form में सिर्फ email address allow करना है, तो हम regex pattern से सिर्फ valid email format को accept करेंगे।
if (!preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}$/", $email)) {
echo "Invalid Email Format";
}
यह method user input को strictly control करता है और किसी भी suspicious data को entry से पहले रोक देता है।
Advantages of Whitelisting
- High security level — only valid input accepted
- Reduces chances of injection attacks
- Simple to maintain for limited input types
Disadvantages of Whitelisting
- Dynamic inputs के लिए difficult
- Maintenance time-consuming हो सकता है
2. Sanitization
Sanitization का मतलब है — input data को clean करना ताकि उसमें से harmful characters या scripts remove हो जाएँ। जब हम किसी input को completely block नहीं करना चाहते, बल्कि safe बनाना चाहते हैं, तब sanitization use करते हैं।
Example of Sanitization
अगर कोई user comment field में HTML tags डालता है, तो हम HTML entities convert करके उसे harmless बना सकते हैं।
$comment = htmlspecialchars($_POST['comment'], ENT_QUOTES, 'UTF-8');
अब अगर कोई user <script> tag डालता है, तो वो display तो होगा, लेकिन execute नहीं होगा।
Sanitization Techniques
- Use
htmlspecialchars()for HTML data - Use
mysqli_real_escape_string()for SQL queries - Use
strip_tags()to remove unwanted tags
Advantages of Sanitization
- Maintains data usability
- Protects against XSS and SQL injection
- Useful when user input flexibility needed
Disadvantages of Sanitization
- May reduce data accuracy
- Complex to implement for large data sets
3. Error Feedback
Error Feedback validation process का वो हिस्सा है जो user को बताता है कि उसका input गलत क्यों है और उसे कैसे सही करना है। यह user experience को improve करता है और data quality भी बढ़ाता है।
Example of Error Feedback
अगर कोई user invalid phone number डालता है, तो message दिखाया जा सकता है:
"Please enter a valid 10-digit mobile number."
इससे user को तुरंत पता चल जाता है कि क्या गलती हुई है और वो उसे correct कर सकता है।
Types of Error Feedback
- Inline Feedback: Input field के नीचे error message दिखाना।
- Alert Box Feedback: Browser alert के जरिए error दिखाना।
- Summary Feedback: Form submit करने पर एक summary में सभी errors दिखाना।
Best Practices for Error Feedback
- Messages clear और simple रखें।
- Technical terms avoid करें।
- Positive tone में explain करें।
Common Validation Techniques
Input validation कई methods से किया जा सकता है — जैसे client-side, server-side या hybrid validation।
| Validation Type | Performed On | Example | Advantages |
|---|---|---|---|
| Client-Side Validation | Browser (JavaScript) | HTML5 attributes like required, pattern |
Fast, reduces server load |
| Server-Side Validation | Backend (PHP, Python) | Check input on server before processing | More secure, avoids bypassing |
| Hybrid Validation | Both browser and server | Double-layer validation | Best practice, ensures full safety |
Common Mistakes in Input Validation
- केवल client-side validation पर भरोसा करना
- Generic error messages देना (जैसे "Invalid input")
- Improper regex patterns use करना
- Data encoding या escaping को ignore करना
Best Practices for Secure Input Validation
- Always use Server-side validation — client-side optional है।
- Validation के बाद हमेशा data को sanitize करें।
- Detailed और user-friendly error feedback दें।
- Use frameworks की built-in validation libraries (like Laravel Validator, Django Forms)।
- Numeric, Email, और Date inputs के लिए predefined patterns use करें।
- All inputs को escape करें जब भी database या HTML में insert करें।
Real-World Example
Example के तौर पर मान लो एक login form है — उसमें email और password fields हैं। अगर user ने गलत format में email डाला, तो validation उसे रोक देगा। अगर attacker ने SQL query डालने की कोशिश की, तो sanitization उसे neutralize कर देगी। और अगर कुछ गलत हुआ, तो user-friendly feedback बताएगा कि क्या correct करना है। यही combination secure system की पहचान है।
$email = $_POST['email'];
$password = $_POST['password'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid Email Address";
} else {
$email = htmlspecialchars($email);
$password = htmlspecialchars($password);
// proceed with login
}
Summary Notes
- Input Validation data integrity और system security का base है।
- Whitelisting — सिर्फ known valid inputs को allow करता है।
- Sanitization — user input को clean और harmless बनाता है।
- Error Feedback — user को सही तरीके से guide करता है।
- Client-side और Server-side दोनों validation use करना best practice है।
- Validation implement करते समय readability, usability और security balance रखें।