SQL Injection
What Is SQL Injection?
// VULNERABLE
String query = "SELECT * FROM users WHERE name = '" + input + "'";
// Input: ' OR '1'='1
// Result: SELECT * FROM users WHERE name = '' OR '1'='1'
// Returns ALL users!
Prevention
// SAFE - PreparedStatement
PreparedStatement stmt = conn.prepareStatement(
"SELECT * FROM users WHERE name = ?");
stmt.setString(1, input);
SQL Injection Signs
- Unexpected data in responses
- Database errors in responses
- Authentication bypass
- Data exfiltration
SQL Best Practices
Query Writing
- Use parameterized queries
- Avoid N+1 queries
- Use appropriate JOIN types
- Limit result sets
Performance
- Use EXPLAIN for analysis
- Optimize subqueries to JOINs
- Use temporary tables for complex queries
- Implement proper pagination
Security
- Never concatenate user input
- Use stored procedures for complex logic
- Implement query timeouts
- Log slow queries
Key Points
- Understanding SQL Injection is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for SQL Injection in a backend system. Consider scalability, error handling, and production readiness.
Solution
// SQL Injection implementation
// Key aspects: validation, error handling, logging, testing
public class SQLInjection {
// Production-ready implementation
}Identify and handle edge cases for SQL Injection. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for SQL Injection. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. How to prevent SQL injection?
2. SQL injection input example?
3. What is the primary purpose of SQL Injection?
4. What is a common mistake when implementing SQL Injection?
Flashcards
Question
Prevent SQL injection?
Click to reveal answer
Answer
PreparedStatement with bind variables
Question
SQL injection sign?
Click to reveal answer
Answer
Unexpected data, auth bypass, DB errors in response
Question
What is SQL Injection?
Click to reveal answer
Answer
SQL Injection is a key concept in backend development.
Question
When to use SQL Injection?
Click to reveal answer
Answer
Use SQL Injection when building production systems that require reliability, scalability, and maintainability.
Question
SQL Injection best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.SQL injection inserts malicious SQL
- 2.Use PreparedStatement to prevent
- 3.Never concatenate user input into SQL
- 4.Input validation is additional defense
Interview Tips
- •Know SQL injection prevention
- •Identify injection vulnerabilities
Cheat Sheet
SQL Injection
- Malicious SQL in user input
- Prevent: PreparedStatement
- Never: concatenate input into SQL
- Signs: unexpected data, auth bypass