Course contentsShow
← Web Development
Lesson 2847 of 2,98251. Content Security PolicyFree lesson

The media-src Directive

Restricting audio and video sources using media-src to control multimedia content origins.

The media-src Directive

You'll learn how to control where your web page can load audio and video content from using CSP's media-src directive.

What is media-src?

The media-src directive in Content Security Policy lets you specify which origins are allowed to serve multimedia content loaded through HTML <audio>, <video>, and <track> elements. This prevents attackers from injecting malicious media files or tracking users through unexpected media sources.

Think of it like a bouncer at a concert venue who checks whether each musician (media source) is on the approved performers list before letting them on stage.

How It Works

When you set media-src, the browser will only load media from the origins you explicitly allow:

Content-Security-Policy: media-src 'self' https://cdn.example.com

This policy allows media files from:

  • Your own domain ('self')
  • Your CDN at https://cdn.example.com

Any <audio> or <video> element trying to load content from other origins will be blocked.

Common Use Cases

Restrict to your own servers:

Content-Security-Policy: media-src 'self'

Allow a specific video hosting service:

Content-Security-Policy: media-src 'self' https://*.vimeo.com

Block all media (rare):

Content-Security-Policy: media-src 'none'

Why It Matters

Without media-src, an XSS attack could inject video or audio elements pointing to malicious servers, potentially tracking users, consuming bandwidth, or delivering harmful content disguised as legitimate media.

Key Takeaway: The media-src directive protects your users by restricting which domains can serve audio and video content, preventing unauthorized multimedia sources from being loaded into your pages.