Challenge - 5 Problems
CSRF Protection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a Django view lacks CSRF token validation?
Consider a Django view that processes POST requests but does not include CSRF token validation. What is the expected behavior when a POST request is made without a valid CSRF token?
Django
from django.views.decorators.csrf import csrf_exempt from django.http import HttpResponse @csrf_exempt def my_view(request): if request.method == 'POST': return HttpResponse('POST received') return HttpResponse('Hello')
Attempts:
2 left
💡 Hint
Think about what the @csrf_exempt decorator does.
✗ Incorrect
The @csrf_exempt decorator disables CSRF protection for the decorated view. Therefore, POST requests without a CSRF token are accepted and processed normally.
📝 Syntax
intermediate2:00remaining
Which template code correctly includes the CSRF token in a Django form?
You want to protect a form in a Django template from CSRF attacks. Which of the following template snippets correctly includes the CSRF token?
Django
<form method="post"> <!-- CSRF token goes here --> <input type="text" name="username"> <button type="submit">Submit</button> </form>
Attempts:
2 left
💡 Hint
Django uses a special template tag to insert the CSRF token.
✗ Incorrect
The correct way to include the CSRF token in a Django template form is using the {% csrf_token %} template tag. It inserts a hidden input with the token automatically.
🔧 Debug
advanced2:00remaining
Why does this Django view raise a CSRF verification failed error?
Examine the following Django view and template. The POST request raises a CSRF verification failed error. What is the cause?
Django
views.py: from django.shortcuts import render from django.views.decorators.csrf import csrf_protect @csrf_protect def submit_view(request): if request.method == 'POST': return render(request, 'success.html') return render(request, 'form.html') form.html: <form method="post"> <input type="text" name="data"> <button type="submit">Send</button> </form>
Attempts:
2 left
💡 Hint
Check the form template for CSRF token inclusion.
✗ Incorrect
The CSRF protection requires the token to be included in the form. The template lacks {% csrf_token %}, so Django rejects the POST request.
🧠 Conceptual
advanced2:00remaining
How does Django's CSRF protection mechanism verify POST requests?
Which of the following best describes how Django verifies that a POST request is safe from CSRF attacks?
Attempts:
2 left
💡 Hint
Think about where Django stores the CSRF token on the client side.
✗ Incorrect
Django stores a CSRF token in a cookie and expects the same token to be sent in the POST data. It compares these tokens to verify the request.
❓ state_output
expert2:00remaining
What is the value of the CSRF cookie after a GET request to a Django view with CSRF protection enabled?
A user visits a Django view protected by CSRF middleware with a GET request. What will be the state of the CSRF cookie in the browser after this request?
Django
from django.shortcuts import render def my_view(request): return render(request, 'page.html')
Attempts:
2 left
💡 Hint
Django sets the CSRF cookie on safe methods if missing.
✗ Incorrect
Django sets the CSRF cookie with a new token on safe HTTP methods like GET if the cookie is missing, to prepare for future POST requests.