Files
vdm/daemon/src/store/pairings.cpp
T
samiandClaude Sonnet 5 dab071c41a daemon: rpc/ws_server — loopback WebSocket transport + pairing (build step 1, second half)
The extension's fallback transport (docs/05 §4). veloxd now also listens
on 127.0.0.1, first free port in 52000-52016, and writes it to
<runtime>/ws.port (0600).

- rpc/ws_frame — RFC 6455 frame codec. Incremental; reassembles
  continuation frames; enforces "client frames MUST be masked" (§5.1);
  caps a reassembled message at 8 MiB. This is the attacker-adjacent
  parser, so it has its own test table.
- rpc/ws_handshake — HTTP upgrade parse, Sec-WebSocket-Accept
  (SHA-1 + base64 via libcrypto), and the two non-negotiable checks:
  an Origin header must be present and must be moz-extension:// (a page
  cannot pair). Version must be 13.
- rpc/ws_server — per-connection Handshake -> Open state machine on the
  shared EventLoop. Token gate: session.pair mints a token behind the
  approver + rate limiter; session.hello must present a valid one;
  every other method is -32002 until authed. Privileged methods are
  refused -32003 by the generated dispatch(). Ping -> Pong; Close
  echoed. session.hello major-version mismatch -> -32001.
- rpc/pairing — PairingApprover interface + EnvAutoApprover dev stub
  (approves iff VELOX_PAIR_AUTO=1); PairingRateLimiter (5 failures / 60 s
  per origin, then 60 s lockout -> -32014, survives reconnect); a
  four-digit code generator.
- store/pairings — the pairings table: create() returns the plaintext
  token once and stores only its SHA-256; find_active_by_token,
  touch, revoke, list_active.
- util/crypto — sha1 / sha256_hex / base64 / random_token over libcrypto.
- store/sqlite — pin the DB file (and -wal/-shm) to 0600.
- runtime_dir — resolve_data_dir() for $XDG_DATA_HOME/velox (velox.db).
- main.cpp — opens + migrates velox.db, starts both transports; a WS
  bind failure is logged, not fatal (capture must fail open, the Unix
  socket still serves the GUI/CLI).

Real gap, flagged not hidden: the pairing prompt is EnvAutoApprover for
now — a GUI dialog / desktop notification is build step 7. Pairing
needs VELOX_PAIR_AUTO=1 until then.

Tests (ASan+UBSan and TSan clean): veloxd.ws_frame (codec + handshake
vectors incl. the RFC 6455 §1.3 accept sample), veloxd.pairings (token
create/find/revoke, hash-not-token, rate-limit window + lockout +
per-origin isolation + success reset), veloxd.ws_server (full flow: 101
handshake, -32002 gate, deny-then-approve pairing, hello-with-token,
-32003 privileged refusal, real download.list). 27 daemon/cli tests
green; full tree green.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Upd9WhG9oppieig5nRDLig
2026-09-10 15:44:28 +04:00

105 lines
4.1 KiB
C++

#include "store/pairings.hpp"
#include <sqlite3.h>
#include <random>
#include "util/crypto.hpp"
namespace velox::daemon::store {
namespace {
std::string uuid4() {
std::random_device rd;
std::uniform_int_distribution<std::uint32_t> d;
std::uint32_t a = d(rd), b = d(rd), c = d(rd), e = d(rd);
b = (b & 0xFFFF0FFFu) | 0x00004000u;
c = (c & 0x3FFFFFFFu) | 0x80000000u;
char buf[37];
std::snprintf(buf, sizeof(buf), "%08x-%04x-%04x-%04x-%04x%08x", a, (b >> 16), (b & 0xFFFF),
(c >> 16), (c & 0xFFFF), e);
return std::string(buf);
}
Pairing read_row(Stmt& s) {
Pairing p;
p.pairing_id = s.column_text(0);
p.origin = s.column_text(1);
p.label = s.column_text(2);
p.created_at = s.column_text(3);
if (!s.column_is_null(4)) p.last_seen_at = s.column_text(4);
if (!s.column_is_null(5)) p.revoked_at = s.column_text(5);
return p;
}
constexpr std::string_view kCols =
"pairing_id, origin, label, created_at, last_seen_at, revoked_at";
} // namespace
DbResult<Pairings::Created> Pairings::create(std::string_view origin, std::string_view label,
std::string_view now_iso) {
Created out{uuid4(), velox::daemon::crypto::random_token(32)};
const std::string hash = velox::daemon::crypto::sha256_hex(out.token);
auto st = db_.prepare(
"INSERT INTO pairings(pairing_id, token_sha256, origin, label, created_at) "
"VALUES(?1, ?2, ?3, ?4, ?5)");
if (!st) return std::unexpected(st.error());
if (auto r = st->bind(1, out.pairing_id); !r) return std::unexpected(r.error());
if (auto r = st->bind(2, std::string_view(hash)); !r) return std::unexpected(r.error());
if (auto r = st->bind(3, origin); !r) return std::unexpected(r.error());
if (auto r = st->bind(4, label); !r) return std::unexpected(r.error());
if (auto r = st->bind(5, now_iso); !r) return std::unexpected(r.error());
if (auto r = st->step(); !r) return std::unexpected(r.error());
return out;
}
DbResult<std::optional<Pairing>> Pairings::find_active_by_token(std::string_view token) {
const std::string hash = velox::daemon::crypto::sha256_hex(token);
auto st = db_.prepare(std::string("SELECT ").append(kCols).append(
" FROM pairings WHERE token_sha256 = ?1 AND revoked_at IS NULL"));
if (!st) return std::unexpected(st.error());
if (auto r = st->bind(1, std::string_view(hash)); !r) return std::unexpected(r.error());
auto row = st->step();
if (!row) return std::unexpected(row.error());
if (!*row) return std::optional<Pairing>{};
return std::optional<Pairing>{read_row(*st)};
}
DbResult<void> Pairings::touch(std::string_view pairing_id, std::string_view now_iso) {
auto st = db_.prepare("UPDATE pairings SET last_seen_at = ?2 WHERE pairing_id = ?1");
if (!st) return std::unexpected(st.error());
if (auto r = st->bind(1, pairing_id); !r) return std::unexpected(r.error());
if (auto r = st->bind(2, now_iso); !r) return std::unexpected(r.error());
if (auto r = st->step(); !r) return std::unexpected(r.error());
return {};
}
DbResult<bool> Pairings::revoke(std::string_view pairing_id, std::string_view now_iso) {
auto st = db_.prepare(
"UPDATE pairings SET revoked_at = ?2 WHERE pairing_id = ?1 AND revoked_at IS NULL");
if (!st) return std::unexpected(st.error());
if (auto r = st->bind(1, pairing_id); !r) return std::unexpected(r.error());
if (auto r = st->bind(2, now_iso); !r) return std::unexpected(r.error());
if (auto r = st->step(); !r) return std::unexpected(r.error());
return sqlite3_changes(db_.raw()) > 0;
}
DbResult<std::vector<Pairing>> Pairings::list_active() {
auto st = db_.prepare(std::string("SELECT ").append(kCols).append(
" FROM pairings WHERE revoked_at IS NULL ORDER BY created_at"));
if (!st) return std::unexpected(st.error());
std::vector<Pairing> out;
for (;;) {
auto row = st->step();
if (!row) return std::unexpected(row.error());
if (!*row) break;
out.push_back(read_row(*st));
}
return out;
}
} // namespace velox::daemon::store