Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Oct 16, 2024, 12:26 AM EDT
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdnjs.cloudflare.com 'sha256-...'; style-src 'self' https://fonts.googleapis.com; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; frame-src 'self'; connect-src 'self' https://api.example.com; report-uri /csp-reportdefault-src: Fallback for all resource types.script-src: Defines valid JavaScript sources.style-src: Defines valid CSS sources.img-src: Defines valid image sources.font-src: Defines valid font sources.connect-src: Defines valid sources for XMLHttpRequest, WebSocket, and EventSource connections.object-src: Disables plugins like Flash or Java applets.frame-src: Restricts the sources for embedded frames.report-uriorreport-to: Defines where to send violation reports.Tuhin PaulRank #7 · 71.9K reputationPosted Oct 16, 2024, 12:25 AM EDT
To fix a Content Security Policy (CSP) vulnerability, you need to properly define and implement a strong CSP header that controls which resources (e.g., scripts, styles, images) are allowed to load and execute on your web pages. A well-defined CSP helps prevent Cross-Site Scripting (XSS), data injection, and other attacks.
Identify which types of resources your web page needs to load. This includes:
script-src)style-src)img-src)font-src)media-src)frame-src)object-srcorconnect-srcThe CSP is implemented via HTTP response headers or HTML
tags. For headers, it's usually added in the web server configuration (e.g., Apache, Nginx) or via backend code (e.g., in .NET, Node.js).Content-Security-Policy: default-src 'self'; script-src 'self' 'sha256-...'; style-src 'self'; img-src 'self'; object-src 'none'default-src 'self': Only allows content from the same domain as the website (no external resources).script-src 'self' 'sha256-...': Only allows scripts from the same domain, and inline scripts are allowed only if they match the given hash (a way to allow specific inline scripts without enablingunsafe-inline).style-src 'self': Only allows stylesheets from the same domain.img-src 'self': Only allows images from the same domain.object-src 'none': Disallows embedding objects like Flash, Java applets.Once you have your policy, configure your web server to send it as an HTTP header. Examples:
Apache: Add the header in your
.htaccessorhttpd.conffile:Header set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self';"Nginx: Add the header in your configuration file:
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self';";ASP.NET Core: In the
Startup.csfile:app.Use(async (context, next) => { context.Response.Headers.Add("Content-Security-Policy", "default-src 'self'; script-src 'self'; style-src 'self';"); await next(); });It’s common to start with a more permissive policy and then tighten it as you identify which resources are actually needed. For example, you might initially allow external resources like Google Fonts or CDN-hosted libraries (e.g., jQuery, Bootstrap), but restrict them later by allowing only specific sources (
font-src https://fonts.googleapis.com).By implementing a strict Content Security Policy tailored to your website's resource needs, you can mitigate many CSP vulnerabilities and protect your application from XSS and other types of attacks.