avatarKirti K

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4864

Abstract

</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>></span> <span class="hljs-tag"><<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>CSP Style-Src Example<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-comment"><!-- Content Security Policy --></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"Content-Security-Policy"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"style-src 'self' https://fonts.googleapis.com"</span>></span> <span class="hljs-tag"></<span class="hljs-name">head</span>></span>

<span class="hljs-tag"></<span class="hljs-name">html</span>></span>

</pre></div><p id="fe5e">Here, stylesheets are allowed to be loaded from the same origin (<code>'self'</code>) and from <code>https://fonts.googleapis.com</code>.</p><p id="b37c">For example, <code><link rel="stylesheet" href="styles.css"></code> (if hosted on the same origin) and <code><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"></code> will be allowed.</p><h2 id="a695">4) font-src:</h2><p id="914a">This directive specifies which sources are allowed to load fonts.</p><div id="6159"><pre><span class="hljs-meta"><!DOCTYPE <span class="hljs-keyword">html</span>></span> <span class="hljs-tag"><<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>></span> <span class="hljs-tag"><<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>CSP Font-Src Example<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-comment"><!-- Content Security Policy --></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"Content-Security-Policy"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"font-src 'self' https://fonts.gstatic.com"</span>></span> <span class="hljs-tag"></<span class="hljs-name">head</span>></span> <span class="hljs-tag"></<span class="hljs-name">html</span>></span></pre></div><p id="dc9f">Fonts are allowed to be loaded from the same origin (<code>'self'</code>) and from <code>https://fonts.gstatic.com</code>.</p><p id="3f73">For instance, <code><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"></code> will be allowed because it loads fonts from <a href="https://fonts.gstatic.com."><code>https://fonts.gstatic.</code>com</a></p><h2 id="6bce">5) img-src:</h2><p id="f8aa">This directive specifies which sources are allowed to load images.</p><div id="0c9b"><pre><span class="hljs-meta"><!DOCTYPE <span class="hljs-keyword">html</span>></span> <span class="hljs-tag"><<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>></span> <span class="hljs-tag"><<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>CSP Img-src Example<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-comment"><!-- Content Security Policy --></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="h

Options

ljs-attr">http-equiv</span>=<span class="hljs-string">"Content-Security-Policy"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"img-src 'self' data:"</span>></span> <span class="hljs-tag"></<span class="hljs-name">head</span>></span> <span class="hljs-tag"></<span class="hljs-name">html</span>></span>

</pre></div><p id="53e3">Images are allowed to be loaded from the same origin (<code>'self'</code>) and from data URIs.</p><p id="f013">For example, <code><img src="image.jpg" alt="Example Image"></code> (if hosted on the same origin) and <code><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA..."></code> will be allowed.</p><p id="323c">Explore below website for more examples and explanation</p><div id="2ea6" class="link-block"> <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP"> <div> <div> <h2>Content Security Policy (CSP) - HTTP | MDN</h2> <div><h3>Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks…</h3></div> <div><p>developer.mozilla.org</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*VPq8w0j-snL172Zb)"></div> </div> </div> </a> </div><h1 id="8d24">Happy Coding and Stay Tuned :)</h1><p id="d9a5" type="7">Kind is always cool !!!</p><p id="7c01" type="7">Share knowledge to gain knowledge !!!!</p><p id="29cd" type="7">Be kind and treat people with respect !</p><h1 id="223b">If you find my blog helpful, then please subscribe, claps and follow to support :)</h1><figure id="20a5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NGrlClFjFv_qmuuGgrVhLw.jpeg"><figcaption></figcaption></figure><div id="ba80" class="link-block"> <a href="https://kirtikau.medium.com/list/6d895127c989"> <div> <div> <h2>100 Days of JavaScript Interview Prep: A Beginner’s Journey</h2> <div><h3>Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation (00 Days of JavaScript Interview Prep: A…</h3></div> <div><p>kirtikau.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*fd5fb9c1a7173e5ac3c86649fd406ffd5c3cdc98.jpeg)"></div> </div> </div> </a> </div><div id="f28f" class="link-block"> <a href="https://rohitsportsphysiorehabclinic.co.in/?source=post_page-----67ed3967afc6--------------------------------"> <div> <div> <h2>Home - Krishna Sports Injuries Physiotherapy & Rehab Clinic</h2> <div><h3>Krishna Sports Injuries Physiotherapy & Rehab Clinic There are plenty ways how to make an appointment. You can call us…</h3></div> <div><p>rohitsportsphysiorehabclinic.co.in</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*cCn4OpG36_qdfx1s)"></div> </div> </div> </a> </div><div id="3486" class="link-block"> <a href="https://kirtikau.medium.com/javascript-day-36-mastering-javascript-basics-a-comprehensive-guide-for-interview-preparation-f576100b0ae2"> <div> <div> <h2>JavaScript Day 36 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation</h2> <div><h3>100 Days of JavaScript Interview Prep: A Beginner’s Journey</h3></div> <div><p>kirtikau.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*RxrBad4p0qBR_79x7ZI83Q.png)"></div> </div> </div> </a> </div><div id="0a44" class="link-block"> <a href="https://kirtikau.medium.com/javascript-day-34-mastering-javascript-basics-a-comprehensive-guide-for-interview-preparation-96181febaa0a"> <div> <div> <h2>JavaScript Day 34 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation</h2> <div><h3>100 Days of JavaScript Interview Prep: A Beginner’s Journey</h3></div> <div><p>kirtikau.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*8aLCtBeTmiwar2o11vM3Eg.png)"></div> </div> </div> </a> </div></article></body>

JavaScript Day 38 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

JavaScript Day 38 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation with Kirti Kaushal

Understanding the CSP (Content Security Policy)

Welcome to Day 37–100 Days of JavaScript Interview Prep: A Beginner’s Journey with understanding the CSP.

How to Add Content Security Policy (CSP) and provide some example?

Answer

Here’s an example of how we can implement CSP in HTML

1) default-src:

This directive sets the default policy for all resource types (scripts, stylesheets, images, etc.)

if a more specific directive is not defined for that resource type.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSP Default-Src Example</title>
    <!-- Content Security Policy -->
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'">
</head>

</html>

It allows resources to be loaded only from the same origin ('self').

2) script-src:

This directive specifies which sources are allowed to load JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSP Script-Src Example</title>
    <!-- Content Security Policy -->
  <meta http-equiv="Content-Security-Policy" content="script-src 'self' https://cdnjs.cloudflare.com">
</head>

</html>

In this example, scripts are allowed to be loaded from the same origin ('self') and from https://cdnjs.cloudflare.com.

So, <script src="script.js"></script> (if hosted on the same origin) and <script src="https://cdnjs.cloudflare.com/xxx.js"></script> will be allowed.

3) style-src:

This directive specifies which sources are allowed to load CSS stylesheets.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSP Style-Src Example</title>
    <!-- Content Security Policy -->
 <meta http-equiv="Content-Security-Policy" content="style-src 'self' https://fonts.googleapis.com">
</head>

</html>

Here, stylesheets are allowed to be loaded from the same origin ('self') and from https://fonts.googleapis.com.

For example, <link rel="stylesheet" href="styles.css"> (if hosted on the same origin) and <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"> will be allowed.

4) font-src:

This directive specifies which sources are allowed to load fonts.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSP Font-Src Example</title>
    <!-- Content Security Policy -->
 <meta http-equiv="Content-Security-Policy" content="font-src 'self' https://fonts.gstatic.com">
</head>
</html>

Fonts are allowed to be loaded from the same origin ('self') and from https://fonts.gstatic.com.

For instance, <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"> will be allowed because it loads fonts from https://fonts.gstatic.com

5) img-src:

This directive specifies which sources are allowed to load images.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSP Img-src Example</title>
    <!-- Content Security Policy -->
   <meta http-equiv="Content-Security-Policy" content="img-src 'self' data:">
</head>
</html>


Images are allowed to be loaded from the same origin ('self') and from data URIs.

For example, <img src="image.jpg" alt="Example Image"> (if hosted on the same origin) and <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA..."> will be allowed.

Explore below website for more examples and explanation

Happy Coding and Stay Tuned :)

Kind is always cool !!!

Share knowledge to gain knowledge !!!!

Be kind and treat people with respect !

If you find my blog helpful, then please subscribe, claps and follow to support :)

Coding
Javascript Interview
Frontend Interviews
Coding Interviews
Programming
Recommended from ReadMedium