Complete the code to add a security header to the Flask response.
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hello, world!') response.headers['[1]'] = 'nosniff' return response
The X-Content-Type-Options header with value nosniff prevents browsers from MIME-sniffing a response away from the declared content-type.
Complete the code to set the Content Security Policy header to allow only scripts from the same origin.
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hello, secure world!') response.headers['Content-Security-Policy'] = "script-src [1]" return response
The Content-Security-Policy header with script-src 'self' allows scripts only from the same origin, improving security.
Fix the error in setting the Strict-Transport-Security header to enforce HTTPS for 1 year.
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Secure HTTPS!') response.headers['Strict-Transport-Security'] = 'max-age=[1]' return response
The max-age value for Strict-Transport-Security is in seconds. 31536000 seconds equals 1 year.
Fill both blanks to add X-Frame-Options header to deny framing and set X-XSS-Protection header to block mode.
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('No framing and XSS protection') response.headers['[1]'] = 'DENY' response.headers['[2]'] = '1; mode=block' return response
X-Frame-Options: DENY prevents the page from being framed to avoid clickjacking.X-XSS-Protection: 1; mode=block enables the browser's XSS filter to block detected attacks.
Fill all three blanks to create a dictionary of security headers with correct keys and values.
security_headers = {
'[1]': 'nosniff',
'[2]': "script-src [3]"
}This dictionary sets X-Content-Type-Options to nosniff to prevent MIME sniffing, and Content-Security-Policy to allow scripts only from the same origin using script-src 'self'.