2. Paste without trusting the clipboard
Paste runs only after a deliberate user action. Do not read the clipboard during startup, on a timer or whenever a field receives focus. Apart from unnecessary access, that would replace the reference when the user had not asked for a change.
The successful callback receives ClipboardText from ctx.input. Only a matching reference reaches application state; empty text and other clipboard contents leave the current value alone. In particular, the error message does not echo rejected clipboard text, which might contain a password or another application's private data.
Replace src/state.rs with the version below. The screen adds a Paste action; native and Web entrypoints remain unchanged.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/state.rs","value":"src-state-rs"},{"index":1,"label":"src/app.rs","value":"src-app-rs"}]}
{"id":"fission-tabs-1","index":0,"label":"src/state.rs","value":"src-state-rs"}
use fission::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation { Copy, Paste }
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Notice {
#[default]
Ready,
Copied,
Pasted,
InvalidReference,
EmptyClipboard,
Unsupported,
Denied,
Failed,
}
impl Notice {
pub fn key(self) -> &'static str {
match self {
Self::Ready => "handoff.ready",
Self::Copied => "handoff.copied",
Self::Pasted => "handoff.pasted",
Self::InvalidReference => "handoff.invalid",
Self::EmptyClipboard => "handoff.empty",
Self::Unsupported => "handoff.unsupported",
Self::Denied => "handoff.denied",
Self::Failed => "handoff.failed",
}
}
}
#[derive(Debug, Default)]
pub struct ClipboardState {
pub reference: String,
pub pending: Option<Operation>,
pub notice: Notice,
}
impl GlobalState for ClipboardState {}
pub fn valid_reference(input: &str) -> bool {
let Some(number) = input.strip_prefix("WO-") else { return false; };
!number.is_empty()
&& number.len() <= 12
&& number.bytes().all(|byte| byte.is_ascii_digit())
}
pub fn copy_reference(state: &mut ClipboardState, ctx: &mut ReducerContext<ClipboardState>) {
if state.pending.is_some() { return; }
let reference = state.reference.trim().to_owned();
if !valid_reference(&reference) {
state.notice = Notice::InvalidReference;
return;
}
state.pending = Some(Operation::Copy);
state.notice = Notice::Ready;
let ok = ctx.effects.bind(ReferenceCopied, reduce_with!(reference_copied));
let err = ctx.effects.bind(ClipboardFailed, reduce_with!(clipboard_failed));
ctx.effects.clipboard()
.write_text(ClipboardWriteTextRequest { text: reference })
.on_ok(ok)
.on_err(err);
}
#[fission_reducer(ReferenceCopied)]
fn reference_copied(state: &mut ClipboardState, ctx: &mut ReducerContext<ClipboardState>) {
if state.pending != Some(Operation::Copy) { return; }
state.pending = None;
state.notice = if ctx.input.capability_ok(WRITE_CLIPBOARD_TEXT).is_some() {
Notice::Copied
} else {
Notice::Failed
};
}
#[fission_reducer(ClipboardFailed)]
fn clipboard_failed(state: &mut ClipboardState, ctx: &mut ReducerContext<ClipboardState>) {
let code = match state.pending.take() {
Some(Operation::Copy) => ctx.input.capability_error(WRITE_CLIPBOARD_TEXT)
.map(|error| error.code),
Some(Operation::Paste) => ctx.input.capability_error(READ_CLIPBOARD_TEXT)
.map(|error| error.code),
None => return,
};
state.notice = match code.as_deref() {
Some("unsupported") => Notice::Unsupported,
Some("permission_denied") | Some("not_allowed") => Notice::Denied,
_ => Notice::Failed,
};
}
pub fn paste_reference(state: &mut ClipboardState, ctx: &mut ReducerContext<ClipboardState>) {
if state.pending.is_some() { return; }
state.pending = Some(Operation::Paste);
state.notice = Notice::Ready;
let ok = ctx.effects.bind(ReferencePasted, reduce_with!(reference_pasted));
let err = ctx.effects.bind(ClipboardFailed, reduce_with!(clipboard_failed));
ctx.effects.clipboard().read_text().on_ok(ok).on_err(err);
}
#[fission_reducer(ReferencePasted)]
fn reference_pasted(state: &mut ClipboardState, ctx: &mut ReducerContext<ClipboardState>) {
if state.pending != Some(Operation::Paste) { return; }
state.pending = None;
match ctx.input.capability_ok(READ_CLIPBOARD_TEXT) {
Some(ClipboardText { text: Some(text) }) if valid_reference(text.trim()) => {
state.reference = text.trim().to_owned();
state.notice = Notice::Pasted;
}
Some(ClipboardText { text: None }) => state.notice = Notice::EmptyClipboard,
Some(ClipboardText { text: Some(text) }) if text.trim().is_empty() => {
state.notice = Notice::EmptyClipboard;
}
Some(_) => state.notice = Notice::InvalidReference,
None => state.notice = Notice::Failed,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_only_the_reference_format() {
assert!(valid_reference("WO-1048"));
for input in ["", "WO-", "WO-a", "WO-1\nprivate note", "WO-12", "WO-1234567890123"] {
assert!(!valid_reference(input), "accepted invalid reference format");
}
}
}
{"id":"fission-tabs-1","index":1,"label":"src/app.rs","value":"src-app-rs"}
use fission::prelude::*;
use crate::state::{self, ClipboardState};
fission_component! {
pub HandoffScreen<ClipboardState> {
state { private_note: String = String::new() }
}
view(screen, state, cx) {
let tokens = &cx.view.env().theme.tokens;
} =>
SafeArea(
child: Scroll(
child: Container(
child: Column(gap: tokens.spacing.m, children: [
Text(cx.tr("handoff.title")).size(tokens.typography.heading2_size),
Text(cx.tr("handoff.context")),
TextInput(
label: Some(cx.tr("handoff.reference").into()),
helper_text: Some(cx.tr("handoff.format").into()),
enabled: state.pending.is_none(),
bind: state.reference,
semantics_identifier: "handoff.reference".to_string(),
),
TextInput(
label: Some(cx.tr("handoff.note").into()),
helper_text: Some(cx.tr("handoff.note_help").into()),
multiline: true,
bind: screen.private_note,
semantics_identifier: "handoff.private-note".to_string(),
),
Button(
child: Text(cx.tr("handoff.copy")),
key: "copy-reference",
disabled: state.pending.is_some(),
on_press: reduce { state::copy_reference(state, cx); },
).semantics_identifier("handoff.copy"),
Button(
child: Text(cx.tr("handoff.paste")),
key: "paste-reference",
disabled: state.pending.is_some(),
on_press: reduce { state::paste_reference(state, cx); },
).semantics_identifier("handoff.paste"),
if let Some(operation) = state.pending {
Spinner(
motion: Some(SpinnerMotion::Default),
label: cx.tr(match operation {
state::Operation::Copy => "handoff.copying",
state::Operation::Paste => "handoff.pasting",
}),
)
} else {
Text(cx.tr(state.notice.key()))
},
]),
).padding_all(tokens.spacing.l),
),
)
}
{"id":"fission-tabs-1"}
Copy one reference, edit the field, then paste to restore the copied value. Next copy unrelated text in another application and paste again. The original reference and private note should remain unchanged, and the message should explain the rejected format without repeating the clipboard contents.
The copy acknowledgement says that the host accepted the write. It does not say that another application has read it, nor that the clipboard can keep it confidential.
Show the app previewHide the app preview
Show the app preview
Hide the app preview