Skip to content

Commit

Permalink
Add exclude_from_history on linux by setting x-kde-passwordManagerHint
Browse files Browse the repository at this point in the history
Addresses 1Password#129
  • Loading branch information
MrSmoer committed Jun 9, 2024
1 parent 151e679 commit 3c9ae19
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
20 changes: 17 additions & 3 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,20 @@ pub(crate) struct Set<'clipboard> {
clipboard: &'clipboard mut Clipboard,
wait: WaitConfig,
selection: LinuxClipboardKind,
exclude_from_history: bool,
}

impl<'clipboard> Set<'clipboard> {
pub(crate) fn new(clipboard: &'clipboard mut Clipboard) -> Self {
Self { clipboard, wait: WaitConfig::default(), selection: LinuxClipboardKind::Clipboard }
Self { clipboard, wait: WaitConfig::default(), selection: LinuxClipboardKind::Clipboard, exclude_from_history: false, }
}

pub(crate) fn text(self, text: Cow<'_, str>) -> Result<(), Error> {
match self.clipboard {
Clipboard::X11(clipboard) => clipboard.set_text(text, self.selection, self.wait),
Clipboard::X11(clipboard) => clipboard.set_text(text, self.selection, self.wait, self.exclude_from_history),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_text(text, self.selection, self.wait),
Clipboard::WlDataControl(clipboard) => clipboard.set_text(text, self.selection, self.wait, self.exclude_from_history),
}
}

Expand Down Expand Up @@ -254,6 +255,14 @@ pub trait SetExtLinux: private::Sealed {
/// # }
/// ```
fn clipboard(self, selection: LinuxClipboardKind) -> Self;


/// Excludes the data which will be set on the clipboard from being added to
/// the cliobard managers' histories by adding the MIME-Type x-kde-passwordMangagerHint
/// with the content 'secret' to the clipboard.
///
/// This is seems to be the convention for this on Linux
fn exclude_from_history(self) -> Self;
}

impl SetExtLinux for crate::Set<'_> {
Expand All @@ -271,6 +280,11 @@ impl SetExtLinux for crate::Set<'_> {
self.platform.wait = WaitConfig::Until(deadline);
self
}

fn exclude_from_history(mut self) -> Self {
self.platform.exclude_from_history = true;
self
}
}

pub(crate) struct Clear<'clipboard> {
Expand Down
24 changes: 20 additions & 4 deletions src/platform/linux/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,31 @@ impl Clipboard {
text: Cow<'_, str>,
selection: LinuxClipboardKind,
wait: WaitConfig,
exclude_from_history: bool,
) -> Result<(), Error> {
let mut opts = Options::new();
opts.foreground(matches!(wait, WaitConfig::Forever));
opts.clipboard(selection.try_into()?);
let source = Source::Bytes(text.into_owned().into_bytes().into_boxed_slice());
opts.copy(source, MimeType::Text).map_err(|e| match e {
CopyError::PrimarySelectionUnsupported => Error::ClipboardNotSupported,
other => into_unknown(other),
})?;
if exclude_from_history {

let password_manager_hint = Source::Bytes(b"secret".to_vec().into());
let password_manager_hint_mime = MimeType::Specific(String::from("x-kde-passwordManagerHint"));

opts.copy_multi(vec![
MimeSource { source: source, mime_type: MimeType::Text },
MimeSource { source: password_manager_hint, mime_type: password_manager_hint_mime },
]).map_err(|e| match e {
CopyError::PrimarySelectionUnsupported => Error::ClipboardNotSupported,
other => into_unknown(other),
})?;
} else {

opts.copy(source, MimeType::Text).map_err(|e| match e {
CopyError::PrimarySelectionUnsupported => Error::ClipboardNotSupported,
other => into_unknown(other),
})?;
}
Ok(())
}

Expand Down
14 changes: 12 additions & 2 deletions src/platform/linux/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ x11rb::atom_manager! {
HTML: b"text/html",

PNG_MIME: b"image/png",
X_KDE_PASSWORDMANAGERHINT: b"x-kde-passwordManagerHint",

// This is just some random name for the property on our window, into which
// the clipboard owner writes the data we requested.
Expand Down Expand Up @@ -878,11 +879,20 @@ impl Clipboard {
message: Cow<'_, str>,
selection: LinuxClipboardKind,
wait: WaitConfig,
exclude_from_history: bool,
) -> Result<()> {
let data = vec![ClipboardData {
let mut data = vec![];
data.push(ClipboardData {
bytes: message.into_owned().into_bytes(),
format: self.inner.atoms.UTF8_STRING,
}];
});
if exclude_from_history {
data.push(ClipboardData {
bytes: b"secret".to_vec(),
format: self.inner.atoms.X_KDE_PASSWORDMANAGERHINT,
});

}
self.inner.write(data, selection, wait)
}

Expand Down

0 comments on commit 3c9ae19

Please sign in to comment.