main: read the pairing URI from stdin with --pair - (I1)

This commit is contained in:
raucao committed 2026-11-30 10:31:00 +04:00
1 parent b5a88b6c4c
commit 5e071a8d9e
2 files changed
+109 -5

No files matched your search

+2 -1
View File
@@ -699,7 +699,8 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
`get_relays`), register the app, then send the `connect` **response** whose
`result` is the client's secret, p-tagged to the client and signed by the
device key. Running `--pair` is the consent; permissions are never
auto-granted, and the URI/secret are never printed or logged. Client-supplied
auto-granted, and the URI/secret are never printed or logged. `--pair -` reads
the URI from **stdin** (I1), so it need not appear in `argv` or history. Client-supplied
relays are **`wss://` only and at most `MAX_CLIENT_RELAYS` (5)**, and a literal
IP that is not globally routable is refused — a `ws://` or `169.254.x`/loopback
relay would otherwise turn a hostile URI into a local-network probe. The
+107 -4
View File
@@ -47,7 +47,8 @@ Options:
--headless run the signer with no window; anything not already allowed
with \"Allow always\" is denied, so pre-approve in the GUI first
--pair <uri> pair with a client from its nostrconnect:// URI, then run
headless
headless; `--pair -` reads the URI from stdin (so the pairing
secret need not appear in argv or shell history)
--key <key> identity (npub or hex) that --pair binds the client to; the
current identity is used when omitted
--background start with no window, running in the background (used for
@@ -84,8 +85,18 @@ fn main() -> glib::ExitCode {
}
};
if parsed.headless || parsed.pair.is_some() || parsed.key.is_some() {
return headless::run(parsed.pair, parsed.key, parsed.passphrase_file);
// `--pair -` reads the URI from stdin, so the pairing secret need not appear
// in argv or shell history.
let pair = match resolve_pairing_argument(parsed.pair, std::io::stdin().lock()) {
Ok(pair) => pair,
Err(error) => {
eprintln!("regent: {error}");
return glib::ExitCode::FAILURE;
}
};
if parsed.headless || pair.is_some() || parsed.key.is_some() {
return headless::run(pair, parsed.key, parsed.passphrase_file);
}
register_resources();
@@ -125,7 +136,21 @@ fn parse_args(args: &[String]) -> Result<Args, String> {
match arg.as_str() {
"--headless" => parsed.headless = true,
"--background" => parsed.background = true,
"--pair" => parsed.pair = Some(value_for(&mut iter, "--pair", "a nostrconnect:// URI")?),
"--pair" => {
// `-` means "read the URI from stdin" and is the one literal
// value that starts with `-`; `value_for` would reject it.
let value = iter
.next()
.ok_or("--pair needs a nostrconnect:// URI, or - for stdin")?
.clone();
if value != "-" && value.starts_with('-') {
return Err(
"--pair needs a nostrconnect:// URI, or - for stdin, not a flag"
.to_string(),
);
}
parsed.pair = Some(value);
}
"--key" => parsed.key = Some(value_for(&mut iter, "--key", "an npub or hex key")?),
"--passphrase-file" => {
parsed.passphrase_file =
@@ -151,6 +176,32 @@ fn parse_args(args: &[String]) -> Result<Args, String> {
Ok(parsed)
}
/// Resolve the `--pair` argument.
///
/// `-` reads one line from `reader` (stdin): the pairing URI embeds the pairing
/// secret, so it need not appear in `argv` or shell history. Anything else is
/// the URI as given. Pure so it is testable.
fn resolve_pairing_argument(
arg: Option<String>,
mut reader: impl std::io::BufRead,
) -> Result<Option<String>, String> {
match arg {
None => Ok(None),
Some(value) if value == "-" => {
let mut line = String::new();
reader
.read_line(&mut line)
.map_err(|error| format!("reading the pairing URI from stdin: {error}"))?;
let uri = line.trim().to_string();
if uri.is_empty() {
return Err("no pairing URI was given on stdin".to_string());
}
Ok(Some(uri))
}
Some(value) => Ok(Some(value)),
}
}
/// The value after a flag, rejecting a following flag so `--pair --headless`
/// fails loudly instead of pairing with the literal string `--headless`.
fn value_for(
@@ -219,3 +270,55 @@ fn register_resources() {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// `--pair -` is accepted as the literal value, while a following flag is
/// still rejected (I1).
#[test]
fn parse_args_accepts_a_pair_dash_and_keeps_flags_strict() {
let dash = vec!["--pair".to_string(), "-".to_string()];
assert_eq!(parse_args(&dash).unwrap().pair.as_deref(), Some("-"));
// The `--pair=-` spelling also works.
let equals = vec!["--pair=-".to_string()];
assert_eq!(parse_args(&equals).unwrap().pair.as_deref(), Some("-"));
assert!(parse_args(&["--pair".to_string()]).is_err(), "needs a value");
assert!(
parse_args(&["--pair".to_string(), "--headless".to_string()]).is_err(),
"--pair must not take a flag"
);
}
/// `-` reads one line from the reader; empty input is an error, and a normal
/// value passes through.
#[test]
fn a_pair_dash_reads_the_uri_from_stdin() {
let uri = resolve_pairing_argument(
Some("-".to_string()),
std::io::Cursor::new(b"nostrconnect://abc\n".to_vec()),
)
.unwrap();
assert_eq!(uri.as_deref(), Some("nostrconnect://abc"));
assert!(
resolve_pairing_argument(Some("-".to_string()), std::io::Cursor::new(Vec::<u8>::new()))
.is_err(),
"empty stdin is an error"
);
let normal = resolve_pairing_argument(
Some("nostrconnect://x".to_string()),
std::io::Cursor::new(Vec::<u8>::new()),
)
.unwrap();
assert_eq!(normal.as_deref(), Some("nostrconnect://x"));
assert_eq!(
resolve_pairing_argument(None, std::io::Cursor::new(Vec::<u8>::new())).unwrap(),
None
);
}
}