Back to course

Content Security Policy (CSP)

How to do that?

Well,

  1. Implement CSP to declare valid sources of content, reducing the risk of script injection.
  2. Use CSP to enforce policies like script-src or style-src to only allow trusted sources.
  3. Regularly update your CSP based on new threats and application changes.

Example:

app.use((req, res, next) => {
  res.setHeader("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline';");
  next();
});

Want to learn more? Check out the MDN Web Docs on CSP