Skip to content

Commit

Permalink
fix(secrets/linux): remove redundant free for secret item values
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Sep 26, 2023
1 parent 3993053 commit 182cf46
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 62 deletions.
54 changes: 19 additions & 35 deletions packages/secrets/src/keyring/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/secrets/src/keyring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ core-foundation = "0.9.3"
core-foundation-sys = "0.8.4"

[target.'cfg(any(target_os = "freebsd", target_os = "linux"))'.dependencies]
glib = "0.17.10"
glib-sys = "0.17.10"
gio = "0.17.10"
libsecret = "0.3.0"
libsecret-sys = "0.3.0"
glib = "0.18.2"
glib-sys = "0.18.1"
gio = "0.18.2"
libsecret = "0.4.0"
libsecret-sys = "0.4.0"

[build-dependencies]
napi-build = "2"

[profile.release]
lto = true
opt-level = "z" # Optimize for size.
strip = "symbols"
strip = "symbols"
22 changes: 11 additions & 11 deletions packages/secrets/src/keyring/__test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ test.serial(
}
);

test.serial("findCredentials works when only one credential is found", async (t) => {
await setPassword("TestKeyring2", "TestOneCred", "pass");

const creds = await findCredentials("TestKeyring2");
t.deepEqual(creds, [{
account: "TestOneCred",
password: "pass"
}]);
await deletePassword("TestKeyring2", "TestOneCred");
});

test.serial("findPassword for ASCII string", async (t) => {
const pw = await findPassword("TestKeyring/TestASCII");
t.is(pw, "ASCII string");
Expand All @@ -183,17 +194,6 @@ test.serial("findPassword for CJK symbols", async (t) => {
t.is(pw, "「こんにちは世界」");
});

test.serial("findCredentials works when only one credential is found", async (t) => {
await setPassword("TestKeyring2", "TestOneCred", "pass");

const creds = await findCredentials("TestKeyring2");
t.deepEqual(creds, [{
account: "TestOneCred",
password: "pass"
}]);
await deletePassword("TestKeyring2", "TestOneCred");
});

test("deletePassword deletes all test credentials", async (t) => {
console.log(
"\nThe deletePassword test is running. There is an intended delay of 5 seconds to wait for the keyring to update."
Expand Down
18 changes: 8 additions & 10 deletions packages/secrets/src/keyring/src/os/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use glib::translate::{FromGlibPtrContainer, ToGlibPtr};
use glib_sys::{g_hash_table_unref};
use libsecret::{
prelude::CollectionExtManual, traits::ItemExt, SearchFlags, Service, ServiceFlags,
password_free
};
use std::collections::HashMap;

Expand Down Expand Up @@ -181,32 +182,29 @@ pub fn find_credentials(
match collection.search_sync(
Some(&get_schema()),
HashMap::from([("service", service.as_str())]),
SearchFlags::ALL | SearchFlags::LOAD_SECRETS,
SearchFlags::ALL | SearchFlags::UNLOCK | SearchFlags::LOAD_SECRETS,
gio::Cancellable::NONE,
) {
Ok(vec) => {
let valid_creds: Vec<(String, String)> = vec
.iter()
.filter_map(|item| {
let attrs: HashMap<String, String> = unsafe {
let attrs =
libsecret_sys::secret_item_get_attributes(item.to_glib_none().0);
FromGlibPtrContainer::from_glib_full(attrs)
};
match item.secret() {
Some(secret) => {
let attrs: HashMap<String, String> = unsafe {
let attrs = libsecret_sys::secret_item_get_attributes(item.to_glib_none().0);
FromGlibPtrContainer::from_glib_full(attrs)
};
let bytes = secret.get();
let acc = attrs.get("account").unwrap().clone();
let pw = String::from_utf8(bytes).unwrap_or("".to_string());

let acc = attrs.get("account").unwrap().clone();
unsafe {
secret.unref();
g_hash_table_unref(attrs.to_glib_full());
}

Some((acc, pw))
}
None => {
g_hash_table_unref(attrs.to_glib_full());
None
},
}
Expand Down

0 comments on commit 182cf46

Please sign in to comment.