Visual Studio Code, rebuilt in Rust
Extensions: scan, install, gallery, signatures
How an extension gets on disk and into a window: the folders, the scan, the install path from a gallery query to a running extension, what is verified, and the built-in set. The code is exthost/src/extension_management.rs, exthost/src/gallery.rs, exthost/src/sign.rs, exthost/src/zip.rs, exthost/src/scan.rs and their callers in app/src. The host process, the wire format and how the workbench reads contributions are in extension-host.md. Upstream is src/vs/platform/extensionManagement; the decisions are in port/notes.md, "Milestone 18: extensions, install, update, gallery".
What is where
| File | Job |
|---|---|
exthost/src/scan.rs |
One folder to one ScannedExtension: package.json with its %key% strings replaced. The built-in scan. |
exthost/src/extension_management.rs |
The user folder (UserExtensions), the disk jobs (Job), and the step that applies a job to the running host (WindowExtensions). |
exthost/src/extension_validator.rs |
extensionValidator.ts: engines.vscode and the manifest checks. |
exthost/src/zip.rs |
The VSIX reader, a port of base/node/zip.ts. |
exthost/src/gallery.rs |
extensionGalleryService.ts against Open VSX, and the download of extensionDownloader.ts. |
exthost/src/sign.rs |
The signature check of a downloaded VSIX. |
app/src/workbench/extension_management.rs |
The workbench commands: gallery lookup and download off the main thread, then jobs to the host thread; the watcher of extensions.json. |
app/src/workbench/trusted_publishers.rs |
The publisher trust prompt before a gallery install. |
app/src/cli_process.rs |
--install-extension and its siblings in the CLI process, without a window or a host. |
app/src/workbench/extensions_view.rs, app/src/extensions_model.rs |
The Extensions view and its model of one extension. |
On disk
- Built-in extensions:
<app root>/extensions, or the folder inVSCODE_GPUI_EXTENSIONS, plus the product.jsonbuiltInExtensionsunder<app root>/.build/builtInExtensions(app/src/main.rs::scan_system_extensions). Nothing writes there at run time. - User extensions:
<user data>/extensions, or--extensions-dir. One folder per extension, named<publisher>.<name>-<version>for a VSIX andexthost/src/gallery.rs::extension_keyfor a gallery install (it adds-<targetPlatform>). extensions.jsonis the list of what a profile has installed. The default profile's file is in the extensions folder; another profile has its own file and shares the folders. The default profile's file also holds the application scoped extensions of every profile (port/notes.md, "Application scope")..obsoletelists<id>-<version>keys of folders to delete at the next start. A folder that starts with.is an install in progress, one that ends in.vsctmpa delete in progress.exthost/src/extension_management.rs::clean_upremoves the.vsctmpfolders and the folder each.obsoletekey names, once per process start, before any window. Nothing removes a.folder that a killed install left.- Gallery downloads:
<user data>/CachedExtensionVSIXs/<key>.
The layout is upstream's (extensionsProfileScannerService.ts, extensionManagementService.ts ExtensionsScanner), down to the old location each entry keeps beside relativeLocation.
Invariant: a folder in the extensions folder is installed only when a profile's extensions.json names it. A scan never lists the folder itself. The one exception is a default profile with no extensions.json: initialize_profile writes the file from the folders it finds.
Invariant: a folder appears and disappears by rename. An install fills a .<suffix> folder and renames it into place (extract); a delete renames to .vsctmp first (delete_folder). A scan that runs at the same time never reads a half-written extension. A new write path keeps this.
The scan
exthost/src/scan.rs::scan_extension reads one package.json, applies the display language's messages, and adds the fields of an IExtensionDescription (identifier, extensionLocation, isBuiltin). Its result is plain data: the workbench and the host get the same ScannedExtension.
The set of a window is built in app/src/main.rs::open_window: the built-ins scanned once per process, then exthost/src/extension_management.rs::add_user_extensions with the profile's UserExtensions::scan. A user extension replaces the built-in with its id unless the built-in is newer, as dedupExtensions does.
- A user extension goes through
validate_extension_manifest, and one that fails is left out with a line on stderr. Built-ins are not validated. - The display language must be resolved before the first scan (
exthost/src/language_packs.rs::set_nls), because the scan bakes the translated strings into the description. - There is no scan cache. Upstream caches the scanned manifests (
extensionsScannerService.tsCachedExtensionsScanner); the port reads everypackage.jsonat each start, on the main thread, before the first frame. A change that makes the scan do more per extension is a startup change: measure it (performance.md).
Trap: --builtin-extensions-dir and --extensions-download-dir are parsed (app/src/argv.rs) and their values are read by nothing. The built-in folder is set with VSCODE_GPUI_EXTENSIONS, as the tools in port/tools do.
From a query to a running extension
- The Extensions view or a command asks
exthost/src/gallery.rs::Gallery. Every call blocks on HTTP, so every caller in a window runs it on the background executor; the CLI process calls it directly.installable_extensionpicks the version: the engine, the target platform, release or pre-release. app/src/workbench/extension_management.rs::install_from_galleryasks about an untrusted publisher (check_trusted_publishers), before any download.- Still off the main thread,
Gallery::dependencies_and_packslists what must come along, andGallery::downloadfetches and verifies each VSIX. The result is a list ofJob::InstallGallery, dependencies first. app/src/workbench/extension_management.rs::run_extension_jobssends each job withapp/src/host_sync.rs::run_extensions. It travels in the same queue as every other message to the host (extension-host.md, "Calls the port makes").- On the
exthost-sessionthread,exthost/src/extension_management.rs::WindowExtensions::rundoes the disk work, then upstream's_deltaExtensions: configuration defaults change first, thenexthost/src/session.rs::delta_extensionstells the host, which activates the extension if one of its events already fired. - The reply is the list of running extensions.
set_running_extensionsreads languages, menus, keybindings and themes from it again.
An install from a VSIX file enters at step 3 with Job::InstallVsix; uninstall, and the settings import's Job::InstallFolder, at step 4. An enablement change is Job::Delta: nothing on disk.
Invariant: the disk job and the host delta run on the session thread, one job at a time, because that thread knows which extensions started to activate. The network part runs on the background executor. Besides clean_up at startup and initialize_profile in a scan, the main thread's one write to the extensions folder is Apply Extension to all Profiles (upstream's capitalization) (toggle_application_scope, called from app/src/workbench/extensions_view.rs), which changes extensions.json and starts nothing.
Invariant: an extension whose activation started is neither removed nor replaced until the host restarts (exthost/src/session.rs::applicable_delta). The files change on disk; running keeps the old one. The Extensions view shows Restart Extensions for this case.
The CLI is a second writer. app/src/cli_process.rs calls UserExtensions directly, in another process. A window sees it through the watcher (start_extensions_watcher): 100 ms after the last change of the profile's extensions.json it scans again and sends at most one Job::Delta (apply_profile_extensions). A read of a half-written file makes no delta; the next change brings the rest.
What is verified, and when
A VSIX and a gallery response are untrusted input. A VSIX is a zip an attacker can shape; a gallery response decides file names, URLs and the public key. The checks, in the order an install meets them:
| When | Check | Where |
|---|---|---|
| Download | The bytes are a zip with extension/package.json, two attempts. |
exthost/src/gallery.rs::download_vsix |
| Download | The signature, unless extensions.verifySignature is false. Anything but Success deletes the file and refuses the install; NotSigned passes only for a private extension. |
exthost/src/gallery.rs::check_verification |
| Install | The manifest's id and version are the ones the gallery named. | exthost/src/extension_management.rs::install |
| Install | engines.vscode accepts this version. |
the same function |
| Install | The folder name built from the manifest has no /, \, .. or leading .. |
the same function |
| Extract | Each entry stays inside the target; its size and CRC match. Encrypted entries and unknown compression methods are refused. | exthost/src/zip.rs::open, exthost/src/zip.rs::extract, exthost/src/zip.rs::read |
| Scan | validate_extension_manifest, at every start. |
exthost/src/extension_management.rs::scan_valid |
The signature is not upstream's scheme. Upstream checks a PKCS#7 signature chained to Microsoft's certificates with @vscode/vsce-sign. Open VSX signs with Ed25519, so exthost/src/sign.rs::verify checks that .signature.sig signs the VSIX bytes with the version's public key, then that .signature.manifest names the size and SHA-256 of the package and of every entry, and answers upstream's ExtensionSignatureVerificationCode names.
- The public key's URL comes from the same gallery response as the VSIX's. The signature proves the file is the one Open VSX signed, and no more: the root of trust is the TLS connection to the gallery. There is no pinned key and no certificate chain.
- A VSIX from disk has no signature. It gets the install and extract checks only, as upstream.
- Install Anyway, the prompt of Install on a gallery extension that has no signature (
install_unsigned), runs the same path with verification off (install_from_gallery_verifying). - A file already in
CachedExtensionVSIXsunder the right key is used instead of a download. With verification on it is verified again; the zip check ofdownload_vsixdoes not run for it. The port never empties that folder.
Invariant: a string from a VSIX or from the gallery is checked before it names a folder or file that an install writes (install, vsix_file_name, extract). The download cache is the exception: exthost/src/gallery.rs::download_and_verify joins extension_key to the folder with no check. New code that joins such a string to a folder makes the same check.
Invariant: zip.rs takes every length and offset from the archive. Each offset and stored length is checked against the archive's size before it is used, and a forged entry count reserves no more than the archive could hold (Archive::open). The uncompressed size is compared after read inflates the entry, and nothing caps the inflated bytes. The reader has no unsafe code and answers an error, never a panic, for any input.
Built-in extensions
The built-ins are upstream's extensions/ folder, run as they are. exthost/src/scan.rs::scan_builtin_extensions takes every child folder with a package.json, in name order, except two lists: REMOVED_BUILTINS (sign-in, AI and tunnels, which the port does not ship) and TEST_BUILTINS (upstream's product leaves them out too). scan_dev_system_extensions adds the extensions product.json downloads (js-debug and its companions), which port/tools/fetch-builtin-extensions puts in place.
- A built-in cannot be uninstalled, and the workbench refuses a gallery install of its id (
install_from_gallery_verifying). A VSIX or a CLI install of the same id becomes a user extension that shadows it, and uninstalling that copy puts the built-in back (WindowExtensions::run,Job::Uninstall). - To drop a built-in from the product, add its folder name to
REMOVED_BUILTINS.port/tools/shipped-files.tsreads the two lists fromscan.rs, so the package follows. - Which extensions of the scanned set run is a separate question: enablement, workspace trust and proposed API (extension-host.md, "Extensions as data").
Check a change
port/tools/cargo-test -p exthost extension_management: the folder layout, profiles, application scope,.obsolete.port/tools/cargo-test -p exthost gallery: version choice, target platforms, the query,verification_failures_refuse_the_install.port/tools/cargo-test -p exthost signandport/tools/cargo-test -p exthost zip: signatures;refuses_entries_outside_the_target,detects_corruption.port/tools/cargo-test -p exthost extension_validator: upstream's table ofextensionValidator.test.ts.port/tools/cargo-test -p app extension_management: the workbench path against a local gallery server, the watcher (extensions_installed_by_another_source_start_and_stop).port/tools/cargo-test -p app cli_process: the CLI commands.port/tools/extensions-check(on the Studio, afterport/tools/build-exthost): packs the fixture VSIXs and installs and uninstalls them in a running app;PORT_GALLERY=1adds an install from Open VSX.exthost/src/bin/exthost-probe.rswith--install-vsix <file> --uninstall <id>: the same against a host, without a window.