@peer2peer/cli 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/dist/ancestry.d.ts +42 -2
- package/dist/ancestry.js +215 -32
- package/dist/app.d.ts +44 -2
- package/dist/app.js +116 -15
- package/dist/cli.d.ts +30 -3
- package/dist/cli.js +133 -8
- package/dist/doctor.js +29 -1
- package/dist/format.d.ts +2 -0
- package/dist/format.js +5 -1
- package/dist/identity.d.ts +6 -2
- package/dist/identity.js +34 -19
- package/dist/pretty.js +2 -1
- package/dist/projects.d.ts +58 -12
- package/dist/projects.js +220 -48
- package/dist/rotation.d.ts +20 -1
- package/dist/rotation.js +280 -14
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/watch.js +43 -13
- package/package.json +3 -3
package/dist/identity.js
CHANGED
|
@@ -47,7 +47,8 @@ export async function init(app, opts) {
|
|
|
47
47
|
const self = { name, relays, createdAt: Date.now() };
|
|
48
48
|
// A person.key from before this identity existed no longer counts (projects.ts renewPersonToken).
|
|
49
49
|
// Replaced before any key is written: if that fails, no identity is made here.
|
|
50
|
-
renewPersonToken(app.home);
|
|
50
|
+
renewPersonToken(app.home, keys.spk);
|
|
51
|
+
app.identityMadeHere = true;
|
|
51
52
|
await app.state.put("keys", keys);
|
|
52
53
|
await app.state.put("self", self);
|
|
53
54
|
app.setKeys(keys, self);
|
|
@@ -195,28 +196,39 @@ export async function relaysAdd(app, url) {
|
|
|
195
196
|
throw new CliError("bad_relay", `at most ${MAX_RELAYS} relays: remove one first`);
|
|
196
197
|
return changeRelays(app, (list) => [...list.filter((x) => x !== r), r]);
|
|
197
198
|
}
|
|
198
|
-
/**
|
|
199
|
-
|
|
200
|
-
|
|
199
|
+
/**
|
|
200
|
+
* Removes relays (at least one must stay), in one new version. The removed relays get it too, so they stop
|
|
201
|
+
* admitting removed state. Several at once (#78 review): two relays that both went silent while holding a
|
|
202
|
+
* newer version each hold up removing the other one alone (App.ownPendingGuard).
|
|
203
|
+
*/
|
|
204
|
+
export async function relaysRemove(app, urls) {
|
|
205
|
+
const rs = [...new Set(normalizeRelays(typeof urls === "string" ? [urls] : [...urls]))];
|
|
206
|
+
if (rs.length === 0)
|
|
207
|
+
throw new CliError("usage", "name the relay to remove");
|
|
201
208
|
const cur = (await app.ownView()).head.relays;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
209
|
+
for (const r of rs)
|
|
210
|
+
if (!cur.includes(r))
|
|
211
|
+
throw new CliError("not_found", `${r} is not one of your relays`);
|
|
212
|
+
if (cur.every((r) => rs.includes(r)))
|
|
213
|
+
throw new CliError("usage", cur.length === 1 ? "that is your only relay: add another first" : "at least one relay must stay: add another first");
|
|
214
|
+
// Caps naming a dropped relay, captured before the re-issue adds new ones.
|
|
215
|
+
const old = (await app.store.listCapsIssued()).filter((c) => c.iss === app.userId && c.relays.some((r) => rs.includes(r)));
|
|
216
|
+
const out = await changeRelays(app, (list) => list.filter((x) => !rs.includes(x)), rs);
|
|
217
|
+
// review L2: revoke them at the dropped relays only (our devices are still in their allowlists). Other
|
|
210
218
|
// relays keep honouring them, so friends who haven't picked up the new caps stay covered.
|
|
211
219
|
// Best effort: the relay may be down.
|
|
212
220
|
let revokedAtRemoved = 0;
|
|
221
|
+
let tried = 0;
|
|
213
222
|
const pool = app.pool ?? (await app.connect({ allowNone: true }));
|
|
214
223
|
for (const c of old) {
|
|
215
|
-
|
|
216
|
-
|
|
224
|
+
for (const r of rs.filter((x) => c.relays.includes(x))) {
|
|
225
|
+
tried++;
|
|
226
|
+
if ((await pool.revoke(c.capid, [r]).catch(() => [])).length > 0)
|
|
227
|
+
revokedAtRemoved++;
|
|
228
|
+
}
|
|
217
229
|
}
|
|
218
|
-
if (revokedAtRemoved <
|
|
219
|
-
app.warn(`couldn't revoke ${
|
|
230
|
+
if (revokedAtRemoved < tried)
|
|
231
|
+
app.warn(`couldn't revoke ${tried - revokedAtRemoved} of your old caps at ${rs.join(", ")}; they expire on their own`);
|
|
220
232
|
return { ...out, revokedAtRemoved };
|
|
221
233
|
}
|
|
222
234
|
export async function devices(app) {
|
|
@@ -496,7 +508,8 @@ export async function linkJoin(app, code, opts = {}) {
|
|
|
496
508
|
}
|
|
497
509
|
const keys = newKeysDoc(userId, { ...device, userId }, opened.cert.jws, opened.ik);
|
|
498
510
|
const self = { name: opened.doc.name, relays: opened.doc.relays, createdAt: Date.now() };
|
|
499
|
-
renewPersonToken(app.home);
|
|
511
|
+
renewPersonToken(app.home, keys.spk);
|
|
512
|
+
app.identityMadeHere = true;
|
|
500
513
|
await app.state.put("keys", keys);
|
|
501
514
|
await app.state.put("self", self);
|
|
502
515
|
app.setKeys(keys, self);
|
|
@@ -591,7 +604,8 @@ async function restoreInto(app, phrase, opts) {
|
|
|
591
604
|
if (found === 0) {
|
|
592
605
|
const name = opts.name ?? defaultName();
|
|
593
606
|
const doc = createIdentityDoc(ik, { ver: 1, prev: null, name, relays, devices: [device.did], keyholders: [device.did], by: device.did, certs: [cert] });
|
|
594
|
-
renewPersonToken(app.home);
|
|
607
|
+
renewPersonToken(app.home, keys.spk);
|
|
608
|
+
app.identityMadeHere = true;
|
|
595
609
|
await app.state.put("keys", keys);
|
|
596
610
|
await app.state.put("self", { ...self, name });
|
|
597
611
|
app.setKeys(keys, { ...self, name });
|
|
@@ -614,7 +628,8 @@ async function restoreInto(app, phrase, opts) {
|
|
|
614
628
|
const existing = await app.refreshIdentity(ik.userId, relays, { force: true, strict: true });
|
|
615
629
|
if (!existing)
|
|
616
630
|
throw new CliError("refresh_failed", "couldn't fit the relays' identity document");
|
|
617
|
-
renewPersonToken(app.home);
|
|
631
|
+
renewPersonToken(app.home, keys.spk);
|
|
632
|
+
app.identityMadeHere = true;
|
|
618
633
|
await app.state.put("keys", keys);
|
|
619
634
|
await app.store.putDeviceCert({ userId: ik.userId, did: device.did, exp: verifyDeviceCertV3(cert).exp, jws: cert });
|
|
620
635
|
await app.state.put("self", { ...self, name: existing.head.name, relays: existing.head.relays });
|
package/dist/pretty.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// NO_COLOR, TERM=dumb (and FORCE_COLOR) decide `color` (tui/terminal.ts colorWanted).
|
|
12
12
|
import { COLOR, PLAIN } from "./prompt/picker.js";
|
|
13
13
|
import { cell, clean, displayWidth } from "./tui/sanitize.js";
|
|
14
|
+
import { shortCode } from "./format.js";
|
|
14
15
|
import { DATA_FLOW_HEADING } from "./doctor.js";
|
|
15
16
|
const styleOf = (p) => (p.color ? COLOR : PLAIN);
|
|
16
17
|
const nowOf = (p) => p.now ?? Date.now() / 1000;
|
|
@@ -144,7 +145,7 @@ export function prettyCodes(rows, p) {
|
|
|
144
145
|
return [
|
|
145
146
|
heading(`Invites (${rows.length})`, st),
|
|
146
147
|
table(["CODE", "FOR", "JOINS", "STATUS"], rows.map((r) => [
|
|
147
|
-
t(r.status === "open" || r.status === "taken" ? r.code : r.code
|
|
148
|
+
t((r.status === "open" || r.status === "taken") && !r.redacted ? r.code : shortCode(r.code), 44, st.dim),
|
|
148
149
|
t(r.to ? clean(r.to.address) : (r.label ?? "anyone"), 28),
|
|
149
150
|
t(r.circles.length ? r.circles.map((c) => clean(c.name)).join(", ") : "friend only", 32),
|
|
150
151
|
status(r),
|
package/dist/projects.d.ts
CHANGED
|
@@ -317,25 +317,67 @@ export declare function agentEnv(env: Record<string, string | undefined> | undef
|
|
|
317
317
|
/** Whether this call runs on an agent's behalf: its environment, or a Claude Code or Codex ancestor (ancestry.ts). */
|
|
318
318
|
export declare function agentContext(env: Record<string, string | undefined> | undefined): boolean;
|
|
319
319
|
export { isAgentProcess };
|
|
320
|
-
/**
|
|
320
|
+
/**
|
|
321
|
+
* `<home>/cli/person.key`: the secret the person's front ends (the Claude mod, the Codex bridge)
|
|
322
|
+
* pass back as P2P_PERSON_TOKEN. On a home with an identity it is derived, never just stored: an
|
|
323
|
+
* HMAC under this device's signing key over the home's salt (`person.salt`, replaced whenever an
|
|
324
|
+
* identity is created or restored here), so a person.key that anything else wrote never verifies
|
|
325
|
+
* (personTokenOk recomputes it). On a fresh home (no identity yet) it is a random value, as the
|
|
326
|
+
* front ends make it on a first run.
|
|
327
|
+
*/
|
|
321
328
|
export declare function personTokenFile(home: string): string;
|
|
322
|
-
/**
|
|
329
|
+
/** `<home>/cli/person.salt`: the per-home salt of the derived person token (replaced with the identity). */
|
|
330
|
+
export declare function personSaltFile(home: string): string;
|
|
331
|
+
/**
|
|
332
|
+
* Whether the files the person token rests on are this home's own: the `cli` folder, its keys and
|
|
333
|
+
* self documents, the token and its salt, and the `v3` store are no symbolic links, and the keys
|
|
334
|
+
* and self documents have one link each (the CLI writes them by rename, so a second hard link was
|
|
335
|
+
* made by something else). A home put together from links to another home's files (to pair that
|
|
336
|
+
* identity's keys with a token of the maker's choosing) is never the person's.
|
|
337
|
+
*/
|
|
338
|
+
export declare function homeFilesOwn(home: string): boolean;
|
|
339
|
+
/**
|
|
340
|
+
* Whether `token` is the one the identity keys `spk` derive in `home` (its salt), and the home's
|
|
341
|
+
* files are its own. Run against the keys a command actually loaded (App.load), so a home whose
|
|
342
|
+
* files changed after the token was checked never lends the token to other keys.
|
|
343
|
+
*/
|
|
344
|
+
export declare function personTokenMatchesKeys(home: string, spk: string, token: string): boolean;
|
|
345
|
+
/**
|
|
346
|
+
* The person token, made or brought up to date in the CLI's own folder (0600; the mod's guards keep
|
|
347
|
+
* the model from it). With an identity here: the derived token, written to person.key when the
|
|
348
|
+
* file holds anything else (a home from before derived tokens, a front end's random first-run
|
|
349
|
+
* value), never into a home whose files aren't its own (homeFilesOwn: "" then). Without one: the
|
|
350
|
+
* random first-run token, made once, exclusively.
|
|
351
|
+
*/
|
|
323
352
|
export declare function personToken(home: string): string;
|
|
324
353
|
/**
|
|
325
354
|
* Replaces the person token with a fresh one. Called whenever an identity is first created or
|
|
326
|
-
* restored on a home (init, link join, backup restore; setup and join create through init
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
|
|
334
|
-
|
|
355
|
+
* restored on a home (init, link join, backup restore; setup and join create through init; the new
|
|
356
|
+
* home a rotation makes), with the new identity's device signing seed (`spk`): a fresh salt, and
|
|
357
|
+
* the token it derives. A person.key that was already there may have been put there by anything
|
|
358
|
+
* that could write the folder before the identity existed, so it must not keep standing in for the
|
|
359
|
+
* person afterwards (it never verifies against the derived token). The command that creates the
|
|
360
|
+
* identity checked its token when it started, so a front end's first run still works; the front
|
|
361
|
+
* ends read person.key again when the CLI refuses their old token (`person_token_stale`). Written
|
|
362
|
+
* whole, before any key is written: if this fails, no identity is made here.
|
|
363
|
+
*/
|
|
364
|
+
export declare function renewPersonToken(home: string, spk?: string): string;
|
|
335
365
|
/** Whether `home` holds an identity: its keys and self documents (state.ts keeps them at <home>/cli/keys.json and self.json). */
|
|
336
366
|
export declare function identityHere(home: string): boolean;
|
|
337
|
-
/**
|
|
367
|
+
/**
|
|
368
|
+
* Whether P2P_PERSON_TOKEN is this home's person token (read only). It must be what person.key
|
|
369
|
+
* holds (a missing file is no match) and, on a home with an identity, the token that identity
|
|
370
|
+
* derives (recomputed here: a person.key written by anything else never verifies). Never on a home
|
|
371
|
+
* whose files aren't its own (homeFilesOwn).
|
|
372
|
+
*/
|
|
338
373
|
export declare function personTokenOk(env: Record<string, string | undefined> | undefined, home: string | undefined): boolean;
|
|
374
|
+
/**
|
|
375
|
+
* After a refused token: brings a person.key that holds something else (a front end's first-run
|
|
376
|
+
* value on a home that has an identity now, one from before derived tokens) up to the derived token,
|
|
377
|
+
* so the front end that reads it again gets one that verifies. A missing person.key stays missing
|
|
378
|
+
* (the next command makes it), and nothing is written into a home whose files aren't its own.
|
|
379
|
+
*/
|
|
380
|
+
export declare function healPersonToken(home: string): void;
|
|
339
381
|
/**
|
|
340
382
|
* Checks the person token once, at the start of a command, and takes it out of the environment
|
|
341
383
|
* (this object and the process's), so no child (git, ix, an extension's tool) ever inherits it.
|
|
@@ -366,6 +408,10 @@ export declare function assertSpeaksFor(app: App, sid: string): Promise<void>;
|
|
|
366
408
|
export declare function personHere(app: Pick<App, "env"> & {
|
|
367
409
|
front?: boolean;
|
|
368
410
|
}): boolean;
|
|
411
|
+
/** personHere for a person-only act: an unreadable process tree counts as an agent's session (fail closed). */
|
|
412
|
+
export declare function personHereStrict(app: Pick<App, "env"> & {
|
|
413
|
+
front?: boolean;
|
|
414
|
+
}): boolean;
|
|
369
415
|
export declare function sessionBinding(app: App, sid: string): Promise<SessionBinding | undefined>;
|
|
370
416
|
/** One reason a session is private, as said to the person (and to a model that reads a refusal). */
|
|
371
417
|
export interface PrivateReason {
|
package/dist/projects.js
CHANGED
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
// session (messages, extension kinds, offline copies) are refused to anyone outside it.
|
|
21
21
|
// - `p2p tui` and `p2p send` typed at a terminal with no --from-session are the person's own
|
|
22
22
|
// session: never bound, no ceiling.
|
|
23
|
-
import { agentAncestor, isAgentProcess } from "./ancestry.js";
|
|
24
|
-
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
23
|
+
import { agentAncestor, ancestryUnknown, ancestryWhy, isAgentProcess } from "./ancestry.js";
|
|
24
|
+
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
25
25
|
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
26
26
|
import { homedir, tmpdir } from "node:os";
|
|
27
27
|
import { basename, delimiter, dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
@@ -1015,57 +1015,98 @@ export function agentContext(env) {
|
|
|
1015
1015
|
return agentEnv(env) || agentAncestor();
|
|
1016
1016
|
}
|
|
1017
1017
|
export { isAgentProcess };
|
|
1018
|
-
/**
|
|
1018
|
+
/**
|
|
1019
|
+
* `<home>/cli/person.key`: the secret the person's front ends (the Claude mod, the Codex bridge)
|
|
1020
|
+
* pass back as P2P_PERSON_TOKEN. On a home with an identity it is derived, never just stored: an
|
|
1021
|
+
* HMAC under this device's signing key over the home's salt (`person.salt`, replaced whenever an
|
|
1022
|
+
* identity is created or restored here), so a person.key that anything else wrote never verifies
|
|
1023
|
+
* (personTokenOk recomputes it). On a fresh home (no identity yet) it is a random value, as the
|
|
1024
|
+
* front ends make it on a first run.
|
|
1025
|
+
*/
|
|
1019
1026
|
export function personTokenFile(home) {
|
|
1020
1027
|
return join(home, "cli", "person.key");
|
|
1021
1028
|
}
|
|
1029
|
+
/** `<home>/cli/person.salt`: the per-home salt of the derived person token (replaced with the identity). */
|
|
1030
|
+
export function personSaltFile(home) {
|
|
1031
|
+
return join(home, "cli", "person.salt");
|
|
1032
|
+
}
|
|
1022
1033
|
const TOKEN_RE = /^[A-Za-z0-9_-]{43}$/;
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1034
|
+
const TOKEN_LABEL = "peer2peer/person-token/v1\0";
|
|
1035
|
+
/** The token for `spk` (a device's b64u signing seed) and `salt`: base64url of HMAC-SHA256, 43 characters. */
|
|
1036
|
+
function deriveToken(spk, salt) {
|
|
1037
|
+
return createHmac("sha256", Buffer.from(spk, "base64url")).update(TOKEN_LABEL).update(salt).digest("base64url");
|
|
1038
|
+
}
|
|
1039
|
+
function readTrim(f) {
|
|
1040
|
+
try {
|
|
1041
|
+
return readFileSync(f, "utf8").trim();
|
|
1042
|
+
}
|
|
1043
|
+
catch {
|
|
1044
|
+
return undefined;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Whether the files the person token rests on are this home's own: the `cli` folder, its keys and
|
|
1049
|
+
* self documents, the token and its salt, and the `v3` store are no symbolic links, and the keys
|
|
1050
|
+
* and self documents have one link each (the CLI writes them by rename, so a second hard link was
|
|
1051
|
+
* made by something else). A home put together from links to another home's files (to pair that
|
|
1052
|
+
* identity's keys with a token of the maker's choosing) is never the person's.
|
|
1053
|
+
*/
|
|
1054
|
+
export function homeFilesOwn(home) {
|
|
1055
|
+
const cli = join(home, "cli");
|
|
1056
|
+
try {
|
|
1057
|
+
const d = lstatSync(cli);
|
|
1058
|
+
if (d.isSymbolicLink() || !d.isDirectory())
|
|
1059
|
+
return false;
|
|
1060
|
+
}
|
|
1061
|
+
catch (e) {
|
|
1062
|
+
return e.code === "ENOENT";
|
|
1063
|
+
}
|
|
1064
|
+
const checks = [
|
|
1065
|
+
[join(cli, "keys.json"), true],
|
|
1066
|
+
[join(cli, "self.json"), true],
|
|
1067
|
+
[personTokenFile(home), false],
|
|
1068
|
+
[personSaltFile(home), false],
|
|
1069
|
+
[join(home, "v3"), false],
|
|
1070
|
+
];
|
|
1071
|
+
for (const [p, single] of checks) {
|
|
1072
|
+
let st;
|
|
1027
1073
|
try {
|
|
1028
|
-
|
|
1029
|
-
return TOKEN_RE.test(t) ? t : undefined;
|
|
1074
|
+
st = lstatSync(p);
|
|
1030
1075
|
}
|
|
1031
|
-
catch {
|
|
1032
|
-
|
|
1076
|
+
catch (e) {
|
|
1077
|
+
if (e.code === "ENOENT")
|
|
1078
|
+
continue;
|
|
1079
|
+
return false;
|
|
1033
1080
|
}
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1081
|
+
if (st.isSymbolicLink())
|
|
1082
|
+
return false;
|
|
1083
|
+
if (single && st.nlink !== 1)
|
|
1084
|
+
return false;
|
|
1085
|
+
}
|
|
1086
|
+
return true;
|
|
1087
|
+
}
|
|
1088
|
+
/** The person token this home's identity derives (undefined: no identity, no salt yet, or keys that can't be read). */
|
|
1089
|
+
function derivedToken(home) {
|
|
1090
|
+
if (!identityHere(home))
|
|
1091
|
+
return undefined;
|
|
1092
|
+
const salt = readTrim(personSaltFile(home));
|
|
1093
|
+
if (!salt || !TOKEN_RE.test(salt))
|
|
1094
|
+
return undefined;
|
|
1095
|
+
let spk;
|
|
1040
1096
|
try {
|
|
1041
|
-
|
|
1042
|
-
return t;
|
|
1097
|
+
spk = JSON.parse(readFileSync(join(home, "cli", "keys.json"), "utf8")).spk;
|
|
1043
1098
|
}
|
|
1044
1099
|
catch {
|
|
1045
|
-
|
|
1046
|
-
const again = read();
|
|
1047
|
-
if (again)
|
|
1048
|
-
return again;
|
|
1049
|
-
throw new CliError("conflict", `${f} isn't a person token: remove it and run the command again`);
|
|
1100
|
+
return undefined;
|
|
1050
1101
|
}
|
|
1102
|
+
return typeof spk === "string" && spk !== "" ? deriveToken(spk, salt) : undefined;
|
|
1051
1103
|
}
|
|
1052
|
-
/**
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
* person.key that was already there may have been put there by anything that could write the
|
|
1056
|
-
* folder before the identity existed, so it must not keep standing in for the person afterwards.
|
|
1057
|
-
* The command that creates the identity checked its token when it started, so a front end's first
|
|
1058
|
-
* run still works; the front ends read person.key again when the CLI refuses their old token
|
|
1059
|
-
* (`person_token_stale`). Written whole: a new 0600 file made exclusively beside it, then renamed
|
|
1060
|
-
* over it (a planted symlink is replaced, never followed).
|
|
1061
|
-
*/
|
|
1062
|
-
export function renewPersonToken(home) {
|
|
1063
|
-
const f = personTokenFile(home);
|
|
1064
|
-
mkdirSync(join(home, "cli"), { recursive: true, mode: 0o700 });
|
|
1065
|
-
const t = randomBytes(32).toString("base64url");
|
|
1104
|
+
/** Writes `text` to `f` whole: a new 0600 file made exclusively beside it, renamed over it (a planted symlink is replaced, not followed). */
|
|
1105
|
+
function writeWhole(f, text) {
|
|
1106
|
+
mkdirSync(dirname(f), { recursive: true, mode: 0o700 });
|
|
1066
1107
|
const tmp = `${f}.${randomBytes(6).toString("hex")}.tmp`;
|
|
1067
1108
|
try {
|
|
1068
|
-
writeFileSync(tmp,
|
|
1109
|
+
writeFileSync(tmp, text, { mode: 0o600, flag: "wx" });
|
|
1069
1110
|
let cleared = false;
|
|
1070
1111
|
for (let tries = 0;; tries++) {
|
|
1071
1112
|
try {
|
|
@@ -1073,7 +1114,7 @@ export function renewPersonToken(home) {
|
|
|
1073
1114
|
break;
|
|
1074
1115
|
}
|
|
1075
1116
|
catch (e) {
|
|
1076
|
-
// Something that isn't a file is in the way (a directory): it goes, then the new
|
|
1117
|
+
// Something that isn't a file is in the way (a directory): it goes, then the new file.
|
|
1077
1118
|
if (!cleared && isDirectory(f)) {
|
|
1078
1119
|
rmSync(f, { recursive: true, force: true });
|
|
1079
1120
|
cleared = true;
|
|
@@ -1090,7 +1131,104 @@ export function renewPersonToken(home) {
|
|
|
1090
1131
|
}
|
|
1091
1132
|
catch (e) {
|
|
1092
1133
|
rmSync(tmp, { force: true });
|
|
1093
|
-
throw
|
|
1134
|
+
throw e;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* The home's salt, made once: exclusively when there is none (two first runs at once agree on
|
|
1139
|
+
* the one that was made first), replaced whole when what is there isn't a salt.
|
|
1140
|
+
*/
|
|
1141
|
+
function ensureSalt(home) {
|
|
1142
|
+
const f = personSaltFile(home);
|
|
1143
|
+
if (TOKEN_RE.test(readTrim(f) ?? ""))
|
|
1144
|
+
return;
|
|
1145
|
+
if (!existsSync(f)) {
|
|
1146
|
+
try {
|
|
1147
|
+
writeFileSync(f, randomBytes(32).toString("base64url"), { mode: 0o600, flag: "wx" });
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
catch {
|
|
1151
|
+
// made meanwhile by another process: that one, if it is a salt
|
|
1152
|
+
if (TOKEN_RE.test(readTrim(f) ?? ""))
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
writeWhole(f, randomBytes(32).toString("base64url"));
|
|
1157
|
+
}
|
|
1158
|
+
/**
|
|
1159
|
+
* Whether `token` is the one the identity keys `spk` derive in `home` (its salt), and the home's
|
|
1160
|
+
* files are its own. Run against the keys a command actually loaded (App.load), so a home whose
|
|
1161
|
+
* files changed after the token was checked never lends the token to other keys.
|
|
1162
|
+
*/
|
|
1163
|
+
export function personTokenMatchesKeys(home, spk, token) {
|
|
1164
|
+
if (!homeFilesOwn(home))
|
|
1165
|
+
return false;
|
|
1166
|
+
const salt = readTrim(personSaltFile(home));
|
|
1167
|
+
if (!salt || !TOKEN_RE.test(salt))
|
|
1168
|
+
return false;
|
|
1169
|
+
return same(token, deriveToken(spk, salt));
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* The person token, made or brought up to date in the CLI's own folder (0600; the mod's guards keep
|
|
1173
|
+
* the model from it). With an identity here: the derived token, written to person.key when the
|
|
1174
|
+
* file holds anything else (a home from before derived tokens, a front end's random first-run
|
|
1175
|
+
* value), never into a home whose files aren't its own (homeFilesOwn: "" then). Without one: the
|
|
1176
|
+
* random first-run token, made once, exclusively.
|
|
1177
|
+
*/
|
|
1178
|
+
export function personToken(home) {
|
|
1179
|
+
const f = personTokenFile(home);
|
|
1180
|
+
if (identityHere(home)) {
|
|
1181
|
+
if (!homeFilesOwn(home))
|
|
1182
|
+
return "";
|
|
1183
|
+
ensureSalt(home);
|
|
1184
|
+
const want = derivedToken(home);
|
|
1185
|
+
if (!want)
|
|
1186
|
+
return "";
|
|
1187
|
+
if (readTrim(f) !== want)
|
|
1188
|
+
writeWhole(f, want);
|
|
1189
|
+
return want;
|
|
1190
|
+
}
|
|
1191
|
+
const read = () => {
|
|
1192
|
+
const t = readTrim(f);
|
|
1193
|
+
return t && TOKEN_RE.test(t) ? t : undefined;
|
|
1194
|
+
};
|
|
1195
|
+
const have = read();
|
|
1196
|
+
if (have)
|
|
1197
|
+
return have;
|
|
1198
|
+
mkdirSync(join(home, "cli"), { recursive: true, mode: 0o700 });
|
|
1199
|
+
const t = randomBytes(32).toString("base64url");
|
|
1200
|
+
try {
|
|
1201
|
+
writeFileSync(f, t, { mode: 0o600, flag: "wx" });
|
|
1202
|
+
return t;
|
|
1203
|
+
}
|
|
1204
|
+
catch {
|
|
1205
|
+
// Made meanwhile by another process (or a file in the way): that one, if it is a token.
|
|
1206
|
+
const again = read();
|
|
1207
|
+
if (again)
|
|
1208
|
+
return again;
|
|
1209
|
+
throw new CliError("conflict", `${f} isn't a person token: remove it and run the command again`);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* Replaces the person token with a fresh one. Called whenever an identity is first created or
|
|
1214
|
+
* restored on a home (init, link join, backup restore; setup and join create through init; the new
|
|
1215
|
+
* home a rotation makes), with the new identity's device signing seed (`spk`): a fresh salt, and
|
|
1216
|
+
* the token it derives. A person.key that was already there may have been put there by anything
|
|
1217
|
+
* that could write the folder before the identity existed, so it must not keep standing in for the
|
|
1218
|
+
* person afterwards (it never verifies against the derived token). The command that creates the
|
|
1219
|
+
* identity checked its token when it started, so a front end's first run still works; the front
|
|
1220
|
+
* ends read person.key again when the CLI refuses their old token (`person_token_stale`). Written
|
|
1221
|
+
* whole, before any key is written: if this fails, no identity is made here.
|
|
1222
|
+
*/
|
|
1223
|
+
export function renewPersonToken(home, spk) {
|
|
1224
|
+
const salt = randomBytes(32).toString("base64url");
|
|
1225
|
+
const t = spk ? deriveToken(spk, salt) : randomBytes(32).toString("base64url");
|
|
1226
|
+
try {
|
|
1227
|
+
writeWhole(personSaltFile(home), salt);
|
|
1228
|
+
writeWhole(personTokenFile(home), t);
|
|
1229
|
+
}
|
|
1230
|
+
catch (e) {
|
|
1231
|
+
throw new CliError("conflict", `couldn't replace the person token in ${personTokenFile(home)} (${e.code ?? "error"}): nothing was set up here. Remove that file and run the command again`);
|
|
1094
1232
|
}
|
|
1095
1233
|
return t;
|
|
1096
1234
|
}
|
|
@@ -1108,22 +1246,47 @@ export function identityHere(home) {
|
|
|
1108
1246
|
// Both documents, as App.load requires: keys alone are an identity whose creation didn't finish.
|
|
1109
1247
|
return existsSync(join(home, "cli", "keys.json")) && existsSync(join(home, "cli", "self.json"));
|
|
1110
1248
|
}
|
|
1111
|
-
/**
|
|
1249
|
+
/**
|
|
1250
|
+
* Whether P2P_PERSON_TOKEN is this home's person token (read only). It must be what person.key
|
|
1251
|
+
* holds (a missing file is no match) and, on a home with an identity, the token that identity
|
|
1252
|
+
* derives (recomputed here: a person.key written by anything else never verifies). Never on a home
|
|
1253
|
+
* whose files aren't its own (homeFilesOwn).
|
|
1254
|
+
*/
|
|
1112
1255
|
export function personTokenOk(env, home) {
|
|
1113
1256
|
const given = env?.P2P_PERSON_TOKEN?.trim();
|
|
1114
1257
|
if (!given || typeof home !== "string" || home === "")
|
|
1115
1258
|
return false;
|
|
1116
|
-
|
|
1117
|
-
try {
|
|
1118
|
-
t = readFileSync(personTokenFile(home), "utf8").trim();
|
|
1119
|
-
}
|
|
1120
|
-
catch {
|
|
1259
|
+
if (!homeFilesOwn(home))
|
|
1121
1260
|
return false;
|
|
1122
|
-
|
|
1261
|
+
const t = readTrim(personTokenFile(home));
|
|
1262
|
+
if (t === undefined || !same(given, t))
|
|
1263
|
+
return false;
|
|
1264
|
+
if (!identityHere(home))
|
|
1265
|
+
return true;
|
|
1266
|
+
const want = derivedToken(home);
|
|
1267
|
+
return want !== undefined && same(given, want);
|
|
1268
|
+
}
|
|
1269
|
+
function same(given, t) {
|
|
1123
1270
|
const a = Buffer.from(given);
|
|
1124
1271
|
const b = Buffer.from(t);
|
|
1125
1272
|
return a.length === b.length && b.length >= 32 && timingSafeEqual(a, b);
|
|
1126
1273
|
}
|
|
1274
|
+
/**
|
|
1275
|
+
* After a refused token: brings a person.key that holds something else (a front end's first-run
|
|
1276
|
+
* value on a home that has an identity now, one from before derived tokens) up to the derived token,
|
|
1277
|
+
* so the front end that reads it again gets one that verifies. A missing person.key stays missing
|
|
1278
|
+
* (the next command makes it), and nothing is written into a home whose files aren't its own.
|
|
1279
|
+
*/
|
|
1280
|
+
export function healPersonToken(home) {
|
|
1281
|
+
if (!identityHere(home) || !existsSync(personTokenFile(home)))
|
|
1282
|
+
return;
|
|
1283
|
+
try {
|
|
1284
|
+
personToken(home);
|
|
1285
|
+
}
|
|
1286
|
+
catch {
|
|
1287
|
+
// the next command tries again
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1127
1290
|
/**
|
|
1128
1291
|
* Checks the person token once, at the start of a command, and takes it out of the environment
|
|
1129
1292
|
* (this object and the process's), so no child (git, ix, an extension's tool) ever inherits it.
|
|
@@ -1154,6 +1317,11 @@ export async function assertPersonForWidening(app, what, confirm, why = "changes
|
|
|
1154
1317
|
if (agentContext(app.env)) {
|
|
1155
1318
|
throw new CliError("refused", `\`p2p ${what}\` ${why}, so only you run it: in your own terminal (not inside Claude Code or Codex), or with /peers in Claude Code. It was refused here because it runs inside an agent's session`);
|
|
1156
1319
|
}
|
|
1320
|
+
// Fail closed: a process tree that couldn't be read (no /proc, no system `ps`, a process gone
|
|
1321
|
+
// mid-walk) may be an agent's session. The person's token still works (above).
|
|
1322
|
+
if (ancestryUnknown()) {
|
|
1323
|
+
throw new CliError("refused", `\`p2p ${what}\` ${why}, so only you run it: with /peers in Claude Code, or with your person token (P2P_PERSON_TOKEN). It was refused here because p2p couldn't read its process tree to tell whether it runs inside an agent's session${ancestryWhy() ? ` (${ancestryWhy()})` : ""}`);
|
|
1324
|
+
}
|
|
1157
1325
|
if (!(await confirm(`p2p ${what} ${why}.`))) {
|
|
1158
1326
|
throw new CliError("refused", `\`p2p ${what}\` ${why}: confirm it with "yes" on your own terminal (it is read from the terminal itself, so a script or a process without one can't)`);
|
|
1159
1327
|
}
|
|
@@ -1177,6 +1345,10 @@ export async function assertSpeaksFor(app, sid) {
|
|
|
1177
1345
|
export function personHere(app) {
|
|
1178
1346
|
return app.front === true || !agentContext(app.env);
|
|
1179
1347
|
}
|
|
1348
|
+
/** personHere for a person-only act: an unreadable process tree counts as an agent's session (fail closed). */
|
|
1349
|
+
export function personHereStrict(app) {
|
|
1350
|
+
return app.front === true || (!agentContext(app.env) && !ancestryUnknown());
|
|
1351
|
+
}
|
|
1180
1352
|
export async function sessionBinding(app, sid) {
|
|
1181
1353
|
return (await app.state.get("sessionProjects"))?.[sid];
|
|
1182
1354
|
}
|
package/dist/rotation.d.ts
CHANGED
|
@@ -31,6 +31,17 @@ export interface RotationState {
|
|
|
31
31
|
accepted?: boolean;
|
|
32
32
|
/** Contesting a rotation this device didn't publish (§2.8: a conflict, so neither applies). */
|
|
33
33
|
contest?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* #75: begun on this device's own branch of a fork that a version leaving this device out made
|
|
36
|
+
* (ownBranchTip): that tip, and the name and relays the final version takes (ownBranchFinalFields).
|
|
37
|
+
* The view's allowlist then leaves this device out, so, as contesting, its stored friendships count
|
|
38
|
+
* without verifying now.
|
|
39
|
+
*/
|
|
40
|
+
ownBranch?: {
|
|
41
|
+
tip: string;
|
|
42
|
+
name: string;
|
|
43
|
+
relays: string[];
|
|
44
|
+
};
|
|
34
45
|
keepKeyholders: boolean;
|
|
35
46
|
devices: Record<string, RotationDeviceRec>;
|
|
36
47
|
/** Excluded devices revoked on the old identity before any link code went out. */
|
|
@@ -69,6 +80,11 @@ export interface RotationState {
|
|
|
69
80
|
archive?: string;
|
|
70
81
|
}
|
|
71
82
|
export declare function readRotationState(home: string): Promise<RotationState | undefined>;
|
|
83
|
+
/**
|
|
84
|
+
* After a swap: the home must now hold the identity the link joined. Anything else (files changed
|
|
85
|
+
* under the swap) keeps no person token and stops the command.
|
|
86
|
+
*/
|
|
87
|
+
export declare function assertAdopted(app: App, userId: string): void;
|
|
72
88
|
/**
|
|
73
89
|
* Makes `src` (a home holding the new identity) this home's identity, after
|
|
74
90
|
* the old identity's work here has stopped (moveHomes).
|
|
@@ -104,6 +120,8 @@ export interface RotateOptions {
|
|
|
104
120
|
backupFileDefault?: boolean;
|
|
105
121
|
/** A key holder contests a rotation it didn't publish: a competing one, so neither applies. */
|
|
106
122
|
contest?: boolean;
|
|
123
|
+
/** #75: which branch is this device's own, when ownBranchTip can't tell (its jwsHash). */
|
|
124
|
+
branch?: string;
|
|
107
125
|
/** Tests: stop after this step, as an interruption would. */
|
|
108
126
|
stopAfter?: RotationStep;
|
|
109
127
|
}
|
|
@@ -200,7 +218,8 @@ export interface RotateResult {
|
|
|
200
218
|
* friendship whose our-side signature comes from this device or a device
|
|
201
219
|
* that stays in the plan counts (a thief's device can plant friendships with
|
|
202
220
|
* accomplices); rotating normally it must also verify now, while a contest
|
|
203
|
-
*
|
|
221
|
+
* (or a rotation from this device's own branch of a fork, #75) also takes
|
|
222
|
+
* stored ones that don't verify now. Anything else
|
|
204
223
|
* is shown in the plan, invited only when the user opts in.
|
|
205
224
|
*/
|
|
206
225
|
export declare function friendVerdict(st: RotationState, peer: string): {
|