Day 03 Injection

Injection Attacks and Input Validation

SQL injection, command injection, LDAP injection, and template injection — the family of vulnerabilities that comes from trusting user input without validation.

~1 hour Intermediate Hands-on Precision AI Academy

Today's Objective

SQL injection, command injection, LDAP injection, and template injection — the family of vulnerabilities that comes from trusting user input without validation.

01

Cross-Site Request Forgery (CSRF)

CSRF tricks an authenticated user's browser into making an unwanted request to your app. If your API uses cookies for auth, an attacker can embed a request in an image or link that runs with the user's session.

CSRF Example — The Attack
<!-- Attacker's page sends a POST as the logged-in user -->
<img src="https://bank.com/transfer?to=attacker&amount=1000">

<!-- Or a form that auto-submits -->
<form method="POST" action="https://bank.com/transfer">
  <input name="to" value="attacker">
  <input name="amount" value="1000">
</form>
<script>document.forms[0].submit();</script>
CSRF Prevention
npm install csurf
// Note: for modern apps, use SameSite cookies instead

// Method 1: SameSite cookie attribute (preferred)
res.cookie('sessionId', token, {
  httpOnly: true,
  secure: true,           // HTTPS only
  sameSite: 'strict',     // Never sent cross-site
  maxAge: 15 * 60 * 1000  // 15 minutes
});

// Method 2: CSRF token in forms (for traditional web apps)
const csrf = require('csurf');
app.use(csrf({ cookie: true }));

app.get('/form', (req, res) => {
  res.render('form', { csrfToken: req.csrfToken() });
});

// In your HTML form:
// <input type="hidden" name="_csrf" value="<%= csrfToken %>">
02

Cross-Site Scripting (XSS)

XSS injects malicious scripts into pages viewed by other users. If you render user input without escaping it, attackers can steal sessions, redirect users, or deface your app.

XSS Prevention
npm install dompurify helmet

// 1. Never render user input as raw HTML
// WRONG:
element.innerHTML = userInput;
// RIGHT:
element.textContent = userInput; // Auto-escapes

// 2. If you MUST render HTML, sanitize it first
import DOMPurify from 'dompurify';
element.innerHTML = DOMPurify.sanitize(userInput);

// 3. Content Security Policy headers (server-side)
const helmet = require('helmet');
app.use(helmet({
  contentSecurityPolicy: {
    directives: {
      defaultSrc: ["'self'"],
      scriptSrc: ["'self'"],  // No inline scripts
      styleSrc: ["'self'", "'unsafe-inline'"],
      imgSrc: ["'self'", "data:", "https:"],
    }
  }
}));
Day 3 Exercise
Secure Your App Against CSRF and XSS
  1. Add SameSite=strict to all session cookies in your app.
  2. Add the helmet middleware to your Express app.
  3. Find every place in your frontend that sets innerHTML — replace with textContent or DOMPurify.
  4. Test your CSP by opening browser devtools and looking for CSP violations after adding the policy.

Day 3 Summary

What's Next

The foundations from today carry directly into Day 4. In the next session the focus shifts to Cryptography for Developers — building directly on everything covered here.

Day 3 Checkpoint

Before moving on, verify you can answer these without looking:

  • What is the core concept introduced in this lesson, and why does it matter?
  • What are the two or three most common mistakes practitioners make with this topic?
  • Can you explain the key code pattern from this lesson to a colleague in plain language?
  • What would break first if you skipped the safeguards or best practices described here?
  • How does today's topic connect to what comes in Day 4?

Live Bootcamp

Learn this in person — 2 days, 5 cities

Thu–Fri sessions in Denver, Los Angeles, New York, Chicago, and Dallas. $1,490 per seat. June–October 2026.

Reserve Your Seat →
Continue To Day 4
Day 4