Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ On device B (another machine, or another `CLIPSYNC_HOME`):
clipsync room join 482913
```

Both devices then show a 6-digit verification code. On device A:

```bash
clipsync room confirm
```

Check that the two codes are the same, then confirm on both devices (type the
code shown on the other device, or `yes`). If they differ, answer anything
else, or run `clipsync room reject` on A: pairing stops and nothing is stored.
A mismatch means something other than your device joined, for example a relay
in the middle or someone who guessed the code. With `--foreground`, device A
asks in the same terminal instead of `room confirm`.

For scripts, `clipsync room confirm <CODE>` compares non-interactively, and
`room join` reads its answer from stdin. `--insecure-skip-sas` on
`room create` / `room join` skips the comparison and removes that protection;
use it only for test setups on a relay you trust. Devices on a version without
verification codes still pair, but cannot show a code to compare.

`clipsync init` is optional. Identity is created on Homebrew install and on
first use.

Expand Down Expand Up @@ -166,8 +185,10 @@ clipsync status

```
clipsync init [--relay-url URL] # optional; also runs on install and first use
clipsync room create [--ttl 10m] [--no-auto-sync] [--foreground]
clipsync room join <CODE> [--no-auto-sync]
clipsync room create [--ttl 10m] [--no-auto-sync] [--foreground] [--insecure-skip-sas]
clipsync room join <CODE> [--no-auto-sync] [--insecure-skip-sas]
clipsync room confirm [CODE] # answer the verification code of a background create
clipsync room reject
clipsync room leave --yes
clipsync room list
clipsync push [--type auto|text|image|files] [--file <path>...]
Expand Down Expand Up @@ -217,6 +238,7 @@ clipsync room create

