ممارسات البرمجة الآمنة
طبّق مبادئ الأمان لمنع الثغرات الشائعة مثل البرمجة النصية عبر المواقع (XSS) وتسريب البيانات
ممارسات البرمجة الآمنة درس مجاني في Browser Extensions Development (Chrome & Edge) على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Browser Extensions Development (Chrome & Edge)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Security Matters for Extensions
Browser extensions operate with significant privileges, interacting closely with user data and browsing sessions. This makes secure coding practices absolutely critical.
Poor security can lead to serious vulnerabilities like Cross-Site Scripting (XSS) and data leakage, compromising user privacy and system integrity.
Understanding XSS Vulnerabilities
Cross-Site Scripting (XSS) occurs when an attacker injects malicious scripts into a trusted web application, or in our case, into your extension's UI or content scripts.
These scripts can steal sensitive data, hijack user sessions, or even deface websites, all within the context of your extension's permissions.
The Danger of `innerHTML`
One of the most common causes of XSS in web development, and thus in extensions, is using innerHTML with untrusted input.
If you take text directly from a user (e.g., from a form field, a URL parameter, or even a web page's content) and inject it into the DOM using innerHTML, any embedded scripts will execute.
Safe DOM Manipulation: `textContent`
To prevent XSS when displaying untrusted plain text, always use the textContent property instead of innerHTML.
textContent treats all input as raw text, preventing any HTML tags or scripts from being parsed and executed by the browser. It's your first line of defense.
Using `textContent` in Practice
Try running this simple JavaScript example. Notice how textContent safely renders the script as plain text, while innerHTML would dangerously attempt to execute it.
const userInput = "<script>alert('XSS!');</script>";
// Simulate a DOM element
const divSafe = { textContent: '' };
const divUnsafe = { innerHTML: '' };
// Safe way: Using textContent
divSafe.textContent = userInput;
console.log("Safe (textContent):", divSafe.textContent);
// Unsafe way: Using innerHTML (DO NOT DO THIS!)
divUnsafe.innerHTML = userInput;
console.log("Unsafe (innerHTML):", divUnsafe.innerHTML);Sanitizing HTML When Needed
What if you genuinely need to allow *some* HTML (like bold or italic) from user input? In these cases, textContent isn't enough.
You must use a robust, well-maintained HTML sanitization library (e.g., DOMPurify). These libraries parse HTML, remove malicious tags/attributes, and return safe HTML. Never build your own HTML sanitizer.
Avoiding Dynamic Code Execution
Functions like eval(), new Function(), setTimeout(string), and setInterval(string) execute JavaScript code from a string.
This is a significant security risk. If an attacker can control the string argument, they can execute arbitrary code within your extension's context, potentially bypassing other security measures.
Protecting Against Data Leakage
Data leakage occurs when sensitive user data is unintentionally or maliciously exposed to unauthorized third parties. This is a critical concern for extensions.
Be extremely cautious when sending data from your extension to external servers. Always verify the destination, use secure protocols (HTTPS), and encrypt sensitive information if necessary.
Principle of Least Privilege
When declaring permissions in your extension's manifest.json, always adhere to the Principle of Least Privilege.
Request only the absolute minimum permissions required for your extension's functionality. Overly broad permissions increase the attack surface and the potential for data leakage if your extension is compromised.
Security Best Practices Check
Which of the following are recommended secure coding practices for browser extensions?
Recap: Keeping Extensions Secure
You've learned crucial secure coding practices for building robust browser extensions:
- Sanitize All Input: Use
textContentfor plain text; use robust libraries like DOMPurify for HTML. - Avoid Dynamic Code: Never execute code from untrusted strings using functions like
eval(). - Least Privilege: Request only the essential permissions your extension needs.
- Prevent Data Leakage: Be cautious when transmitting user data, ensuring secure destinations and protocols.
By integrating these practices, you build more trustworthy extensions that protect user privacy and security.
الأسئلة الشائعة
هل درس «ممارسات البرمجة الآمنة» مجاني؟
نعم — نص درس «ممارسات البرمجة الآمنة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Browser Extensions Development (Chrome & Edge)، انتقل إلى CoddyKit PRO. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
ماذا ستتعلم في «ممارسات البرمجة الآمنة»؟
طبّق مبادئ الأمان لمنع الثغرات الشائعة مثل البرمجة النصية عبر المواقع (XSS) وتسريب البيانات تتمرن على Browser Extensions Development (Chrome & Edge) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Browser Extensions Development (Chrome & Edge)؟
لا تُشترط خبرة سابقة. Browser Extensions Development (Chrome & Edge) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «ممارسات البرمجة الآمنة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Browser Extensions Development (Chrome & Edge) هذا؟
نعم. كل درس في Browser Extensions Development (Chrome & Edge) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- فهم الأذونات المتقدمة
- ممارسات البرمجة الآمنة
- سياسة أمان المحتوى (CSP)
- الأذونات الاختيارية والطلبات أثناء التشغيل