Browser & Web Performance · Senior · Question 39 of 75

What's wrong with this CORS check?

const origin = req.headers.origin;
if (origin && origin.endsWith("example.com")) {
  res.setHeader("Access-Control-Allow-Origin", origin);
  res.setHeader("Access-Control-Allow-Credentials", "true");
}

Explanation

Compare against an exact allowlist of full origins (scheme + host + port). Suffix and regex checks are a classic CORS bug.

Further reading

Problem with a question

Browser & Web Performance · Senior · question 39

Test yourself

Take the Browser & Web Performance · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz