@xiaolikt/ai-vault 0.13.0 → 0.14.1
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/Cargo.lock +1 -1
- package/README.md +53 -51
- package/README.zh.md +28 -35
- package/crates/ai-vault/Cargo.toml +1 -1
- package/crates/ai-vault/src/cli.rs +196 -34
- package/crates/ai-vault/src/config.rs +88 -11
- package/crates/ai-vault/src/crypto.rs +10 -1
- package/crates/ai-vault/src/sealed.rs +3 -1
- package/crates/ai-vault/src/session.rs +74 -0
- package/package.json +6 -6
- package/packages/pi-ai-vault/README.md +3 -2
- package/packages/pi-ai-vault/README.zh.md +4 -3
- package/packages/pi-ai-vault/package.json +2 -2
- package/packages/pi-ai-vault/src/bitwarden.ts +3 -3
- package/packages/pi-ai-vault/src/cli.ts +11 -5
- package/packages/pi-ai-vault/src/ente.ts +2 -2
- package/packages/pi-ai-vault/src/index.ts +121 -51
- package/packages/pi-ai-vault/src/settings.ts +14 -2
- package/test/cli.sh +67 -2
- package/tests/vault-cli.test.mjs +98 -3
package/Cargo.lock
CHANGED
package/README.md
CHANGED
|
@@ -180,7 +180,7 @@ ai-vault [--dir DIR] [--json] <command>
|
|
|
180
180
|
| `doctor` | paths, whether initialised, desktop/zenity availability, KDF, version |
|
|
181
181
|
| `init [--password-stdin]` | create the store (a dialog sets the password twice); prints a token |
|
|
182
182
|
| `unlock [--password-stdin] [--requester ID]` | unlock under the current policy; prints a token |
|
|
183
|
-
| `mode [get]` / `mode set
|
|
183
|
+
| `mode [get]` / `mode set every\|time\|never [--hours N]` | show / switch the unlock policy (moving towards `never` needs the master password; `--password-stdin`, or `AI_VAULT_PASSWORD` for a host, supplies it) |
|
|
184
184
|
| `settings [key] [value]` | every setting: `vault.*` `bitwarden.*` `ente.*` |
|
|
185
185
|
| `bw-session get\|put\|clear` | the persisted `bw` session key (used when `bitwarden.unlock != session`; sealed at rest) |
|
|
186
186
|
| `ente-session get\|put\|clear` | Ente's `ENTE_CLI_VAULT_KEY` (used when `ente.unlock != session`; sealed the same way) |
|
|
@@ -308,63 +308,70 @@ else stays in Bitwarden and asks every time.
|
|
|
308
308
|
### Unlock policies
|
|
309
309
|
|
|
310
310
|
When `ai-vault` asks for its master password is a per-machine setting
|
|
311
|
-
(
|
|
311
|
+
(`ai-vault settings vault.unlock …`, `/vault mode …`). Three honest modes —
|
|
312
|
+
the old `session` ("once per process") was never what anyone wanted in a
|
|
313
|
+
multi-tab host: every tab, every subagent and every `/new` was a new process,
|
|
314
|
+
so it asked each time. It is read as `time` where an old setting names it.
|
|
312
315
|
|
|
313
316
|
| Mode | Behaviour | For |
|
|
314
317
|
|---|---|---|
|
|
318
|
+
| `time` (**default**, 4 h) | asks once, then any pi/omp process on the machine — another herdr tab, a subagent, a `/fork`, a `--restore` — uses that unlock until N hours have passed (`--hours`, 1–2160) | everyday: a work sitting without asking again |
|
|
319
|
+
| `every` | asks on **every call**; no token is ever written, so nothing on disk can open the store. The password travels in the child's environment, never on a command line | the strict end: a shared machine, or a secret you want to be asked about each time |
|
|
315
320
|
| `never` | never asks. The key lives in `~/.ai-vault/agent.session.json` (0600) and **reads open no window either** | handing the agent a set of "use freely" passwords |
|
|
316
|
-
| `time` | asks again N hours after the unlock (default 8, `--hours`) | long bounded tasks |
|
|
317
|
-
| `session` | once per session; dies with it (default) | everyday |
|
|
318
|
-
| `vault.confirm` | whether each read opens a confirm window **while the session is unlocked** (default **off**). The master password that opened the session is the approval — a window the model cannot answer for you — and every read is still audited entry by entry. `on` puts a second window on every read; turning it off costs the master password, turning it back on is free | `off` for everyday; `on` to see each request before it is answered |
|
|
319
|
-
|
|
320
|
-
**Why `session` no longer means "every tab asks".** Every pi/omp process on the
|
|
321
|
-
machine shares one store-wide host secret (`~/.ai-vault/.host`, 0600), so a
|
|
322
|
-
session opened in one process is usable in the next: a second herdr tab, a
|
|
323
|
-
subagent, a `/fork` — none of them asks again while an unlocked session
|
|
324
|
-
exists. The secret is protected by exactly what protects the store and its
|
|
325
|
-
wrap key, the user's home directory; a token found on disk is still refused
|
|
326
|
-
without it.
|
|
327
321
|
|
|
328
322
|
```bash
|
|
329
323
|
ai-vault mode # show
|
|
330
|
-
ai-vault mode set
|
|
331
|
-
ai-vault mode set time --hours
|
|
332
|
-
ai-vault mode set
|
|
324
|
+
ai-vault mode set every # stricter: no password needed to switch
|
|
325
|
+
ai-vault mode set time --hours 8
|
|
326
|
+
ai-vault mode set never # lowering: asks the master password
|
|
333
327
|
```
|
|
334
|
-
In the extension: `/vault mode`, `/vault mode time
|
|
335
|
-
|
|
328
|
+
In the extension: `/vault mode`, `/vault mode time 8`. Moving **away** from `every`
|
|
329
|
+
or `time` towards `never` costs the master password; moving towards `every`
|
|
330
|
+
is free. Switching to `every` deletes every live token of the store at once.
|
|
331
|
+
|
|
332
|
+
**Why one unlock serves every tab.** Every pi/omp process on the machine
|
|
333
|
+
shares one store-wide host secret (`~/.ai-vault/.host`, 0600). A call that
|
|
334
|
+
comes without a token looks for a live session of this store that it can
|
|
335
|
+
verify under that secret, and — in `time` mode — **adopts the unlock**: it
|
|
336
|
+
gets a session of its own under the same key (its own token, no grants), so
|
|
337
|
+
the password is not asked again while any tab still holds an unlock, yet a
|
|
338
|
+
subagent still approves its own reads and `/vault lock` still locks only the
|
|
339
|
+
tab that ran it. A process without the secret — a stolen token, another
|
|
340
|
+
user — is refused; a human at a terminal (the wrap key) cannot adopt a
|
|
341
|
+
host's session either. The secret is protected by exactly what protects the
|
|
342
|
+
store and its wrap key, the user's home directory.
|
|
343
|
+
|
|
344
|
+
In `every` mode there is nothing to adopt: every call brings its password.
|
|
345
|
+
|
|
346
|
+
**`every` and the plaintext hint.** The settings live inside the ciphertext,
|
|
347
|
+
but the CLI must know to *ask* before it can read them. So the file header
|
|
348
|
+
carries one plaintext bit, `askEvery`. It only says "ask": whether a call is
|
|
349
|
+
allowed is still decided by the settings under the key. Cleared, the CLI finds
|
|
350
|
+
no token and refuses; set on a store whose real policy is not `every`, the CLI
|
|
351
|
+
asks for a password it would not otherwise have asked for and then refuses
|
|
352
|
+
anyway. Either edit lowers nothing.
|
|
353
|
+
|
|
354
|
+
### The per-read window
|
|
355
|
+
|
|
356
|
+
| Setting | Meaning |
|
|
357
|
+
|---|---|
|
|
358
|
+
| `vault.confirm` | whether each read opens a confirm window **while the session is unlocked** (default **off**). The master password that opened the session is the approval — a window the model cannot answer for you — and every read is still audited entry by entry. `on` puts a second window on every read; turning it off costs the master password, turning it back on is free |
|
|
336
359
|
|
|
337
|
-
`
|
|
338
|
-
|
|
339
|
-
is to separate "the AI may use these" from "ask me every time".
|
|
360
|
+
In `every` mode there is no second window: the password just typed for the
|
|
361
|
+
call is the approval.
|
|
340
362
|
|
|
341
|
-
### Bitwarden
|
|
363
|
+
### Bitwarden and Ente keys have their own switch
|
|
342
364
|
|
|
343
365
|
| Setting | Value | Behaviour |
|
|
344
366
|
|---|---|---|
|
|
345
|
-
| `bitwarden.unlock` | `
|
|
346
|
-
| | `
|
|
347
|
-
| | `
|
|
348
|
-
| `bitwarden.import` | `ask`
|
|
349
|
-
| | `always` | import on a hit and return it, **no window at all** (fully automatic; use with care) |
|
|
350
|
-
| | `never` | read once, ask once, never write to the local store |
|
|
351
|
-
|
|
352
|
-
```bash
|
|
353
|
-
ai-vault settings # everything
|
|
354
|
-
ai-vault settings bitwarden.unlock never
|
|
355
|
-
ai-vault settings bitwarden.import always
|
|
356
|
-
ai-vault settings bitwarden.hours 24
|
|
357
|
-
```
|
|
358
|
-
In the extension: `/vault settings`, `/vault settings bitwarden.import always`.
|
|
359
|
-
|
|
360
|
-
> `bitwarden.import = always` together with `bitwarden.unlock = never` means
|
|
361
|
-
> "anything in Bitwarden that gets asked for is imported automatically and read
|
|
362
|
-
> without a password from then on". Fastest and least guarded combination; only
|
|
363
|
-
> with an agent you fully trust.
|
|
367
|
+
| `bitwarden.unlock` / `ente.unlock` | `session` (default) | the `bw` session key / Ente key stays in the CLI's own memory or keychain; nothing is written |
|
|
368
|
+
| | `never` | kept on disk, sealed under the store's wrap key (`~/.ai-vault/bitwarden.session.json` / `ente.session.json`, 0600), so no password or keychain is needed |
|
|
369
|
+
| | `time` | the same, expired after `bitwarden.hours` / `ente.hours` |
|
|
370
|
+
| `bitwarden.import` | `ask` (default) / `always` / `never` | a Bitwarden hit on a local miss: ask, import silently, or read once only |
|
|
364
371
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
372
|
+
These keys have no `every`: their CLI keeps its own session, and "ask on
|
|
373
|
+
every call" is not a thing a `bw` session can do. Lowering them (`never`,
|
|
374
|
+
`time`, `import = always`) costs the master password.
|
|
368
375
|
|
|
369
376
|
### Where the settings live
|
|
370
377
|
|
|
@@ -377,11 +384,6 @@ only its harmless keys travel; `confirm = false` and `unlock = never` are not
|
|
|
377
384
|
taken from a file, so an edit made while the file was still plain text grants
|
|
378
385
|
nothing.
|
|
379
386
|
|
|
380
|
-
Every change that **lowers** protection costs the master password — `unlock =
|
|
381
|
-
never`, keeping the Bitwarden or Ente key on disk, importing Bitwarden hits
|
|
382
|
-
without asking. Turning `confirm` *on* is free; it is off by default, so
|
|
383
|
-
turning it off costs nothing until you have turned it on.
|
|
384
|
-
|
|
385
387
|
## Extension commands
|
|
386
388
|
|
|
387
389
|
```
|
|
@@ -408,8 +410,8 @@ turning it off costs nothing until you have turned it on.
|
|
|
408
410
|
- Every binding (a session, or a subagent) runs its own `ai-vault unlock` on
|
|
409
411
|
first use and gets **its own token**. A subagent cannot inherit the parent's
|
|
410
412
|
unlock or grants; the confirm window says "requester: subagent 0-Explore".
|
|
411
|
-
- Token lifetime follows the unlock policy (above): `
|
|
412
|
-
`
|
|
413
|
+
- Token lifetime follows the unlock policy (above): `time` until N hours,
|
|
414
|
+
`never` never; `every` has no tokens at all. `/new`, `/fork` and quitting make the
|
|
413
415
|
extension drop its own token; `/resume` keeps it. Expired, tampered or
|
|
414
416
|
old-store token files are swept on **every** CLI call.
|
|
415
417
|
- Read grants live in the token, so they are isolated per requester by
|
package/README.zh.md
CHANGED
|
@@ -140,7 +140,7 @@ ai-vault [--dir DIR] [--json] <命令>
|
|
|
140
140
|
| `doctor` | 路径、是否初始化、有没有桌面和 zenity、KDF、版本 |
|
|
141
141
|
| `init [--password-stdin]` | 建库(弹窗两次设密码),成功后直接给一个令牌 |
|
|
142
142
|
| `unlock [--password-stdin] [--requester ID]` | 按当前模式解锁,stdout 一个令牌 |
|
|
143
|
-
| `mode [get]` / `mode set
|
|
143
|
+
| `mode [get]` / `mode set every\|time\|never [--hours N]` | 查看/切换解锁模式(往 `never` 走要主密码;`--password-stdin`,宿主用 `AI_VAULT_PASSWORD`) |
|
|
144
144
|
| `settings [键] [值]` | 全部设置:`vault.*` `bitwarden.*` `ente.*` |
|
|
145
145
|
| `bw-session get\|put\|clear` | Bitwarden 会话密钥的落盘副本(`bitwarden.unlock != session` 时用,加密封存) |
|
|
146
146
|
| `ente-session get\|put\|clear` | Ente 的 `ENTE_CLI_VAULT_KEY`(`ente.unlock != session` 时用,同样加密封存) |
|
|
@@ -247,63 +247,56 @@ vault_get "github"
|
|
|
247
247
|
|
|
248
248
|
### 解锁模式
|
|
249
249
|
|
|
250
|
-
`ai-vault`
|
|
250
|
+
`ai-vault` 什么时候要主密码,由这台机器的设置决定(`ai-vault settings vault.unlock …`,`/vault mode …`)。三个诚实的模式——旧的 `session`(「每进程一次」)在多 tab 的宿主里从来不是谁想要的:每个 tab、每个子代理、每次 `/new` 都是新进程,所以每次都问。旧设置里写 `session` 的一律按 `time` 读。
|
|
251
251
|
|
|
252
252
|
| 模式 | 行为 | 适合 |
|
|
253
253
|
|---|---|---|
|
|
254
|
+
| `time`(**默认**,4 小时) | 问一次,之后机器上任何 pi/omp 进程——另一个 herdr tab、子代理、`/fork`、`--restore`——都用这次解锁,直到 N 小时后(`--hours`,1–2160) | 日常:一段工作只输一次 |
|
|
255
|
+
| `every` | **每次调用**都问;从不写令牌,磁盘上没有任何东西能打开库。密码走子进程环境,绝不进命令行 | 最严的一端:共享的机器,或你希望每次都被问的密码 |
|
|
254
256
|
| `never` | 永远不问。密钥写在 `~/.ai-vault/agent.session.json`(0600),**读取也不再弹确认窗** | 把一批「随便用」的密码交给 agent |
|
|
255
|
-
| `time` | 解锁后 N 小时(默认 8,`--hours` 可改)再问一次 | 有边界的长任务 |
|
|
256
|
-
| `session` | 每个会话问一次,会话结束就失效(默认) | 日常 |
|
|
257
|
-
| `vault.confirm` | 会话解锁期间,每次读取是否**再弹一次确认窗**(默认**关**)。打开会话的主密码就是授权——那个窗口模型替你答不了——每次读取仍逐条写审计。`on` 是每次读取都再弹一次;关要主密码,开回去免费 | 日常用 `off`;想每个请求在被回答前看一眼就 `on` |
|
|
258
|
-
|
|
259
|
-
**为什么 `session` 不再是「每个 tab 都要问」。** 机器上所有 pi/omp 进程共用一个库级 host secret(`~/.ai-vault/.host`,0600),所以一个进程开的会话在下一个进程里直接可用:第二个 herdr tab、子代理、`/fork`——只要还有解锁着的会话,谁都不会再要密码。这个 secret 和库、和 wrap key 受同一种保护(你的 home 目录);磁盘上捡到的令牌没有它照样被拒。
|
|
260
257
|
|
|
261
258
|
```bash
|
|
262
|
-
ai-vault mode #
|
|
263
|
-
ai-vault mode set
|
|
264
|
-
ai-vault mode set time --hours
|
|
265
|
-
ai-vault mode set
|
|
259
|
+
ai-vault mode # 看当前
|
|
260
|
+
ai-vault mode set every # 更严:切换不要密码
|
|
261
|
+
ai-vault mode set time --hours 8
|
|
262
|
+
ai-vault mode set never # 降低保护:要主密码
|
|
266
263
|
```
|
|
267
|
-
|
|
264
|
+
插件里:`/vault mode`、`/vault mode time 8`。从 `every`/`time` 往 `never` 走要主密码;往 `every` 走免费。切到 `every` 会立刻删掉这个库所有活着的令牌。
|
|
268
265
|
|
|
269
|
-
|
|
266
|
+
**为什么一次解锁所有 tab 都能用。** 机器上所有 pi/omp 进程共用一个库级 host secret(`~/.ai-vault/.host`,0600),所以一个进程开的会话在下一个进程里直接可用。它和库、和 wrap key 受同一种保护(你的 home 目录);磁盘上捡到的令牌没有它照样被拒。
|
|
270
267
|
|
|
271
|
-
|
|
268
|
+
**`every` 和那个明文标记。** 设置存在密文里,但 CLI 得先知道「要不要问」才能读到它。所以文件头带一个明文位 `askEvery`。它只表示「问」:放不放行仍由密文里的设置决定。把它清掉,CLI 找不到令牌就拒绝;在一个真实模式不是 `every` 的库上把它置上,CLI 会多问一次本来不会问的密码,然后照样拒绝。两个方向都降低不了任何保护。
|
|
272
269
|
|
|
273
|
-
|
|
274
|
-
|---|---|---|
|
|
275
|
-
| `bitwarden.unlock` | `never` | `bw` 会话密钥落盘(`~/.ai-vault/bitwarden.session.json`,0600),**不再问 Bitwarden 主密码** |
|
|
276
|
-
| | `time` | 同上,但 `bitwarden.hours` 小时后失效,重新要密码 |
|
|
277
|
-
| | `session` | 密钥只在 agent 进程内存里,每个会话问一次(默认) |
|
|
278
|
-
| `bitwarden.import` | `ask` | `vault_get` 本地没命中 → 显示 Bitwarden 命中项 → 读一次 → 问是否导入(默认) |
|
|
279
|
-
| | `always` | 命中就直接导入并返回,**一个窗口都不开**(完全自动,慎用) |
|
|
280
|
-
| | `never` | 只读一次,问一次,永不写入本地库 |
|
|
270
|
+
### 每次读取的确认窗
|
|
281
271
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
ai-vault settings bitwarden.import always
|
|
286
|
-
ai-vault settings bitwarden.hours 24
|
|
287
|
-
```
|
|
288
|
-
插件里:`/vault settings`、`/vault settings bitwarden.import always`。
|
|
272
|
+
| 设置 | 含义 |
|
|
273
|
+
|---|---|
|
|
274
|
+
| `vault.confirm` | 会话解锁期间,每次读取是否**再弹一次确认窗**(默认**关**)。打开会话的主密码就是授权——那个窗口模型替你答不了——每次读取仍逐条写审计。`on` 是每次读取都再弹一次;关要主密码,开回去免费 |
|
|
289
275
|
|
|
290
|
-
|
|
276
|
+
`every` 模式下没有第二个窗口:为这次调用刚输的密码就是授权。
|
|
291
277
|
|
|
292
|
-
|
|
278
|
+
### Bitwarden 和 Ente 的密钥有自己的开关
|
|
279
|
+
|
|
280
|
+
| 设置 | 值 | 行为 |
|
|
281
|
+
|---|---|---|
|
|
282
|
+
| `bitwarden.unlock` / `ente.unlock` | `session`(默认) | `bw` 会话密钥 / Ente 密钥留在 CLI 自己的内存或钥匙串里;什么都不写 |
|
|
283
|
+
| | `never` | 落盘,用库的 wrap key 封存(`~/.ai-vault/bitwarden.session.json` / `ente.session.json`,0600),不再要密码或钥匙串 |
|
|
284
|
+
| | `time` | 同上,`bitwarden.hours` / `ente.hours` 后失效 |
|
|
285
|
+
| `bitwarden.import` | `ask`(默认)/ `always` / `never` | 本地没命中、Bitwarden 命中时:问、直接导入、只读一次 |
|
|
286
|
+
|
|
287
|
+
这些密钥没有 `every`:它们的 CLI 自己管会话,「每次都问」对 `bw` 会话做不到。降低它们(`never`、`time`、`import = always`)要主密码。
|
|
293
288
|
|
|
294
289
|
### 设置存在哪里
|
|
295
290
|
|
|
296
291
|
不在文件里。设置在 `store.json` 里面,和密码用同一把 key 封在一起,所以读写设置都需要库的 key——主密码,或一个用它打开的会话。磁盘上没有东西可改:动 `store.json` 一个字节就是篡改,整个库会被拒。旧版留下的明文 `config.json` 会在下次解锁时带进去,但只带无害的键;`confirm = false` 和 `unlock = never` 不会从文件里来,所以趁文件还是明文时做的改动什么都换不到。
|
|
297
292
|
|
|
298
|
-
所有**降低**保护的改动都要主密码——`unlock = never`、把 Bitwarden / Ente 的密钥落盘、不询问就导入 Bitwarden 条目。把 `confirm` **打开**是免费的;它默认是关的,所以不动它就不花钱。
|
|
299
|
-
|
|
300
293
|
## 插件里的命令
|
|
301
294
|
|
|
302
295
|
```
|
|
303
296
|
/vault init 创建本地库(弹窗设主密码)
|
|
304
297
|
/vault add card:Visa brand=Visa code 类型:名称,字段写在行里,只写字段名的弹窗输
|
|
305
298
|
/vault unlock | lock 解锁 / 锁定本会话
|
|
306
|
-
/vault mode [
|
|
299
|
+
/vault mode [every|time|never] 查看/切换解锁模式(往 never 走要主密码)
|
|
307
300
|
/vault settings [键 值] 查看/修改设置(bitwarden.* 也在这里)
|
|
308
301
|
/vault ente-key 保存 ente 的 ENTE_CLI_VAULT_KEY(免钥匙串/免登录)
|
|
309
302
|
/vault status 状态、过期时间、授权剩余
|
|
@@ -321,7 +314,7 @@ ai-vault settings bitwarden.hours 24
|
|
|
321
314
|
## 会话与隔离
|
|
322
315
|
|
|
323
316
|
- 每个绑定(一个会话,或一个子代理)在第一次用到库时各自 `ai-vault unlock`,拿到**自己的令牌**。父会话的解锁和授权子代理继承不到;确认窗口里会写「请求方: 子代理 0-Explore」。
|
|
324
|
-
- 令牌寿命由解锁模式决定(见上):`
|
|
317
|
+
- 令牌寿命由解锁模式决定(见上):`time` 到 N 小时、`never` 不过期;`every` 根本没有令牌。`/new`、`/fork`、退出会让插件把自己的令牌删掉;`/resume` 保留。失效、被改、属于旧库的令牌文件在**每次** CLI 调用时都会被清掉。
|
|
325
318
|
- 读授权存在令牌里,所以天然按请求方隔离;`/vault grant`、`/vault revoke` 改的就是它。
|
|
326
319
|
- 库文件本身共享:读改写有 `flock`,两个 agent 同时写不会丢更新(旧版是后写覆盖先写)。
|
|
327
320
|
|
|
@@ -33,6 +33,14 @@ struct Cli {
|
|
|
33
33
|
/// Store directory (default: $AI_VAULT_DIR or ~/.ai-vault)
|
|
34
34
|
#[arg(long, global = true, env = "AI_VAULT_DIR", value_name = "DIR")]
|
|
35
35
|
dir: Option<PathBuf>,
|
|
36
|
+
/// The master password, for `vault.unlock = every` (and for any mode
|
|
37
|
+
/// instead of a window): read from stdin. One line.
|
|
38
|
+
#[arg(long, global = true)]
|
|
39
|
+
password_stdin: bool,
|
|
40
|
+
/// The master password, for `vault.unlock = every`, passed by a host that
|
|
41
|
+
/// already asked for it.
|
|
42
|
+
#[arg(long, global = true, env = "AI_VAULT_PASSWORD", hide_env_values = true)]
|
|
43
|
+
password: Option<String>,
|
|
36
44
|
/// Machine-readable JSON on stdout
|
|
37
45
|
#[arg(long, global = true)]
|
|
38
46
|
json: bool,
|
|
@@ -275,6 +283,15 @@ pub fn run() -> i32 {
|
|
|
275
283
|
}
|
|
276
284
|
};
|
|
277
285
|
let paths = Paths::resolve(cli.dir.clone());
|
|
286
|
+
PASSWORD_SOURCE.get_or_init(|| {
|
|
287
|
+
if let Some(password) = cli.password.as_deref().filter(|p| !p.is_empty()) {
|
|
288
|
+
PasswordSource::Env(password.to_string())
|
|
289
|
+
} else if cli.password_stdin {
|
|
290
|
+
PasswordSource::Stdin
|
|
291
|
+
} else {
|
|
292
|
+
PasswordSource::Window
|
|
293
|
+
}
|
|
294
|
+
});
|
|
278
295
|
migrate_legacy(&paths);
|
|
279
296
|
// Dead token files (expired, unsigned, from an older store) carry keys
|
|
280
297
|
// that open nothing; drop them on sight rather than after a week.
|
|
@@ -423,12 +440,60 @@ fn policy(idle_min: Option<i64>, config: &config::Config) -> (i64, Option<i64>,
|
|
|
423
440
|
match policy.mode {
|
|
424
441
|
UnlockMode::Never => (0, None, policy),
|
|
425
442
|
UnlockMode::Time => (0, Some(now + policy.hours.clamp(1, 24 * 90) * 3_600_000), policy),
|
|
426
|
-
UnlockMode::Session => (0, Some(now +
|
|
443
|
+
UnlockMode::Session => (0, Some(now + 24 * 3_600_000), policy),
|
|
444
|
+
// Every: no token is minted at all (see load_session / unlock).
|
|
445
|
+
UnlockMode::Every => (0, None, policy),
|
|
427
446
|
}
|
|
428
447
|
}
|
|
429
448
|
|
|
430
449
|
// ---------------------------------------------------------------- input
|
|
431
450
|
|
|
451
|
+
/// Where the master password for an `every` call comes from. The env var and
|
|
452
|
+
/// the flag are for a host that has already asked the user; a human gets the
|
|
453
|
+
/// window. The password lives for the length of one call and is zeroed after.
|
|
454
|
+
static PASSWORD_SOURCE: std::sync::OnceLock<PasswordSource> = std::sync::OnceLock::new();
|
|
455
|
+
|
|
456
|
+
enum PasswordSource {
|
|
457
|
+
Env(String),
|
|
458
|
+
Stdin,
|
|
459
|
+
Window,
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
thread_local! {
|
|
463
|
+
/// The password an `every` call was opened with, for the one call only:
|
|
464
|
+
/// a settings change that must verify the master password reuses it
|
|
465
|
+
/// instead of asking for the same password twice.
|
|
466
|
+
static EVERY_PASSWORD: std::cell::RefCell<Option<zeroize::Zeroizing<String>>> = const { std::cell::RefCell::new(None) };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/// The master password for this call, in `every` mode. Never written anywhere.
|
|
470
|
+
fn master_password_for_call() -> Result<zeroize::Zeroizing<String>, Fault> {
|
|
471
|
+
match PASSWORD_SOURCE.get() {
|
|
472
|
+
Some(PasswordSource::Env(password)) => Ok(zeroize::Zeroizing::new(password.clone())),
|
|
473
|
+
Some(PasswordSource::Stdin) => read_password().map(zeroize::Zeroizing::new),
|
|
474
|
+
// A host asks the user in its own UI: tell it to, rather than opening a
|
|
475
|
+
// window of our own behind the host's back.
|
|
476
|
+
_ if is_host() => Err(Fault::NeedsUi("password")),
|
|
477
|
+
_ => ask_password(tr("ai-vault · unlock", "ai-vault · 解锁"), tr("master password (asked on every call)", "主密码(每次调用都会问)"))
|
|
478
|
+
.map(zeroize::Zeroizing::new),
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/// The password for a verification: the environment a host passed, then
|
|
483
|
+
/// stdin, then a window. A host that passed nothing is told to ask.
|
|
484
|
+
fn master_password_from(why: &str, password_stdin: bool) -> Result<zeroize::Zeroizing<String>, Fault> {
|
|
485
|
+
if let Some(PasswordSource::Env(password)) = PASSWORD_SOURCE.get() {
|
|
486
|
+
return Ok(zeroize::Zeroizing::new(password.clone()));
|
|
487
|
+
}
|
|
488
|
+
if password_stdin {
|
|
489
|
+
return read_password().map(zeroize::Zeroizing::new);
|
|
490
|
+
}
|
|
491
|
+
if is_host() {
|
|
492
|
+
return Err(Fault::NeedsUi("password"));
|
|
493
|
+
}
|
|
494
|
+
ask_password(tr("ai-vault · master password", "ai-vault · 主密码"), why).map(zeroize::Zeroizing::new)
|
|
495
|
+
}
|
|
496
|
+
|
|
432
497
|
fn read_password() -> Result<String, Fault> {
|
|
433
498
|
let line = read_line()?;
|
|
434
499
|
let password = line.trim_end_matches(['\r', '\n']).to_string();
|
|
@@ -590,13 +655,63 @@ fn load_session(paths: &Paths, token: Option<&str>) -> Result<Session, Fault> {
|
|
|
590
655
|
let (host, wrap) = signing_keys(paths)?;
|
|
591
656
|
return session::load(paths, token, host.as_deref(), wrap.as_deref()).map_err(Fault::Auth);
|
|
592
657
|
}
|
|
593
|
-
auto_session(paths)
|
|
594
|
-
|
|
595
|
-
}
|
|
658
|
+
if let Some(auto) = auto_session(paths) {
|
|
659
|
+
return Ok(auto);
|
|
660
|
+
}
|
|
661
|
+
// `time` mode: a token another process opened for this store is as good
|
|
662
|
+
// as one this process opened. They share the store-wide host secret, so
|
|
663
|
+
// the newest live one verifies here too — which is the whole point of
|
|
664
|
+
// `time`: a second herdr tab, a subagent or a --restore picks up the
|
|
665
|
+
// unlock that already exists instead of asking again.
|
|
666
|
+
if let Some(existing) = session::find_live(paths, &signing_keys(paths)?) {
|
|
667
|
+
// What is shared is the *unlock*, not the session: the caller gets a
|
|
668
|
+
// session of its own under the same key — its own token, no grants.
|
|
669
|
+
// So a subagent still approves its own reads, and `/vault lock` in one
|
|
670
|
+
// tab locks that tab only, while nobody is asked for the password a
|
|
671
|
+
// second time.
|
|
672
|
+
let store = open_store(paths)?;
|
|
673
|
+
let Some(key) = existing.key() else { return Ok(existing) };
|
|
674
|
+
let mut own = Session::new(&existing.requester, existing.idle_ms, existing.expires_at, &store, &key);
|
|
675
|
+
own.kind = existing.kind;
|
|
676
|
+
let (host, wrap) = signing_keys(paths)?;
|
|
677
|
+
let signer = match existing.signed_by.as_deref() {
|
|
678
|
+
Some("host") => host.map(session::Signer::Host),
|
|
679
|
+
Some("wrap") => wrap.map(session::Signer::Wrap),
|
|
680
|
+
_ => None,
|
|
681
|
+
};
|
|
682
|
+
if let Some(signer) = signer {
|
|
683
|
+
own.save(paths, &signer)?;
|
|
684
|
+
return Ok(own);
|
|
685
|
+
}
|
|
686
|
+
return Ok(existing);
|
|
687
|
+
}
|
|
688
|
+
// `vault.unlock = every`: no token exists by design. The password comes
|
|
689
|
+
// with the call, opens the store in memory, and leaves with it. The hint
|
|
690
|
+
// in the file header says to ask; the settings under the key decide.
|
|
691
|
+
let store = open_store(paths)?;
|
|
692
|
+
if store.ask_every {
|
|
693
|
+
let password = master_password_for_call()?;
|
|
694
|
+
EVERY_PASSWORD.with(|cell| *cell.borrow_mut() = Some(password.clone()));
|
|
695
|
+
let (payload, key) = crypto::open_with_passphrase(&store, &password).map_err(|e| match e {
|
|
696
|
+
crypto::OpenError::WrongPassword => {
|
|
697
|
+
audit::append(&paths.audit(), "every", "unlock_failed", None, Some("bad-password"));
|
|
698
|
+
Fault::Auth("wrong master password".into())
|
|
699
|
+
}
|
|
700
|
+
crypto::OpenError::Broken(message) => Fault::Error(message),
|
|
701
|
+
})?;
|
|
702
|
+
// The hint only said "ask"; the settings under the key decide whether
|
|
703
|
+
// this call is allowed to proceed without a token. A hint set on a
|
|
704
|
+
// store whose real policy is not `every` is ignored (it asked for
|
|
705
|
+
// nothing the policy did not already require).
|
|
706
|
+
if payload.config.unlock.mode != UnlockMode::Every {
|
|
707
|
+
return Err(Fault::Auth("vault is locked (no session token) — unlock first".into()));
|
|
708
|
+
}
|
|
709
|
+
audit::append(&paths.audit(), "every", "unlock", None, Some("every"));
|
|
710
|
+
return Ok(Session::new("every", 0, None, &store, &key).with_kind(session::SessionKind::Every));
|
|
711
|
+
}
|
|
712
|
+
Err(Fault::Auth("vault is locked (no session token) — unlock first".into()))
|
|
596
713
|
}
|
|
597
714
|
|
|
598
|
-
/// Write a session back where it came from: auto sessions stay machine-wide,
|
|
599
|
-
/// the others stay in the per-agent token file.
|
|
600
715
|
fn persist(paths: &Paths, session: &Session) {
|
|
601
716
|
let auto = session::load_auto(paths)
|
|
602
717
|
.ok()
|
|
@@ -674,7 +789,7 @@ fn rekey(paths: &Paths, out: &Out, password_stdin: bool) -> Outcome {
|
|
|
674
789
|
// will be told to unlock again. The machine session is simply re-minted.
|
|
675
790
|
let _ = session::delete_auto(paths);
|
|
676
791
|
if payload.config.unlock.mode == UnlockMode::Never {
|
|
677
|
-
let auto = Session::new("rekey", 0, None, &sealed, &new_key);
|
|
792
|
+
let auto = Session::new("rekey", 0, None, &sealed, &new_key).with_kind(session::SessionKind::Auto);
|
|
678
793
|
session::save_auto(paths, &auto)?;
|
|
679
794
|
}
|
|
680
795
|
audit::append(&paths.audit(), "cli", "rekey", None, Some(&format!("N={}", kdf.n)));
|
|
@@ -795,18 +910,25 @@ fn init(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_mi
|
|
|
795
910
|
session::ensure_session_dir(paths)?;
|
|
796
911
|
let (idle, deadline, policy) = policy(idle_min, &config);
|
|
797
912
|
let session = Session::new(requester, idle, deadline, &sealed, &key);
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
913
|
+
match policy.mode {
|
|
914
|
+
UnlockMode::Never => {
|
|
915
|
+
session::save_auto(paths, &session.clone().with_kind(session::SessionKind::Auto))?;
|
|
916
|
+
}
|
|
917
|
+
UnlockMode::Every => {
|
|
918
|
+
// Nothing is kept: every later call brings its own password.
|
|
919
|
+
}
|
|
920
|
+
_ => {
|
|
921
|
+
let signer = signer(paths)?;
|
|
922
|
+
session.save(paths, &signer)?;
|
|
923
|
+
}
|
|
803
924
|
}
|
|
804
925
|
audit::append(&paths.audit(), requester, "init", None, None);
|
|
805
926
|
let value = json!({
|
|
806
927
|
"ok": true,
|
|
807
928
|
"path": paths.store(),
|
|
808
|
-
|
|
809
|
-
"
|
|
929
|
+
// In `every` there is no token to hand back; the host asks per call.
|
|
930
|
+
"token": if policy.mode == UnlockMode::Every { None } else { Some(session.token.clone()) },
|
|
931
|
+
"hostSecret": if matches!(policy.mode, UnlockMode::Never | UnlockMode::Every) || !is_host() { None } else { host_secret() },
|
|
810
932
|
"expiresAt": session.expiry(),
|
|
811
933
|
"items": 0,
|
|
812
934
|
"mode": policy.mode.as_str(),
|
|
@@ -850,17 +972,23 @@ fn unlock(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_
|
|
|
850
972
|
// The host brings its own secret (so the same process can sign later
|
|
851
973
|
// calls); everyone else is signed with the wrap key.
|
|
852
974
|
let session = Session::new(requester, idle, deadline, &store, &key);
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
975
|
+
match policy.mode {
|
|
976
|
+
UnlockMode::Never => {
|
|
977
|
+
session::save_auto(paths, &session.clone().with_kind(session::SessionKind::Auto))?;
|
|
978
|
+
}
|
|
979
|
+
UnlockMode::Every => {
|
|
980
|
+
// Nothing is kept: every later call brings its own password.
|
|
981
|
+
}
|
|
982
|
+
_ => {
|
|
983
|
+
let signer = signer(paths)?;
|
|
984
|
+
session.save(paths, &signer)?;
|
|
985
|
+
}
|
|
858
986
|
}
|
|
859
987
|
audit::append(&paths.audit(), requester, "unlock", None, Some(policy.mode.as_str()));
|
|
860
988
|
let value = json!({
|
|
861
989
|
"ok": true,
|
|
862
|
-
"token": session.token,
|
|
863
|
-
"hostSecret": if policy.mode
|
|
990
|
+
"token": if policy.mode == UnlockMode::Every { None } else { Some(session.token.clone()) },
|
|
991
|
+
"hostSecret": if matches!(policy.mode, UnlockMode::Never | UnlockMode::Every) || !is_host() { None } else { host_secret() },
|
|
864
992
|
"expiresAt": session.expiry(),
|
|
865
993
|
"items": items.len(),
|
|
866
994
|
"requester": requester,
|
|
@@ -917,7 +1045,7 @@ fn settings_value(config: &config::Config, paths: &Paths) -> serde_json::Value {
|
|
|
917
1045
|
fn settings_human(config: &config::Config, paths: &Paths) -> String {
|
|
918
1046
|
let yes_no = |yes: bool| tr(if yes { "yes" } else { "no" }, if yes { "有" } else { "无" });
|
|
919
1047
|
[
|
|
920
|
-
format!("vault.unlock: {} (
|
|
1048
|
+
format!("vault.unlock: {} (every | time | never)", config.unlock.mode.as_str()),
|
|
921
1049
|
format!("vault.hours: {}{}", config.unlock.hours, tr(" (time mode)", " (time 模式)")),
|
|
922
1050
|
format!(
|
|
923
1051
|
"vault.confirm: {}{}",
|
|
@@ -992,10 +1120,19 @@ fn settings(
|
|
|
992
1120
|
|
|
993
1121
|
match key {
|
|
994
1122
|
"vault.unlock" => {
|
|
995
|
-
let mode = UnlockMode::
|
|
996
|
-
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" —
|
|
997
|
-
//
|
|
998
|
-
|
|
1123
|
+
let mode = UnlockMode::parse_store(raw_value)
|
|
1124
|
+
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — every, time or never")))?;
|
|
1125
|
+
// Moving away from `every` is the lowering direction (something will
|
|
1126
|
+
// be kept that opens the store without a password): verify.
|
|
1127
|
+
let (payload, vault_key) = if mode == UnlockMode::Every {
|
|
1128
|
+
// `every` promises that nothing on disk opens the store: every
|
|
1129
|
+
// live token goes, including the one this call came in on.
|
|
1130
|
+
session::delete_all_of(paths, &store);
|
|
1131
|
+
let payload = payload_with(&session, &store)?;
|
|
1132
|
+
(payload, crypto::Key::new([0u8; 32]))
|
|
1133
|
+
} else {
|
|
1134
|
+
verify_master_with(paths, &store, password_stdin, tr("switching the unlock policy", "切换解锁模式"))?
|
|
1135
|
+
};
|
|
999
1136
|
config.unlock.mode = mode;
|
|
1000
1137
|
if let Some(hours) = hours {
|
|
1001
1138
|
config.unlock.hours = hours.clamp(1, 24 * 90);
|
|
@@ -1003,7 +1140,7 @@ fn settings(
|
|
|
1003
1140
|
config_write(paths, &session, &config)?;
|
|
1004
1141
|
session::ensure_session_dir(paths)?;
|
|
1005
1142
|
if mode == UnlockMode::Never {
|
|
1006
|
-
let auto = Session::new("mode", 0, None, &store, &vault_key);
|
|
1143
|
+
let auto = Session::new("mode", 0, None, &store, &vault_key).with_kind(session::SessionKind::Auto);
|
|
1007
1144
|
session::save_auto(paths, &auto)?;
|
|
1008
1145
|
} else {
|
|
1009
1146
|
session::delete_auto(paths);
|
|
@@ -1036,7 +1173,8 @@ fn settings(
|
|
|
1036
1173
|
}
|
|
1037
1174
|
"bitwarden.unlock" => {
|
|
1038
1175
|
let mode = UnlockMode::parse(raw_value)
|
|
1039
|
-
.
|
|
1176
|
+
.filter(|m| *m != UnlockMode::Every)
|
|
1177
|
+
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — never, time or session (the Bitwarden and Ente keys have no `every`: their CLI keeps its own session)")))?;
|
|
1040
1178
|
// Keeping the bw session key on disk is a downgrade of its own.
|
|
1041
1179
|
if mode != UnlockMode::Session {
|
|
1042
1180
|
verify_master(paths, &store, password_stdin, tr("keeping the Bitwarden session on disk", "把 Bitwarden 会话落盘"))?;
|
|
@@ -1060,7 +1198,8 @@ fn settings(
|
|
|
1060
1198
|
}
|
|
1061
1199
|
"ente.unlock" => {
|
|
1062
1200
|
let mode = UnlockMode::parse(raw_value)
|
|
1063
|
-
.
|
|
1201
|
+
.filter(|m| *m != UnlockMode::Every)
|
|
1202
|
+
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — never, time or session (the Bitwarden and Ente keys have no `every`: their CLI keeps its own session)")))?;
|
|
1064
1203
|
if mode != UnlockMode::Session {
|
|
1065
1204
|
verify_master(paths, &store, password_stdin, tr("keeping the Ente key on disk", "把 Ente 密钥落盘"))?;
|
|
1066
1205
|
}
|
|
@@ -1107,10 +1246,22 @@ fn verify_master(
|
|
|
1107
1246
|
password_stdin: bool,
|
|
1108
1247
|
why: &str,
|
|
1109
1248
|
) -> Result<(crypto::Payload, crypto::Key), Fault> {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1249
|
+
verify_master_with(paths, store, password_stdin, why)
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
/// Prove the master password. The sources, in order: the password an `every`
|
|
1253
|
+
/// call was opened with (already proven, never asked twice), `AI_VAULT_PASSWORD`
|
|
1254
|
+
/// from a host that asked in its own UI, `--password-stdin`, then a window.
|
|
1255
|
+
fn verify_master_with(
|
|
1256
|
+
paths: &Paths,
|
|
1257
|
+
store: &crypto::StoreFile,
|
|
1258
|
+
password_stdin: bool,
|
|
1259
|
+
why: &str,
|
|
1260
|
+
) -> Result<(crypto::Payload, crypto::Key), Fault> {
|
|
1261
|
+
let every = EVERY_PASSWORD.with(|cell| cell.borrow().clone());
|
|
1262
|
+
let password = match every {
|
|
1263
|
+
Some(password) => password,
|
|
1264
|
+
None => master_password_from(why, password_stdin)?,
|
|
1114
1265
|
};
|
|
1115
1266
|
crypto::open_with_passphrase(store, &password).map_err(|e| match e {
|
|
1116
1267
|
crypto::OpenError::WrongPassword => {
|
|
@@ -1206,6 +1357,10 @@ fn status(paths: &Paths, out: &Out, token: Option<String>) -> Outcome {
|
|
|
1206
1357
|
let value = json!({
|
|
1207
1358
|
"ok": true,
|
|
1208
1359
|
"unlocked": true,
|
|
1360
|
+
// The session this call ran under: when the caller came without a
|
|
1361
|
+
// token and adopted (or was given) one, this is how it learns the
|
|
1362
|
+
// token to sign its relayed approvals with.
|
|
1363
|
+
"token": if session.kind == session::SessionKind::Auto { None } else { Some(session.token.clone()) },
|
|
1209
1364
|
"items": items.len(),
|
|
1210
1365
|
"requester": session.requester,
|
|
1211
1366
|
"idleMs": session.idle_ms,
|
|
@@ -1312,7 +1467,11 @@ fn get(
|
|
|
1312
1467
|
let now = session::now_ms();
|
|
1313
1468
|
// `never` mode is the "the agent may use these freely" mode: the machine-wide
|
|
1314
1469
|
// session is the authorization, so no window opens on each read.
|
|
1315
|
-
|
|
1470
|
+
// Only the `never`-mode machine session *is* the approval. An `every`
|
|
1471
|
+
// session was opened by the password that came with this call, which is
|
|
1472
|
+
// the approval too — but it is a different thing, and is labelled so.
|
|
1473
|
+
let auto = session.kind == session::SessionKind::Auto;
|
|
1474
|
+
let every = session.kind == session::SessionKind::Every;
|
|
1316
1475
|
// `vault.confirm = false`: the master password that opened this session is
|
|
1317
1476
|
// the approval. Safe for exactly that reason — a session the extension
|
|
1318
1477
|
// opened can only exist because the user typed the password (a token the
|
|
@@ -1321,6 +1480,9 @@ fn get(
|
|
|
1321
1480
|
let no_confirm = payload.config.unlock.confirm == false;
|
|
1322
1481
|
let via = if auto {
|
|
1323
1482
|
"no-password"
|
|
1483
|
+
} else if every {
|
|
1484
|
+
audit::append(&paths.audit(), &session.requester, "read_allow", Some(&item.name), Some("every"));
|
|
1485
|
+
"every"
|
|
1324
1486
|
} else if no_confirm {
|
|
1325
1487
|
audit::append(&paths.audit(), &session.requester, "read_allow", Some(&item.name), Some("confirm-off"));
|
|
1326
1488
|
"unlocked-no-confirm"
|