

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Cross-Site Scripting (XSS)
incomplete
2: Fix Cross-Site Scripting
incomplete
3: Cross-Site Request Forgery (CSRF)
incomplete
4: CSRF Tokens
incomplete
5: Content Security Policy
incomplete
6: Legitimate Inline Scripts
incomplete
7: Sandboxing 'iframe' Elements
incomplete
8: Clickjacking
incomplete
9: Same-Origin and Referrer Policies
incomplete
10: Cross-Origin Resource Sharing
incomplete
11: CORS in Express
incomplete
12: Helmet
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Express provides the cors package to set CORS headers. You should only place the middleware it provides on routes that are intentionally available to other browser origins.
Imagine an app that applies this custom middleware to every API route:
export const apiCors: RequestHandler = (req, res, next) => {
const origin = req.header("Origin");
if (origin) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
}
next();
};
Reflecting an arbitrary origin while allowing credentials gives every requesting website permission to read authenticated API responses. Scary!
Not every API route needs the same policy. A storefront might have two categories:
The public route can use narrowly scoped middleware:
import cors from "cors";
app.use(
"/api/products",
cors({
origin: "*",
credentials: false,
methods: ["GET"],
allowedHeaders: [],
}),
);
Using * is fine here because the response is public and doesn't allow (or need any) credentials. The authenticated routes shouldn't get CORS middleware at all. Same-origin browser requests and non-browser clients don't need CORS permission headers.
The cors package also handles relevant preflight OPTIONS requests automatically, so you don't need to add a separate route in your application logic for them.
Bearly Secure grants every requesting origin access to authenticated APIs. Replace its global policy with route-scoped CORS.
npm run attacker-lab
If you're on Firefox, the test may still pass. You'll need to switch to Chrome to verify the tests manually.
With Bearly Secure and Bearly Evil still running, run and submit the CLI tests from the project root.