Text of code.visualstudio.com › blogs › 2022 › 11 › 28 › vscode-sandbox as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Migrating VS Code to Process Sandboxing

Join VS Code at Microsoft Ignite or catch the sessions online. A year of VS Code, wrapped. Explore the highlights. On this page Enabling the sandbox in Electron renderer processes is a critical requirement for secure and reliable Electron applications such as Visual Studio Code. The sandbox reduces the harm that malicious code can cause by limiting access to most system resources. In this blog post, we provide a detailed overview into how we managed to enable process sandboxing in VS Code, a journey that we started in early 2020 and plan to finish at the beginning of 2023.

To help understand the challenge of process sandboxing, this blog post also describes details of the VS Code process model and how it evolved during this journey. This was a team effort as fundamental architectural changes as well as code modifications were required in almost all VS Code components. The VS Code process architecture was overhauled and in the process significantly strengthened. We highlight the major milestones along the way, which we hope provides valuable insights for others to learn from.

For the last few months, process sandbox mode has been running successfully in VS Code Insiders , giving us feedback about the impact of this change. Do not hesitate to reach out to us if you find an issue , have a suggestion for how to improve the experience, or have general questions. If you are not familiar with VS Code or Electron or sandboxing, you may want to first review the Terminology section at the end of the blog post.

There you will find explanations of the terms used and links to background material. Process sandboxing in a nutshell For a long time, Electron has allowed direct use of Node.js APIs in HTML and JavaScript. The code snippet below provides a simple example of a web page that not only prints "Hello World" to the user but also writes to a file on the local disk: The Electron process that is responsible for presenting the web page to the user is called the renderer process.

Enabling sandbox mode for the renderer process reduces its capabilities for improved security and to align more with the web model: while HTML and JavaScript are still allowed, usage of Node.js is not. Components in the renderer process that require access to system resources will have to delegate to another process that is not sandboxed. The code below no longer depends on Node.js but uses a vscode global variable that provides the functionality to update settings.

The implementation of the method involves sending a message to another process that has access to Node.js. As such, it is also no longer executing synchronously but asynchronously: How we came to have the vscode global in the renderer process and how it is implemented is detailed in the Timeline sections below. Blocking Node.js from renderer processes is an encouraged Electron security recommendation . We have had security issues in the past where attackers were able to execute arbitrary Node.js code from the renderer process.

A sandboxed renderer process greatly reduces the risk of these attacks. How did we get there? A change as large as removing all our Node.js dependencies from the renderer process comes at the risk of regressions and bugs. Code that was previously running in one process will have to be split and run across multiple processes. Node modules that are native and thus cannot be web-packaged have to move out as well. Certain global objects such as Node.js Buffer will have to be replaced by browser compatible variants such as Uint8Array .

The diagram below shows our process architecture before the sandbox effort started. As you can see, most processes are Node.js child processes (in green) forked from the renderer process. Most of the (inter-process communication) IPC is implemented via Node.js sockets and the renderer process is a major client of Node.js APIs – for example to read and write files. We quickly decided that we wanted to work on process sandboxing without having to ship a separate VS Code application that is sandboxed.

We wanted to incrementally make the VS Code renderer process sandbox-ready and then flip the switch at the end. Over the past years, we have shipped monthly stable releases of VS Code with changes that contribute to the sandbox goal without fully enabling it. Imagine flying an airplane that is being fundamentally rebuild while it is in the air. And in our case, users have mostly been unaware of the changes to VS Code.

Our technology timeline: The next sections go into detail about how sandboxing came together over the past years. The main task was to remove all Node.js dependencies from the renderer process but along that way more challenges came up, such as figuring out an efficient sandbox-ready IPC solution with the help of MessagePort or finding new hosts for the various Node.js child processes that we could fork from the renderer process.

For the most part, the order of topics follows the actual timeline. To keep each section brief, we link to other documents and tutorials explaining a certain technical aspect in greater detail. And even though we planned for this work in early 2020, it is unfair to leave out some of the previous work that helped with this task. Let's take a closer look… Standing on the shoulder of giants When we started to consider sandboxing in early 2020, we had already shipped a version of VS Code that was able to run in web browsers.

You can run vscode.dev in your browser and see Visual Studio Code for the Web in action. While creating a web version of VS Code, we had learned how to remove Node.js dependencies from the workbench – the main VS Code user interface window. Removing dependencies to Node.js meant finding alternatives. For example, our dependency on the Node.js Buffer type was replaced with a VSBuffer equivalent that would fall back to Uint8Array in browser environments.

We were also able to package some Node.js modules ( oniguruma , iconv-lite ) to run in web environments. But even before VS Code for the Web became a reality, we had enabled support for remote development , which allows source code to be edited on a remote host, such as through an SSH connection (and later even powered GitHub Codespaces ). For remote development, we had to implement a solution where the UI facing pieces of VS Code run locally, while the actual file operations run on a remote machine.

This model applies to a sandboxed workbench as well, where privileged operations must run in a different process. In both cases, the renderer process communicates via IPC to a privileged host to perform the operations. Enabling a communication channel from the renderer When a renderer process cannot use Node.js, work must be delegated to another process where Node.js is available. One solution in the web context could be to rely on HTTP methods, where a server accepts the requests.

However, this did not feel like the best solution for desktop applications, where running a local server on a port could be blocked by a firewall for security reasons. Electron provides the ability to inject preload scripts into the renderer process that execute before the main script executes. These scripts have access to Electron's own IPC mechanism . Preload scripts can enrich the API available to the main script of the renderer through the context bridge API.

While the preload script can use Electron's IPC directly, the main script cannot. As such, we expose certain methods via context bridge to the main script. In the example that we used in the beginning, here is how a method for updating settings could be exposed from a preload script into the main script: Preload scripts are our fundamental building block for splitting privileged code from unprivileged. For example, writing to a file on disk means an IPC message with the new contents will travel from the main script to the preload script and from there into the main process that has access to Node.js.

Fast inter-process communication via me

The rest of this page is on the live site.