Skip to content
StackDecode
System Design
13. Security ArchitectureAnimated

SQL Injection Prevention

Stop attackers inserting SQL through input.

The idea

Use parameterised queries so input is data, never code.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

AttackerAppDatabasename = x' OR '1'='1
  • current message
  • earlier
  • failure or attack

The attacker types SQL into a form field.

step:
1/5
from:
Attacker
to:
App
1/5

Time runs downward. Solid arrows are requests, dashed arrows are replies.

Step by step

  1. 1Never concatenate input into SQL
  2. 2Use prepared statements
  3. 3Validate input
  4. 4Use least-privilege database users

Where it is used

Any database-backed app.

Watch out for

ORMs can still be used unsafely.

Related