All posts

Senior Backend Interview: Security — SQL Injection, XSS, CSRF and Passwords

Four attacks that come up in every interview, and answers that show you know where the defence actually belongs.

Marijan Kopčić••2 min read
Senior Backend Interview: Security — SQL Injection, XSS, CSRF and Passwords

Security is the topic where it is easiest to say the right name and the wrong mechanism. These answers focus on where the defence belongs.

SQL injection

It happens when user input ends up in a query as code. You prevent it with prepared statements and parameters — the ORM does it for you, but as soon as you write raw SQL you have to be careful. Filtering input is not a substitute for parameters.

// wrong
DB::select("select * from users where email = '{$email}'");

// right
DB::select('select * from users where email = ?', [$email]);

XSS

When user content ends up on the page as HTML or JavaScript. You prevent it by encoding on output, not on input — in Blade {{ }} encodes, {!! !!} doesn't.

CSRF

Someone else's page makes the browser send a request with your cookie. You prevent it with a token the attacker can't read, plus SameSite cookies.

How passwords are stored

They are hashed with a slow, salted algorithm — bcrypt or Argon2. Never MD5 or SHA, because they are too fast. In PHP it is password_hash() and password_verify(), and the salt is generated for you.

$hash = password_hash($password, PASSWORD_ARGON2ID);
$ok = password_verify($password, $hash);

Secrets management

Secrets don't go into the repository. They come through environment variables and infrastructure-level secrets, with different values per environment and rotation. .env is in .gitignore, and an .env.example without values serves as documentation.

Keep reading