Content Security Policy (CSP): defining where resources can be loaded from

The video is hosted by YouTube (Google). When played, YouTube may set cookies.

The Content Security Policy (CSP) response header helps prevent several types of attacks, such as cross-site scripting (XSS) and data injection, by letting website developers define the sources from which the browser may execute scripts, load resources and interact with the page.

By specifying a CSP header, developers can define which domains are allowed to run JavaScript, CSS and other resources on a page, and control how inline scripts and styles are handled. This can stop attackers from injecting malicious scripts or resources into a web page and protects the page against other forms of data injection.

Beyond preventing data injection, a CSP header can also reduce the impact of cross-site scripting by preventing malicious scripts from running in the context of a web page. By restricting the sources scripts can run from, a CSP header limits the damage an attacker can do, even if they manage to inject a malicious script into a page.

Overall, the CSP header is an important security feature that helps developers protect their websites and users against a wide range of attacks. By implementing a CSP header and regularly reviewing and updating the policy, developers can significantly reduce the risk of a successful attack on their site.

Remediation:

Apache2
  1. Add the following line to the Apache2 configuration file (list only the hosts allowed to serve scripts, stylesheets and images):
    Header set Content-Security-Policy "default-src 'self';"
  2. Restart the web server

Nginx

  1. Add the following line to the Nginx configuration file:
    add_header Content-Security-Policy "default-src 'self';";
  2. Restart the web server

IIS

  1. Add the following to the IIS configuration file:
    <system.webServer>
    <httpProtocol>
    <customHeaders>
    <add name="Content-Security-Policy" value="default-src 'self';" />
    </customHeaders>
    </httpProtocol>
    </system.webServer>
  2. Restart the web server

The above is just a simple example; the policy must be built for your needs. Another example:

object-src 'self'
default-src 'self'
script-src 'self' 'unsafe-eval' 'unsafe-inline'
frame-src 'self'
frame-ancestors 'self'
style-src 'self' 'unsafe-inline' blob:
form-action 'self'
img-src 'self' data:
report-uri https://your_report_uri.org/DS_CSP_Violation

References:

  • https://content-security-policy.com/
  • response headers
  • security headers
  • content security policy
  • csp
  • cybersecurity
Iulian Băncău

Security Engineer · All Safe

More than CyberSecurity

We offer a variety of services to help companies be prepared for real cyber attacks.