openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

RPC bearer token comparison is not constant-time despite documented contract

Open obchain opened this issue 6 months ago • 1 comments

Summary

bearer_matches in src/core/auth.rs:210-212 compares the in-process RPC bearer token with plain ==. Two in-file doc-comments already promise constant-time semantics, but the implementation does not deliver them — str::eq short-circuits at the first mismatching byte, so wall-clock latency of an auth check leaks the length of the matching prefix.

Problem

The current helper short-circuits at the first differing byte:

// src/core/auth.rs:207-212
/// Single source of truth for token comparison. Hex tokens of fixed length
/// make the comparison non-secret-shaped, but we still pin a deliberate
/// helper so adding constant-time semantics later is a one-line change.
fn bearer_matches(supplied: &str, expected: &str) -> bool {
    !supplied.is_empty() && supplied == expected
}

The wrapper that funnels every transport through this helper already documents the stronger (not-yet-true) contract:

// src/core/auth.rs:154-158 — doc-comment on verify_bearer_token
//
// Keeping the comparison in one helper means every transport gets the same
// constant-time equality semantics.

So the existing in-tree contract is "constant-time", but the implementation is short-circuiting. The TODO at lines 207-209 is the maintainer-acknowledged gap.

What happens vs what is expected

  • Expected (per verify_bearer_token rustdoc + per bearer_matches rustdoc): byte-wise compare that takes the same wall-clock time regardless of how many bytes matched, so timing observation reveals nothing about the expected token.
  • Actually happens: str::eq returns as soon as the first differing byte is found. A caller that probes candidate tokens one byte at a time can measure response-latency differences of ~tens of nanoseconds per matching byte and binary-search the correct value at each position.

Reachability + threat model

verify_bearer_token is the single funnel for HTTP POST /rpc, the ?token= query path on GET /events/webhooks, and any future Socket.IO / WebSocket surface — so any of those transports inherits the leak.

The RPC server binds loopback only (127.0.0.1:<port>). The exposed surface is therefore "any local process running as the same UID":

  1. Connect to 127.0.0.1:<port> over TCP — no auth required to open the socket.
  2. Send POST /rpc with a candidate Authorization: Bearer <guess> header.
  3. Measure response latency.
  4. Repeat across candidate values at each byte position to recover the 64-char hex token byte-by-byte.

In the Tauri-spawned core path the token never touches disk — it lives only in CoreProcessHandle.rpc_token + the spawned core's env var (src/core/auth.rs:9-14). So timing recovery is the lowest-friction path for a same-UID process that does not already have file-read access to user data. Once the token is recovered the attacker process can submit arbitrary JSON-RPC, which is the full agent surface.

This is defense-in-depth that closes the maintainer-acknowledged TODO. There is no known PoC and no CVE attached.

Steps to reproduce

  1. cargo check --manifest-path Cargo.toml against src/core/auth.rs:210-212.
  2. Inspect bearer_matches: confirm the body is !supplied.is_empty() && supplied == expected.
  3. Confirm both rustdocs above already document constant-time semantics that the body does not deliver.

Version / platform

  • All builds (Tauri-spawned core path + standalone openhuman-core serve CLI path) on macOS, Linux, Windows.
  • Reproduces on upstream/main at the time of filing (latest commit on main).

Solution (optional)

Replace the short-circuiting equality in bearer_matches with a byte-wise XOR-and-reduce compare that always iterates over max(a.len(), b.len()) so neither the matching-prefix length nor the expected-token length is observable through timing.

The existing pattern lives at src/openhuman/security/pairing.rs:259-276 (pub fn constant_time_eq). To avoid a new cross-module reach from src/core/ into src/openhuman/security/, inline an equivalent private helper inside src/core/auth.rs — no new dependency, no public API change.

Shape of the fix:

fn bearer_matches(supplied: &str, expected: &str) -> bool {
    if supplied.is_empty() {
        return false;
    }
    constant_time_eq_bytes(supplied.as_bytes(), expected.as_bytes())
}

/// Byte-wise XOR + reduce. Does not short-circuit on length or content;
/// iterates over `max(a.len(), b.len())` so timing observation cannot
/// reveal the expected token length or the matching-prefix length.
fn constant_time_eq_bytes(a: &[u8], b: &[u8]) -> bool {
    let len_diff = a.len() ^ b.len();
    let max_len = a.len().max(b.len());
    let mut byte_diff = 0u8;
    for i in 0..max_len {
        byte_diff |= *a.get(i).unwrap_or(&0) ^ *b.get(i).unwrap_or(&0);
    }
    (len_diff == 0) & (byte_diff == 0)
}

Acceptance criteria

  • [ ] Repro gone — bearer_matches no longer short-circuits on the first differing byte and no longer returns early on length mismatch.
  • [ ] Regression safety — Unit tests in src/core/auth.rs cover: exact match, single-byte mismatch at the end of the token, single-byte mismatch at the start, length mismatch (supplied shorter / longer than expected), empty supplied input.
  • [ ] Diff coverage ≥ 80% — the fix PR meets the changed-lines coverage gate (Vitest + cargo-llvm-cov, enforced by .github/workflows/coverage.yml).
  • [ ] Docs match impl — the doc-comments on verify_bearer_token (src/core/auth.rs:154-158) and bearer_matches (src/core/auth.rs:207-209) accurately describe the implementation once the change lands.

Related

  • Existing constant-time helper used by the device-pairing handshake: src/openhuman/security/pairing.rs:259-276.
  • Documentation contract that motivates the fix: src/core/auth.rs:154-158, :207-211.

obchain avatar May 25 '26 06:05 obchain

Closing — already shipped in #2572 (merged 2026-05-25 05:01Z, commit c88fea10). My audit ran against a stale local checkout and missed that the constant-time compare and regression tests had already landed on main. Apologies for the noise.

obchain avatar May 25 '26 06:05 obchain