Browser Extensions Development (Chrome & Edge) · 课时

内容安全策略(CSP)

配置并强制实施稳健的内容安全策略,以缓解注入攻击并控制资源加载

第 3 / 4 课11 个步骤

内容安全策略(CSP) 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Browser Extensions Development (Chrome & Edge) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What is CSP?

Welcome to Content Security Policy (CSP)! This powerful security feature helps protect your browser extensions and web pages from dangerous attacks like Cross-Site Scripting (XSS).

Think of CSP as a bouncer for your extension. It tells the browser exactly which sources are allowed to load scripts, styles, images, and other resources.

How CSP Works

CSP operates by defining a set of "directives" in your extension's manifest.json file. Each directive specifies valid sources for a particular type of resource.

  • Scripts: Where JavaScript can load from.
  • Styles: Where CSS can come from.
  • Images: Allowed sources for images.
  • Frames: Which URLs can be embedded in iframes.

If a resource tries to load from an unapproved source, the browser blocks it!

MV3's Default CSP

For Manifest V3 extensions, Chrome and Edge automatically apply a very strict default Content Security Policy. This helps ensure a baseline level of security.

You can customize or override this default CSP using the content_security_policy key within your manifest.json file. This is crucial when your extension needs to load resources from specific external domains.

`default-src`: The Catch-All

The default-src directive is your CSP's fallback. If you don't specify a directive for a particular resource type (like script-src or img-src), the browser will use the rules defined in default-src.

Common values:

  • 'self': Allows resources only from the extension's own origin.
  • https://example.com: Allows resources from a specific HTTPS domain.
  • *: Allows resources from any origin (use with extreme caution!).

Restricting JavaScript (`script-src`)

The script-src directive is vital for preventing Cross-Site Scripting (XSS). It dictates where your extension can load JavaScript code from.

For extensions, always avoid using 'unsafe-inline' or 'unsafe-eval'. These directives allow inline scripts and eval(), which are major security risks. Load scripts from your extension's package or explicitly whitelisted safe domains.

Manifest with CSP

Here's how you'd define a basic CSP in your manifest.json to allow scripts only from your extension's package and images from any source:

{
  "name": "My Secure Extension",
  "version": "1.0",
  "manifest_version": 3,
  "action": {
    "default_popup": "popup.html"
  },
  "content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self'; img-src *"
  }
}

Styles, Images & More

Just like with scripts, you can control other resource types:

  • style-src: Defines valid sources for CSS stylesheets. Use 'self' or specific HTTPS URLs.
  • img-src: Specifies allowed sources for images. You might use 'self', data: (for base64 images), or specific image CDNs.

Always be as restrictive as possible to enhance security!

Beyond Basic Directives

CSP offers many more directives for fine-grained control:

  • object-src: Restricts sources for plugins like <object>, <embed>.
  • frame-src: Controls which URLs can be loaded into <frame>, <iframe>, etc.
  • connect-src: Limits where your extension can make network requests (e.g., fetch(), XMLHttpRequest).

Principle of Least Privilege

When defining your CSP, always follow the "Principle of Least Privilege". This means only allowing exactly what your extension needs and nothing more.

  • Start with the strictest possible CSP.
  • Gradually add directives and sources as required.
  • Avoid wildcards (*) unless absolutely necessary and justified.
  • Regularly review your CSP as your extension evolves.

CSP Rule Check

You are building an extension and want to allow scripts only from your extension's own files, and images from any external HTTPS source. Which of the following CSP configurations would achieve this for extension_pages?

Recap: Secure with CSP

Great job! You've learned how Content Security Policy (CSP) is a critical defense mechanism for your browser extensions.

  • CSP helps prevent XSS and other injection attacks.
  • It works by whitelisting trusted sources for resources.
  • Manifest V3 enforces a strict default CSP.
  • Use directives like default-src, script-src, style-src, and img-src to control content.
  • Always apply the Principle of Least Privilege when defining your CSP.

A well-configured CSP is key to building secure and robust extensions!

免费开始

用 AI 导师学习 JavaScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「内容安全策略(CSP)」课时是免费的吗?

是的 — 「内容安全策略(CSP)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Browser Extensions Development (Chrome & Edge) 课程的其余内容,请升级到 CoddyKit PRO。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。

「内容安全策略(CSP)」这节课中我会学到什么?

配置并强制实施稳健的内容安全策略,以缓解注入攻击并控制资源加载 你通过在浏览器中直接运行的动手代码来练习 Browser Extensions Development (Chrome & Edge),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Browser Extensions Development (Chrome & Edge) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Browser Extensions Development (Chrome & Edge) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「内容安全策略(CSP)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?

能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 了解高级权限
  2. 安全编码实践
  3. 内容安全策略(CSP)
  4. 可选权限与运行时请求
← 返回 Browser Extensions Development (Chrome & Edge)