security-patterns

Use when working with Electron - IPC security, renderer isolation, Node API access

⭐2stars🔀0forks📁View on GitHub🕐Updated Dec 15, 2025

When & Why to Use This Skill

This Claude skill provides a comprehensive framework for implementing secure Electron applications by focusing on critical security patterns. It guides developers through IPC (Inter-Process Communication) bridge implementation, renderer isolation, and safe Node.js API access. By adhering to these best practices, developers can effectively prevent malicious code execution and ensure robust system resource protection within the Electron environment.

Use Cases

  • Implementing secure communication between the Electron renderer and main process using contextBridge and preload scripts.
  • Enforcing renderer isolation to prevent unauthorized direct access to Node.js APIs from the frontend layer.
  • Developing type-safe IPC interfaces to ensure consistent and secure data exchange between application processes.
  • Validating and sanitizing all IPC inputs in the main process to mitigate potential security risks originating from the renderer.
namesecurity-patterns
descriptionUse when working with Electron - IPC security, renderer isolation, Node API access

Electron Security Patterns

Use this skill when implementing features that interact with Electron APIs or system resources.

Checklist

Core Security Principle

  • Renderer process cannot access Node.js APIs directly
    • This is enforced by Electron's contextIsolation
    • All system access must go through IPC bridges
    • Prevents malicious code from accessing system resources

IPC Bridge Pattern

  • Define IPC handlers in electron/preload.ts
    • Use contextBridge.exposeInMainWorld() to expose APIs
    • Create type-safe interfaces for exposed APIs
    • Follow the window.electronAPI namespace convention

Example: Adding New Electron API

Step 1: Define handler in preload.ts

// electron/preload.ts
import { contextBridge, ipcRenderer } from 'electron'

contextBridge.exposeInMainWorld('electronAPI', {
  backend: {
    start: () => ipcRenderer.invoke('backend:start'),
    stop: () => ipcRenderer.invoke('backend:stop'),
    // Add new method here
    getStatus: () => ipcRenderer.invoke('backend:status')
  }
})

Step 2: Implement handler in main process

// electron/main.ts
ipcMain.handle('backend:status', async () => {
  // Access Node.js APIs safely here
  return await checkBackendStatus()
})

Step 3: Use in renderer process

// src/components/MyComponent.tsx
'use client'

const status = await window.electronAPI.backend.getStatus()

Security Checklist

  • Never expose raw IPC methods to renderer
    • Don't expose ipcRenderer.send() or ipcRenderer.invoke() directly
    • Create specific, scoped methods instead
  • Validate all IPC inputs in main process
    • Don't trust data from renderer process
    • Sanitize file paths, validate ranges, check types
  • Use typed interfaces for IPC communication
    • Define types in types/ directory
    • Share types between main and renderer processes
  • Minimize exposed surface area
    • Only expose what's necessary
    • Don't create generic "execute command" handlers

Common Patterns

File system access:

// ✅ Good - specific, validated
contextBridge.exposeInMainWorld('electronAPI', {
  files: {
    readConfig: () => ipcRenderer.invoke('files:read-config'),
    saveImage: (data: Buffer) => ipcRenderer.invoke('files:save-image', data)
  }
})

// ❌ Bad - too generic, security risk
contextBridge.exposeInMainWorld('electronAPI', {
  files: {
    read: (path: string) => ipcRenderer.invoke('files:read', path) // Unsafe!
  }
})

Process management:

// ✅ Good - scoped to backend process
backend: {
  start: () => ipcRenderer.invoke('backend:start'),
  stop: () => ipcRenderer.invoke('backend:stop')
}

// ❌ Bad - can execute arbitrary commands
system: {
  exec: (command: string) => ipcRenderer.invoke('exec', command) // Very unsafe!
}

Reference

See Electron Security Guide: https://www.electronjs.org/docs/latest/tutorial/security