Skip to content Skip to sidebar Skip to footer

Content Security Policy using Meta Tag: A Quick Guide

Web application security is one of the things that must be guaranteed if you don't want your website to be hacked by irresponsible people. Of course, there are many ways to anticipate hacker attacks such as XSS XSF or SQL Injection, one of which is to implement a Content Security Policy (CSP) on your website. However, many beginners don't know how to implement CSP because most of them don't have access to the server. The good news is that CSP can be implemented via Meta Tags.

content security policy meta tag, quick guide content security policy, easy content security policy, content security policy meta tag examples


What is the Content Security Policy (CSP)?

According to Mozilla:

Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution.

CSP is a set of rules that determine what website resources are allowed such as javascript files, images, videos, iframes, code, and so on. The easiest example we can think of is, "do you want some remote executable code to affect your website?" For example, I can remotely change the appearance of your website or add text or images to your website pages. I can do this without having to upload image files or type code in your website's source code. This is referred to as Cross-Site Scripting (XSS). Implementing CSP is the most appropriate step to anticipate changes to the website that occur remotely.

However, you may want it to happen; you want code from other sources or images hosted on other websites to be used on your website. For example, you might use a CSS or JS framework like Bootstrap or jQuery and you deploy a CDN to achieve this. This does not include XSS but if you implement CSP incorrectly then these framework files will not run on your website.

Whitelisting CDN

By properly implementing CSP, you can whitelist files or code or images from other sources. If you really want to protect your entire website and not allow other sources to be involved, then your solution should be to host all files (including framework files) in your website directory; but this is less practical.

How to implement CSP using a Meta Tag

To deploy CSP, you don't need access to your server. But you need access to your website files, especially the header section which is enclosed in <head> tags. Following are two examples of CSP meta tags where one does not allow code or files from other sources and the other allows code or files from other trusted sources.

Example A:

<meta http-equiv="Content-Security-Policy" content=" default-src 'self'; img-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; connect-src 'self'; font-src 'self'; frame-src 'self'; form-action 'self'; ">

In the example above, all files and code can only be accessed from the website directory (from the same domain, same host, and same port).

Example B:

<meta http-equiv="Content-Security-Policy" content=" default-src 'self'; img-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.css ; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/js/bootstrap.min.js https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js; connect-src 'self'; font-src 'self'; frame-src 'self'; form-action 'self'; ">

In the example above, apart from files and code from the directory, files and code from the Bootstrap and jQuery cdn are also allowed. I think styles, scripts, img, connect and so on are self-explanatory. Additionally, connect refers to a database connection (PHP) and form refers to, of course, form. In these two examples, database connections can only be made through your website (self), as well as data that can only be received from forms on your website.

It turns out that implementing CSP on a website is very easy, right? You only need to use a meta tag and a series of directives that I demonstrated in this blog. Okay, I think this is enough for a quick guide, good luck.

Please, only relevant comments are accepted. Comments that are irrelevant and/or containing active links will be deleted. Thank you.

Post a Comment for "Content Security Policy using Meta Tag: A Quick Guide"