Bird
0
0

You want to secure a Flask route so only logged-in users can access it. Which approach correctly combines Flask tools to achieve this?

hard📝 Application Q15 of 15
Flask - Security Best Practices
You want to secure a Flask route so only logged-in users can access it. Which approach correctly combines Flask tools to achieve this?
@app.route('/settings')
def settings():
    # What should be added here?
    return 'User settings page'
ARedirect to homepage without checking session
BAlways return the page without checks
CCheck if 'user' in session; if not, abort(401); else return page
DUse print() to log user info but no access control
Step-by-Step Solution
Solution:
  1. Step 1: Understand access control with Flask session

    To secure a route, check if the user is logged in by verifying if 'user' exists in the session.
  2. Step 2: Use abort(401) to block unauthorized users

    If the user is not logged in, call abort(401) to stop access and send an error.
  3. Step 3: Return the page only if user is authenticated

    Only after passing the check should the page content be returned.
  4. Final Answer:

    Check if 'user' in session; if not, abort(401); else return page -> Option C
  5. Quick Check:

    Session check + abort(401) = secure route [OK]
Quick Trick: Check session user, abort if missing, then return page [OK]
Common Mistakes:
MISTAKES
  • Skipping session check
  • Using print() instead of access control
  • Redirecting without verifying login

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes