Challenge - 5 Problems
Admin Panel Protector
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a non-admin user tries to access the admin panel?
Consider this Flask route protecting an admin panel:
What will a user with session role 'user' see when visiting '/admin'?
from flask import Flask, session, redirect, url_for
app = Flask(__name__)
app.secret_key = 'secret'
@app.route('/admin')
def admin_panel():
if session.get('role') != 'admin':
return redirect(url_for('login'))
return 'Welcome to admin panel'What will a user with session role 'user' see when visiting '/admin'?
Attempts:
2 left
💡 Hint
Check what happens when the session role is not 'admin'.
✗ Incorrect
The code checks if the session role is 'admin'. If not, it redirects to the login page. So a user with role 'user' is redirected.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Flask admin check
What is wrong with this Flask route code?
@app.route('/admin')
def admin():
if session['role'] = 'admin':
return 'Admin access'
return 'Access denied'Attempts:
2 left
💡 Hint
Check the operator used in the if statement.
✗ Incorrect
The '=' operator is for assignment, not comparison. The correct operator is '=='.
❓ state_output
advanced2:00remaining
What is the output after setting session role to admin?
Given this Flask snippet:
What will a user see after visiting '/set_admin' then '/admin' in the same browser session?
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'
@app.route('/set_admin')
def set_admin():
session['role'] = 'admin'
return 'Role set'
@app.route('/admin')
def admin():
if session.get('role') == 'admin':
return 'Welcome admin'
return 'Access denied'What will a user see after visiting '/set_admin' then '/admin' in the same browser session?
Attempts:
2 left
💡 Hint
Session data persists between requests in the same browser.
✗ Incorrect
Setting session['role'] to 'admin' stores it. The next request sees this and allows admin access.
🔧 Debug
advanced2:00remaining
Why does this admin check always deny access?
Examine this Flask code:
Even when session['role'] is set to 'admin', users see 'Denied'. Why?
@app.route('/admin')
def admin():
if session.get('role') is 'admin':
return 'Admin panel'
return 'Denied'Even when session['role'] is set to 'admin', users see 'Denied'. Why?
Attempts:
2 left
💡 Hint
Check how string comparison should be done in Python.
✗ Incorrect
'is' checks identity, not equality. Use '==' to compare string values.
🧠 Conceptual
expert3:00remaining
Which method best secures an admin panel in Flask?
You want to protect your Flask admin panel so only logged-in admins can access it. Which approach is best?
Attempts:
2 left
💡 Hint
Think about secure, server-side checks for user roles.
✗ Incorrect
Checking session role server-side in each route is secure and reliable. Global variables or client-side hiding are insecure. Plain text passwords are unsafe.