Add /wipe, /wipe-all, and threat model docs.
/wipe overwrites the message log and VACUUMs; identity and friends stay. /wipe-all deletes the data dir after confirm so the next open is a new person. Neither runs without a typed confirm screen.
This commit is contained in:
parent
07aa97711c
commit
5412e020bb
7 changed files with 415 additions and 18 deletions
13
src/main.rs
13
src/main.rs
|
|
@ -1,3 +1,5 @@
|
|||
use onionwire::tui::AppExit;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
if let Err(e) = boot().await {
|
||||
|
|
@ -9,9 +11,14 @@ async fn main() {
|
|||
async fn boot() -> Result<(), String> {
|
||||
let home = onionwire::Store::home_dir().map_err(|e| e.to_string())?;
|
||||
eprintln!("onionwire: bootstrapping Arti…");
|
||||
let node = onionwire::node::Node::start(home).await?;
|
||||
let node = onionwire::node::Node::start(home.clone()).await?;
|
||||
let handle = tokio::runtime::Handle::current();
|
||||
tokio::task::spawn_blocking(move || onionwire::tui::run(node, handle))
|
||||
let exit = tokio::task::spawn_blocking(move || onionwire::tui::run(node, handle))
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.map_err(|e| e.to_string())??;
|
||||
if exit == AppExit::WipeAll {
|
||||
onionwire::Store::wipe_all(&home).map_err(|e| e.to_string())?;
|
||||
eprintln!("onionwire: all local data wiped");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,6 +141,14 @@ impl Node {
|
|||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn wipe_messages(&self) -> Result<(), String> {
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.wipe_messages()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub async fn connect_onion(&self, onion: &str) -> Result<(), String> {
|
||||
tokio::time::timeout(
|
||||
Duration::from_secs(20),
|
||||
|
|
|
|||
19
src/store.rs
19
src/store.rs
|
|
@ -364,6 +364,25 @@ impl Store {
|
|||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Overwrite message bodies, delete rows, VACUUM. Identity + friends stay.
|
||||
pub fn wipe_messages(&self) -> Result<()> {
|
||||
self.conn.execute(
|
||||
"UPDATE messages SET plaintext = zeroblob(length(plaintext))",
|
||||
[],
|
||||
)?;
|
||||
self.conn.execute("DELETE FROM messages", [])?;
|
||||
self.conn.execute_batch("VACUUM")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete the data dir. Next `open_at` is a new identity. Caller must drop Store first.
|
||||
pub fn wipe_all(home: &Path) -> Result<()> {
|
||||
if home.exists() {
|
||||
fs::remove_dir_all(home)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn friend_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Friend> {
|
||||
|
|
|
|||
186
src/tui.rs
186
src/tui.rs
|
|
@ -23,6 +23,97 @@ Offline friends CANNOT find you until they rescan your QR.\n\
|
|||
Type ROTATE to confirm Esc to cancel"
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WipeKind {
|
||||
Messages,
|
||||
All,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WipeDecision {
|
||||
Pending,
|
||||
Confirm,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AppExit {
|
||||
Quit,
|
||||
WipeAll,
|
||||
}
|
||||
|
||||
pub fn parse_slash(raw: &str) -> Option<WipeKind> {
|
||||
match raw.trim() {
|
||||
"/wipe" => Some(WipeKind::Messages),
|
||||
"/wipe-all" => Some(WipeKind::All),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wipe_screen_text(kind: WipeKind) -> &'static str {
|
||||
match kind {
|
||||
WipeKind::Messages => {
|
||||
"Wipe message log?\n\
|
||||
Identity key and friends stay.\n\
|
||||
Type WIPE to confirm Esc to cancel"
|
||||
}
|
||||
WipeKind::All => {
|
||||
"Wipe ALL local data?\n\
|
||||
This deletes your identity key. You become a new person.\n\
|
||||
Type WIPEALL to confirm Esc to cancel"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WipePrompt {
|
||||
expected: &'static str,
|
||||
buf: String,
|
||||
}
|
||||
|
||||
impl WipePrompt {
|
||||
pub fn messages() -> Self {
|
||||
Self {
|
||||
expected: "WIPE",
|
||||
buf: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn all() -> Self {
|
||||
Self {
|
||||
expected: "WIPEALL",
|
||||
buf: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_esc(&self) -> WipeDecision {
|
||||
WipeDecision::Cancel
|
||||
}
|
||||
|
||||
pub fn on_char(&mut self, c: char) -> WipeDecision {
|
||||
if c == '\n' {
|
||||
return WipeDecision::Pending;
|
||||
}
|
||||
if c == '\u{8}' {
|
||||
self.buf.pop();
|
||||
return WipeDecision::Pending;
|
||||
}
|
||||
if !c.is_ascii_alphabetic() {
|
||||
return WipeDecision::Pending;
|
||||
}
|
||||
self.buf.push(c);
|
||||
if self.buf == self.expected {
|
||||
WipeDecision::Confirm
|
||||
} else {
|
||||
WipeDecision::Pending
|
||||
}
|
||||
}
|
||||
|
||||
pub fn typed(&self) -> &str {
|
||||
&self.buf
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum RotateDecision {
|
||||
Pending,
|
||||
|
|
@ -68,7 +159,7 @@ impl RotatePrompt {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<(), String> {
|
||||
pub fn run(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<AppExit, String> {
|
||||
let mut app = App::new(node, rt)?;
|
||||
let mut terminal = ratatui::init();
|
||||
let result = app.run(&mut terminal);
|
||||
|
|
@ -86,6 +177,7 @@ struct App {
|
|||
me_onion: String,
|
||||
alert: Option<String>,
|
||||
status_note: Option<String>,
|
||||
composer: String,
|
||||
}
|
||||
|
||||
enum Screen {
|
||||
|
|
@ -94,6 +186,7 @@ enum Screen {
|
|||
Paste { buf: String, err: Option<String> },
|
||||
Approve { payload: QrPayload },
|
||||
Rotate { prompt: RotatePrompt },
|
||||
Wipe { kind: WipeKind, prompt: WipePrompt },
|
||||
}
|
||||
|
||||
impl App {
|
||||
|
|
@ -109,10 +202,11 @@ impl App {
|
|||
selected: 0,
|
||||
alert: None,
|
||||
status_note: None,
|
||||
composer: String::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<(), String> {
|
||||
fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<AppExit, String> {
|
||||
loop {
|
||||
terminal.draw(|f| self.draw(f)).map_err(|e| e.to_string())?;
|
||||
let Event::Key(key) = event::read().map_err(io_err)? else {
|
||||
|
|
@ -122,13 +216,16 @@ impl App {
|
|||
continue;
|
||||
}
|
||||
if key.code == KeyCode::Char('q') && key.modifiers.contains(KeyModifiers::CONTROL) {
|
||||
return Ok(());
|
||||
return Ok(AppExit::Quit);
|
||||
}
|
||||
if let Some(exit) = self.handle_key(key)? {
|
||||
return Ok(exit);
|
||||
}
|
||||
self.handle_key(key)?;
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_key(&mut self, key: KeyEvent) -> Result<(), String> {
|
||||
fn handle_key(&mut self, key: KeyEvent) -> Result<Option<AppExit>, String> {
|
||||
let mut wipe_confirm = None;
|
||||
match &mut self.screen {
|
||||
Screen::Main => match key.code {
|
||||
KeyCode::Up => {
|
||||
|
|
@ -151,6 +248,23 @@ impl App {
|
|||
prompt: RotatePrompt::new(),
|
||||
};
|
||||
}
|
||||
KeyCode::Esc => self.composer.clear(),
|
||||
KeyCode::Backspace => {
|
||||
self.composer.pop();
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(kind) = parse_slash(&self.composer) {
|
||||
self.composer.clear();
|
||||
let prompt = match kind {
|
||||
WipeKind::Messages => WipePrompt::messages(),
|
||||
WipeKind::All => WipePrompt::all(),
|
||||
};
|
||||
self.screen = Screen::Wipe { kind, prompt };
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
self.composer.push(c);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Screen::Share { .. } => {
|
||||
|
|
@ -194,8 +308,38 @@ impl App {
|
|||
}
|
||||
_ => {}
|
||||
},
|
||||
Screen::Wipe { kind, prompt } => {
|
||||
let kind = *kind;
|
||||
match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
if prompt.on_char(c) == WipeDecision::Confirm {
|
||||
wipe_confirm = Some(kind);
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
prompt.on_char('\u{8}');
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(kind) = wipe_confirm {
|
||||
return self.confirm_wipe(kind);
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn confirm_wipe(&mut self, kind: WipeKind) -> Result<Option<AppExit>, String> {
|
||||
match kind {
|
||||
WipeKind::Messages => {
|
||||
self.node.wipe_messages()?;
|
||||
self.status_note = Some("wiped messages".into());
|
||||
self.screen = Screen::Main;
|
||||
Ok(None)
|
||||
}
|
||||
WipeKind::All => Ok(Some(AppExit::WipeAll)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn confirm_rotate(&mut self) -> Result<(), String> {
|
||||
|
|
@ -268,12 +412,18 @@ impl App {
|
|||
Screen::Paste { buf, err } => draw_paste(f, buf, err.as_deref()),
|
||||
Screen::Approve { payload } => draw_approve(f, payload),
|
||||
Screen::Rotate { prompt } => draw_rotate(f, prompt.typed()),
|
||||
Screen::Wipe { kind, prompt } => draw_wipe(f, *kind, prompt.typed()),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_main(&self, f: &mut Frame) {
|
||||
let area = f.area();
|
||||
let cols = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).split(area);
|
||||
let cols = Layout::vertical([
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
let body = if self.alert.is_some() {
|
||||
Layout::vertical([Constraint::Length(1), Constraint::Min(1)]).split(cols[0])
|
||||
} else {
|
||||
|
|
@ -329,7 +479,13 @@ impl App {
|
|||
truncate_fp(&self.me_fp),
|
||||
onion
|
||||
);
|
||||
f.render_widget(Paragraph::new(status), cols[1]);
|
||||
let cmd = if self.composer.is_empty() {
|
||||
"/wipe /wipe-all".to_string()
|
||||
} else {
|
||||
self.composer.clone()
|
||||
};
|
||||
f.render_widget(Paragraph::new(cmd), cols[1]);
|
||||
f.render_widget(Paragraph::new(status), cols[2]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -377,6 +533,20 @@ fn draw_rotate(f: &mut Frame, typed: &str) {
|
|||
);
|
||||
}
|
||||
|
||||
fn draw_wipe(f: &mut Frame, kind: WipeKind, typed: &str) {
|
||||
let title = match kind {
|
||||
WipeKind::Messages => "/wipe",
|
||||
WipeKind::All => "/wipe-all",
|
||||
};
|
||||
let body = format!("{}\n\n{typed}", wipe_screen_text(kind));
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(Block::default().borders(Borders::ALL).title(title)),
|
||||
f.area(),
|
||||
);
|
||||
}
|
||||
|
||||
fn truncate_fp(fp: &str) -> String {
|
||||
let s: String = fp.chars().take(8).collect();
|
||||
format!("{s}…")
|
||||
|
|
|
|||
Loading…
Reference in a new issue