typevault 0.1.0 → 0.3.0
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 +156 -115
- package/package.json +14 -2
- package/src/access/env.ts +77 -17
- package/src/access/keys.ts +14 -14
- package/src/access/schemas.ts +6 -12
- package/src/access/scope.ts +14 -9
- package/src/access/scoped.ts +118 -44
- package/src/access/sealed-access.ts +50 -23
- package/src/access/sealed.ts +56 -31
- package/src/access/types.ts +27 -30
- package/src/admin.ts +68 -0
- package/src/audit/log.ts +28 -12
- package/src/cli/commands/admin.ts +93 -48
- package/src/cli/commands/daemon.ts +42 -17
- package/src/cli/commands/entries.ts +195 -60
- package/src/cli/commands/grant.ts +24 -22
- package/src/cli/commands/members.ts +307 -0
- package/src/cli/commands/run.ts +191 -44
- package/src/cli/context.ts +163 -15
- package/src/cli/index.ts +33 -5
- package/src/cli/password.ts +74 -0
- package/src/cli/web-prompt.ts +79 -0
- package/src/core/admin.ts +144 -76
- package/src/core/adopt.ts +40 -9
- package/src/core/crypto.ts +6 -4
- package/src/core/doctor.ts +15 -16
- package/src/core/entries.ts +66 -52
- package/src/core/errors.ts +8 -2
- package/src/core/fsx.ts +7 -3
- package/src/core/holder.ts +87 -0
- package/src/core/identity.ts +132 -0
- package/src/core/init.ts +73 -23
- package/src/core/json-file.ts +20 -0
- package/src/core/keyring.ts +100 -56
- package/src/core/layout.ts +143 -26
- package/src/core/lock.ts +128 -16
- package/src/core/naming.ts +150 -50
- package/src/core/password.ts +111 -0
- package/src/core/payload.ts +42 -38
- package/src/core/revisions.ts +11 -6
- package/src/core/rotate.ts +59 -42
- package/src/core/state.ts +102 -54
- package/src/core/txn.ts +9 -7
- package/src/core/vault.ts +108 -46
- package/src/core/worktrees.ts +51 -0
- package/src/daemon/admin-routes.ts +33 -21
- package/src/daemon/agent-routes.ts +31 -11
- package/src/daemon/client.ts +24 -2
- package/src/daemon/daemon.ts +218 -33
- package/src/daemon/grants.ts +25 -7
- package/src/daemon/http.ts +11 -2
- package/src/daemon/live-access.ts +8 -4
- package/src/daemon/protocol.ts +17 -12
- package/src/daemon/requests.ts +75 -51
- package/src/daemon/spawn.ts +218 -9
- package/src/export/1password.ts +157 -0
- package/src/import/1password-live.ts +399 -0
- package/src/import/1password.ts +26 -5
- package/src/import/bitwarden.ts +3 -3
- package/src/import/csv.ts +1 -1
- package/src/import/dotenv.ts +4 -3
- package/src/import/index.ts +1 -0
- package/src/import/keepass.ts +2 -2
- package/src/import/lastpass.ts +2 -2
- package/src/import/shared.ts +67 -10
- package/src/import/typeclaw.ts +2 -2
- package/src/import/types.ts +1 -1
- package/src/index.ts +58 -0
- package/src/owner.ts +156 -0
- package/src/prompt/browser.ts +48 -15
- package/src/prompt/index.ts +121 -64
- package/src/prompt/page.ts +27 -11
- package/src/prompt/styles.ts +64 -34
- package/src/prompt/types.ts +7 -4
- package/src/redact/index.ts +48 -12
- package/src/redact/scrub-file.ts +15 -7
package/src/core/admin.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { encodeSecretKey, generateSecretKey, publicKeyOf, seal, type SecretKey } from './crypto'
|
|
2
|
-
import { utf8 } from './encoding'
|
|
1
|
+
import { encodeSecretKey, generateSecretKey, type PublicKey, publicKeyOf, seal, type SecretKey } from './crypto'
|
|
2
|
+
import { fromBase64, utf8 } from './encoding'
|
|
3
3
|
import { signerOf } from './entries'
|
|
4
4
|
import { TypeVaultError } from './errors'
|
|
5
|
-
import { wrapPath } from './keyring'
|
|
5
|
+
import { ownerWrapPath, wrapPath } from './keyring'
|
|
6
6
|
import { withLock } from './lock'
|
|
7
|
-
import {
|
|
7
|
+
import { rotateTiers } from './rotate'
|
|
8
8
|
import {
|
|
9
9
|
ceilingsDoc,
|
|
10
|
-
type
|
|
11
|
-
|
|
10
|
+
type MemberKind,
|
|
11
|
+
membersDoc,
|
|
12
|
+
type Tier,
|
|
13
|
+
tiersDoc,
|
|
12
14
|
includersOf,
|
|
13
|
-
|
|
15
|
+
parseMemberName,
|
|
16
|
+
parseTierName,
|
|
14
17
|
reach,
|
|
15
18
|
signersDoc,
|
|
16
19
|
type VaultState,
|
|
@@ -18,16 +21,26 @@ import {
|
|
|
18
21
|
import { commitTxn, Txn } from './txn'
|
|
19
22
|
import type { Vault } from './vault'
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
export type NewMember = PublicKey & {
|
|
25
|
+
readonly kind: MemberKind
|
|
26
|
+
readonly owner: boolean
|
|
27
|
+
readonly tiers: readonly string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Every tier `tiers` reach: rotating all of them keeps old generations out of a member's hands. */
|
|
31
|
+
const reachOf = (state: VaultState, tiers: readonly string[]): readonly string[] =>
|
|
32
|
+
[...new Set(tiers.flatMap((tier) => [...reach(state.tiers, tier)]))].sort()
|
|
33
|
+
|
|
34
|
+
/** Owner-only changes to tiers, members, signers and ceilings. Each one lands as a single transaction. */
|
|
22
35
|
export class Admin {
|
|
23
36
|
readonly #vault: Vault
|
|
24
|
-
readonly #
|
|
37
|
+
readonly #owner: SecretKey
|
|
25
38
|
|
|
26
39
|
constructor(vault: Vault) {
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
40
|
+
const owner = vault.keyring.owner
|
|
41
|
+
if (owner === undefined) throw new TypeVaultError('denied', 'this needs the owner key')
|
|
29
42
|
this.#vault = vault
|
|
30
|
-
this.#
|
|
43
|
+
this.#owner = owner
|
|
31
44
|
}
|
|
32
45
|
|
|
33
46
|
#mutate(build: (state: VaultState, txn: Txn) => Promise<void>): Promise<void> {
|
|
@@ -40,127 +53,182 @@ export class Admin {
|
|
|
40
53
|
})
|
|
41
54
|
}
|
|
42
55
|
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const meta = state.groups[included]
|
|
46
|
-
if (key === undefined || meta === undefined)
|
|
47
|
-
throw new TypeVaultError('not_found', `cannot unlock group "${included}"`)
|
|
48
|
-
const sealed = await seal([holder.group.recipient], utf8(encodeSecretKey(key)))
|
|
49
|
-
txn.put(wrapPath(included, meta.gen, { kind: 'group', group: holder.name, gen: holder.group.gen }), sealed)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
addGroup(rawName: string): Promise<void> {
|
|
53
|
-
const name = parseGroupName(rawName)
|
|
56
|
+
addTier(rawName: string): Promise<void> {
|
|
57
|
+
const name = parseTierName(rawName)
|
|
54
58
|
return this.#mutate(async (state, txn) => {
|
|
55
|
-
if (state.
|
|
56
|
-
const key = await generateSecretKey({ kind: '
|
|
57
|
-
const
|
|
58
|
-
txn.put(
|
|
59
|
-
|
|
60
|
-
await seal([state.operator.recipient], utf8(encodeSecretKey(key))),
|
|
61
|
-
)
|
|
62
|
-
txn.put('groups.json', groupsDoc({ ...state.groups, [name]: group }, this.#operator, state.seqs.groups + 1))
|
|
59
|
+
if (state.tiers[name] !== undefined) throw new TypeVaultError('conflict', `tier "${name}" already exists`)
|
|
60
|
+
const key = await generateSecretKey({ kind: 'tier', tier: name, gen: 1 })
|
|
61
|
+
const tier: Tier = { includes: [], gen: 1, ...(await publicKeyOf(key)) }
|
|
62
|
+
txn.put(wrapPath(name, 1, { kind: 'owner' }), await seal([state.owner.recipient], utf8(encodeSecretKey(key))))
|
|
63
|
+
txn.put('tiers.json', tiersDoc({ ...state.tiers, [name]: tier }, this.#owner, state.seqs.tiers + 1))
|
|
63
64
|
})
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
|
|
67
|
+
removeTier(name: string): Promise<void> {
|
|
67
68
|
return this.#mutate(async (state, txn) => {
|
|
68
|
-
if (state.
|
|
69
|
-
if (includersOf(state.
|
|
70
|
-
throw new TypeVaultError('conflict', `other
|
|
69
|
+
if (state.tiers[name] === undefined) throw new TypeVaultError('not_found', `no tier "${name}"`)
|
|
70
|
+
if (includersOf(state.tiers, name).length > 0)
|
|
71
|
+
throw new TypeVaultError('conflict', `other tiers include "${name}"`)
|
|
72
|
+
const holders = Object.entries(state.members).filter(([, member]) => member.tiers.includes(name))
|
|
73
|
+
if (holders.length > 0) {
|
|
74
|
+
const names = holders.map(([member]) => member).join(', ')
|
|
75
|
+
throw new TypeVaultError('conflict', `members still hold "${name}": ${names}`)
|
|
76
|
+
}
|
|
71
77
|
const { entries } = await this.#vault.scan()
|
|
72
|
-
if (entries.some((entry) => entry.envelope.
|
|
73
|
-
throw new TypeVaultError('conflict', `
|
|
78
|
+
if (entries.some((entry) => entry.envelope.tier === name)) {
|
|
79
|
+
throw new TypeVaultError('conflict', `tier "${name}" still has entries`)
|
|
74
80
|
}
|
|
75
|
-
const { [name]: _removed, ...rest } = state.
|
|
76
|
-
for (const included of state.
|
|
77
|
-
const gen = state.
|
|
78
|
-
txn.remove(wrapPath(included, gen, { kind: '
|
|
81
|
+
const { [name]: _removed, ...rest } = state.tiers
|
|
82
|
+
for (const included of state.tiers[name]?.includes ?? []) {
|
|
83
|
+
const gen = state.tiers[included]?.gen ?? 0
|
|
84
|
+
txn.remove(wrapPath(included, gen, { kind: 'tier', tier: name, gen: state.tiers[name]?.gen ?? 0 }))
|
|
79
85
|
}
|
|
80
|
-
txn.remove(`
|
|
81
|
-
txn.put('
|
|
86
|
+
txn.remove(`tiers/${name}`)
|
|
87
|
+
txn.put('tiers.json', tiersDoc(rest, this.#owner, state.seqs.tiers + 1))
|
|
82
88
|
})
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Makes `tier` include `included`, rotating every tier `included` reaches in the same transaction, so the includer
|
|
93
|
+
* can open neither their older ciphertext in git history nor entries deleted before the include.
|
|
94
|
+
*/
|
|
95
|
+
include(tier: string, included: string): Promise<void> {
|
|
86
96
|
return this.#mutate(async (state, txn) => {
|
|
87
|
-
const holder = state.
|
|
88
|
-
if (holder === undefined || state.
|
|
89
|
-
throw new TypeVaultError('not_found', 'no such
|
|
97
|
+
const holder = state.tiers[tier]
|
|
98
|
+
if (holder === undefined || state.tiers[included] === undefined)
|
|
99
|
+
throw new TypeVaultError('not_found', 'no such tier')
|
|
90
100
|
if (holder.includes.includes(included)) return
|
|
91
|
-
if (reach(state.
|
|
92
|
-
throw new TypeVaultError('invalid', `"${included}" already reaches "${
|
|
101
|
+
if (reach(state.tiers, included).has(tier)) {
|
|
102
|
+
throw new TypeVaultError('invalid', `"${included}" already reaches "${tier}"; including it would form a cycle`)
|
|
93
103
|
}
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
104
|
+
const tiers = { ...state.tiers, [tier]: { ...holder, includes: [...holder.includes, included] } }
|
|
105
|
+
const context = { vault: this.#vault, owner: this.#owner, state: { ...state, tiers }, txn, exposed: false }
|
|
106
|
+
await rotateTiers(context, reachOf(state, [included]))
|
|
97
107
|
})
|
|
98
108
|
}
|
|
99
109
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Drops an include and rotates the included tier in one transaction: a crash leaves both or neither, never a
|
|
112
|
+
* dropped include whose old wrap still opens the included tier.
|
|
113
|
+
*/
|
|
114
|
+
async exclude(tier: string, included: string): Promise<readonly string[]> {
|
|
115
|
+
let affected: readonly string[] = []
|
|
102
116
|
await this.#mutate(async (state, txn) => {
|
|
103
|
-
const holder = state.
|
|
117
|
+
const holder = state.tiers[tier]
|
|
104
118
|
if (holder === undefined || !holder.includes.includes(included)) {
|
|
105
|
-
throw new TypeVaultError('not_found', `"${
|
|
119
|
+
throw new TypeVaultError('not_found', `"${tier}" does not include "${included}"`)
|
|
106
120
|
}
|
|
107
|
-
const meta = state.groups[included]
|
|
108
|
-
txn.remove(wrapPath(included, meta?.gen ?? 0, { kind: 'group', group, gen: holder.gen }))
|
|
109
121
|
const updated = { ...holder, includes: holder.includes.filter((name) => name !== included) }
|
|
110
|
-
|
|
122
|
+
// Rotating removes the included tier's old generation, the dropped wrap with it, and rewrites tiers.json.
|
|
123
|
+
const context = { vault: this.#vault, owner: this.#owner, txn, exposed: true }
|
|
124
|
+
affected = await rotateTiers({ ...context, state: { ...state, tiers: { ...state.tiers, [tier]: updated } } }, [
|
|
125
|
+
included,
|
|
126
|
+
])
|
|
111
127
|
})
|
|
112
|
-
return
|
|
128
|
+
return affected
|
|
113
129
|
}
|
|
114
130
|
|
|
115
|
-
/**
|
|
131
|
+
/** Rotates every tier `name` reaches. Returns their entry names, now flagged for rotation at the source. */
|
|
116
132
|
async rotate(name: string): Promise<readonly string[]> {
|
|
117
133
|
let affected: readonly string[] = []
|
|
118
134
|
await this.#mutate(async (state, txn) => {
|
|
119
|
-
affected = await
|
|
135
|
+
affected = await rotateTiers(
|
|
136
|
+
{ vault: this.#vault, owner: this.#owner, state, txn, exposed: true },
|
|
137
|
+
reachOf(state, [name]),
|
|
138
|
+
)
|
|
120
139
|
})
|
|
121
140
|
return affected
|
|
122
141
|
}
|
|
123
142
|
|
|
124
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Lists a member and wraps the owner key (for owners) and their tiers' keys to them. Every tier they reach is
|
|
145
|
+
* rotated in the same transaction, so they can open neither older ciphertext in git history nor entries deleted
|
|
146
|
+
* before they joined.
|
|
147
|
+
*/
|
|
148
|
+
addMember(rawName: string, member: NewMember): Promise<void> {
|
|
149
|
+
const name = parseMemberName(rawName)
|
|
125
150
|
return this.#mutate(async (state, txn) => {
|
|
151
|
+
if (state.members[name] !== undefined) throw new TypeVaultError('conflict', `member "${name}" already exists`)
|
|
152
|
+
const unknown = member.tiers.find((tier) => state.tiers[tier] === undefined)
|
|
153
|
+
if (unknown !== undefined) throw new TypeVaultError('not_found', `no tier "${unknown}"`)
|
|
154
|
+
const { kind, recipient, signPublic, owner, tiers } = member
|
|
155
|
+
const members = { ...state.members, [name]: { kind, recipient, signPublic, owner, tiers: [...tiers] } }
|
|
156
|
+
if (owner) txn.put(ownerWrapPath(name), await seal([recipient], utf8(encodeSecretKey(this.#owner))))
|
|
157
|
+
txn.put('members.json', membersDoc(members, this.#owner, state.seqs.members + 1))
|
|
158
|
+
const context = { vault: this.#vault, owner: this.#owner, state: { ...state, members }, txn, exposed: false }
|
|
159
|
+
await rotateTiers(context, reachOf(state, tiers))
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Drops a member and rotates every tier they reached, in one transaction: a crash leaves both or neither, never a
|
|
165
|
+
* removed member whose keys still open new ciphertext. Returns the entries they could read, now flagged for
|
|
166
|
+
* rotation: what they already decrypted can only be revoked at each secret's source.
|
|
167
|
+
*/
|
|
168
|
+
async removeMember(name: string): Promise<readonly string[]> {
|
|
169
|
+
let affected: readonly string[] = []
|
|
170
|
+
await this.#mutate(async (state, txn) => {
|
|
171
|
+
const member = state.members[name]
|
|
172
|
+
if (member === undefined) throw new TypeVaultError('not_found', `no member "${name}"`)
|
|
173
|
+
// Every entry is also sealed to the owner key, which can't be rotated yet: a former owner would keep reading.
|
|
174
|
+
if (member.owner) throw new TypeVaultError('invalid', `"${name}" is an owner; removing an owner is not supported`)
|
|
175
|
+
const { [name]: _removed, ...rest } = state.members
|
|
176
|
+
txn.remove(`members/${name}.key`)
|
|
177
|
+
txn.put('members.json', membersDoc(rest, this.#owner, state.seqs.members + 1))
|
|
178
|
+
const context = { vault: this.#vault, owner: this.#owner, state: { ...state, members: rest }, txn, exposed: true }
|
|
179
|
+
affected = await rotateTiers(context, reachOf(state, member.tiers))
|
|
180
|
+
})
|
|
181
|
+
return affected
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async setSigner(id: string, signPublic: string, tiers: readonly string[]): Promise<void> {
|
|
185
|
+
const name = parseTierName(id)
|
|
186
|
+
let key: Uint8Array
|
|
187
|
+
try {
|
|
188
|
+
key = fromBase64(signPublic)
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (error instanceof Error) throw new TypeVaultError('invalid', 'signer public key is not valid base64')
|
|
191
|
+
throw error
|
|
192
|
+
}
|
|
193
|
+
if (key.length !== 32) throw new TypeVaultError('invalid', 'signer public key must be 32 bytes')
|
|
194
|
+
if (tiers.length === 0) throw new TypeVaultError('invalid', 'signer needs at least one tier')
|
|
195
|
+
await this.#mutate(async (state, txn) => {
|
|
196
|
+
const unknown = tiers.find((tier) => tier !== '*' && state.tiers[tier] === undefined)
|
|
197
|
+
if (unknown !== undefined) throw new TypeVaultError('not_found', `no tier "${unknown}"`)
|
|
126
198
|
txn.put(
|
|
127
199
|
'signers.json',
|
|
128
|
-
signersDoc({ ...state.signers, [
|
|
200
|
+
signersDoc({ ...state.signers, [name]: { signPublic, tiers } }, this.#owner, state.seqs.signers + 1),
|
|
129
201
|
)
|
|
130
202
|
})
|
|
131
203
|
}
|
|
132
204
|
|
|
133
205
|
removeSigner(id: string): Promise<void> {
|
|
134
206
|
return this.#mutate(async (state, txn) => {
|
|
207
|
+
if (state.signers[id] === undefined) throw new TypeVaultError('not_found', `no signer "${id}"`)
|
|
135
208
|
const { [id]: _removed, ...rest } = state.signers
|
|
136
|
-
txn.put('signers.json', signersDoc(rest, this.#
|
|
209
|
+
txn.put('signers.json', signersDoc(rest, this.#owner, state.seqs.signers + 1))
|
|
137
210
|
})
|
|
138
211
|
}
|
|
139
212
|
|
|
140
|
-
setCeiling(name: string,
|
|
213
|
+
setCeiling(name: string, tier: string): Promise<void> {
|
|
141
214
|
return this.#mutate(async (state, txn) => {
|
|
142
|
-
if (state.
|
|
215
|
+
if (state.tiers[tier] === undefined) throw new TypeVaultError('not_found', `no tier "${tier}"`)
|
|
143
216
|
txn.put(
|
|
144
217
|
'ceilings.json',
|
|
145
|
-
ceilingsDoc({ ...state.ceilings, [
|
|
218
|
+
ceilingsDoc({ ...state.ceilings, [parseTierName(name)]: tier }, this.#owner, state.seqs.ceilings + 1),
|
|
146
219
|
)
|
|
147
220
|
})
|
|
148
221
|
}
|
|
149
222
|
|
|
150
223
|
removeCeiling(name: string): Promise<void> {
|
|
151
224
|
return this.#mutate(async (state, txn) => {
|
|
225
|
+
if (state.ceilings[name] === undefined) throw new TypeVaultError('not_found', `no ceiling "${name}"`)
|
|
152
226
|
const { [name]: _removed, ...rest } = state.ceilings
|
|
153
|
-
txn.put('ceilings.json', ceilingsDoc(rest, this.#
|
|
227
|
+
txn.put('ceilings.json', ceilingsDoc(rest, this.#owner, state.seqs.ceilings + 1))
|
|
154
228
|
})
|
|
155
229
|
}
|
|
156
230
|
|
|
157
|
-
async exportKey(group: string): Promise<string> {
|
|
158
|
-
const key = await this.#vault.keyring.unlock(group)
|
|
159
|
-
if (key === undefined) throw new TypeVaultError('not_found', `no group "${group}"`)
|
|
160
|
-
return encodeSecretKey(key)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
231
|
get signer(): ReturnType<typeof signerOf> {
|
|
164
|
-
return signerOf(this.#
|
|
232
|
+
return signerOf(this.#owner)
|
|
165
233
|
}
|
|
166
234
|
}
|
package/src/core/adopt.ts
CHANGED
|
@@ -1,33 +1,64 @@
|
|
|
1
1
|
import { join } from 'node:path'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { sha256Hex } from './crypto'
|
|
4
|
+
import { fromBase64, utf8 } from './encoding'
|
|
4
5
|
import { signCiphertext, signerOf } from './entries'
|
|
5
6
|
import { atomicWrite } from './fsx'
|
|
6
7
|
import { withLock } from './lock'
|
|
8
|
+
import type { EntryName } from './naming'
|
|
7
9
|
import type { Vault } from './vault'
|
|
8
10
|
|
|
11
|
+
/** A write whose certificate expired before adoption; `digest` pins this exact write, revision and content. */
|
|
12
|
+
export type ExpiredWrite = {
|
|
13
|
+
readonly name: string
|
|
14
|
+
readonly rev: number
|
|
15
|
+
readonly at: string
|
|
16
|
+
readonly digest: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type Adoption = {
|
|
20
|
+
readonly adopted: readonly string[]
|
|
21
|
+
readonly expired: readonly ExpiredWrite[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A write the owner chose to keep, by its name and the digest `doctor` listed it with. */
|
|
25
|
+
export type ExpiredPin = { readonly name: EntryName; readonly digest: string }
|
|
26
|
+
|
|
27
|
+
const digestOf = (sig: string): string => sha256Hex(utf8(sig)).slice(0, 16)
|
|
28
|
+
|
|
9
29
|
/**
|
|
10
30
|
* Re-signs entries written under a sealed grant's certificate with a key this process holds, so they outlive the
|
|
11
31
|
* certificate: readers stop accepting a certificate once their own clock passes its expiry. Run by the daemon
|
|
12
|
-
* periodically and by `doctor --fix`.
|
|
32
|
+
* periodically and by `doctor --fix`.
|
|
33
|
+
*
|
|
34
|
+
* A write whose certificate already expired is adopted only when the owner pins it in `expired`: its `at` is chosen
|
|
35
|
+
* by the holder, who keeps the signing key after expiry, so nothing proves it was written before the expiry. A name
|
|
36
|
+
* alone can't pin it: a later forgery under the same name replaces the file, so the pin also carries the digest of
|
|
37
|
+
* the exact write the owner reviewed.
|
|
13
38
|
*/
|
|
14
39
|
export function adoptCertWrites(
|
|
15
40
|
vault: Vault,
|
|
16
|
-
options: { readonly expired?:
|
|
17
|
-
): Promise<
|
|
41
|
+
options: { readonly expired?: readonly ExpiredPin[] } = {},
|
|
42
|
+
): Promise<Adoption> {
|
|
18
43
|
return withLock(vault.layout.lockFile, 'exclusive', async () => {
|
|
19
|
-
|
|
20
|
-
const { entries } = await vault.scan({ acceptExpiredCerts:
|
|
44
|
+
const keep = new Set((options.expired ?? []).map((pin) => `${pin.name}@${pin.digest}`))
|
|
45
|
+
const { entries } = await vault.scan({ acceptExpiredCerts: true })
|
|
21
46
|
const adopted: string[] = []
|
|
47
|
+
const expired: ExpiredWrite[] = []
|
|
22
48
|
for (const { envelope, payload } of entries) {
|
|
23
49
|
if (envelope.signer.kind !== 'cert') continue
|
|
24
|
-
const
|
|
50
|
+
const digest = digestOf(envelope.sig)
|
|
51
|
+
if (Date.now() > Date.parse(envelope.signer.cert.exp) && !keep.has(`${payload.name}@${digest}`)) {
|
|
52
|
+
expired.push({ name: payload.name, rev: envelope.rev, at: envelope.at, digest })
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
const key = (await vault.keyring.unlock(envelope.tier)) ?? vault.keyring.owner
|
|
25
56
|
if (key === undefined) continue
|
|
26
|
-
const draft = { id: envelope.id,
|
|
57
|
+
const draft = { id: envelope.id, tier: envelope.tier, rev: envelope.rev + 1, at: new Date().toISOString() }
|
|
27
58
|
const resigned = signCiphertext(draft, fromBase64(envelope.payload), vault.state, signerOf(key))
|
|
28
59
|
atomicWrite(join(vault.layout.entries, `${envelope.id}.json`), `${JSON.stringify(resigned, null, 2)}\n`)
|
|
29
60
|
adopted.push(payload.name)
|
|
30
61
|
}
|
|
31
|
-
return adopted
|
|
62
|
+
return { adopted, expired }
|
|
32
63
|
})
|
|
33
64
|
}
|
package/src/core/crypto.ts
CHANGED
|
@@ -8,8 +8,9 @@ import { fromBase64, fromBase64Url, fromUtf8, toBase64, toBase64Url, utf8 } from
|
|
|
8
8
|
import { TypeVaultError } from './errors'
|
|
9
9
|
|
|
10
10
|
export type KeyRole =
|
|
11
|
-
| { readonly kind: '
|
|
12
|
-
| { readonly kind: '
|
|
11
|
+
| { readonly kind: 'owner' }
|
|
12
|
+
| { readonly kind: 'tier'; readonly tier: string; readonly gen: number }
|
|
13
|
+
| { readonly kind: 'member'; readonly name: string }
|
|
13
14
|
|
|
14
15
|
/** An age identity for decryption plus an Ed25519 key for signing. */
|
|
15
16
|
export type SecretKey = { readonly role: KeyRole; readonly identity: string; readonly signSecret: Uint8Array }
|
|
@@ -18,8 +19,9 @@ export type PublicKey = { readonly recipient: string; readonly signPublic: strin
|
|
|
18
19
|
const KEY_PREFIX = 'tvk1:'
|
|
19
20
|
|
|
20
21
|
const RoleSchema = z.discriminatedUnion('kind', [
|
|
21
|
-
z.object({ kind: z.literal('
|
|
22
|
-
z.object({ kind: z.literal('
|
|
22
|
+
z.object({ kind: z.literal('owner') }),
|
|
23
|
+
z.object({ kind: z.literal('tier'), tier: z.string(), gen: z.number().int().positive() }),
|
|
24
|
+
z.object({ kind: z.literal('member'), name: z.string() }),
|
|
23
25
|
])
|
|
24
26
|
const EncodedKeySchema = z.object({ role: RoleSchema, age: z.string().startsWith('AGE-SECRET-KEY-1'), ed: z.string() })
|
|
25
27
|
|
package/src/core/doctor.ts
CHANGED
|
@@ -10,12 +10,12 @@ export type DoctorReport = {
|
|
|
10
10
|
readonly problems: readonly ScanProblem[]
|
|
11
11
|
readonly mergesResolved: readonly string[]
|
|
12
12
|
readonly duplicatesRemoved: readonly string[]
|
|
13
|
-
/** Same name in several
|
|
13
|
+
/** Same name in several tiers: reported for the owner, never deleted automatically. */
|
|
14
14
|
readonly duplicatesKept: readonly string[]
|
|
15
15
|
readonly needsRotation: readonly string[]
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const CONFLICT = /^<<<<<<< [^\n]*\n([\s\S]*?)^=======\n([\s\S]*?)^>>>>>>> [^\n]*\n?/
|
|
18
|
+
const CONFLICT = /^<<<<<<< [^\n]*\n([\s\S]*?)^=======\n([\s\S]*?)^>>>>>>> [^\n]*\n?/gm
|
|
19
19
|
|
|
20
20
|
function parseSide(text: string): Envelope | undefined {
|
|
21
21
|
try {
|
|
@@ -31,12 +31,11 @@ function parseSide(text: string): Envelope | undefined {
|
|
|
31
31
|
function resolveMerge(vault: Vault, file: string): boolean {
|
|
32
32
|
const path = join(vault.layout.entries, file)
|
|
33
33
|
const text = readFileSync(path, 'utf8')
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
.map((side) => parseSide(before + side + after))
|
|
34
|
+
const ours = text.replace(CONFLICT, '$1')
|
|
35
|
+
if (ours === text) return false
|
|
36
|
+
const theirs = text.replace(CONFLICT, '$2')
|
|
37
|
+
const sides = [ours, theirs]
|
|
38
|
+
.map(parseSide)
|
|
40
39
|
.filter((side): side is Envelope => side !== undefined && verifyEnvelope(side, vault.state) === undefined)
|
|
41
40
|
const winner = sides.reduce<Envelope | undefined>(
|
|
42
41
|
(best, side) =>
|
|
@@ -60,18 +59,18 @@ export function doctor(vault: Vault, fix: boolean): Promise<DoctorReport> {
|
|
|
60
59
|
const duplicatesKept: string[] = []
|
|
61
60
|
for (const [name, copies] of byName) {
|
|
62
61
|
if (copies.length < 2) continue
|
|
63
|
-
// Only same-
|
|
64
|
-
// the
|
|
65
|
-
for (const [
|
|
66
|
-
const keep = resolveName(vault.state.
|
|
67
|
-
for (const copy of
|
|
62
|
+
// Only same-tier copies are removed: across tiers the higher tier already wins reads, and
|
|
63
|
+
// the owner decides what to delete.
|
|
64
|
+
for (const [tier, inTier] of Map.groupBy(copies, (entry) => entry.envelope.tier)) {
|
|
65
|
+
const keep = resolveName(vault.state.tiers, inTier)
|
|
66
|
+
for (const copy of inTier) {
|
|
68
67
|
if (copy === keep) continue
|
|
69
68
|
if (fix) rmSync(join(vault.layout.entries, `${copy.envelope.id}.json`))
|
|
70
|
-
duplicatesRemoved.push(`${name} (${
|
|
69
|
+
duplicatesRemoved.push(`${name} (${tier}, ${copy.envelope.id})`)
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
|
-
const
|
|
74
|
-
if (
|
|
72
|
+
const tiers = new Set(copies.map((entry) => entry.envelope.tier))
|
|
73
|
+
if (tiers.size > 1) duplicatesKept.push(`${name} (in ${[...tiers].sort().join(', ')})`)
|
|
75
74
|
}
|
|
76
75
|
const needsRotation = entries
|
|
77
76
|
.filter((entry) => entry.payload.needsRotation === true)
|