Skip to content

netpaste 2.0: new protocol, encryption, hub, mDNS, TypeScript - #2

Merged
leplik merged 9 commits into
mainfrom
v2
Nov 19, 2026
Merged

leplik merged 9 commits into
mainfrom
v2

Conversation

@leplik

@leplik leplik commented Nov 19, 2026

Copy link
Copy Markdown
Owner

A rewrite of netpaste. A review of 1.x found more than 25 problems, several of them critical; this PR fixes all of them. The wire protocol changes, so all machines must be updated.

Critical problems in 1.x (all reproduced)

  • 4 bytes from any machine crashed the server (printf NetP | nc host 8134).
  • Text larger than one TCP segment arrived corrupted: 3,000 characters became 1,822 characters of garbage, and the integrity check passed it into the clipboard.
  • The client crashed on its first reconnect: setTimeout(createConnection) passed no arguments.
  • Text could be changed or forged without the passphrase: AES-CTR had no MAC, and the HELLO plaintext was known.
  • The server gave every connecting machine material for offline passphrase guessing (PBKDF2, 10,000 iterations).
  • Passphrase input corrupted the passphrase: Backspace was stored as \x7f, arrow keys as the text undefined.

The full list is in CHANGELOG.md.

What 2.0 does

  • Framing: u32 length | u8 type | body, decoded from a byte stream, size checked before buffering, unread input bounded by TCP flow control.
  • Handshake: ephemeral X25519 + scrypt (128 MiB, per-connection salt) + HKDF; mutual passphrase proof; the hub runs scrypt only after the client proof.
  • Messages: AES-256-GCM, per-direction keys, counter nonces, header as AAD (no tampering, replay or reordering).
  • Hub for many machines (--no-clipboard = relay), reconnect with backoff, heartbeats, idle timeout.
  • Strangers are limited: 4 unauthenticated connections per address, 32 in total; a wrong passphrase blocks the address (1 s doubling to 60 s).
  • Clipboard: no trim(), echo suppression by content instead of a 2-second window, one poller that runs only while a peer is connected.
  • netpaste serve | connect [HOST[:PORT]] | discover; mDNS discovery; passphrase from a prompt, --passphrase-file or NETPASTE_PASSPHRASE; launchd and systemd templates.
  • TypeScript (strict) run directly by Node.js 22.18+; chalk replaced by util.styleText; clipboardy 5 (Wayland).

The design and threat model are in docs/PROTOCOL.md, including the known limits: offline guessing by an active attacker who poses as the hub (use a long passphrase), and crossing copies made within ~1 s on two machines.

Testing

  • 95 tests (node:test):
    • framing split at every byte;
    • crypto tampering, replay and reordering;
    • handshake with a man-in-the-middle;
    • clipboard races;
    • passphrase editor;
    • CLI;
    • end-to-end hub tests with an in-memory clipboard, including floods, parallel guessing, a slow link and reconnects.
  • Every commit type-checks and passes its tests on its own.
  • Two independent reviews: of 1.x, and of this branch. Their findings are fixed in this PR.
  • Manual runs on macOS, with real processes on loopback: wrong passphrase, busy port, no TTY, junk input, restart and reconnect, passphrase typing in a PTY.
  • Not tested yet: real clipboard sync between macOS and Linux.

🤖 Generated with Claude Code

leplik and others added 9 commits November 19, 2026 08:34
Version 2 is a rewrite with a new wire protocol, so the 1.x files go away.
Node.js 22.18+ runs the TypeScript sources directly; tsc only type-checks.

- chalk is replaced by util.styleText (respects NO_COLOR and non-TTY output)
- clipboardy 4 -> 5.3.2: native Wayland support (wl-clipboard)
- bonjour-service for mDNS discovery; typescript 7 and @types/node 22 for checks
- package-lock.json had a stale "ISC" license; regenerated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Framing: u32 length | u8 type | body, decoded from a byte stream with a
size check before buffering. 1.x split messages on a "NetP" marker and
assumed that every TCP read starts with a whole fragment: texts larger
than one segment arrived corrupted, and 4 bytes crashed the process.

Handshake: ephemeral X25519 + scrypt(passphrase, per-connection salt) +
HKDF-SHA256. The client proves the passphrase first; the server proves it
back. A passive eavesdropper cannot test guesses, a stranger receives
nothing. Wrong passphrase: delayed ERROR, so online guessing is slow.

