0Pricing
Electron Desktop App Development · درس

وضع الحماية لعملية العرض

فعّل وضع الحماية لعمليات العرض واضبطه للحد من وصولها إلى موارد النظام، مما يعزّز أمان تطبيقك

وضع الحماية لعملية العرض درس مجاني في Electron Desktop App Development على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Electron Desktop App Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Electron Desktop App Development 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What is Sandboxing?

Imagine your computer as a house. Some guests (apps) you trust completely, others you want to keep in a specific room with limited access.

Sandboxing is like putting your app's renderer process in a secure, isolated "room." It limits what that part of your app can do and access on your system.

This is crucial for security, especially when displaying untrusted web content or protecting against malicious scripts.

Why Sandboxing Matters

Electron apps combine web content (HTML, CSS, JS) with native desktop capabilities. Without proper isolation, a vulnerability in your web content could be exploited to access your user's file system or other system resources.

Sandboxing prevents this by isolating the renderer process, making it much harder for malicious code to "break out" and harm the user's system.

Chromium's Security Layer

Electron uses Chromium, the same engine that powers Google Chrome. A key security feature of Chromium is its renderer sandbox.

By default in browsers, web pages run in a sandboxed renderer process, meaning they can't directly interact with your operating system. Electron extends this robust security model to your desktop applications.

Activating the Sandbox

Enabling sandboxing in Electron is straightforward. You do it when creating a new BrowserWindow by setting the sandbox option to true in webPreferences.

This is a critical step for enhancing the security posture of your Electron application.

const { app, BrowserWindow } = require('electron');

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      sandbox: true // Set to true to enable sandboxing
    }
  });
  win.loadFile('index.html'); // Loads your web content
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

Renderer Process Limitations

When a renderer process is sandboxed, it faces significant restrictions:

  • It cannot directly access Node.js APIs (e.g., require, process, fs).
  • It cannot use native modules.
  • Its access to system resources (like files) is severely limited.

This isolation prevents web content from performing unauthorized actions on the user's computer.

Node.js Access Denied Demo

Let's see what happens when a sandboxed renderer tries to access Node.js directly. The code below creates a window with sandboxing enabled and attempts to use the fs module.

Run this example and check the developer console (usually F12 or Cmd+Option+I) for the error message.

const { app, BrowserWindow } = require('electron');

function createWindow () {
  const win = new BrowserWindow({
    width: 600,
    height: 400,
    webPreferences: {
      sandbox: true, // Sandboxing enabled
      nodeIntegration: false, // Explicitly disabled (default with sandbox)
      contextIsolation: true // Explicitly enabled (default with sandbox)
    }
  });

  // Load HTML that tries to use Node.js 'fs' module
  win.loadURL(`data:text/html;charset=utf-8,
        <!DOCTYPE html>
        <html>
        <head>
            <title>Sandboxed Demo</title>
        </head>
        <body>
            <h1>Sandboxed Renderer Test</h1>
            <p id="message">Attempting to access Node.js...</p>
            <script>
                try {
                    const fs = require('fs'); // This will fail!
                    document.getElementById('message').innerText = 'Node.js fs module loaded!';
                } catch (error) {
                    document.getElementById('message').innerText = 'Error: ' + error.message;
                    console.error('Renderer error:', error);
                }
            </script>
        </body>
        </html>
      `);

  win.webContents.openDevTools(); // Open DevTools to see the error
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

Secure Interaction: Preload

Since sandboxed renderers can't directly access Node.js, how do they perform system tasks?

You use preload scripts with context isolation. These scripts run before your web content, have Node.js access, and can securely expose a limited API to your sandboxed renderer via contextBridge.

This ensures that your main process controls exactly what functionality is exposed.

Sandbox & Context Isolation

It's important to note that when webPreferences.sandbox is true, both nodeIntegration will be false and contextIsolation will be true by default, regardless of what you explicitly set.

This combination provides the strongest security for your renderer processes.

Best Practices

  • Always enable sandboxing: It's a fundamental security measure.
  • Minimize exposed APIs: Only expose essential functionality via preload scripts.
  • Validate all inputs: Any data passed from a renderer to the main process should be validated.

These practices create a robust and secure Electron application.

Quick Check

Understanding sandboxing is key to building secure Electron applications.

Recap: Securing Your App

In this lesson, we explored sandboxing in Electron. We learned that it's a critical security feature that isolates your renderer processes, preventing them from directly accessing system resources or Node.js APIs.

By setting webPreferences.sandbox: true, you activate this powerful protection, ensuring a safer application. Remember to use preload scripts with context isolation for secure communication between your sandboxed renderer and the main process.

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

هل درس «وضع الحماية لعملية العرض» مجاني؟

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

ماذا ستتعلم في «وضع الحماية لعملية العرض»؟

فعّل وضع الحماية لعمليات العرض واضبطه للحد من وصولها إلى موارد النظام، مما يعزّز أمان تطبيقك تتمرن على Electron Desktop App Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Electron Desktop App Development؟

لا تُشترط خبرة سابقة. Electron Desktop App Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «وضع الحماية لعملية العرض»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Electron Desktop App Development هذا؟

نعم. كل درس في Electron Desktop App Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. أنماط IPC الآمنة
  2. عزل السياق ونصوص التحميل المسبق
  3. وضع الحماية لعملية العرض
  4. تحصين التطبيق ضد مخاطر المحتوى البعيد
← العودة إلى Electron Desktop App Development