Back to Home

Secure SQL Lab

This lab demonstrates secure SQL practices using prepared statements. Try entering any text, including potential attack payloads, and see how the code safely handles them.

Search Users

Secure Code:

# Python (Flask) query = "SELECT * FROM users WHERE username = ?" result = db.execute(query, (user_input,))

Results

Enter a search query and click Search to see results

Test with these payloads:

Why is this secure?

  • Parameterized Queries: The SQL structure is defined separately from the data, preventing injection.
  • Type Safety: Parameters are treated as data values, never as executable code.
  • Database Driver Protection: The database driver handles proper escaping and validation.

Best Practices Summary

  1. Always use prepared statements or parameterized queries
  2. Never concatenate user input into SQL strings
  3. Validate and sanitize all user input
  4. Use least privilege database accounts
  5. Implement proper error handling
  6. Keep database software updated