Challenge - 5 Problems
Flask-CORS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a Flask route with CORS enabled for all origins?
Consider a Flask app with Flask-CORS configured to allow all origins. What will be the value of the
Access-Control-Allow-Origin header in the response when a browser sends a cross-origin request?Flask
from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route('/') def home(): return 'Hello World!' # Assume a browser sends a cross-origin GET request to '/'
Attempts:
2 left
💡 Hint
Think about what happens when you enable CORS without restrictions.
✗ Incorrect
When Flask-CORS is initialized with default settings (CORS(app)), it allows all origins by setting the Access-Control-Allow-Origin header to '*'. This means any website can access the resource.
📝 Syntax
intermediate2:00remaining
Which Flask-CORS configuration syntax correctly restricts CORS to only 'https://example.com'?
You want to configure Flask-CORS to allow cross-origin requests only from 'https://example.com'. Which code snippet correctly achieves this?
Attempts:
2 left
💡 Hint
Check the parameter name and its expected type in Flask-CORS docs.
✗ Incorrect
The correct parameter is 'origins' and it expects a list or string. Using a list with one origin is the recommended way.
🔧 Debug
advanced3:00remaining
Why does this Flask-CORS setup fail to add CORS headers to a blueprint route?
Given the code below, why does the route '/api/data' not include CORS headers in the response?
Flask
from flask import Flask, Blueprint, jsonify from flask_cors import CORS app = Flask(__name__) cors = CORS(app, resources={r"/api/users/*": {"origins": "*"}}) api_bp = Blueprint('api', __name__, url_prefix='/api') @api_bp.route('/data') def data(): return jsonify({'key': 'value'}) app.register_blueprint(api_bp)
Attempts:
2 left
💡 Hint
Check if the resource pattern matches the full request path including the blueprint url_prefix.
✗ Incorrect
Flask-CORS only adds CORS headers for request paths that match the specified resource patterns. The pattern r'/api/users/*' does not match '/api/data', so no CORS headers are added.
🧠 Conceptual
advanced2:00remaining
What is the effect of setting 'supports_credentials=True' in Flask-CORS?
In Flask-CORS, what does setting the parameter
supports_credentials=True do to the CORS response headers?Attempts:
2 left
💡 Hint
Think about how browsers handle cookies and credentials in cross-origin requests.
✗ Incorrect
Setting supports_credentials=True adds the Access-Control-Allow-Credentials header with value true, which tells browsers to allow sending cookies and credentials in cross-origin requests.
❓ state_output
expert3:00remaining
What is the value of the 'Access-Control-Allow-Origin' header after this Flask-CORS configuration?
Given the Flask app below, what will be the value of the 'Access-Control-Allow-Origin' header in the response to a cross-origin request from 'https://evil.com'?
Flask
from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app, origins=["https://trusted.com", "https://partner.com"]) @app.route('/') def home(): return 'Welcome!' # A browser from 'https://evil.com' sends a cross-origin request to '/'
Attempts:
2 left
💡 Hint
Consider how Flask-CORS handles origins not in the allowed list.
✗ Incorrect
If the request origin is not in the allowed origins list, Flask-CORS does not add the Access-Control-Allow-Origin header, so the browser blocks the response for cross-origin requests.