Прескочи на главни садржај
← 📄 HTML & the platform·Модул A3 · Лекција 15
ЗадатакBuild a transfer form that's CSRF-safe. <form method='post' action='/transfer'> with a hidden input name='_csrf' value='SERVER_RENDERED_TOKEN', a labeled input for 'to' (text) and 'amount' (number, min 1, required), and a submit button.

ЦСРФ токени: зашто је вашем обрасцу потребна тајна коју нападач не може да прочита

100 XP9 min
Теорија

The attack you're defending against

You're logged into bank.com. An attacker tricks you into visiting evil.com. evil.com ships HTML like:

<form action="https://bank.com/transfer" method="post">
  <input name="to" value="attacker" />
  <input name="amount" value="9999" />
</form>
<script>document.forms[0].submit()</script>

Your browser cheerfully attaches your bank.com cookies (you're logged in!) and POSTs the transfer. That's CSRF — Cross-Site Request Forgery.

The fix: server-rendered secret in the form

The server puts a random token into your form when it renders. The token is tied to your session, stored server-side, and is NOT readable from other origins (different origin = can't read the HTML the server sent you).

<form action="/transfer" method="post">
  <input type="hidden" name="_csrf" value="a7f3...c4b2" />
  <input name="to" />
  <input name="amount" />
</form>

The server checks: "does _csrf match what I gave this session?" If not → 403. The attacker on evil.com can't read your CSRF cookie via JavaScript (different origin), can't read the rendered HTML (different origin), and can't guess the random token. Forgery blocked.

Three places people get this wrong

  1. One global token reused per session — works but leaks one token across many forms; safer is per-form rotation.
  2. Token in URL — leaks via Referer header, browser history, server logs. Always put it in a hidden form field, never a query string.
  3. No token on POST that "feels safe" — newsletter sign-up, "follow user" buttons. ANY state-changing POST needs a token.
🔒

Региструј се да почнеш са кодом

Теорија је отворена за све. Едитор, преглед уживо и провера се откључавају са 7-дневним бесплатним пробним периодом — потребна картица.

Регистрација — бесплатан пробни период →

Првих 10 лекција у сваком путу су бесплатне. Без картице.

← ПретходнаСледећа лекција →

Један савет за Python или веб дневно — на имејл

Кратко, ручно писано, без спама. Одјава у једном клику.