Bird
0
0

Why does this Flask code fail to add the Referrer-Policy header?

medium📝 Debug Q7 of 15
Flask - Security Best Practices
Why does this Flask code fail to add the Referrer-Policy header?
@app.after_request
def add_header():
    response.headers['Referrer-Policy'] = 'no-referrer'
    return response
Aafter_request cannot modify headers
BReferrer-Policy is not a valid header
CThe function is missing the response parameter
DHeaders must be set in before_request
Step-by-Step Solution
Solution:
  1. Step 1: Check function signature

    after_request functions must accept the response parameter to modify it.
  2. Step 2: Identify missing parameter

    The function lacks the response argument, so 'response' is undefined.
  3. Final Answer:

    The function is missing the response parameter -> Option C
  4. Quick Check:

    after_request needs response parameter to modify headers [OK]
Quick Trick: after_request functions must accept response argument [OK]
Common Mistakes:
MISTAKES
  • Omitting response parameter
  • Thinking Referrer-Policy is invalid
  • Trying to set headers in before_request

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes