Get ready for your next interview with our comprehensive question library
Input validation is the process of verifying that user-supplied data meets expected criteria before processing it. It's crucial because unvalidated input is the root cause of many security vulnerabilities including injection attacks, buffer overflows, and data corruption.
Key principles:
Example of proper validation:
import re
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if re.match(pattern, email) and len(email) <= 254:
return True
return False
Authentication verifies "who you are" - confirming the identity of a user or system.
Authorization determines "what you can do" - granting or denying access to resources based on identity.
Example flow:
SQL injection occurs when user input is directly concatenated into SQL queries, allowing attackers to manipulate the database.
Example of vulnerable code:
# VULNERABLE
query = f"SELECT * FROM users WHERE username = '{username}'"
Attack example: username = "admin'; DROP TABLE users; --"
Prevention methods:
Secure example:
# SECURE - Using parameterized query
cursor.execute("SELECT * FROM users WHERE username = %s", (username,))
Cross-Site Scripting (XSS) allows attackers to inject malicious scripts into web pages viewed by other users.
Three main types:
Stored XSS (Persistent): Malicious script stored on server
<script>alert('XSS')</script>Reflected XSS (Non-persistent): Script reflected from request
DOM-based XSS: Vulnerability in client-side JavaScript
document.write(location.hash.substring(1))Impact: Session hijacking, credential theft, defacement, malware distribution
Prevention: Input validation, output encoding, Content Security Policy (CSP), sanitization
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime