Recruitment
Viettel IDC

What Is CSP? A Guide to Content Security Policy for Websites

Aug 27, 2026

CSP (Content Security Policy) is a security policy that allows browsers to load content only from trusted sources, helping prevent XSS attacks, block malicious code, and protect website data. Together with Viettel IDC, let’s explore what CSP is, how it works, and why every modern website should implement it.

What Is CSP? A Guide to Content Security Policy for Websites

What Is CSP?

CSP (short for Content-Security-Policy) is a security policy that allows browsers to control the resources a website is permitted to load, thereby preventing XSS attacks and limiting malicious code injection into web pages. CSP works by allowing a website to load scripts, CSS, images, fonts, or iframes only from trusted sources that have been predefined.

A basic Content Security Policy configuration example:

Content-Security-Policy:

  default-src 'self';

  script-src 'self' https://cdn.example.com;

  style-src 'self' https://fonts.googleapis.com;

  font-src https://fonts.gstatic.com;

  img-src 'self' data:;

  connect-src 'self' https://api.example.com;

  frame-ancestors 'none';

Specific explanations:

- default-src 'self': Only loads content from the website itself.

- script-src: Allows JavaScript to run only from the primary domain and trusted CDN.

- style-src: Allows CSS to be loaded from Google Fonts.

- img-src: Allows images from the website and Base64-encoded images.

- connect-src: Allows API requests from authorized sources.

- frame-ancestors: Prevents other websites from embedding your website.

Why Is CSP Important for Web Security?

CSP acts as a proactive layer of protection directly in the browser, helping prevent unsafe content from being loaded and executed. By strictly controlling resource sources, CSP has become an important component in protecting the integrity and security of websites.

- Mitigates XSS attacks: CSP only allows JavaScript to run from defined sources while blocking unsafe inline JavaScript and eval(). As a result, even if an attacker manages to inject a malicious script, the browser will refuse to execute it.

- Protects HTTPS connections: CSP helps reinforce the use of HTTPS, ensuring that data exchanged between clients and servers remains encrypted. When CSP requires resources to be loaded only over HTTPS, websites can more easily enforce secure connections and maintain content integrity.

- Blocks untrusted content: CSP prevents websites from unintentionally loading content such as scripts, images, or iframes from unsafe or unknown sources. This helps prevent improper data collection or information leakage to unwanted third parties. As a result, websites are better protected against malware and external attacks.

How Does CSP Work as a Security Mechanism?

CSP is enforced by the browser based on how a website sends its security policy along with each page. There are two common implementation methods:

CSP via HTTP Header

CSP can be sent to the browser through the Content-Security-Policy HTTP header or through a <meta> tag in HTML. When the browser receives this header, it interprets it as a set of rules (directives) defining which resource sources are permitted.

For example, if the policy declares script-src 'self' example.com, the browser will only allow JavaScript to be loaded from the current website and the example.com domain; all other sources will be blocked.

CSP via the Meta Tag in HTML

You can also configure CSP directly in HTML when you cannot modify the server. This approach is suitable for quick testing or for applying a policy to individual pages. For example, a CSP configuration using an HTML meta tag:

<meta http-equiv="Content-Security-Policy" content="default-src 'self';">

When the browser encounters this tag inside the <head> section, it will only allow resources to be loaded from the current domain and automatically block all content from external sources.

Why Is CSP Important for Web Security?

The Most Important CSP Directives

CSP consists of multiple directives, with each directive controlling a specific type of resource on the website. The table below provides a quick overview of the functions and configuration of the most important CSP directives.

Directive

Function

Configuration Example

default-src

Default source for all resources when no specific directive is defined.

default-src 'self';

script-src

Controls the sources from which JavaScript can be loaded and executed, helping prevent XSS attacks.

script-src 'self' https://cdn.example.com;

style-src

Specifies the sources from which CSS can be loaded.

style-src 'self' 'unsafe-inline';

img-src

Specifies the permitted image sources.

img-src 'self' data:;

font-src

Allows fonts to be loaded from valid domains.

font-src https://fonts.gstatic.com;

connect-src

Specifies sources for AJAX, fetch(), WebSocket, and API connections.

connect-src 'self' https://api.example.com;

frame-ancestors

Determines which websites are allowed to embed your page through an iframe.

frame-ancestors 'self';

form-action

Specifies the domains allowed to receive data from forms.

form-action 'self' https://secure.example.com;

media-src

Specifies sources for audio and video.

media-src media1.com media2.com;

object-src

Specifies sources for plug-ins and content such as PDF and Flash.

object-src 'none';

base-uri

Restricts the value of the <base> tag to prevent changes to link destinations.

base-uri 'self';

How to Implement Content Security Policy

Step 1: Create a CSP Header on the Server

The most complete way to implement CSP is to configure the Content-Security-Policy HTTP header directly on the server. When this header is sent with each response, the browser applies the entire declared security policy and strictly controls which resources are allowed to load.

This method also supports all CSP features and is suitable for systems running on Nginx, Apache, or similar platforms. Example Nginx configuration:

add_header Content-Security-Policy "<policy>";

Step 2: Test CSP in Report-Only Mode

Before officially enforcing CSP, you should enable Content-Security-Policy-Report-Only mode to identify potential violations. In this mode, the browser does not block resources but still logs violations in the console or sends violation reports to an endpoint through report-uri or report-to.

This allows you to identify blocked script, style, image, or API sources without causing display errors or affecting the user experience. Example configuration:

Content-Security-Policy-Report-Only: <policy>;

Step 3: Identify and Add Trusted Resource Sources

After collecting data in Report-Only mode, you need to compile a complete list of the valid domains currently used by the website, including CDNs, third-party APIs, script sources, styles, fonts, and communication components such as WebSockets.

This step is important to ensure that CSP does not incorrectly block legitimate resources when it is officially enabled. For required inline scripts, you should use a nonce or hash instead of allowing unsafe-inline, in order to maintain the highest possible level of security.

Step 4: Finalize the Policy and Deploy CSP

Once the whitelist is complete and there are no remaining critical violations, you can switch to the Content-Security-Policy header to enable full enforcement. This is the strict enforcement state, in which any source not included in the policy is rejected.

Note that the policy should be sent with all responses, not just the homepage, so that the browser applies it consistently across the entire website. Example of official enforcement:

Content-Security-Policy: <policy>;

Step 5: Monitor and Update the CSP Policy

After deployment, you should regularly review reports from report-uri or report-to to identify new violations when the website's interface changes or new third-party services are added. You can also use tools such as CSP Evaluator or browser security testing tools to review and optimize the policy.

Challenges and Solutions When Implementing CSP

Implementing CSP strengthens security, but incorrect configuration can cause display issues or unintentionally block legitimate resources. Below are some common challenges and corresponding solutions to ensure that CSP operates reliably.

- It can break website functionality: A strict CSP may sometimes block legitimate scripts or resources that have not been declared, causing forms, maps, or videos to stop working. You can minimize this issue by running Report-Only mode first and then adding trusted sources to the whitelist based on violation logs.

- Handling dynamic content and inline scripts: Inline or dynamic scripts, such as widgets, chat tools, or counters, may be blocked by CSP if they are not configured correctly. To allow these components to operate securely, you should use nonces or hashes in accordance with CSP Level 3 and explicitly define permitted CDNs in script-src.

- CSP does not replace secure coding practices: CSP cannot prevent XSS vulnerabilities originating from the source code itself, particularly server-side code injection vulnerabilities. Therefore, you still need to maintain security practices such as input validation, output encoding, and adherence to secure coding principles.

- Limited support in older browsers: Some older browsers may not understand or may ignore CSP, meaning the policy may not be fully enforced. The best approach is to identify your target browser group and implement additional protection mechanisms, such as X-Frame-Options, for platforms that do not support CSP.

Conclusion

Through this article, you now have a clear understanding of what CSP is and why Content Security Policy is an important security layer for protecting websites against XSS, blocking malicious code, and controlling all resources loaded by the website. When implemented correctly, CSP becomes an essential part of a modern security strategy, helping ensure that websites operate safely and reliably against threats on the Internet.

For consultation and information about Viettel's services, you can contact Viettel IDC directly through the following channels:

- Hotline: 1800 8088 (toll-free)

- Fanpage: https://www.facebook.com/viettelidc

- Website: https://viettelidc.com.vn

 

Comment ()

Login | Sign Up
to send comment
Your comment will be reviewed before being posted.
Your comment will be reviewed before being posted.
Your comment will be reviewed before being posted.
Read more

Related news

20/11/2026

Data Center Asset Management: 8 Core Components and Best Practices

Discover the 8 core components of Data Center Asset Management, key lifecycle stages, and best practices to optimize infrastructure, reduce costs, and improve operational efficiency.

20/11/2026

What Is Data Center Automation? Benefits & 5 Steps to Implement

Discover what Data Center Automation is, its key benefits, practical applications, and five steps to automate data center operations effectively.

20/11/2026

What Is Data Center Change Management? 5 Key Steps

Learn what Change Management in a data center is, why it matters for uptime, and how a 5-step process helps minimize operational risks and service disruptions.

20/11/2026

What Is Cross Connect in a Data Center? Benefits & Uses

Learn what Cross Connect in a data center is, how it reduces latency, enhances security, and improves network reliability for enterprise connectivity.

20/11/2026

Can a Data Center Be Hacked? Risks and Security Measures

Can a data center be hacked? Explore common attack methods, security risks, potential consequences, and effective ways to protect data center infrastructure.

20/11/2026

What Is a Mantrap in a Data Center? Benefits & Security

Learn what a Data Center Mantrap is, how it works, and how it prevents tailgating, unauthorized access, equipment theft, and physical security threats.

20/11/2026

Data Center Overheating: Causes, Risks & Prevention Solutions

Discover the main causes and risks of Data Center overheating, its impact on IT infrastructure, and effective cooling solutions to prevent downtime and hardware failures.

20/11/2026

Active-Active vs. Active-Passive in Data Centers: Key Differences

Compare Active-Active vs. Active-Passive Data Center architectures, including their working principles, advantages, disadvantages, costs, RTO, RPO, and use cases.

20/11/2026

What Is a BCP? Understanding Its Role in Data Centers

Learn what a Business Continuity Plan (BCP) is, its key components, development process, and importance in ensuring Data Center availability and resilience.

20/11/2026

ISO 27001 in Data Centers: Benefits, Requirements & Certification

Learn what ISO 27001 means for Data Centers, how it strengthens information security, its key benefits, certification process, and implementation challenges.