tilo-secrets 0.5.2

App-scoped secret storage on Linux, macOS, Windows and the web: the Secret Service, the Keychain, Credential Manager, and WebCrypto-wrapped IndexedDB.
Documentation
//! The existing Linux `OSCrypt` password for one explicitly selected owner profile.
use crate::{Secret, SecretError};
use std::{fs::File, os::unix::fs::MetadataExt};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChromiumBrand {
    Chrome,
    Chromium,
    /// Public Slack 4.52.178 / Electron 44.4.5 `OSCrypt` contract; callers must
    /// authenticate its exact public application before selected capture.
    Slack452178,
}
impl ChromiumBrand {
    pub(crate) fn application(self) -> &'static str {
        match self {
            Self::Chrome => "chrome",
            Self::Chromium => "chromium",
            Self::Slack452178 => "Slack",
        }
    }
}

pub struct ChromiumKey {
    profile: File,
    identity: (u32, u64, u64),
    brand: ChromiumBrand,
}
impl ChromiumKey {
    /// Call only after explicit consent to include this browser's selected sessions.
    ///
    /// # Errors
    /// Refuses a non-directory, foreign owner, or elevated caller.
    pub fn new(profile: &File, brand: ChromiumBrand) -> Result<Self, SecretError> {
        let facts = profile.metadata().map_err(|_| SecretError::Denied)?;
        let owner = rustix::process::getuid().as_raw();
        if !facts.is_dir()
            || facts.uid() != owner
            || facts.mode() & 0o022 != 0
            || owner == 0
            || rustix::process::geteuid().as_raw() != owner
        {
            return Err(SecretError::Denied);
        }
        Ok(Self {
            profile: profile.try_clone().map_err(|_| SecretError::Denied)?,
            identity: (owner, facts.dev(), facts.ino()),
            brand,
        })
    }
    /// Existing password only: no item, collection, password, or fallback is created.
    /// The owner's Secret Service may display its own unlock prompt.
    pub fn read_blocking(&self) -> Result<Option<Secret>, SecretError> {
        let facts = self.profile.metadata().map_err(|_| SecretError::Denied)?;
        if !facts.is_dir()
            || (facts.uid(), facts.dev(), facts.ino()) != self.identity
            || facts.mode() & 0o022 != 0
            || rustix::process::getuid().as_raw() != self.identity.0
            || rustix::process::geteuid().as_raw() != self.identity.0
        {
            return Err(SecretError::Denied);
        }
        crate::linux::read_chromium(self.identity.0, self.brand.application())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn owner_scope_refuses_files_and_elevation_without_contacting_a_keyring() {
        tilo_test_env::require_capability!(
            NonRoot,
            tilo_test_env::non_root_identity(),
            "elevation is only refused to an ordinary account"
        );
        let directory = tempfile::tempdir().unwrap();
        let file = File::create(directory.path().join("not-profile")).unwrap();
        assert!(ChromiumKey::new(&file, ChromiumBrand::Chrome).is_err());
        assert!(
            ChromiumKey::new(
                &File::open(directory.path()).unwrap(),
                ChromiumBrand::Chromium
            )
            .is_ok()
        );
        assert_eq!(ChromiumBrand::Chrome.application(), "chrome");
        assert_eq!(ChromiumBrand::Chromium.application(), "chromium");
        assert_eq!(ChromiumBrand::Slack452178.application(), "Slack");
    }
}