Visual Studio Code, rebuilt in Rust
Rust
feat(windows): keep secrets in Credential Manager under their service
rokartur7eb22e38c78b
diff --git a/native/crates/exthost/src/main_thread_secret_state.rs b/native/crates/exthost/src/main_thread_secret_state.rsindex 071bd71ef06..e59223c5eba 100644--- a/native/crates/exthost/src/main_thread_secret_state.rs+++ b/native/crates/exthost/src/main_thread_secret_state.rs@@ -41,8 +41,9 @@ enum Secrets {Keychain(SecKeychain),#[cfg(target_os = "linux")]Keyring(oo7::Keyring),+ /// The credentials whose targets start with `<service>/`.#[cfg(target_os = "windows")]- CredentialManager,+ CredentialManager { service: String },/// Extension tests: application storage is in memory, and so are the secrets kept in it.Memory(BTreeMap<String, String>),}@@ -63,7 +64,7 @@ impl MainThreadSecretState {#[cfg(target_os = "windows")]pub fn keychain() -> Result<Self, KeychainError> {- Ok(MainThreadSecretState { secrets: Secrets::CredentialManager })+ Ok(MainThreadSecretState { secrets: Secrets::CredentialManager { service: SERVICE.to_owned() } })}pub fn in_memory() -> Self {@@ -135,7 +136,7 @@ impl MainThreadSecretState {Ok(Some(String::from_utf8_lossy(&block_on(item.secret())?).into_owned()))}#[cfg(target_os = "windows")]- Secrets::CredentialManager => windows::get(SERVICE, account),+ Secrets::CredentialManager { service } => windows::get(service, account),}}@@ -150,7 +151,7 @@ impl MainThreadSecretState {#[cfg(target_os = "linux")]Secrets::Keyring(keyring) => block_on(keyring.create_item(SERVICE, &[("service", SERVICE), ("account", account)], value, true)),#[cfg(target_os = "windows")]- Secrets::CredentialManager => windows::set(SERVICE, account, value),+ Secrets::CredentialManager { service } => windows::set(service, account, value),}}@@ -176,7 +177,7 @@ impl MainThreadSecretState {Ok(!items.is_empty())}#[cfg(target_os = "windows")]- Secrets::CredentialManager => windows::delete(SERVICE, account),+ Secrets::CredentialManager { service } => windows::delete(service, account),}}@@ -199,7 +200,7 @@ impl MainThreadSecretState {fn accounts(&self) -> Result<Vec<String>, KeychainError> {match &self.secrets {Secrets::Memory(secrets) => Ok(secrets.keys().cloned().collect()),- Secrets::CredentialManager => windows::accounts(SERVICE),+ Secrets::CredentialManager { service } => windows::accounts(service),}}@@ -282,6 +283,17 @@ mod tests {check_flow(&mut MainThreadSecretState { secrets: Secrets::Keyring(keyring) });}+ /// A service name of its own, so the user's secrets are never touched.+ #[test]+ #[cfg(target_os = "windows")]+ fn secrets_in_credential_manager() {+ let service = format!("VSRustium Test Secret Storage {}", std::process::id());+ let mut state = MainThreadSecretState { secrets: Secrets::CredentialManager { service } };+ check_flow(&mut state);+ call(&mut state, "$deletePassword", &["pub.ext", "other"]);+ call(&mut state, "$deletePassword", &["pub.another", "token"]);+ }+#[test]fn secrets_in_memory() {check_flow(&mut MainThreadSecretState::in_memory());diff --git a/native/crates/exthost/src/main_thread_secret_state/windows.rs b/native/crates/exthost/src/main_thread_secret_state/windows.rsindex 7c28d5352cb..4c85b4fd577 100644--- a/native/crates/exthost/src/main_thread_secret_state/windows.rs+++ b/native/crates/exthost/src/main_thread_secret_state/windows.rs@@ -1,11 +1,11 @@//! The secrets in Windows Credential Manager with keytar's layout: a generic credential whose target is//! `<service>/<account>`, user name the account and blob the UTF-8 secret.-use std::io::{Error, Result};+use std::io::{Error, ErrorKind, Result};use windows_sys::Win32::Foundation::{ERROR_NOT_FOUND, FILETIME};use windows_sys::Win32::Security::Credentials::{- CRED_PERSIST_ENTERPRISE, CRED_TYPE_GENERIC, CREDENTIALW, CredDeleteW, CredEnumerateW, CredFree, CredReadW, CredWriteW,+ CRED_MAX_CREDENTIAL_BLOB_SIZE, CRED_PERSIST_ENTERPRISE, CRED_TYPE_GENERIC, CREDENTIALW, CredDeleteW, CredEnumerateW, CredFree, CredReadW, CredWriteW,};pub fn get(service: &str, account: &str) -> Result<Option<String>> {@@ -19,7 +19,11 @@ pub fn get(service: &str, account: &str) -> Result<Option<String>> {Ok(Some(secret))}+/// Fails for a secret over 2560 bytes, Credential Manager's limit, as keytar does.pub fn set(service: &str, account: &str, value: &str) -> Result<()> {+ if value.len() > CRED_MAX_CREDENTIAL_BLOB_SIZE as usize {+ return Err(Error::new(ErrorKind::InvalidInput, format!("a secret of {} bytes is over Credential Manager's limit of {CRED_MAX_CREDENTIAL_BLOB_SIZE} bytes", value.len())));+ }let mut target = wide(&format!("{service}/{account}"));let mut user_name = wide(account);let credential = CREDENTIALW {@@ -51,9 +55,10 @@ pub fn delete(service: &str, account: &str) -> Result<bool> {}}-/// keytar's `findCredentials`: the user name of every credential whose target starts with `<service>`.+/// keytar's `findCredentials`: the user name of every credential whose target starts with `<service>/` (keytar's+/// filter `<service>*` also matches services that start with this one).pub fn accounts(service: &str) -> Result<Vec<String>> {- let filter = wide(&format!("{service}*"));+ let filter = wide(&format!("{service}/*"));let mut count = 0;let mut credentials = std::ptr::null_mut::<*mut CREDENTIALW>();if unsafe { CredEnumerateW(filter.as_ptr(), 0, &mut count, &mut credentials) } == 0 {diff --git a/port/ledger.md b/port/ledger.mdindex a6327857847..ed1552d1f1b 100644--- a/port/ledger.md+++ b/port/ledger.md@@ -76,7 +76,7 @@ Status: `open`, `in progress`, `waiting on Artur`, `later` (postponed), `done`,| M24 Windows | exthost/src/lib.rs:31 | in progress | [windows L] extension host connection over a named pipe (`\\.\pipe\vscode-ipc-<uuid>-sock`, ipc.net.ts `createRandomIPCHandle` on Windows) instead of a Unix socket; Bun's `net.connect` takes the pipe path. Done and cross-built, never run: exthost/src/pipe.rs (overlapped pipe instances as libuv's `uv_pipe_bind`, local clients only, first instance exclusive; `generateUuid` from the OS random source, no `/dev/urandom` on Windows). | `cargo gc test -p exthost` on the windows runner: the handshake test || M24 Windows | app/src/launch.rs:5 | in progress | [windows M] single instance and second-instance arguments over a named pipe (main.ts `mainIPCHandle` on Windows). Done and cross-built, never run: the pipe `\\.\pipe\<sha256(user data)[..8]>-<version>-main-sock` (`createStaticIPCHandle`, host test `the_windows_pipe_is_named_from_the_user_data_hash_as_create_static_ipc_handle`), exthost/src/pipe.rs as the transport, `windowsAllowSetForegroundWindow` with the pipe server's pid. | `cargo gc test -p app launch` on the host for the pipe name; `cargo gc test -p app launch` on the windows runner || M24 Windows | app/src/cli.rs:7 | in progress | [windows M] the command line on Windows: `bin\<applicationName>.cmd` from resources/win32/bin/code.cmd (written by port/tools/package-windows, its row). Done and cross-built, never run: stdin read and terminal width through the console API; a GUI-subsystem binary as Electron's that attaches to the starting console (electron_main_win.cc `RouteStdioToConsole` unless `ELECTRON_NO_ATTACH_CONSOLE`). | `cargo gc test -p app cli` on the windows runner |-| M24 Windows | exthost/src/main_thread_secret_state.rs:19 | open | [windows S] secrets in Credential Manager (keyring), with keytar's target name `<service>/<account>` so VS Code's stored secrets keep their keys. | `cargo gc test -p exthost secret` on the windows runner |+| M24 Windows | exthost/src/main_thread_secret_state.rs:19 | in progress | [windows S] secrets in Credential Manager (keyring), with keytar's target name `<service>/<account>` so VS Code's stored secrets keep their keys. Done and cross-built, never run: main_thread_secret_state/windows.rs (generic credentials, user name the account, blob the UTF-8 secret, `CRED_PERSIST_ENTERPRISE`, keys from `CredEnumerateW` on `<service>/*`); the runner test `secrets_in_credential_manager` uses a service of its own. Difference: a secret over 2560 bytes (`CRED_MAX_CREDENTIAL_BLOB_SIZE`) fails to store, as with keytar, where upstream's safeStorage has no limit. | `cargo gc test -p exthost secret` on the windows runner || M24 Windows | exthost/src/request.rs:9 | open | [windows M] extension and gallery HTTP with the system proxy (WinHTTP's IE proxy settings, Chromium's ProxyConfigServiceWin), `http.proxy` and the `<local>` bypass. | `cargo gc test -p exthost request` on the windows runner || M24 Windows | app/src/workbench/menubar.rs:173 | open | [windows L] menu bar in the custom title bar (menubarControl.ts `CustomMenubarControl`, `window.titleBarStyle: custom`, `window.menuBarVisibility`) and the window controls (minimize, maximize, close) titlebarPart.ts draws on Windows. The shared mechanism is M24's (decision "Menu bar and keybinding tables"); it unblocks the M23 Linux row app/src/workbench/menubar.rs:173. Windows shows now: no menu bar. | `cargo gc test -p app menubar` on the host; a ui-pair against the reference not possible (macOS reference) || M24 Windows | app/src/keybindings.rs:86 | open | [windows L] Windows keybindings: `default-win` table, the `win` key of extension and user rules, Windows labels (keybindingLabels.ts for `OperatingSystem.Windows`: `Ctrl+Shift+P`, `Windows`, `win`), and windowsKeyboardMapper.ts for layouts. The per-platform mechanism is M24's (decision "Menu bar and keybinding tables"), with a Linux table hook; it unblocks the M23 Linux row app/src/keybindings.rs:86. Windows shows now: the macOS table on GPUI's platform modifier (Ctrl). | `cargo gc test -p app keybindings` on the host with the Windows table |