- SPAKE2 balanced PAKE; the 6-digit code is only a one-time password
- Mutual key confirmation bound to protocol version, session, device IDs, and SPAKE2 transcript
- 6-digit verification code compared on both devices before anything is stored; it catches a guessed code or a relay in the middle, except a relay that searches offline for matching codes (see docs/security.md)
- AES-256-GCM payloads, HKDF-SHA-256, X25519 device identity
- Noise `KK` handshake on reconnect
- Text latest-one in-memory queue, 60s TTL; images/files are online-only 256 KiB chunks
Expand Down
21 changes: 20 additions & 1 deletion crates/clipsync-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ pub enum Commands {

#[derive(Debug, Subcommand)]
pub enum RoomCmd {
/// Create a pairing room and print a 6-digit join code (waits in the background)
/// Create a pairing room and print a 6-digit join code (waits in the background;
/// confirm the verification code with `clipsync room confirm`)
Create {
/// How long the code stays valid (e.g. 2m)
#[arg(long)]
Expand All @@ -90,12 +91,30 @@ pub enum RoomCmd {
/// Internal worker used by background room create
#[arg(long, hide = true)]
background_worker: bool,
/// Pair without comparing the verification code. This removes the
/// protection against a malicious relay or a guessed pairing code;
/// use it only for scripted setups on a network and relay you trust.
#[arg(long)]
insecure_skip_sas: bool,
},
/// Join with a 6-digit code, then confirm the verification code both devices show
Join {
code: String,
#[arg(long)]
no_auto_sync: bool,
/// Pair without comparing the verification code. This removes the
/// protection against a malicious relay or a guessed pairing code;
/// use it only for scripted setups on a network and relay you trust.
#[arg(long)]
insecure_skip_sas: bool,
},
/// Confirm the verification code of a background `room create`
Confirm {
/// Code shown on the other device; prompts when omitted
code: Option<String>,
},
/// Reject a background `room create` whose verification code does not match
Reject,
Leave {
#[arg(long)]
yes: bool,
Expand Down
88 changes: 67 additions & 21 deletions crates/clipsync-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod cli;
mod output;
mod pairing_bg;
mod sas_prompt;

use std::io::{self, Write};

Expand All @@ -15,6 +16,7 @@ use clipsync_storage::LocalStore;

use crate::cli::{Cli, Commands, ConfigCmd, DaemonCmd, RoomCmd, SyncCmd};
use crate::output::{emit, emit_err};
use crate::sas_prompt::{answer_background, background_confirmer, terminal_confirmer, SasMode};

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -76,6 +78,7 @@ async fn run(cli: Cli) -> Result<(), CoreError> {
foreground,
background: _,
background_worker,
insecure_skip_sas,
}) => {
let app = App::open()?;
let _ = app.identity()?;
Expand All @@ -90,30 +93,46 @@ async fn run(cli: Cli) -> Result<(), CoreError> {
&app,
ttl_raw.as_deref(),
no_auto_sync,
insecure_skip_sas,
json,
)
.await;
}
let offer = app
.room_create(dur, no_auto_sync, json, |offer| {
if json {
println!(
"{}",
serde_json::json!({
"event": "pairing_code",
"pairing_code": offer.pairing_code,
"room_id": offer.room_id,
"expires_at": offer.expires_at,
})
);
} else {
println!("pairing code: {}", offer.pairing_code);
println!("room: {}", offer.room_id);
println!("expires: {}", offer.expires_at);
println!("waiting for the other device to join…");
}
let _ = io::stdout().flush();
let confirm = if background_worker {
background_confirmer(app.store.clone(), insecure_skip_sas)
} else {
terminal_confirmer(SasMode {
json,
non_interactive: cli.non_interactive,
skip: insecure_skip_sas,
})
};
let offer = app
.room_create(
dur,
no_auto_sync,
json,
|offer| {
if json {
println!(
"{}",
serde_json::json!({
"event": "pairing_code",
"pairing_code": offer.pairing_code,
"room_id": offer.room_id,
"expires_at": offer.expires_at,
})
);
} else {
println!("pairing code: {}", offer.pairing_code);
println!("room: {}", offer.room_id);
println!("expires: {}", offer.expires_at);
println!("waiting for the other device to join…");
}
let _ = io::stdout().flush();
},
confirm,
)
.await?;
emit(
json,
Expand All @@ -128,9 +147,18 @@ async fn run(cli: Cli) -> Result<(), CoreError> {
}
Ok(())
}
Commands::Room(RoomCmd::Join { code, no_auto_sync }) => {
Commands::Room(RoomCmd::Join {
code,
no_auto_sync,
insecure_skip_sas,
}) => {
let app = App::open()?;
let room = app.room_join(&code, no_auto_sync).await?;
let confirm = terminal_confirmer(SasMode {
json: cli.json,
non_interactive: cli.non_interactive,
skip: insecure_skip_sas,
});
let room = app.room_join(&code, no_auto_sync, confirm).await?;
emit(
cli.json,
serde_json::json!({"room_id": room.room_id, "role": room.role}),
Expand All @@ -140,6 +168,24 @@ async fn run(cli: Cli) -> Result<(), CoreError> {
}
Ok(())
}
Commands::Room(RoomCmd::Confirm { code }) => {
let app = App::open()?;
let mode = SasMode {
json: cli.json,
non_interactive: cli.non_interactive,
skip: false,
};
answer_background(&app, code.as_deref(), false, mode).await
}
Commands::Room(RoomCmd::Reject) => {
let app = App::open()?;
let mode = SasMode {
json: cli.json,
non_interactive: cli.non_interactive,
skip: false,
};
answer_background(&app, None, true, mode).await
}
Commands::Room(RoomCmd::Leave { yes }) => {
if !yes && !cli.yes && !cli.non_interactive {
eprintln!("pass --yes to leave the current room");
Expand Down
25 changes: 19 additions & 6 deletions crates/clipsync-cli/src/pairing_bg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ pub async fn spawn_background_create(
app: &App,
ttl: Option<&str>,
no_auto_sync: bool,
insecure_skip_sas: bool,
json: bool,
) -> Result<(), CoreError> {
if let Ok(Some(existing)) = app.store.load_pairing_wait() {
if existing.status == "waiting" && pid_alive(existing.pid) {
let active = existing.status == "waiting"
|| existing.status == crate::sas_prompt::STATUS_CONFIRM_SAS;
if active && !crate::sas_prompt::replaceable(&existing) {
let next = if existing.status == "waiting" {
format!("join with: clipsync room join {}", existing.pairing_code)
} else {
"a device joined; compare codes with: clipsync room confirm (or clipsync room reject)"
.into()
};
return Err(CoreError::Message(format!(
"pairing already waiting in background (pid {}); join with: clipsync room join {}",
existing.pid, existing.pairing_code
"pairing already waiting in background (pid {}); {next}",
existing.pid
)));
}
}
Expand All @@ -47,6 +56,9 @@ pub async fn spawn_background_create(
if no_auto_sync {
cmd.arg("--no-auto-sync");
}
if insecure_skip_sas {
cmd.arg("--insecure-skip-sas");
}
if let Ok(home) = std::env::var("CLIPSYNC_HOME") {
cmd.env("CLIPSYNC_HOME", home);
}
Expand Down Expand Up @@ -122,6 +134,7 @@ fn print_offer(wait: &PairingWait, pid: u32, json: bool) {
"join from another device: clipsync room join {}",
wait.pairing_code
);
println!("then compare verification codes here: clipsync room confirm");
}
}

Expand All @@ -135,15 +148,15 @@ fn extract_error_line(text: &str) -> Option<String> {
}

#[cfg(unix)]
fn pid_alive(pid: u32) -> bool {
pub(crate) fn pid_alive(pid: u32) -> bool {
extern "C" {
fn kill(pid: i32, sig: i32) -> i32;
}
unsafe { kill(pid as i32, 0) == 0 }
}

#[cfg(windows)]
fn pid_alive(pid: u32) -> bool {
pub(crate) fn pid_alive(pid: u32) -> bool {
extern "system" {
fn OpenProcess(access: u32, inherit: i32, pid: u32) -> *mut std::ffi::c_void;
fn CloseHandle(handle: *mut std::ffi::c_void) -> i32;
Expand All @@ -161,7 +174,7 @@ fn pid_alive(pid: u32) -> bool {
}

#[cfg(not(any(unix, windows)))]
fn pid_alive(_pid: u32) -> bool {
pub(crate) fn pid_alive(_pid: u32) -> bool {
false
}

Expand Down
Loading
Loading