Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:3641397194 wq Gpt6 Astra window security

From Leeroopedia
Knowledge Sources
Domains Security, Electron
Last Updated 2026-10-26 14:00 GMT

Overview

Concrete tool for enforcing IPC trust boundaries and navigation guards on Electron BrowserWindow contents, ensuring only the trusted local workbench document can invoke desktop APIs, provided by the ColdBrew Zero desktop lib.

Description

The window-security module provides three exported functions that form the Electron security boundary:

  • isTrustedDocument(value, entryFile): validates that a `file:` URL resolves to the exact entry HTML file, rejecting non-file protocols, URLs with host/search/credentials, and path mismatches (case-insensitive on Windows).
  • assertTrustedSender(event, window, entryFile): verifies that an IPC event originates from the window's main frame and that the sender frame's URL is the trusted entry file. Throws if any check fails.
  • protectWindow(window): hardens a window's webContents by preventing all navigation (`will-navigate`, `will-frame-navigate`), blocking webview attachment, and denying all window-open attempts. The workbench is a fixed local document; external URLs are handled only by the host-allowlisted shell handler in main.js.

Usage

  • IPC handler guards: wrap every `ipcMain.handle` / `ipcMain.on` callback with `assertTrustedSender` to ensure only the workbench renderer can call privileged APIs.
  • Window creation: call `protectWindow(window)` immediately after creating each BrowserWindow to enforce the no-navigation, no-popup policy.
  • Custom protocol checks: use `isTrustedDocument` directly when validating file:// URLs from custom protocol handlers.

Code Reference

Source Location

Signature

function isTrustedDocument(value: string, entryFile: string) -> boolean
function assertTrustedSender(event: IpcMainEvent, window: BrowserWindow, entryFile: string) -> void  // throws on untrusted
function protectWindow(window: BrowserWindow) -> void

module.exports = { isTrustedDocument, assertTrustedSender, protectWindow };

Import

const { isTrustedDocument, assertTrustedSender, protectWindow } = require('./window-security');

I/O Contract

Inputs

Name Type Required Description
value string Yes (isTrustedDocument) URL string to validate as a trusted file:// document
entryFile string Yes Absolute path to the trusted entry HTML file
event IpcMainEvent Yes (assertTrustedSender) The IPC event from ipcMain handler
window BrowserWindow Yes The BrowserWindow to guard or verify against

Outputs

Name Type Description
isTrustedDocument returns boolean True if value is a file:// URL resolving to entryFile
assertTrustedSender returns void Returns normally if trusted; throws Error if untrusted
protectWindow returns void Registers navigation/webview/open prevention handlers on window.webContents

Usage Examples

Guard an IPC Handler

const { ipcMain } = require('electron');
const { assertTrustedSender } = require('./window-security');

const ENTRY = path.join(__dirname, '../renderer/workbench.html');

ipcMain.handle('seat:install', (event, ...args) => {
  assertTrustedSender(event, win, ENTRY); // throws if sender is untrusted
  // ...privileged operation...
  return { ok: true };
});

Harden a New Window

const { BrowserWindow } = require('electron');
const { protectWindow } = require('./window-security');

const win = new BrowserWindow({ webPreferences: { contextIsolation: true, sandbox: true } });
win.loadFile(path.join(__dirname, '../renderer/workbench.html'));
protectWindow(win); // block navigation, webview, popups

Validate a File URL

const { isTrustedDocument } = require('./window-security');
const entry = '/app/src/renderer/workbench.html';

isTrustedDocument('file:///app/src/renderer/workbench.html', entry); // true
isTrustedDocument('https://evil.com/workbench.html', entry);         // false
isTrustedDocument('file:///app/src/renderer/other.html', entry);     // false

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment