What is the purpose of setting secure HTTP headers in a Flask application?
Secure HTTP headers help protect your web app from common attacks like cross-site scripting (XSS), clickjacking, and data sniffing by instructing browsers how to behave securely.
Click to reveal answer
beginner
Name three common secure headers you can set in Flask.
Content-Security-Policy (CSP), X-Frame-Options, and Strict-Transport-Security (HSTS) are common secure headers to improve security.
Click to reveal answer
intermediate
How can you add secure headers to all responses in a Flask app?
Use the @app.after_request decorator to modify the response object and add headers before sending it to the client.
Click to reveal answer
intermediate
What does the 'Content-Security-Policy' header do?
It controls which resources (like scripts, images) the browser is allowed to load, helping prevent malicious code injection.
Click to reveal answer
intermediate
Why is the 'Strict-Transport-Security' header important?
It forces browsers to use HTTPS only, preventing attackers from intercepting data over insecure HTTP connections.
Click to reveal answer
Which Flask decorator is best to add secure headers to every response?
A@app.after_request
B@app.route
C@app.before_request
D@app.errorhandler
✗ Incorrect
The @app.after_request decorator lets you modify the response after the view function runs, perfect for adding headers.
What does the 'X-Frame-Options' header protect against?
ACross-site scripting
BClickjacking
CSQL injection
DData encryption
✗ Incorrect
X-Frame-Options prevents your site from being embedded in frames or iframes, protecting against clickjacking attacks.
Which header tells browsers to only use HTTPS for your site?
AContent-Security-Policy
BX-Content-Type-Options
CStrict-Transport-Security
DReferrer-Policy
✗ Incorrect
Strict-Transport-Security (HSTS) forces browsers to use HTTPS, improving security.
What is a simple way to add a Content-Security-Policy header in Flask?
AAdd it in the response headers using @app.after_request
BSet it in the HTML meta tags
CUse Flask's built-in CSP function
DModify the request headers
✗ Incorrect
You add CSP by modifying the response headers, typically in an @app.after_request function.
Which header helps prevent browsers from MIME-sniffing a response away from the declared content-type?
AStrict-Transport-Security
BX-Frame-Options
CContent-Security-Policy
DX-Content-Type-Options
✗ Incorrect
X-Content-Type-Options with the value 'nosniff' stops browsers from guessing the content type, reducing some attacks.
Explain how to add secure headers to all responses in a Flask app and why it is important.
Think about how Flask lets you change the response before sending it.
You got /4 concepts.
Describe the role of Content-Security-Policy and Strict-Transport-Security headers in web security.
One controls what the browser loads, the other controls how it connects.
You got /4 concepts.
Practice
(1/5)
1. Which Flask feature is commonly used to add secure headers to every response?
easy
A. The @app.before_first_request decorator
B. The @app.route decorator
C. The @app.after_request decorator
D. The @app.teardown_request decorator
Solution
Step 1: Understand Flask request lifecycle
Flask allows modifying responses after the view function runs using @app.after_request.
Step 2: Identify where headers are added
Secure headers are added to the response object, which is accessible in @app.after_request.
Final Answer:
The @app.after_request decorator -> Option C
Quick Check:
Use @app.after_request to add headers [OK]
Hint: Add headers in @app.after_request for all responses [OK]
Common Mistakes:
Using @app.route which only defines routes
Trying to add headers in @app.before_first_request which runs once
Using @app.teardown_request which is for cleanup, not response modification
2. Which of the following is the correct syntax to add a Content-Security-Policy header in Flask?
easy
A. response.set_header('Content-Security-Policy', "default-src 'self'")
B. response.header['Content-Security-Policy'] = "default-src 'self'"
C. response.headers.add('Content-Security-Policy', "default-src 'self'")
D. response.headers['Content-Security-Policy'] = "default-src 'self'"
Solution
Step 1: Recall Flask response header syntax
Flask response headers are a dictionary-like object accessed via response.headers.
Step 2: Assign header value correctly
Use response.headers['Header-Name'] = value to set or overwrite headers.
Final Answer:
response.headers['Content-Security-Policy'] = "default-src 'self'" -> Option D
Quick Check:
Headers set by assignment to response.headers [OK]
Hint: Use response.headers['Name'] = value to set headers [OK]
Common Mistakes:
Using header instead of headers
Trying to call set_header which Flask response lacks
Using headers.add which is not a Flask method
3. Given this Flask code snippet, what will be the value of the X-Frame-Options header in the response?
Header names are case-insensitive; using 'Strict-Transport-Security' is valid. The value is a valid string with quotes.
Step 2: Check function signature and usage
@app.after_request functions take one parameter (response) and return it. This matches the code.
Final Answer:
No error; the code is correct -> Option B
Quick Check:
Header added correctly in @app.after_request [OK]
Hint: Check function signature and header syntax carefully [OK]
Common Mistakes:
Thinking header names must be lowercase
Expecting two parameters in after_request function
Missing quotes around header value string
5. You want to add multiple secure headers in Flask, but only for HTTPS requests. Which is the best way to do this securely and efficiently?
hard
A. Use @app.after_request to check request.is_secure and add headers conditionally
B. Add headers in each route function only if request.scheme == 'https'
C. Set headers globally without checking protocol, assuming HTTPS everywhere
D. Use @app.before_request to add headers before response is created
Solution
Step 1: Understand when headers should be added
Secure headers like Strict-Transport-Security should only be sent over HTTPS to avoid security risks.
Step 2: Choose the best place to add headers conditionally
@app.after_request runs after response creation and can access request.is_secure to conditionally add headers efficiently for all routes.
Step 3: Evaluate other options
Adding headers in each route duplicates code; setting headers globally without checking protocol risks sending them over HTTP; @app.before_request cannot modify response headers.
Final Answer:
Use @app.after_request to check request.is_secure and add headers conditionally -> Option A
Quick Check:
Conditional header addition in @app.after_request is best [OK]
Hint: Check request.is_secure in @app.after_request [OK]
Common Mistakes:
Adding headers in each route causing repetition
Sending secure headers over HTTP by default
Trying to set headers in @app.before_request which can't modify response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.