Using a Content Security Policy
Central to the web security ecosystem is the software we use daily to interact with our ASP.NET Core web application – the modern browser. Browsers have built-in security mechanisms to protect its users from attacks, making the overall user experience safe from web-based vulnerabilities. Additionally, how we write our code in our web apps is crucial to instructing the browser on how to enable these security features.
In the Fixing disabled security features recipe of Chapter 7, Security Misconfiguration, we learned that we can send special HTTP response headers to trigger the security features and tell the browser how to behave. We can tell the browser which hosts are safe to pull resources from and where it is safe to execute the scripts. These whitelisting rules can be defined using a CSP.
In this recipe, we will learn how to implement a basic CSP so that we can whitelist where the browser will retrieve our web resources.