Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

Pairing a desktop agent with RFC 8628, and the confirm-step attack the RFC warns about

Some of the people using our apps keep their files in a folder or on a Windows share in their office, while the app runs in the cloud. To get the files across we wrote a small agent in Go, the satellite. It runs next to

Some of the people using our apps keep their files in a folder or on a Windows share in their office, while the app runs in the cloud. To get the files across we wrote a small agent in Go, the satellite. It runs next to the share and only makes outbound HTTPS calls, so nobody has to open a port or set up a VPN. It has to be paired with one tenant of the app once, by someone who is an admin there.

That is the problem OAuth 2.0 Device Authorization Grant (RFC 8628) solves for TVs and CLIs: the device shows a short code, a person types it into a browser where they are already logged in, and the device collects a token. We used it as written. The part that took a second look was section 5.4, remote phishing, and the step in our own UI that the first version got wrong.

The flow

The satellite posts its client_id to /connectors/v1/device_authorization. The server answers with a long device_code, a short user_code like 7KQ2-M9XD, a verification_uri, expires_in: 600 and interval: 5. The satellite shows the code in its setup window and polls /connectors/v1/token with the device code every five seconds.

On the server side we store only hashes of both codes. The user code is 8 characters from Crockford's base32 alphabet (32 symbols, no I, L, O or U), so one random byte modulo 32 picks a symbol without bias. When a person types it, case, dashes and spaces don't matter, and I, L and O are read as 1, 1 and 0.

In the app, a TenantAdmin opens the pairing screen, types the code and clicks Check. If the code belongs to an open request, the screen shows when and from which IP address the request came, so the admin can see whether it was their own machine. Then they give the device a name and confirm. Confirming creates a connector in the admin's tenant and marks the request approved. The next poll returns a token ksat_…, which the server stores as a SHA-256 hash and which the admin can revoke per device.

The satellite's side of this is about 150 lines of Go in internal/pairing/pairing.go. The poll loop handles authorization_pending, slow_down (add five seconds to the interval), expired_token and access_denied, and gives up when expires_in runs out.

What section 5.4 says

The device flow has a built-in weakness: the device that asks for access doesn't have to be the one in front of the person who approves it. An attacker starts a pairing on their own machine, gets a valid user code, and sends it to an admin with a message like "please enter this code to finish the setup". If the admin types it in and confirms, the attacker's machine gets a token for the admin's tenant.

RFC 8628 says to tell the user they are authorizing a device and to confirm it is in their possession. It is stricter about verification_uri_complete, the optional link that already carries the code: since the user no longer types the code, the server should show it and ask the user to check that it matches what their device displays.

For us the attack has teeth. A paired satellite can upload files into the tenant's inbox, and an inbox is where people open attachments.

Version one

Our pairing screen had two steps from the start: Check, then name and confirm. Check showed time and IP of the request. That looked like the mitigation the RFC asks for.

It was a step in the UI only. The check handler read the request and changed nothing. The confirm handler took a user code and a name and claimed any pending request with that code. If an admin's browser landed on the confirm step with a code already filled in, for example through a link someone sent them, they skipped the part where a human looks at the time and the address. The safeguard lived in the order of the screens, and nothing on the server knew about it.

The fix went into the same pull request eight minutes after the first commit, on 19 November, before anything was merged.

The fix

The check now writes. It records on the pending request which admin checked the code and when:

const marked = await updateMany(
  ctx.db,
  deviceAuthorizationTable,
  { checkedByUserId: event.payload.userId, checkedAt: now },
  {
    userCodeHash: event.payload.userCodeHash,
    status: DEVICE_AUTHORIZATION_STATUS.pending,
    expiresAt: { gt: now },
  },
);

The confirm is one conditional update from pending to approved. It only matches when the same admin checked this code in the last 300 seconds:

const claimed = await updateMany(
  ctx.db,
  deviceAuthorizationTable,
  { status: DEVICE_AUTHORIZATION_STATUS.approved, ...claim },
  {
    userCodeHash,
    status: DEVICE_AUTHORIZATION_STATUS.pending,
    expiresAt: { gt: ctx.tz.now() },
    checkedByUserId: claim.approvedByUserId,
    checkedAt: { gt: ctx.tz.now().subtract({ seconds: CODE_CHECK_VALID_SECONDS }) },
  },
);

If nothing matches, the handler returns the same "invalid code" error as for an unknown or expired code, and the connector it just created rolls back with the transaction. Because it is a single update, a code confirmed twice at once produces one connector and one winner. There's a test for that race, and one that walks through the cases: no check, a check by another admin, a check that is 301 seconds old, and finally a fresh check by the confirming admin, where only the last one pairs.

With that in place we could add verification_uri_complete later the same day. The satellite shows a link that opens the pairing screen with the code filled in. The admin still has to click Check, look at the time and address, and confirm. A link sent by an attacker gets them to the same screen, where the request shows up with a stranger's IP.

The rest of the hardening

The client_id of the satellite is public; it's in an open source binary. So anyone can start pairings, and the start endpoint is the one that needed limits:

  • at most 3 open requests per address, 1000 in total, and 10 starts per minute per address. Above that the endpoint answers 429 slow_down and stores nothing.
  • IPv6 addresses count per /64. A per-address limit on a full IPv6 address is easy to get around with a fresh address per request.
  • codes expire after 600 seconds. A redeemed request is deleted, together with its address.
  • checking and confirming codes is rate limited to 20 tries per 5 minutes per admin.
  • there is no reject button. Before it is confirmed a request belongs to no tenant, so nobody has anything to reject, and it disappears after ten minutes.

What we took from it

A two-step screen is not a security control by itself. The server has to be able to tell that the first step happened, for this user and this code, and recently. Writing it as a condition in the same update that does the claim kept it to two columns and one where clause.

The satellite is open source at github.com/CosmicDriftGameStudio/kumiko-satellite, and the server side is the connectors feature in kumiko-framework. If you run a device flow with verification_uri_complete, I'd be curious how you bind the approval to the person who looked at the code.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.