Consider this Flask route that sets security headers. What headers will the client receive?
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hello') response.headers['X-Frame-Options'] = 'DENY' response.headers['Content-Security-Policy'] = "default-src 'self'" return response
Look at the exact header keys and values set in the code.
The code sets X-Frame-Options to DENY and Content-Security-Policy to default-src 'self'. So the client receives these exact headers.
Identify which code snippet will cause a syntax error when setting headers in Flask.
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hi') # Header setting line below # Replace with one of the options return response
Check if the header value is a valid Python string.
Option A misses quotes around the string value, causing a syntax error. It should be a string literal.
This Flask middleware adds a security header. What is the header's value after a request?
from flask import Flask, request app = Flask(__name__) @app.after_request def add_hsts_header(response): response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains' return response @app.route('/') def index(): return 'Secure site'
Look at the exact string assigned to the header in the middleware.
The middleware sets the header to 'max-age=31536000; includeSubDomains' on every response.
Given this Flask code, why is the 'X-Content-Type-Options' header missing in the response?
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hello') response.headers.add('X-Content-Type-Options', 'nosniff') return 'Hello World!'
Check what the route function returns.
The function returns a string instead of the response object with headers set, so headers are lost.
Choose the header setting that most effectively prevents clickjacking.
Both 'X-Frame-Options' and 'Content-Security-Policy' can control framing, but one is more modern and flexible.
While 'X-Frame-Options: DENY' blocks all framing, 'Content-Security-Policy: frame-ancestors 'none'' is a modern, more flexible way to prevent clickjacking and is recommended.