Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

graff keys: review fixes for refresh races, rotation, and transport

justrach25d5dd0a81a9
diff --git a/scripts/vault_edge_dev.py b/scripts/vault_edge_dev.py
index 7e962b8a..f058f5ae 100755
--- a/scripts/vault_edge_dev.py
+++ b/scripts/vault_edge_dev.py
@@ -118,7 +118,7 @@ class Edge(BaseHTTPRequestHandler):
if it.get("lease") not in (None, dev_id) and it.get("leaseUntil", 0) > time.time():
return self.reply(409, {"error": "lease_held"})
it.update(lease=dev_id, leaseUntil=time.time() + min(int(data.get("ttlSeconds", 60)), 60))
- return self.reply(200, {"leaseHolder": dev_id})
+ return self.reply(200, {"leaseHolder": dev_id, "version": it["version"]})
if action == "status":
if not it:
return self.reply(404, {})
diff --git a/src/keys_vault_cli.zig b/src/keys_vault_cli.zig
index 8ec2a439..c241c7f9 100644
--- a/src/keys_vault_cli.zig
+++ b/src/keys_vault_cli.zig
@@ -97,6 +97,13 @@ fn loadIdentityFile(io: Io, arena: Allocator, path: []const u8, create: bool) !?
return .{ .id = id, .keys = keys };
}
+/// Where the previous vault key waits during a rotation: beside the device
+/// key file, or under ~/.graff.
+pub fn prevKeyPath(arena: Allocator, home: []const u8, device_file: ?[]const u8) ![]const u8 {
+ if (device_file) |f| if (std.fs.path.dirname(f)) |dir| return std.fmt.allocPrint(arena, "{s}/vault-prev-key", .{dir});
+ return std.fmt.allocPrint(arena, "{s}/.graff/vault-prev-key", .{home});
+}
+
const Opts = struct {
action: []const u8 = "",
arg: []const u8 = "",
@@ -170,9 +177,21 @@ pub fn command(gpa: Allocator, io: Io, arena: Allocator, home: []const u8, env:
try out.writeAll("This device is not enrolled. Run `graff keys enable` first.\n");
exitFlushed(out, 2);
};
- var http: client.Http = .{ .io = io, .gpa = gpa, .base = env.get("HARNESS_EDGE_URL") orelse client.default_edge };
+ const edge_url = env.get("HARNESS_EDGE_URL") orelse client.default_edge;
+ client.checkEdgeUrl(edge_url) catch {
+ try out.print("graff keys: HARNESS_EDGE_URL must be https (plain http only to this machine): {s}\n", .{edge_url});
+ exitFlushed(out, 2);
+ };
+ var http: client.Http = .{ .io = io, .gpa = gpa, .base = edge_url };
var c: client.Client = .{ .io = io, .arena = arena, .transport = http.transport(), .bearer = bearer, .device_id = ident.id, .keys = ident.keys };
var s = try sync.Session.open(io, arena, &c);
+ s.prev_key_path = try prevKeyPath(arena, home, env.get("GRAFF_VAULT_DEVICE_FILE"));
+ // Finish a rotation an earlier run was interrupted in.
+ if (!creating) {
+ if (Io.Dir.cwd().statFile(io, s.prev_key_path.?, .{})) |_| {
+ s.migrate() catch |err| try out.print("note: finishing an interrupted key rotation failed ({t}); it will be retried\n", .{err});
+ } else |_| {}
+ }
const fp = crypto.fingerprint(ident.keys.box.public_key);
if (creating) {
@@ -191,7 +210,7 @@ pub fn command(gpa: Allocator, io: Io, arena: Allocator, home: []const u8, env:
}
try out.print("this device: {s} fingerprint {s}\n", .{ ident.id, &fp });
for (v.devices) |d| {
- const dfp = s.deviceFingerprint(d) catch "????????".*;
+ const dfp = s.deviceFingerprint(d) catch @as(crypto.Fingerprint, @splat('?'));
try out.print(" {s:<10} {s} {s} {s}\n", .{ d.status, &dfp, d.deviceId, d.name });
}
if (std.mem.eql(u8, o.action, "status")) for (v.items) |it| {
diff --git a/src/vault_client.zig b/src/vault_client.zig
index 09c1695a..52765a39 100644
--- a/src/vault_client.zig
+++ b/src/vault_client.zig
@@ -12,6 +12,24 @@ pub const default_edge = "https://edge.codegraff.com";
pub const Response = struct { status: u16, body: []const u8 };
+/// The bearer and ciphertext never travel in clear: https, or plain http to
+/// this machine only (the dev edge).
+pub fn checkEdgeUrl(url: []const u8) error{InsecureEdgeUrl}!void {
+ if (std.mem.startsWith(u8, url, "https://")) return;
+ for ([_][]const u8{ "http://127.0.0.1:", "http://127.0.0.1/", "http://localhost:", "http://localhost/", "http://[::1]:", "http://[::1]/" }) |ok| {
+ if (std.mem.startsWith(u8, url, ok)) return;
+ }
+ return error.InsecureEdgeUrl;
+}
+
+test "checkEdgeUrl allows https and loopback http only" {
+ try checkEdgeUrl("https://edge.example.com");
+ try checkEdgeUrl("http://127.0.0.1:27640");
+ try checkEdgeUrl("http://localhost:8787");
+ try std.testing.expectError(error.InsecureEdgeUrl, checkEdgeUrl("http://edge.example.com"));
+ try std.testing.expectError(error.InsecureEdgeUrl, checkEdgeUrl("http://127.0.0.1.evil.com"));
+}
+
pub const Transport = struct {
ctx: *anyopaque,
callFn: *const fn (ctx: *anyopaque, arena: Allocator, method: []const u8, path: []const u8, headers: []const std.http.Header, body: []const u8) anyerror!Response,
@@ -77,12 +95,14 @@ pub const Client = struct {
};
}
- /// true = lease granted, false = another device holds it.
- pub fn lease(c: *Client, agent: []const u8, slot: []const u8, ttl_s: u32) !bool {
+ /// The item's version at the moment the lease was granted, or null when
+ /// another device holds it. Decide what to do from this version, never
+ /// from an earlier read: another device may have committed in between.
+ pub fn lease(c: *Client, agent: []const u8, slot: []const u8, ttl_s: u32) !?u64 {
const r = try c.call("POST", try c.itemPath(agent, slot, "/lease"), try c.json(.{ .ttlSeconds = @min(ttl_s, 60) }), true, &.{});
return switch (r.status) {
- 200 => true,
- 409 => false,
+ 200 => (try parseField(c.arena, r.body, "version")) orelse 0,
+ 409 => null,
else => statusError(r.status),
};
}
diff --git a/src/vault_crypto.zig b/src/vault_crypto.zig
index b713fab9..b77fbea6 100644
--- a/src/vault_crypto.zig
+++ b/src/vault_crypto.zig
@@ -5,7 +5,8 @@
//! - Vault key: 32 random bytes, created by the first device.
//! - Wrap: ephemeral X25519 to the recipient's public key → HKDF-SHA256
//! (salt = ephPub‖recipientPub, info = "harness-vault-v1 wrap") →
-//! XChaCha20-Poly1305, random 24-byte nonce, aad = "wrap|userId|deviceId".
+//! XChaCha20-Poly1305, random 24-byte nonce,
+//! aad = "wrap|userId|deviceId|keyEpoch" (so an old epoch cannot be replayed).
//! wrappedKey = ephPub ‖ nonce ‖ ciphertext ‖ tag.
//! - Item: XChaCha20-Poly1305 under the vault key, random nonce,
//! aad = "userId|agent|slot|version|keyEpoch"; stored as ciphertext ‖ tag.
@@ -59,17 +60,32 @@ pub const DeviceKeys = struct {
}
};
-/// First 8 hex chars of sha256(X25519 public key): what `approve` asks the
-/// user to compare, and what `status` prints for this device.
-pub fn fingerprint(box_public: [X25519.public_length]u8) [8]u8 {
+pub const fingerprint_hex = 20;
+/// Display width: 20 hex chars in groups of 4.
+pub const Fingerprint = [fingerprint_hex + fingerprint_hex / 4 - 1]u8;
+
+/// First 80 bits of sha256(X25519 public key), as "abcd ef01 2345 6789 abcd":
+/// what `approve` asks the user to compare and `status` prints. 80 bits so a
+/// bearer holder cannot grind a look-alike device key.
+pub fn fingerprint(box_public: [X25519.public_length]u8) Fingerprint {
var digest: [Sha256.digest_length]u8 = undefined;
Sha256.hash(&box_public, &digest, .{});
const hex = std.fmt.bytesToHex(digest, .lower);
- return hex[0..8].*;
+ var out: Fingerprint = undefined;
+ var o: usize = 0;
+ for (hex[0..fingerprint_hex], 0..) |ch, i| {
+ if (i > 0 and i % 4 == 0) {
+ out[o] = ' ';
+ o += 1;
+ }
+ out[o] = ch;
+ o += 1;
+ }
+ return out;
}
-fn wrapAad(buf: []u8, user_id: []const u8, device_id: []const u8) ![]const u8 {
- return std.fmt.bufPrint(buf, "wrap|{s}|{s}", .{ user_id, device_id });
+fn wrapAad(buf: []u8, user_id: []const u8, device_id: []const u8, key_epoch: u64) ![]const u8 {
+ return std.fmt.bufPrint(buf, "wrap|{s}|{s}|{d}", .{ user_id, device_id, key_epoch });
}
fn wrapKeyFor(shared: [X25519.shared_length]u8, eph_pub: [32]u8, recipient_pub: [32]u8) [key_len]u8 {
@@ -83,7 +99,7 @@ fn wrapKeyFor(shared: [X25519.shared_length]u8, eph_pub: [32]u8, recipient_pub:
}
/// Wrap the vault key to one device's X25519 public key.
-pub fn wrap(io: Io, vault_key: VaultKey, recipient_pub: [32]u8, user_id: []const u8, device_id: []const u8) ![wrapped_len]u8 {
+pub fn wrap(io: Io, vault_key: VaultKey, recipient_pub: [32]u8, user_id: []const u8, device_id: []const u8, key_epoch: u64) ![wrapped_len]u8 {
const eph = X25519.KeyPair.generate(io);
const shared = try X25519.scalarmult(eph.secret_key, recipient_pub);
const k = wrapKeyFor(shared, eph.public_key, recipient_pub);
@@ -92,20 +108,22 @@ pub fn wrap(io: Io, vault_key: VaultKey, recipient_pub: [32]u8, user_id: []const
const nonce = out[32..][0..nonce_len];
io.random(nonce);
var abuf: [512]u8 = undefined;
- const aad = try wrapAad(&abuf, user_id, device_id);
+ const aad = try wrapAad(&abuf, user_id, device_id, key_epoch);
const ct = out[32 + nonce_len ..][0..key_len];
const tag = out[32 + nonce_len + key_len ..][0..tag_len];
Aead.encrypt(ct, tag, &vault_key, aad, nonce.*, k);
return out;
}
-pub fn unwrap(keys: DeviceKeys, wrapped: []const u8, user_id: []const u8, device_id: []const u8) !VaultKey {
+/// `key_epoch` is the epoch GET /vault reports; a wrapped key from another
+/// epoch fails to open.
+pub fn unwrap(keys: DeviceKeys, wrapped: []const u8, user_id: []const u8, device_id: []const u8, key_epoch: u64) !VaultKey {
if (wrapped.len != wrapped_len) return error.BadWrappedKey;
const eph_pub = wrapped[0..32].*;
const shared = try X25519.scalarmult(keys.box.secret_key, eph_pub);
const k = wrapKeyFor(shared, eph_pub, keys.box.public_key);
var abuf: [512]u8 = undefined;
- const aad = try wrapAad(&abuf, user_id, device_id);
+ const aad = try wrapAad(&abuf, user_id, device_id, key_epoch);
var out: VaultKey = undefined;
const nonce = wrapped[32..][0..nonce_len].*;
const ct = wrapped[32 + nonce_len ..][0..key_len];
@@ -199,11 +217,13 @@ test "wrap/unwrap round-trips to the right device and rejects the wrong aad or d
const b = DeviceKeys.generate(io);
var vk: VaultKey = undefined;
io.random(&vk);
- const w = try wrap(io, vk, a.box.public_key, "u1", "dev-a");
- try std.testing.expectEqualSlices(u8, &vk, &(try unwrap(a, &w, "u1", "dev-a")));
- try std.testing.expectError(error.WrongKeyOrTampered, unwrap(a, &w, "u2", "dev-a"));
- try std.testing.expectError(error.WrongKeyOrTampered, unwrap(a, &w, "u1", "dev-b"));
- try std.testing.expectError(error.WrongKeyOrTampered, unwrap(b, &w, "u1", "dev-a"));
+ const w = try wrap(io, vk, a.box.public_key, "u1", "dev-a", 3);
+ try std.testing.expectEqualSlices(u8, &vk, &(try unwrap(a, &w, "u1", "dev-a", 3)));
+ try std.testing.expectError(error.WrongKeyOrTampered, unwrap(a, &w, "u2", "dev-a", 3));
+ try std.testing.expectError(error.WrongKeyOrTampered, unwrap(a, &w, "u1", "dev-b", 3));
+ try std.testing.expectError(error.WrongKeyOrTampered, unwrap(b, &w, "u1", "dev-a", 3));
+ // An edge replaying an older epoch's wrapped key is caught.
+ try std.testing.expectError(error.WrongKeyOrTampered, unwrap(a, &w, "u1", "dev-a", 4));
}
test "item seal/open round-trips and any aad field change is rejected" {
@@ -255,5 +275,8 @@ test "userIdFromBearer reads the JWT sub and the dev forms" {
try std.testing.expectEqualStrings("alice", try userIdFromBearer(arena, "alice@org1"));
try std.testing.expectEqualStrings("bob", try userIdFromBearer(arena, "bob"));
const pub_key: [32]u8 = @splat(1);
- try std.testing.expectEqual(@as(usize, 8), fingerprint(pub_key).len);
+ const fp = fingerprint(pub_key);
+ try std.testing.expectEqual(@as(usize, 24), fp.len);
+ try std.testing.expectEqual(@as(u8, ' '), fp[4]);
+ try std.testing.expectEqual(@as(usize, 20), std.mem.count(u8, &fp, " ") * 0 + fp.len - 4);
}
diff --git a/src/vault_live.zig b/src/vault_live.zig
index 355c3954..13ef09db 100644
--- a/src/vault_live.zig
+++ b/src/vault_live.zig
@@ -58,11 +58,14 @@ pub fn before(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, provid
if (!force and !due(io, arena, file, margin_s)) return null;
const ident = (keys_vault_cli.loadIdentity(io, gpa, arena, home, false, env("GRAFF_VAULT_DEVICE_FILE")) catch return null) orelse return null;
const http = arena.create(client.Http) catch return null;
- http.* = .{ .io = io, .gpa = gpa, .base = env("HARNESS_EDGE_URL") orelse client.default_edge };
+ const edge_url = env("HARNESS_EDGE_URL") orelse client.default_edge;
+ client.checkEdgeUrl(edge_url) catch return null;
+ http.* = .{ .io = io, .gpa = gpa, .base = edge_url };
const c = arena.create(client.Client) catch return null;
c.* = .{ .io = io, .arena = arena, .transport = http.transport(), .bearer = bearer, .device_id = ident.id, .keys = ident.keys };
const s = arena.create(sync.Session) catch return null;
s.* = sync.Session.open(io, arena, c) catch return null;
+ s.prev_key_path = keys_vault_cli.prevKeyPath(arena, home, env("GRAFF_VAULT_DEVICE_FILE")) catch null;
const versions = refresh.Versions.at(io, arena, home) catch return null;
const p = refresh.plan(s, versions, provider, file) catch return null;
const live = arena.create(Live) catch return null;
diff --git a/src/vault_mock_edge.zig b/src/vault_mock_edge.zig
index 42a02bb3..5d90b187 100644
--- a/src/vault_mock_edge.zig
+++ b/src/vault_mock_edge.zig
@@ -45,6 +45,11 @@ pub const MockEdge = struct {
m.arena.deinit();
}
+ /// Test hook: a lease that expired while its holder was still working.
+ pub fn dropLease(m: *MockEdge, key: []const u8) void {
+ if (m.items.getPtr(key)) |it| it.lease_holder = null;
+ }
+
pub fn transport(m: *MockEdge) client.Transport {
return .{ .ctx = m, .callFn = handle };
}
@@ -172,7 +177,7 @@ pub const MockEdge = struct {
}
if (item.lease_holder) |h| if (!std.mem.eql(u8, h, caller)) return .{ .status = 409, .body = "{\"error\":\"lease_held\"}" };
item.lease_holder = try a.dupe(u8, caller);
- return resp(arena, 200, .{ .leaseHolder = caller });
+ return resp(arena, 200, .{ .leaseHolder = caller, .version = item.version });
}
if (std.mem.eql(u8, action, "/status")) {
item.status = try a.dupe(u8, parsed.object.get("status").?.string);
diff --git a/src/vault_refresh.zig b/src/vault_refresh.zig
index cb9b6be3..9295192f 100644
--- a/src/vault_refresh.zig
+++ b/src/vault_refresh.zig
@@ -65,17 +65,21 @@ fn adopt(s: *sync.Session, versions: Versions, slot: []const u8, file: []const u
pub fn plan(s: *sync.Session, versions: Versions, slot: []const u8, file: []const u8) !Plan {
const local = versions.get(slot);
- const current: u64 = if (try s.c.getItem("graff", slot)) |it| it.version else 0;
+ // Decide from the version the lease reports, not an earlier read: a
+ // device that commits between a read and our lease would otherwise make
+ // us refresh with a spent token, which providers treat as theft.
+ const current = (try s.c.lease("graff", slot, 60)) orelse return .wait;
if (current > local) {
- if (try adopt(s, versions, slot, file)) |v| return .{ .adopted = v };
+ defer s.c.release("graff", slot) catch {};
+ return if (try adopt(s, versions, slot, file)) |v| .{ .adopted = v } else .wait;
}
- if (!try s.c.lease("graff", slot, 60)) return .wait;
return .{ .refresh = current };
}
/// After a refresh under the lease: write the new login as the next version.
/// If the vault moved anyway, the vault wins and the local result is
-/// discarded (its refresh token was already spent elsewhere).
+/// discarded (its refresh token was already spent elsewhere). If only the
+/// lease was lost, nothing is overwritten unless the vault is actually newer.
pub fn commit(s: *sync.Session, versions: Versions, slot: []const u8, file: []const u8, base: u64) !u64 {
defer s.c.release("graff", slot) catch {};
const bytes = try Io.Dir.cwd().readFileAlloc(s.io, file, s.arena, .limited(1 << 20));
@@ -84,7 +88,12 @@ pub fn commit(s: *sync.Session, versions: Versions, slot: []const u8, file: []co
try versions.set(slot, v);
return v;
},
- .conflict, .lease_held => return (try adopt(s, versions, slot, file)) orelse error.Conflict,
+ .conflict => return (try adopt(s, versions, slot, file)) orelse error.Conflict,
+ .lease_held => {
+ const now: u64 = if (try s.c.getItem("graff", slot)) |it| it.version else 0;
+ if (now > base) return (try adopt(s, versions, slot, file)) orelse error.LeaseLost;
+ return error.LeaseLost;
+ },
}
}
diff --git a/src/vault_refresh_tests.zig b/src/vault_refresh_tests.zig
index 329485c1..274c2173 100644
--- a/src/vault_refresh_tests.zig
+++ b/src/vault_refresh_tests.zig
@@ -60,10 +60,7 @@ test "one refresher at a time: the other device adopts the committed version ins
// Both devices hit expiry. A plans first and gets the lease.
const pa = try refresh.plan(&a.s, a.versions, "xai", a.file);
try testing.expect(pa == .refresh);
- // B has never synced: it adopts v1 rather than refreshing blind…
- const pb = try refresh.plan(&b.s, b.versions, "xai", b.file);
- try testing.expectEqual(refresh.Plan{ .adopted = v1 }, pb);
- // …and once caught up, it must wait while A holds the lease.
+ // B must not refresh while A holds the lease.
try testing.expect((try refresh.plan(&b.s, b.versions, "xai", b.file)) == .wait);
// A refreshes and commits the rotated token.
@@ -75,6 +72,62 @@ test "one refresher at a time: the other device adopts the committed version ins
try testing.expectEqualStrings("token-v2", try b.read(arena));
}
+test "a commit landing between a read and the lease is adopted, not refreshed over" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var edge = MockEdge.init(testing.allocator);
+ defer edge.deinit();
+ var a: Dev = undefined;
+ try a.init(arena, &edge, "dev-a", try tmpPath(arena, &tmp, "a"));
+ var b: Dev = undefined;
+ try b.init(arena, &edge, "dev-b", try tmpPath(arena, &tmp, "b"));
+ _ = try a.s.enable("laptop");
+ _ = try b.s.enable("vps");
+ try a.s.approve("dev-b");
+ try a.write("token-v1");
+ try a.versions.set("xai", try a.s.push("graff", "xai", "rotating", "token-v1"));
+ // A read v1 and decided to refresh; before its lease, B refreshes and commits v2.
+ const pb = try refresh.plan(&b.s, b.versions, "xai", b.file);
+ try testing.expect(pb == .adopted);
+ const pb2 = try refresh.plan(&b.s, b.versions, "xai", b.file);
+ try b.write("token-v2");
+ _ = try refresh.commit(&b.s, b.versions, "xai", b.file, pb2.refresh);
+ // A's lease reports v2 > its v1: adopt, never refresh with the spent token.
+ const pa = try refresh.plan(&a.s, a.versions, "xai", a.file);
+ try testing.expect(pa == .adopted);
+ try testing.expectEqualStrings("token-v2", try a.read(arena));
+ // And A released the lease it took to look.
+ try testing.expect((try b.c.lease("graff", "xai", 60)) != null);
+}
+
+test "a commit after the lease expired does not overwrite the vault" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var edge = MockEdge.init(testing.allocator);
+ defer edge.deinit();
+ var a: Dev = undefined;
+ try a.init(arena, &edge, "dev-a", try tmpPath(arena, &tmp, "a"));
+ var b: Dev = undefined;
+ try b.init(arena, &edge, "dev-b", try tmpPath(arena, &tmp, "b"));
+ _ = try a.s.enable("laptop");
+ _ = try b.s.enable("vps");
+ try a.s.approve("dev-b");
+ try a.write("token-v1");
+ try a.versions.set("xai", try a.s.push("graff", "xai", "rotating", "token-v1"));
+ const pa = try refresh.plan(&a.s, a.versions, "xai", a.file);
+ edge.dropLease("graff/xai"); // A's provider call outlived the lease…
+ try testing.expect((try b.c.lease("graff", "xai", 60)) != null); // …and B took it.
+ try a.write("token-a-refreshed");
+ try testing.expectError(error.LeaseLost, refresh.commit(&a.s, a.versions, "xai", a.file, pa.refresh));
+ try testing.expectEqualStrings("token-v1", (try a.s.pull("graff", "xai")).?.bytes);
+}
+
test "a rejected refresh adopts a newer vault login, else marks needs_signin" {
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
defer arena_state.deinit();
diff --git a/src/vault_sync.zig b/src/vault_sync.zig
index 391a7988..6ef0686c 100644
--- a/src/vault_sync.zig
+++ b/src/vault_sync.zig
@@ -8,12 +8,18 @@ const Io = std.Io;
const Allocator = std.mem.Allocator;
const crypto = @import("vault_crypto.zig");
const client = @import("vault_client.zig");
+const credential_store = @import("credential_store.zig");
pub const Session = struct {
io: Io,
arena: Allocator,
c: *client.Client,
user_id: []const u8,
+ /// Where the previous vault key waits (0600) until every item has been
+ /// re-encrypted under the new epoch. null disables crash recovery.
+ prev_key_path: ?[]const u8 = null,
+ /// The previous key while this process is mid-rotation.
+ prev_key: ?Key = null,
/// A session for `c`. The user id comes from the edge (GET /vault) and
/// falls back to the bearer's `sub` on edges that do not return it.
@@ -38,7 +44,7 @@ pub const Session = struct {
if (!any_enrolled) {
var vk: crypto.VaultKey = undefined;
s.io.random(&vk);
- const w = try crypto.wrap(s.io, vk, s.c.keys.box.public_key, s.user_id, s.c.device_id);
+ const w = try crypto.wrap(s.io, vk, s.c.keys.box.public_key, s.user_id, s.c.device_id, v.keyEpoch);
wrapped = try crypto.encode(s.arena, &w);
}
const status = try s.c.registerDevice(name, wrapped);
@@ -51,17 +57,45 @@ pub const Session = struct {
const v = try s.c.getVault();
const wrapped = v.wrappedKey orelse return error.NotEnrolled;
const raw = try crypto.decode(s.arena, wrapped);
- return .{ .key = try crypto.unwrap(s.c.keys, raw, s.user_id, s.c.device_id), .epoch = v.keyEpoch };
+ return .{ .key = try crypto.unwrap(s.c.keys, raw, s.user_id, s.c.device_id, v.keyEpoch), .epoch = v.keyEpoch };
}
- pub const Pulled = struct { bytes: []u8, version: u64, kind: []const u8, status: []const u8 };
+ fn loadPrevKey(s: *Session) ?Key {
+ if (s.prev_key) |k| return k;
+ const path = s.prev_key_path orelse return null;
+ const data = Io.Dir.cwd().readFileAlloc(s.io, path, s.arena, .limited(256)) catch return null;
+ const t = std.mem.trim(u8, data, " \t\r\n");
+ const colon = std.mem.indexOfScalar(u8, t, ':') orelse return null;
+ var k: Key = .{ .key = undefined, .epoch = std.fmt.parseInt(u64, t[0..colon], 10) catch return null };
+ if (t.len - colon - 1 != 2 * crypto.key_len) return null;
+ _ = std.fmt.hexToBytes(&k.key, t[colon + 1 ..]) catch return null;
+ return k;
+ }
+
+ fn savePrevKey(s: *Session, k: Key) !void {
+ s.prev_key = k;
+ const path = s.prev_key_path orelse return;
+ if (std.fs.path.dirname(path)) |dir| Io.Dir.cwd().createDirPath(s.io, dir) catch {};
+ const text = try std.fmt.allocPrint(s.arena, "{d}:{s}\n", .{ k.epoch, &std.fmt.bytesToHex(k.key, .lower) });
+ try credential_store.replaceFile(s.io, Io.Dir.cwd(), path, text, credential_store.private_file);
+ }
+
+ /// The key an item was sealed under: the current one, or the cached
+ /// previous one for an item a crashed rotation left behind.
+ fn keyFor(s: *Session, item_epoch: u64) !Key {
+ const k = try s.vaultKey();
+ if (item_epoch == k.epoch) return k;
+ if (s.loadPrevKey()) |p| if (p.epoch == item_epoch) return p;
+ return error.StaleItemEpoch;
+ }
+
+ pub const Pulled = struct { bytes: []u8, version: u64, kind: []const u8, status: []const u8, key_epoch: u64 };
pub fn pull(s: *Session, agent: []const u8, slot: []const u8) !?Pulled {
const item = (try s.c.getItem(agent, slot)) orelse return null;
- const k = try s.vaultKey();
- if (item.keyEpoch != k.epoch) return error.StaleItemEpoch;
+ const k = try s.keyFor(item.keyEpoch);
const bytes = try crypto.open(s.arena, k.key, .{ .user_id = s.user_id, .agent = agent, .slot = slot, .version = item.version, .key_epoch = item.keyEpoch }, try crypto.decode(s.arena, item.nonce), try crypto.decode(s.arena, item.ciphertext));
- return .{ .bytes = bytes, .version = item.version, .kind = item.kind, .status = item.status };
+ return .{ .bytes = bytes, .version = item.version, .kind = item.kind, .status = item.status, .key_epoch = item.keyEpoch };
}
pub const Put = union(enum) { ok: u64, conflict: u64, lease_held };
@@ -109,7 +143,7 @@ pub const Session = struct {
return null;
}
- pub fn deviceFingerprint(s: *Session, d: client.Device) ![8]u8 {
+ pub fn deviceFingerprint(s: *Session, d: client.Device) !crypto.Fingerprint {
const raw = try crypto.decode(s.arena, d.publicKey);
if (raw.len != 32) return error.BadPublicKey;
return crypto.fingerprint(raw[0..32].*);
@@ -122,24 +156,22 @@ pub const Session = struct {
const pub_raw = try crypto.decode(s.arena, d.publicKey);
if (pub_raw.len != 32) return error.BadPublicKey;
const k = try s.vaultKey();
- const w = try crypto.wrap(s.io, k.key, pub_raw[0..32].*, s.user_id, device_id);
+ const w = try crypto.wrap(s.io, k.key, pub_raw[0..32].*, s.user_id, device_id, k.epoch);
try s.c.approve(device_id, try crypto.encode(s.arena, &w));
}
- /// Remove a device and rotate: new vault key wrapped to every remaining
- /// enrolled device, then every item re-encrypted under the new epoch
- /// (rotating items under their lease).
+ /// Remove a device and rotate: a new vault key wrapped to every remaining
+ /// enrolled device, then every item re-encrypted under the new epoch.
pub fn remove(s: *Session, device_id: []const u8) !void {
+ try s.rotateKey(device_id);
+ try s.migrate();
+ }
+
+ /// First half of `remove`. The old key is cached before the edge rotates,
+ /// so a crash before `migrate` finishes strands nothing.
+ pub fn rotateKey(s: *Session, device_id: []const u8) !void {
const old = try s.vaultKey();
- const before = try s.c.getVault();
- const Plain = struct { agent: []const u8, slot: []const u8, kind: []const u8, version: u64, bytes: []u8, leased: bool };
- var plains: std.ArrayList(Plain) = .empty;
- for (before.items) |h| {
- const rotating = std.mem.eql(u8, h.kind, "rotating");
- if (rotating and !try s.c.lease(h.agent, h.slot, 60)) return error.LeaseHeld;
- const p = (try s.pull(h.agent, h.slot)) orelse continue;
- try plains.append(s.arena, .{ .agent = h.agent, .slot = h.slot, .kind = h.kind, .version = p.version, .bytes = p.bytes, .leased = rotating });
- }
+ try s.savePrevKey(old);
try s.c.deleteDevice(device_id);
const after = try s.c.getVault();
var new_key: crypto.VaultKey = undefined;
@@ -149,17 +181,34 @@ pub const Session = struct {
if (!std.mem.eql(u8, d.status, "enrolled")) continue;
const pub_raw = try crypto.decode(s.arena, d.publicKey);
if (pub_raw.len != 32) return error.BadPublicKey;
- const w = try crypto.wrap(s.io, new_key, pub_raw[0..32].*, s.user_id, d.deviceId);
+ const w = try crypto.wrap(s.io, new_key, pub_raw[0..32].*, s.user_id, d.deviceId, old.epoch + 1);
try wrapped.put(s.arena, d.deviceId, .{ .string = try crypto.encode(s.arena, &w) });
}
try s.c.rotate(old.epoch + 1, wrapped);
- for (plains.items) |p| {
- defer if (p.leased) s.c.release(p.agent, p.slot) catch {};
- switch (try s.putAt(p.agent, p.slot, p.kind, p.version, p.bytes)) {
+ }
+
+ /// Re-encrypt every item still behind the vault's epoch (rotating ones
+ /// under their lease, all released whatever happens), then drop the
+ /// cached previous key. Safe to rerun after a crash; a no-op once current.
+ pub fn migrate(s: *Session) !void {
+ const v = try s.c.getVault();
+ const Leased = struct { agent: []const u8, slot: []const u8 };
+ var leased: std.ArrayList(Leased) = .empty;
+ defer for (leased.items) |l| s.c.release(l.agent, l.slot) catch {};
+ for (v.items) |h| {
+ if (h.keyEpoch >= v.keyEpoch) continue;
+ if (std.mem.eql(u8, h.kind, "rotating")) {
+ if ((try s.c.lease(h.agent, h.slot, 60)) == null) return error.LeaseHeld;
+ try leased.append(s.arena, .{ .agent = h.agent, .slot = h.slot });
+ }
+ const p = (try s.pull(h.agent, h.slot)) orelse continue;
+ switch (try s.putAt(h.agent, h.slot, h.kind, p.version, p.bytes)) {
.ok => {},
.conflict, .lease_held => return error.Conflict,
}
}
+ s.prev_key = null;
+ if (s.prev_key_path) |path| Io.Dir.cwd().deleteFile(s.io, path) catch {};
}
};
diff --git a/src/vault_sync_tests.zig b/src/vault_sync_tests.zig
index 8e958eeb..3228a228 100644
--- a/src/vault_sync_tests.zig
+++ b/src/vault_sync_tests.zig
@@ -74,8 +74,8 @@ test "compare-and-swap: a stale If-Match gets 412, and a lease blocks other writ
.conflict => |cur| try testing.expectEqual(@as(u64, 1), cur),
else => return error.ExpectedConflict,
}
- try testing.expect(try a.c.lease("graff", "kimi", 60));
- try testing.expect(!try b.c.lease("graff", "kimi", 60));
+ try testing.expect((try a.c.lease("graff", "kimi", 60)) != null);
+ try testing.expect((try b.c.lease("graff", "kimi", 60)) == null);
try testing.expect((try b.s.putAt("graff", "kimi", "rotating", 1, "b-refresh")) == .lease_held);
try testing.expect((try a.s.putAt("graff", "kimi", "rotating", 1, "a-refresh")) == .ok);
try a.c.release("graff", "kimi");
@@ -123,5 +123,61 @@ test "removing a device rotates the key; the survivor still reads, the removed o
// The removed device has no wrapped key any more and its signatures fail.
try testing.expectError(error.NotEnrolled, b.s.pull("graff", "codex"));
// Leases taken for the rotation were released.
- try testing.expect(try a.c.lease("graff", "codex", 60));
+ try testing.expect((try a.c.lease("graff", "codex", 60)) != null);
+}
+
+test "a crash between rotating the key and re-encrypting strands nothing" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var edge = MockEdge.init(testing.allocator);
+ defer edge.deinit();
+ var a: Dev = undefined;
+ a.init(arena, &edge, "dev-a");
+ a.s.prev_key_path = try std.fmt.allocPrint(arena, ".zig-cache/tmp/{s}/prev.key", .{tmp.sub_path});
+ var b: Dev = undefined;
+ b.init(arena, &edge, "dev-b");
+ _ = try a.s.enable("laptop");
+ _ = try b.s.enable("old-vps");
+ try a.s.approve("dev-b");
+ _ = try a.s.push("graff", "codex", "rotating", "codex-login");
+ try a.s.rotateKey("dev-b"); // …and the process dies here.
+ // Items are still at the old epoch; the cached key still opens them.
+ try testing.expectEqualStrings("codex-login", (try a.s.pull("graff", "codex")).?.bytes);
+ try a.s.migrate();
+ const now = try a.c.getVault();
+ for (now.items) |h| try testing.expectEqual(now.keyEpoch, h.keyEpoch);
+ try testing.expectEqualStrings("codex-login", (try a.s.pull("graff", "codex")).?.bytes);
+ // The previous key is gone once nothing needs it.
+ try testing.expectError(error.FileNotFound, std.Io.Dir.cwd().statFile(testing.io, a.s.prev_key_path.?, .{}));
+}
+
+test "a rotation that cannot take every lease releases the ones it took" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var edge = MockEdge.init(testing.allocator);
+ defer edge.deinit();
+ var a: Dev = undefined;
+ a.init(arena, &edge, "dev-a");
+ var b: Dev = undefined;
+ b.init(arena, &edge, "dev-b");
+ var c: Dev = undefined;
+ c.init(arena, &edge, "dev-c");
+ _ = try a.s.enable("laptop");
+ _ = try b.s.enable("vps");
+ _ = try c.s.enable("old");
+ try a.s.approve("dev-b");
+ try a.s.approve("dev-c");
+ _ = try a.s.push("graff", "codex", "rotating", "c");
+ _ = try a.s.push("graff", "xai", "rotating", "x");
+ // B is mid-refresh on one rotating login.
+ const busy = if (std.mem.eql(u8, (try a.c.getVault()).items[0].slot, "codex")) "xai" else "codex";
+ try testing.expect((try b.c.lease("graff", busy, 60)) != null);
+ try testing.expectError(error.LeaseHeld, a.s.remove("dev-c"));
+ // Whatever A leased before stopping is free again for B.
+ const other = if (std.mem.eql(u8, busy, "codex")) "xai" else "codex";
+ try testing.expect((try b.c.lease("graff", other, 60)) != null);
}