Browser Extensions Development (Chrome & Edge) · درس

سياسة أمان المحتوى (CSP)

اضبط سياسة قوية لأمان المحتوى وطبّقها للحد من هجمات الحقن والتحكم في تحميل الموارد

الدرس 3 من 411 خطوة

سياسة أمان المحتوى (CSP) درس مجاني في Browser Extensions Development (Chrome & Edge) على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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!

البدء مجانًا

تعلم JavaScript مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «سياسة أمان المحتوى (CSP)» مجاني؟

نعم — نص درس «سياسة أمان المحتوى (CSP)» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Browser Extensions Development (Chrome & Edge)، انتقل إلى CoddyKit PRO. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.

ماذا ستتعلم في «سياسة أمان المحتوى (CSP)»؟

اضبط سياسة قوية لأمان المحتوى وطبّقها للحد من هجمات الحقن والتحكم في تحميل الموارد تتمرن على Browser Extensions Development (Chrome & Edge) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Browser Extensions Development (Chrome & Edge)؟

لا تُشترط خبرة سابقة. Browser Extensions Development (Chrome & Edge) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «سياسة أمان المحتوى (CSP)»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Browser Extensions Development (Chrome & Edge) هذا؟

نعم. كل درس في Browser Extensions Development (Chrome & Edge) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فهم الأذونات المتقدمة
  2. ممارسات البرمجة الآمنة
  3. سياسة أمان المحتوى (CSP)
  4. الأذونات الاختيارية والطلبات أثناء التشغيل
← العودة إلى Browser Extensions Development (Chrome & Edge)