CSRF (Cross-Site Request Forgery) — attack where an attacker makes a user perform an action on your site (transfer money, change password) via a cross-site request. Mitigation: 1) **SameSite cookie** (default Strict/Lax in 2026 browsers), 2) **CSRF token** in forms (synchronizer token pattern), 3) **Check Origin/Referer** headers. Modern frameworks automate this.
Below: step-by-step, working examples, common pitfalls, FAQ.
SameSite=Lax (default in modern browsers, but explicit is better) + HttpOnly + SecureX-CSRF-Token) — simple requests cannot set it without JS| Scenario | Config |
|---|---|
| PHP session-based token | <?php
session_start();
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
// Form:
echo '<input type="hidden" name="_token" value="' . $_SESSION['csrf_token'] . '">';
// Validate:
if (!hash_equals($_SESSION['csrf_token'], $_POST['_token'] ?? '')) {
http_response_code(403);
exit('CSRF');
} |
| Django (auto) | # settings.py
MIDDLEWARE = ['django.middleware.csrf.CsrfViewMiddleware', ...]
# In template:
# <form method="post">{% csrf_token %} ... </form> |
| Express.js (csurf) | const csrf = require('csurf');
app.use(csrf({ cookie: true }));
app.get('/form', (req, res) => {
res.render('form', { csrfToken: req.csrfToken() });
}); |
| SameSite cookie | Set-Cookie: session=abc; HttpOnly; Secure; SameSite=Lax
# Lax — allows top-level GET, blocks cross-site POST
# Strict — blocks all cross-site (breaks OAuth callbacks) |
| Origin header check | $allowed = ['https://example.com', 'https://www.example.com'];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (!in_array($origin, $allowed)) { http_response_code(403); exit; } |
=== — timing attack. Use hash_equals()The tool checks HTTP security headers, SSL/TLS configuration, server info leaks, and protection against common attacks (XSS, clickjacking, MIME sniffing). A grade fromA to F shows overall security level.
Checking Content-Security-Policy, HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and more.
TLS version, certificate expiry, chain of trust, HSTS support.
Finding exposed server versions, debug modes, open configs, and directories.
Detailed report explaining each issue with specific steps to fix it.
HTTP header audit
config verification
CSP & HSTS setup
compliance checks
Strict-Transport-Security.Server: Apache/2.4.52 helps attackers find exploits. Hide the version.DENY or SAMEORIGIN.nosniff, browsers may misinterpret file types (MIME sniffing).Content-Security-Policy-Report-Only, monitor violations, then enforce.Server, X-Powered-By, X-AspNet-Version from responses.Security check history and HTTP security header monitoring.
Sign up freeFor top-level GET — yes. For POST/PUT/DELETE — Lax blocks cross-site, core CSRF mitigation. But defence-in-depth: token on state-changing forms is mandatory.
Yes, if cookies. JWT in header (Bearer) — CSRF ok (attacker cannot set custom header). Cookies — vulnerable without CSRF token.
Double-Submit Cookie: CSRF token in cookie + in request header. Attacker cannot read cookie (SameOrigin), does not know value for header.
Create test page on another domain with <code>fetch('/your-site/action', {method: 'POST', credentials: 'include'})</code>. Should be blocked.