Messages: AES-256-GCM with per-direction keys and counter nonces, the
frame header as AAD. Changed, replayed, reordered or reflected messages
fail. 1.x used AES-CTR without a MAC, so text could be changed or forged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Text is passed through unchanged; 1.x trimmed indentation and final newlines.
- Echo suppression by content instead of a 2-second flag. 1.x lost copies made
  within 2 s of a received one, echoed copies made about 2 s apart, and a single
  failed write blocked all later copies.
- One sequential poll loop; a read that overlaps a network write is dropped.
- After a failed first read, the first successful read is the baseline, so old
  clipboard content is not sent just because netpaste started.
- A failed write resynchronizes instead of sending the old text out as new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Passphrase input in 1.x stored Backspace as \x7f and arrow keys as the text
"undefined", so a corrected typo silently gave the machines different
passphrases. It also never finished when stdin was a pipe.

- Raw-mode line editor: Backspace, Ctrl+U, escape sequences (also split
  across reads), bracketed paste, Ctrl+C / Ctrl+D.
- --passphrase-file (warns about group/world-readable files) and
  NETPASTE_PASSPHRASE (removed from the environment of child processes).
- util.parseArgs with strict validation: no silent fallback to port 8134,
  flags in any order, per-command options, sizes like 10mb.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hub announces _netpaste._tcp; "netpaste connect" without HOST finds it.

Two problems of bonjour-service are worked around:
- It announces A/AAAA records for os.hostname(). On macOS that is the name
  that mDNSResponder owns, so macOS renames the computer (Name-2.local).
  We announce a unique SRV target, netpaste-<name>-<random>.local.
- It does not listen for 'error' on its multicast socket, so a busy UDP
  port would crash the process. We report it as a warning instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- netpaste serve | connect [HOST[:PORT]] | discover
- The hub relays text from one client to all others (1.x did not);
  --no-clipboard turns it into a pure relay.
- The client reconnects with exponential backoff and jitter. 1.x crashed on
  its first reconnect: setTimeout(createConnection) passed no arguments.
- PING/PONG heartbeats, idle timeout and TCP keepalive detect dead peers
  after sleep or a network change.
- One clipboard poller per process, active only while a peer is connected.
  1.x started a new poller per connection and never stopped it.
- After a wrong passphrase the hub blocks the address, 1 s doubling to 60 s.
- Clear errors and exit codes: wrong passphrase, busy port, old protocol, no TTY.
- bin/netpaste.js checks the Node.js version before it loads TypeScript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- README: install, usage, passphrase sources, firewall, autostart, security
  limits, upgrade notes from 1.x.
- docs/PROTOCOL.md: framing, handshake, key schedule, messages, threat model.
- CHANGELOG.md: every 1.x problem that 2.0 fixes.
- launchd LaunchAgent and systemd user service templates.
- GitHub Actions: typecheck and tests on Linux and macOS, Node.js 22/24/26.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Security and robustness:
- Unread input is bounded: FrameReader pauses the socket when more than one
  maximum-size frame + 64 KiB waits unread. Before, a stranger could make the
  hub buffer 600 MB while it ran scrypt.
- The hub runs scrypt only after the client proof arrives, so a HELLO alone
  costs it nothing; a connection dropped during scrypt gets no answer.
- Connections without the passphrase are limited (4 per address, 32 total),
  separately from authenticated peers (32). IPv6 addresses count per /64.
- A wrong passphrase blocks the address at once (the block counts from the
  delayed reply) and closes its other pending connections: at most 2 guesses
  per block period instead of 32.
- ERROR frames are not authenticated: "wrong passphrase" counts only after our
  own proof, and it stops the client only for an explicitly given hub. For a
  hub found with mDNS (or several of them) the client keeps trying.

Correctness:
- Client.close() during mDNS discovery stopped nothing; the search is now
  abortable and the client checks the signal before it connects.
- The idle timeout counts every received chunk, so a large text on a slow
  link is not cut; heartbeats use a monotonic clock and are checked every 5 s.
- The hub sends no text to a peer before its INFO (unknown limit), nor to a
  peer that does not read (bounded send queue).
- Clipboard watcher: after a failed write, the next read becomes the baseline
  instead of a concurrent re-read (no echo, no old text sent out).
- Passphrase prompt: a stray ESC no longer swallows Enter or Ctrl+C; TAB is
  kept; NETPASTE_PASSPHRASE is removed even when --passphrase-file wins.
- CLI: host names are validated; --host with a specific address turns the
  mDNS announcement off; --max-size is capped at 64 MiB (clipboardy reads
  through a 100 MB buffer); the Node.js check also rejects 23.0-23.5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leplik
leplik merged commit de67559 into main Nov 19, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant