Cookie-Authenticated Actions and CSRF Defenses
Follow a signed-in administrator’s role change and separate identity, permission, and proof that the user intended the request.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
An administrator is signed in to a team app. While that session is active, another page causes the browser to submit a role-change request. The server recognizes a real administrator—but did the administrator choose that action?
This is the question behind cross-site request forgery, or CSRF. A browser can attach credentials automatically. The application still needs evidence that a sensitive request came through the intended workflow.
| Question | What answers it? |
|---|---|
| Who is making the request? | A verified session |
| May this person change roles? | The administrator and target permission checks |
| Did they intend this request? | The application's CSRF and request-origin protections |
We'll keep these questions separate, then work out which requests the browser can actually send with a session cookie.
Separate authentication, authorization, and intent
A login decorator establishes an actor. A role check may establish that the actor is an administrator. Neither establishes that the administrator initiated this particular change from a trusted application workflow. CSRF defenses bind the request to an intended browser interaction or reject an untrusted request context.
@csrf_exempt
@login_required
def change_role(request):
if not request.user.is_staff:
return forbidden()
body = json.loads(request.body)
target = users.get(id=body['userId'])
target.role = body['role']
target.save()
return JsonResponse({'ok': True})
The included administrator check is real and relevant. Do not report its absence. Trace instead why the framework exemption was added, which methods reach this function, how the body is parsed, and what origin/intent validation remains. A helper named requireAdmin solves a different question from a CSRF middleware.
Trace the browser context and server decisions separately.
Browser. SameSite and browser request rules decide whether the session accompanies this request.
Origin: https://uploads.example.test
Content-Type: text/plain
Cookie: session=ADMIN_FIXTURE
Identity. The session can establish a legitimate administrator without proving they initiated the action.
actor = require_session(request)
if not actor.is_staff:
return forbidden()
Intent. Framework CSRF and origin policy must cover the actual unsafe operation.
enforce_framework_csrf(request)
body = parse_role_update(request.body)
apply_authorized_change(actor, body)
Review question: A working role check and a missing intent check can coexist.