Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of Trusted Host Middleware in FastAPI?
Trusted Host Middleware helps protect your app by allowing requests only from specific hostnames you trust. It blocks requests from unknown or suspicious hosts.
Click to reveal answer
beginner
How do you add Trusted Host Middleware in a FastAPI app?
You import TrustedHostMiddleware from starlette.middleware.trustedhost and add it to your app with app.add_middleware(TrustedHostMiddleware, allowed_hosts=[...]).
Click to reveal answer
beginner
What happens if a request comes from a host not in the allowed_hosts list?
The middleware returns a 400 Bad Request response and blocks the request from reaching your app.
Click to reveal answer
intermediate
Why is it important to include both domain names and localhost in allowed_hosts during development?
Including localhost allows testing on your machine, while domain names protect your app in production by only accepting trusted hosts.
Click to reveal answer
intermediate
Can you use wildcards in allowed_hosts with Trusted Host Middleware?
Yes, you can use patterns like '*.example.com' to allow all subdomains of example.com.
Click to reveal answer
What status code does Trusted Host Middleware return for disallowed hosts?
A400 Bad Request
B404 Not Found
C403 Forbidden
D500 Internal Server Error
✗ Incorrect
Trusted Host Middleware returns 400 Bad Request when the host is not in the allowed list.
Which FastAPI method is used to add Trusted Host Middleware?
Aapp.use_middleware()
Bapp.include_middleware()
Capp.add_middleware()
Dapp.register_middleware()
✗ Incorrect
You use app.add_middleware() to add middleware like TrustedHostMiddleware in FastAPI.
Which import is correct to use Trusted Host Middleware in FastAPI?
A. The request will be redirected to 'example.com'
B. The request will succeed and return 'Hello World'
C. The app will crash with an exception
D. The request will be blocked with a 400 Bad Request error
Solution
Step 1: Check allowed hosts list
The allowed hosts are 'example.com' and 'localhost'. 'malicious.com' is not in this list.
Step 2: Understand middleware behavior on unknown hosts
TrustedHostMiddleware blocks requests from hosts not in the allowed list by returning a 400 error.
Final Answer:
The request will be blocked with a 400 Bad Request error -> Option D
Quick Check:
Unknown host causes 400 error = A [OK]
Hint: Requests from hosts not allowed get 400 error [OK]
Common Mistakes:
Assuming the request passes through
Thinking the app crashes on unknown hosts
Believing the request is redirected automatically
4. Identify the error in this FastAPI app setup using TrustedHostMiddleware:
from fastapi import FastAPI
from starlette.middleware.trustedhost import TrustedHostMiddleware
app = FastAPI()
app.add_middleware(TrustedHostMiddleware, allowed_hosts='example.com')
@app.get('/')
def home():
return {'msg': 'Welcome'}
medium
A. The route function must be async
B. TrustedHostMiddleware is not imported correctly
C. allowed_hosts should be a list, not a string
D. Missing middleware initialization parameters
Solution
Step 1: Check allowed_hosts argument type
The allowed_hosts parameter expects a list of strings, but a single string was given.
Step 2: Understand impact of wrong type
Passing a string instead of a list will cause the middleware to treat each character as a host, leading to incorrect behavior or errors.
Final Answer:
allowed_hosts should be a list, not a string -> Option C
Quick Check:
allowed_hosts must be list = A [OK]
Hint: allowed_hosts always needs a list, not a string [OK]
Common Mistakes:
Passing a single string instead of list
Thinking route functions must be async
Assuming import is incorrect without error
5. You want to allow requests from any subdomain of example.com and also from localhost. Which allowed_hosts list correctly configures TrustedHostMiddleware for this?
hard
A. ['*.example.com', 'localhost']
B. ['example.com', 'localhost']
C. ['example.com/*', 'localhost']
D. ['*example.com', 'localhost']
Solution
Step 1: Understand wildcard usage in allowed_hosts
TrustedHostMiddleware supports wildcards like *.example.com to allow all subdomains.
Step 2: Check each option for correct wildcard syntax
['*.example.com', 'localhost'] uses '*.example.com' which correctly matches all subdomains; others use incorrect patterns.
Final Answer:
['*.example.com', 'localhost'] -> Option A
Quick Check:
Use '*.example.com' for subdomains = B [OK]
Hint: Use '*.domain.com' to allow all subdomains [OK]