Bird
Raised Fist0
Flaskframework~20 mins

CORS configuration with Flask-CORS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flask-CORS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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 '/'
A"Access-Control-Allow-Origin" header is set to "*"
B"Access-Control-Allow-Origin" header is missing
C"Access-Control-Allow-Origin" header is set to the requesting origin only if it matches a whitelist
D"Access-Control-Allow-Origin" header is set to "null"
Attempts:
2 left
💡 Hint
Think about what happens when you enable CORS without restrictions.
📝 Syntax
intermediate
2: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?
ACORS(app, origins='https://example.com')
BCORS(app, origins=['https://example.com'])
CCORS(app, origin='https://example.com')
DCORS(app, allow_origin='https://example.com')
Attempts:
2 left
💡 Hint
Check the parameter name and its expected type in Flask-CORS docs.
🔧 Debug
advanced
3: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)
AThe route returns a dictionary instead of a string, causing CORS headers to be skipped
BCORS was applied to app but not explicitly to the blueprint, so blueprint routes lack CORS headers
CThe origins pattern in resources is incorrect and does not match '/api/data'
DThe blueprint was registered after CORS initialization, so CORS does not apply to it
Attempts:
2 left
💡 Hint
Check if the resource pattern matches the full request path including the blueprint url_prefix.
🧠 Conceptual
advanced
2: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?
AIt adds the header 'Access-Control-Allow-Credentials: true' allowing cookies and credentials in cross-origin requests
BIt sets 'Access-Control-Allow-Origin' to '*' regardless of origin
CIt disables CORS for all routes to prevent credential leaks
DIt automatically adds authentication headers to all responses
Attempts:
2 left
💡 Hint
Think about how browsers handle cookies and credentials in cross-origin requests.
❓ state_output
expert
3: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 '/'
A'Access-Control-Allow-Origin' is set to 'https://trusted.com'
B'Access-Control-Allow-Origin' is set to 'https://evil.com'
C'Access-Control-Allow-Origin' is set to '*'
DNo 'Access-Control-Allow-Origin' header is included in the response
Attempts:
2 left
💡 Hint
Consider how Flask-CORS handles origins not in the allowed list.

Practice

(1/5)
1. What is the main purpose of using Flask-CORS in a Flask application?
easy
A. To allow or restrict cross-origin requests to the Flask app
B. To speed up the Flask app by caching responses
C. To handle database connections automatically
D. To manage user authentication and sessions

Solution

  1. Step 1: Understand what CORS means

    CORS stands for Cross-Origin Resource Sharing, which controls which websites can access resources from your Flask app.
  2. Step 2: Identify Flask-CORS role

    Flask-CORS is a tool to add headers that allow or restrict these cross-origin requests easily.
  3. Final Answer:

    To allow or restrict cross-origin requests to the Flask app -> Option A
  4. Quick Check:

    CORS controls access = A [OK]
Hint: Remember CORS controls cross-site access permissions [OK]
Common Mistakes:
  • Confusing CORS with caching or authentication
  • Thinking Flask-CORS speeds up the app
  • Assuming Flask-CORS manages databases
2. Which of the following is the correct way to enable CORS for all routes in a Flask app using Flask-CORS?
easy
A. from flask_cors import CORS app = Flask(__name__) CORS()
B. from flask_cors import CORS app = Flask(__name__) app.enable_cors()
C. from flask_cors import CORS app = Flask(__name__) CORS(app)
D. from flask_cors import CORS app = Flask(__name__) CORS(app, origins='none')

Solution

  1. Step 1: Import and create Flask app

    Import CORS from flask_cors and create the Flask app instance.
  2. Step 2: Enable CORS on the app

    Call CORS(app) to enable CORS for all routes. This is the correct syntax.
  3. Final Answer:

    from flask_cors import CORS\napp = Flask(__name__)\nCORS(app) -> Option C
  4. Quick Check:

    Enable CORS by passing app to CORS() [OK]
Hint: Use CORS(app) to enable CORS globally [OK]
Common Mistakes:
  • Calling CORS() without app argument
  • Using non-existent app.enable_cors() method
  • Setting origins='none' disables all CORS
3. Given this Flask app code snippet, what will be the CORS behavior for the route /data?
from flask import Flask, jsonify
from flask_cors import CORS

app = Flask(__name__)
CORS(app, origins=['https://example.com'])

@app.route('/data')
def data():
    return jsonify({'key': 'value'})
medium
A. Only localhost can access /data cross-origin
B. All origins can access /data without restriction
C. No origins can access /data due to missing CORS headers
D. Only requests from https://example.com will be allowed cross-origin access

Solution

  1. Step 1: Check CORS configuration

    CORS is enabled with origins=['https://example.com'], so only that origin is allowed cross-origin access.
  2. Step 2: Understand route behavior

    The /data route returns JSON and inherits the CORS policy set on the app.
  3. Final Answer:

    Only requests from https://example.com will be allowed cross-origin access -> Option D
  4. Quick Check:

    Origins list limits access = A [OK]
Hint: Origins list restricts which sites can access [OK]
Common Mistakes:
  • Assuming all origins are allowed by default
  • Thinking CORS disables all access without explicit allow
  • Confusing localhost with example.com
4. Identify the error in this Flask app CORS setup:
from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(origins='*')

@app.route('/')
def home():
    return 'Hello'
medium
A. CORS is called without passing the Flask app instance
B. The origins parameter '*' is invalid and causes an error
C. The route function home() must return JSON for CORS to work
D. Flask app instance must be created after calling CORS

Solution

  1. Step 1: Check how CORS is called

    CORS() is called without passing the app instance, so it does not enable CORS on the app.
  2. Step 2: Understand consequences

    Without passing app, no CORS headers are added, so cross-origin requests will fail.
  3. Final Answer:

    CORS is called without passing the Flask app instance -> Option A
  4. Quick Check:

    Pass app to CORS() to enable it [OK]
Hint: Always pass app instance to CORS() [OK]
Common Mistakes:
  • Forgetting to pass app to CORS()
  • Thinking '*' is invalid (it's valid)
  • Believing CORS requires JSON response
5. You want to allow CORS only on the route /api/data for requests from https://trusted.com. Which Flask-CORS setup correctly achieves this?
hard
A. CORS(app, resources={r'/api/*': {'origins': '*'}})
B. CORS(app, resources={r'/api/data': {'origins': 'https://trusted.com'}})
C. CORS(app, origins='https://trusted.com')
D. CORS(app, resources={r'/api/data': {'origins': '*'}})

Solution

  1. Step 1: Understand resource-specific CORS

    To restrict CORS to a specific route, use the resources parameter with a route pattern and origins.
  2. Step 2: Match the route and origin

    CORS(app, resources={r'/api/data': {'origins': 'https://trusted.com'}}) correctly sets CORS only on '/api/data' with origins 'https://trusted.com'.
  3. Final Answer:

    CORS(app, resources={r'/api/data': {'origins': 'https://trusted.com'}}) -> Option B
  4. Quick Check:

    Use resources to limit routes and origins [OK]
Hint: Use resources dict to target specific routes [OK]
Common Mistakes:
  • Setting origins globally instead of per route
  • Using wildcard '*' when only one origin is needed
  • Using wrong route pattern like '/api/*' for exact match
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard