Advanced Web Development Quiz

Q15: Content Security Policy CSP Header

Lydia Hallie

Lydia Hallie

Lydia Hallie
Advanced Web Development Quiz

Check out a free preview of the full Advanced Web Development Quiz course

The "Q15: Content Security Policy CSP Header" Lesson is part of the full, Advanced Web Development Quiz course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to determine which resources will be allowed given the specified Content Security Policy (CSP) header


Transcript from the "Q15: Content Security Policy CSP Header" Lesson

>> All right, which resources will be allowed with following content Content Security Policy header? Is it A, a script like a local script, B, fetching from, a font face with fonts, or like just loading local fonts? I'll just say it like that. A data URI image, an inline style, an iframe that embeds from, a stylesheet from, or a video from

All right, so let's look at the answer. The answer is we can only load our local scripts and the stylesheet from So with a Content Security Policy header, we can control where our website can load resources from. So we can do this for scripts, images, fonts, stylesheets, video, audio embedded content, iframes, web and service workers, fetch and web sockets, and form submissions.

Now by default here, we say default-src none. So this basically sets everything to none. We can not load any resources, not even local resources, nothing, we cannot do anything. But we can override this with, in this case, a script-src, sorry about that, which I set it to self, very subtle.

So in this case, we're saying scripts can only be loaded from the same origin, from our website, not from any other location. Then we set img-src to self, so we can load images from the same origin, but also from the domain. We set the style-src only to

So it can only fetch from, not from your own fonts, no, only from that domain. And then the style sheet, sorry, it says CSS from there, and then the fonts from gstatic or So also not from self, just from fonts.gstatic. So if we now compare that, first we have, well, our local scripts.

And this is allowed because we have the script-src self. So we just said that it's okay to load resources from the same origin or yeah, from your own website. That's totally fine. But then we make a fetch request to, but we have the connect src still to none because we didn't specify any overrides.

So it defaulted back to that default-src. So that'll be blocked by the Content Security Policy. Same for font-face, we only set okay, you can load fonts from but not from your own fonts, not from your own domain. So this one gets blocked. For images, this is a bit deceiving cuz you think, okay, it's a data image, is this self, it's not.

So with img-src, you need to then define the data. You need to add the data attribute to img-src to allow these base 64 images as well. So this one is blocked because we don't have that data in the CSB policy. Then we have inline styles also not allowed because we only specify that styles we come from, and this includes inline styles.

Then we have iframes, frame-src is none, so we cannot embed anything. Finally, we have stylesheet. This is allowed because we're fetching it from so that domain is allowed. And then lastly, we have video, which is the media-src, we also set that to none. So this one will be blocked.

Yeah, I'll just keep it on this one. So yeah, the Content Security Policy is just a great way to really fully control where a website can load resources from to prevent any attacks like cross-site scripting, all that kind of stuff. Especially important when you're working at the bank or anything that uses very sensitive data.

Making sure that if you know exactly where your resources are coming from, setting up a Content Security Policy, either in like a metatag or as a response header is a great way to ensure that your website is a bit more secure, yes.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now