Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui_windows: Handle null UserName and CredentialBlob from CredReadW (#64735)

Agus Zubiaga8720fd1d0939
diff --git a/crates/gpui_windows/src/platform.rs b/crates/gpui_windows/src/platform.rs
index eab3b5e55c7..348aaa3f23f 100644
--- a/crates/gpui_windows/src/platform.rs
+++ b/crates/gpui_windows/src/platform.rs
@@ -964,19 +964,13 @@ impl Platform for WindowsPlatform {
}
if credentials.is_null() {
- Ok(None)
- } else {
- let username: String = unsafe { (*credentials).UserName.to_string()? };
- let credential_blob = unsafe {
- std::slice::from_raw_parts(
- (*credentials).CredentialBlob,
- (*credentials).CredentialBlobSize as usize,
- )
- };
- let password = credential_blob.to_vec();
- unsafe { CredFree(credentials as *const _ as _) };
- Ok(Some((username, password)))
+ return Ok(None);
}
+
+ // SAFETY: `CredReadW` succeeded, so this points to a valid `CREDENTIALW` until `CredFree` below.
+ let result = unsafe { username_and_password(&*credentials) };
+ unsafe { CredFree(credentials as *const _ as _) };
+ result.map(Some)
})
}
@@ -1634,14 +1628,104 @@ unsafe extern "system" fn window_procedure(
result
}
+/// Copies the username and secret out of a credential returned by `CredReadW`.
+///
+/// Both `UserName` and `CredentialBlob` are optional in Credential Manager and
+/// come back as null pointers when absent, so they are treated as empty here.
+///
+/// # Safety
+///
+/// A non-null `UserName` must point to a NUL-terminated wide string and a
+/// non-null `CredentialBlob` must be readable for `CredentialBlobSize` bytes,
+/// as is the case for credentials returned by `CredReadW`.
+unsafe fn username_and_password(credential: &CREDENTIALW) -> Result<(String, Vec<u8>)> {
+ let username = if credential.UserName.is_null() {
+ String::new()
+ } else {
+ // SAFETY: guaranteed by the caller.
+ unsafe { credential.UserName.to_string()? }
+ };
+ let password = if credential.CredentialBlob.is_null() {
+ Vec::new()
+ } else {
+ // SAFETY: guaranteed by the caller.
+ unsafe {
+ std::slice::from_raw_parts(
+ credential.CredentialBlob,
+ credential.CredentialBlobSize as usize,
+ )
+ }
+ .to_vec()
+ };
+ Ok((username, password))
+}
+
#[cfg(test)]
mod tests {
use std::ffi::{OsStr, OsString};
use crate::{read_from_clipboard, write_to_clipboard};
use gpui::ClipboardItem;
+ use windows::Win32::Security::Credentials::{
+ CRED_PERSIST_SESSION, CRED_TYPE_GENERIC, CREDENTIALW, CredDeleteW, CredFree, CredReadW,
+ CredWriteW,
+ };
+ use windows::core::{PCWSTR, PWSTR};
- use super::encode_restart_arguments;
+ use super::{encode_restart_arguments, username_and_password};
+
+ #[test]
+ fn test_read_credential_with_username() {
+ assert_eq!(
+ round_trip_credential(Some("alice"), b"secret"),
+ ("alice".to_string(), b"secret".to_vec())
+ );
+ }
+
+ #[test]
+ fn test_read_credential_without_username() {
+ assert_eq!(
+ round_trip_credential(None, b"secret"),
+ (String::new(), b"secret".to_vec())
+ );
+ }
+
+ fn round_trip_credential(username: Option<&str>, secret: &[u8]) -> (String, Vec<u8>) {
+ let mut target_name: Vec<u16> = format!(
+ "zed-test-{}-{}",
+ std::process::id(),
+ username.unwrap_or_default()
+ )
+ .encode_utf16()
+ .chain(Some(0))
+ .collect();
+ let mut username: Vec<u16> = username
+ .map(|username| username.encode_utf16().chain(Some(0)).collect())
+ .unwrap_or_default();
+ let mut secret = secret.to_vec();
+ let credential = CREDENTIALW {
+ Type: CRED_TYPE_GENERIC,
+ TargetName: PWSTR::from_raw(target_name.as_mut_ptr()),
+ CredentialBlobSize: secret.len() as u32,
+ CredentialBlob: secret.as_mut_ptr(),
+ Persist: CRED_PERSIST_SESSION,
+ UserName: if username.is_empty() {
+ PWSTR::null()
+ } else {
+ PWSTR::from_raw(username.as_mut_ptr())
+ },
+ ..CREDENTIALW::default()
+ };
+ let target_name = PCWSTR::from_raw(target_name.as_ptr());
+ unsafe { CredWriteW(&credential, 0) }.unwrap();
+
+ let mut credentials: *mut CREDENTIALW = std::ptr::null_mut();
+ unsafe { CredReadW(target_name, CRED_TYPE_GENERIC, None, &mut credentials) }.unwrap();
+ let result = unsafe { username_and_password(&*credentials) };
+ unsafe { CredFree(credentials as *const _ as _) };
+ unsafe { CredDeleteW(target_name, CRED_TYPE_GENERIC, None) }.unwrap();
+ result.unwrap()
+ }
#[test]
fn test_encode_restart_arguments() {