Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On windows, don't make slices from empty passwords. #171

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords = ["password", "credential", "keychain", "keyring", "cross-platform"]
license = "MIT OR Apache-2.0"
name = "keyring"
repository = "https://github.com/hwchen/keyring-rs.git"
version = "2.3.2"
version = "2.3.3"
rust-version = "1.68"
edition = "2021"
exclude = [".github/"]
Expand Down
2 changes: 1 addition & 1 deletion src/keyutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl KeyutilsCredential {

// Construct the credential with a URI-style description
let description = match target {
Some(value) if value.is_empty() => {
Some("") => {
return Err(ErrorCode::Invalid(
"target".to_string(),
"cannot be empty".to_string(),
Expand Down
6 changes: 6 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ fn extract_password(credential: &CREDENTIALW) -> Result<String> {
// get password blob
let blob_pointer: *const u8 = credential.CredentialBlob;
let blob_len: usize = credential.CredentialBlobSize as usize;
if blob_len == 0 {
return Ok(String::new());
}
let blob = unsafe { std::slice::from_raw_parts(blob_pointer, blob_len) };
// 3rd parties may write credential data with an odd number of bytes,
// so we make sure that we don't try to decode those as utf16
Expand Down Expand Up @@ -355,6 +358,9 @@ unsafe fn from_wstr(ws: *const u16) -> String {
}
// this code from https://stackoverflow.com/a/48587463/558006
let len = (0..).take_while(|&i| *ws.offset(i) != 0).count();
if len == 0 {
return String::new();
}
let slice = std::slice::from_raw_parts(ws, len);
String::from_utf16_lossy(slice)
}
Expand Down