AMS.

Keeping a six-digit key safe: closing the holes in my P2P file-transfer side project

5 min listen

Cover: A turquoise combination padlock on a weathered door by Nilo Velez, CC0 1.0.

Why the key matters so much

I wanted something like Send Anywhere: pick a file, press Send, get a six-digit key, and whoever types it in receives the file. The server only acts as an introducer. It helps the two devices swap connection details, and after that the file goes from one device to the other without passing through the server, so running it costs almost nothing.

Sender creates akeyReceiver types thekeyServer introducesthe two devicesDevices open adirect channelFile goes straightacross
How a file gets from one device to another

To keep it quick, I removed the step where the sender approves the receiver. That means whoever guesses the key gets the file, so how hard the key is to guess is the heart of the whole design.

What I tried first

  • A rate limit on its own. Each network address could try 10 keys a minute, and a key expires after 10 minutes. I thought that was enough.
  • Reading my own code. It's hard to spot the gaps in something you just wrote.
  • Cloudflare's security-audit skill. It sends a few agents to map the code, hunt for problems, argue against each finding and check what survives, then writes a report. I used its quick mode, which only reads the source.

The first item in the report said my rate limit wasn't really doing its job.

How I closed the holes

Give the sender its own pass

The limit only counted receivers. Connecting as the sender had no limit, and the server answered differently for a key in use and a key that didn't exist. So someone could scan freely for keys in use, then join each one as a receiver.

I changed three things:

  • When a key is created, the server also hands its creator a random pass. Connecting as the sender requires it.
  • An unknown key, a wrong pass and a key that already has a sender all get the same "not found" answer, so nobody can tell them apart.
  • Every connection counts toward the limit of 10 a minute, whatever role it claims.

The check on the server looks like this:

// Every refusal looks the same, so nothing reveals which keys are in use
if (role === 'host') {
  const stored = await storage.get('hostToken')
  if (hasHost || !stored || !sameToken(url.searchParams.get('token') ?? '', stored)) {
    return notFound()
  }
}

Files only go one way

The sender's side of the app also knew how to receive files, and on a desktop it saved them automatically. Someone who guessed a key could push any file onto the sender's computer.

Now the sender only sends and the receiver only receives. The sender accepts nothing but "got it" receipts, and I removed the receiver's "send more" button from the mobile app.

Count IPv6 users by their whole block

An IPv6 user usually holds a large block of addresses and can switch between them freely, so limiting by single address barely limits them. I now count the first half of the address, the block known as a slash 64, while IPv4 stays as it was:

// One IPv6 user usually holds a whole /64, so count the prefix, not the address
function limitKey(ip: string): string {
  if (!ip.includes(':')) return ip
  const groups = expandIPv6(ip)
  return groups.slice(0, 4).join(':') + '::/64'
}

Relay accounts only for paired devices

Some networks stop two devices from reaching each other, and then the file goes through a relay server, which charges for traffic. I had a public address where anyone could ask for temporary relay accounts as often as they liked.

That address is gone. The relay details now ride inside the "you're paired" message and last only as long as the pairing. To get them, someone has to create a key and have someone join it, and both steps are rate limited.

Things that tripped me up

  • The site joined twice. Typing the sixth digit already joins, so pressing Enter joined again, and the second attempt said "not found" because the key was used up. Now a repeat join for a key that's already connecting is ignored.
  • The mobile app stalled partway through a big file. The WebRTC library the app uses reports how much data is still queued with a delay. The app thought the queue was short, kept filling it and the connection dropped. A small patch to the library and counting data sent in the last moment fixed it.
  • The first messages could go missing. The same library sometimes dropped the earliest messages, and those carry the list of files. The app now holds them until the channel is ready.
  • The limit has a cost. More than 10 connections a minute from one address gets blocked for a minute, even for normal use. One person won't notice, but a busy office sharing one address might.

What improved

  • Scanning for keys now needs the sender's pass, gets the same answer every time and counts toward the limit.
  • A receiver can no longer push files onto the sender.
  • IPv6 users are limited per block, not per address.
  • Relay accounts only go to devices that have actually paired.
  • A test file still arrived byte for byte the same after all the changes.

Comments