@xiaolikt/ai-vault 0.7.0 → 0.9.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/Cargo.lock +1 -1
- package/README.md +44 -1
- package/crates/ai-vault/Cargo.toml +1 -1
- package/crates/ai-vault/src/cli.rs +71 -24
- package/crates/ai-vault/src/config.rs +25 -7
- package/crates/ai-vault/src/main.rs +1 -1
- package/crates/ai-vault/src/paths.rs +0 -6
- package/crates/ai-vault/src/{bwsession.rs → sealed.rs} +48 -21
- package/package.json +2 -2
- package/packages/pi-ai-vault/README.md +8 -0
- package/packages/pi-ai-vault/package.json +2 -2
- package/packages/pi-ai-vault/src/bitwarden.ts +12 -9
- package/packages/pi-ai-vault/src/ente.ts +304 -0
- package/packages/pi-ai-vault/src/index.ts +26 -1
- package/packages/pi-ai-vault/src/settings.ts +3 -0
- package/test/cli.sh +12 -1
- package/tests/ente.test.mjs +137 -0
- package/tests/helpers.mjs +5 -0
- package/tests/mocks/bin/ente +68 -0
- package/tests/register.test.mjs +5 -1
package/Cargo.lock
CHANGED
package/README.md
CHANGED
|
@@ -93,8 +93,9 @@ ai-vault [--dir DIR] [--json] <命令>
|
|
|
93
93
|
| `init [--password-stdin]` | 建库(弹窗两次设密码),成功后直接给一个令牌 |
|
|
94
94
|
| `unlock [--password-stdin] [--requester ID]` | 按当前模式解锁,stdout 一个令牌 |
|
|
95
95
|
| `mode [get]` / `mode set never\|time\|session [--hours N]` | 查看/切换解锁模式(切换要主密码) |
|
|
96
|
-
| `settings [键] [值]` | 全部设置:`vault
|
|
96
|
+
| `settings [键] [值]` | 全部设置:`vault.*` `bitwarden.*` `ente.unlock` `ente.hours` |
|
|
97
97
|
| `bw-session get\|put\|clear` | Bitwarden 会话密钥的落盘副本(`bitwarden.unlock != session` 时用,加密封存) |
|
|
98
|
+
| `ente-session get\|put\|clear` | Ente 的 `ENTE_CLI_VAULT_KEY`(`ente.unlock != session` 时用,同样加密封存) |
|
|
98
99
|
| `rekey` | 把旧库的 scrypt 参数升到当前默认(要主密码) |
|
|
99
100
|
| `status --token T` | 条目数、过期时间、还活着的读授权 |
|
|
100
101
|
| `lock --token T \| --all` | 丢掉令牌(`--all` 清空本机所有会话) |
|
|
@@ -146,6 +147,10 @@ ai-vault get <条目> --token <T> --answer-stdin --approval <hex> <<< 'y'
|
|
|
146
147
|
| `bw_inject` | 注入 bash 环境(**仅 pi**) | exec | 否 |
|
|
147
148
|
| `bw_clipboard` | 复制到剪贴板,到时清除 | write | 否 |
|
|
148
149
|
| `bw_reveal` | 明文返回(默认关闭,每次确认) | exec | 是,用户当场允许 |
|
|
150
|
+
| `ente_list` | Ente Auth 条目元数据(名称/签发方/账号/类型) | read | 否 |
|
|
151
|
+
| `ente_code` | 读一条当前 TOTP/Steam 验证码,每次弹窗确认 | write | 是,用户当场允许 |
|
|
152
|
+
|
|
153
|
+
Ente 的会话由 `ente` 自己管(登录一次);`ente.unlock` 只决定那把 `ENTE_CLI_VAULT_KEY` 要不要由 ai-vault 加密封存。
|
|
149
154
|
|
|
150
155
|
字段选择:`password`(默认)· `totp`(种子)· `code`(动态验证码)· `notes` · `field:<自定义字段名>`。
|
|
151
156
|
|
|
@@ -244,6 +249,44 @@ ai-vault settings bitwarden.hours 24
|
|
|
244
249
|
- `/bw lock` 只丢弃当前绑定的会话密钥;同一进程里还有别的绑定持有时不会执行全局 `bw lock`,最后一个绑定释放时才真正作废 CLI 会话。
|
|
245
250
|
- Bitwarden 会话密钥只在 agent 进程内存里,不落盘、不进 bash 环境。
|
|
246
251
|
|
|
252
|
+
## Ente(Auth 验证码)
|
|
253
|
+
|
|
254
|
+
`ente` 是 Ente 官方的终端客户端(0.4+)。它**自己登录一次**:
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
ente account login --app auth # 邮箱 + 密码(+ 2FA 验证码)
|
|
258
|
+
ente auth code list # 之后每次都不再问密码
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
会话存在 `$XDG_DATA_HOME/ente-cli`(Linux 默认 `~/.local/share/ente-cli`),数据库密钥默认放系统钥匙串;无桌面/无人值守时用
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
ente vault key generate # 打印一个 key
|
|
265
|
+
export ENTE_CLI_VAULT_KEY=<那个 key> # 之后连钥匙串都不用
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
所以在 `session` 这种日常形态下,**每次查看不会再要你的密码**,最多要一次系统钥匙串解锁。
|
|
269
|
+
|
|
270
|
+
想连钥匙串都不要(无人值守):
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
ente vault key generate # 打印一个 key,拿一次就固定
|
|
274
|
+
/vault ente-key # 在弹窗里粘进去,ai-vault 把它加密封存
|
|
275
|
+
/vault settings ente.unlock never # 之后 ente 子进程自动带上它
|
|
276
|
+
```
|
|
277
|
+
`ente.unlock` 和 `bitwarden.unlock` 是同一套三档(`never`/`time`/`session`),`ente-session get|put|clear` 是它的 CLI 入口;密钥用库目录的 wrap key 封存(0600),单独拷走 `ente.session.json` 解不开。插件永远不会把你 shell 里导出的 `ENTE_CLI_VAULT_KEY` 传给子进程——只有它自己封存的那把会。
|
|
278
|
+
|
|
279
|
+
pi-ai-vault 只是这套 CLI 的客户端,本身不保存 Ente 的任何密钥:
|
|
280
|
+
|
|
281
|
+
| 工具 | 作用 |
|
|
282
|
+
|---|---|
|
|
283
|
+
| `ente_list` | 列出条目(name / issuer / accountName / type),**不含验证码** |
|
|
284
|
+
| `ente_code <名字>` | 弹窗显示实际命中的条目,允许后返回当前验证码和过期时间 |
|
|
285
|
+
|
|
286
|
+
安全上和 `bw_*` 一套规则:列表只给元数据;读码每次确认;拒绝后不重试;允许/拒绝都写审计(`~/.bw/pi-extension/audit.log`,`ente.code` 事件,不含码本身);条目名/签发方进窗口前会被压成一行,防伪造。
|
|
287
|
+
|
|
288
|
+
插件启动的 bash 会剔除 `ENTE_CLI_VAULT_KEY` / `ENTE_CLI_PASSWORD`,避免你 shell 里导出的免密 key 漏进模型自己的命令环境。**注意**:`ente` 二进制本身也在 PATH 上,模型直接跑 `ente auth code view` 仍能绕过这个扩展的确认窗——要收紧要靠 `ENTE_CLI_VAULT_KEY` + 把模型的 `ENTE_CLI_HOME` 指到别处,或者干脆不让 agent 有 shell。
|
|
289
|
+
|
|
247
290
|
## 安全
|
|
248
291
|
|
|
249
292
|
### 威胁模型:谁能做什么
|
|
@@ -9,7 +9,7 @@ use std::path::PathBuf;
|
|
|
9
9
|
use clap::{Parser, Subcommand};
|
|
10
10
|
use serde_json::json;
|
|
11
11
|
|
|
12
|
-
use crate::
|
|
12
|
+
use crate::sealed::{self, Kind};
|
|
13
13
|
use crate::config::{self, ImportPolicy, UnlockMode, UnlockPolicy};
|
|
14
14
|
use crate::crypto::{self, Kdf};
|
|
15
15
|
use crate::dialog::{self, Answer};
|
|
@@ -84,6 +84,11 @@ enum Cmd {
|
|
|
84
84
|
/// get | put | clear
|
|
85
85
|
action: String,
|
|
86
86
|
},
|
|
87
|
+
/// Ente's `ENTE_CLI_VAULT_KEY` (ente.unlock = never | time)
|
|
88
|
+
EnteSession {
|
|
89
|
+
/// get | put | clear
|
|
90
|
+
action: String,
|
|
91
|
+
},
|
|
87
92
|
/// Show or change how often the master password is asked for
|
|
88
93
|
Mode {
|
|
89
94
|
/// "get" or "set"
|
|
@@ -321,7 +326,8 @@ fn dispatch(cmd: Cmd, paths: &Paths, json: bool) -> Outcome {
|
|
|
321
326
|
Cmd::Settings { key, value, password_stdin } => {
|
|
322
327
|
settings(paths, &out, key.as_deref(), value.as_deref(), None, password_stdin)
|
|
323
328
|
}
|
|
324
|
-
Cmd::BwSession { action } =>
|
|
329
|
+
Cmd::BwSession { action } => sealed_session(paths, &out, Kind::Bitwarden, &action),
|
|
330
|
+
Cmd::EnteSession { action } => sealed_session(paths, &out, Kind::Ente, &action),
|
|
325
331
|
Cmd::Mode { action, value, hours, password_stdin } => {
|
|
326
332
|
mode(paths, &out, &action, value.as_deref(), hours, password_stdin)
|
|
327
333
|
}
|
|
@@ -488,7 +494,7 @@ fn signing_key(paths: &Paths) -> Result<Option<String>, String> {
|
|
|
488
494
|
if let Some(secret) = host_secret() {
|
|
489
495
|
return Ok(Some(secret));
|
|
490
496
|
}
|
|
491
|
-
|
|
497
|
+
sealed::wrap_key_hex(paths).map(Some)
|
|
492
498
|
}
|
|
493
499
|
|
|
494
500
|
/// The host integration (the extension) is recognised by the secret it
|
|
@@ -496,6 +502,13 @@ fn signing_key(paths: &Paths) -> Result<Option<String>, String> {
|
|
|
496
502
|
/// and is treated like a human caller until it has a secret to sign with.
|
|
497
503
|
/// This keeps a half-configured host from getting a session it can neither
|
|
498
504
|
/// verify nor relay for.
|
|
505
|
+
fn kind_label(kind: Kind) -> &'static str {
|
|
506
|
+
match kind {
|
|
507
|
+
Kind::Bitwarden => "bitwarden",
|
|
508
|
+
Kind::Ente => "ente",
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
499
512
|
fn is_host() -> bool {
|
|
500
513
|
std::env::var_os("AI_VAULT_HOST").is_some_and(|v| v == "1") && host_secret().is_some()
|
|
501
514
|
}
|
|
@@ -749,12 +762,15 @@ fn setting_key(raw: &str) -> Option<&'static str> {
|
|
|
749
762
|
"bitwarden.unlock" | "bitwarden.mode" | "bw.unlock" | "bw.mode" => Some("bitwarden.unlock"),
|
|
750
763
|
"bitwarden.hours" | "bw.hours" => Some("bitwarden.hours"),
|
|
751
764
|
"bitwarden.import" | "bw.import" | "import" => Some("bitwarden.import"),
|
|
765
|
+
"ente.unlock" | "ente.mode" => Some("ente.unlock"),
|
|
766
|
+
"ente.hours" => Some("ente.hours"),
|
|
752
767
|
_ => None,
|
|
753
768
|
}
|
|
754
769
|
}
|
|
755
770
|
|
|
756
771
|
fn settings_value(config: &config::Config, paths: &Paths) -> serde_json::Value {
|
|
757
|
-
let bw_saved =
|
|
772
|
+
let bw_saved = sealed::load(paths, Kind::Bitwarden).ok().flatten();
|
|
773
|
+
let ente_saved = sealed::load(paths, Kind::Ente).ok().flatten();
|
|
758
774
|
json!({
|
|
759
775
|
"ok": true,
|
|
760
776
|
"vault": {
|
|
@@ -763,12 +779,18 @@ fn settings_value(config: &config::Config, paths: &Paths) -> serde_json::Value {
|
|
|
763
779
|
"auto": paths.auto_session().exists(),
|
|
764
780
|
},
|
|
765
781
|
"bitwarden": {
|
|
766
|
-
"unlock": config.bitwarden.unlock.as_str(),
|
|
767
|
-
"hours": config.bitwarden.hours,
|
|
782
|
+
"unlock": config.bitwarden.store.unlock.as_str(),
|
|
783
|
+
"hours": config.bitwarden.store.hours,
|
|
768
784
|
"import": config.bitwarden.import.as_str(),
|
|
769
785
|
"persisted": bw_saved.is_some(),
|
|
770
786
|
"expiresAt": bw_saved.as_ref().and_then(|s| s.expires_at),
|
|
771
787
|
},
|
|
788
|
+
"ente": {
|
|
789
|
+
"unlock": config.ente.unlock.as_str(),
|
|
790
|
+
"hours": config.ente.hours,
|
|
791
|
+
"persisted": ente_saved.is_some(),
|
|
792
|
+
"expiresAt": ente_saved.as_ref().and_then(|s| s.expires_at),
|
|
793
|
+
},
|
|
772
794
|
})
|
|
773
795
|
}
|
|
774
796
|
|
|
@@ -780,12 +802,15 @@ fn settings_human(config: &config::Config, paths: &Paths) -> String {
|
|
|
780
802
|
"vault 机器级会话: {}",
|
|
781
803
|
if paths.auto_session().exists() { "有(免密)" } else { "无" }
|
|
782
804
|
),
|
|
783
|
-
format!("bitwarden.unlock: {} (never | time | session)", config.bitwarden.unlock.as_str()),
|
|
784
|
-
format!("bitwarden.hours: {}", config.bitwarden.hours),
|
|
805
|
+
format!("bitwarden.unlock: {} (never | time | session)", config.bitwarden.store.unlock.as_str()),
|
|
806
|
+
format!("bitwarden.hours: {}", config.bitwarden.store.hours),
|
|
785
807
|
format!("bitwarden.import: {} (ask | always | never)", config.bitwarden.import.as_str()),
|
|
808
|
+
format!("ente.unlock: {} (never | time | session)", config.ente.unlock.as_str()),
|
|
809
|
+
format!("ente.hours: {}", config.ente.hours),
|
|
810
|
+
format!("ente 已落盘的密钥: {}", if sealed::load(paths, Kind::Ente).ok().flatten().is_some() { "有" } else { "无" }),
|
|
786
811
|
format!(
|
|
787
812
|
"bitwarden 已落盘的会话: {}",
|
|
788
|
-
match
|
|
813
|
+
match sealed::load(paths, Kind::Bitwarden).ok().flatten() {
|
|
789
814
|
Some(saved) => match saved.expires_at {
|
|
790
815
|
Some(at) => format!("有({} 后过期)", ((at - session::now_ms()) / 60_000).max(0)),
|
|
791
816
|
None => "有(不过期)".to_string(),
|
|
@@ -822,8 +847,10 @@ fn settings(
|
|
|
822
847
|
let shown = match key {
|
|
823
848
|
"vault.unlock" => config.unlock.mode.as_str().to_string(),
|
|
824
849
|
"vault.hours" => config.unlock.hours.to_string(),
|
|
825
|
-
"bitwarden.unlock" => config.bitwarden.unlock.as_str().to_string(),
|
|
826
|
-
"bitwarden.hours" => config.bitwarden.hours.to_string(),
|
|
850
|
+
"bitwarden.unlock" => config.bitwarden.store.unlock.as_str().to_string(),
|
|
851
|
+
"bitwarden.hours" => config.bitwarden.store.hours.to_string(),
|
|
852
|
+
"ente.unlock" => config.ente.unlock.as_str().to_string(),
|
|
853
|
+
"ente.hours" => config.ente.hours.to_string(),
|
|
827
854
|
_ => config.bitwarden.import.as_str().to_string(),
|
|
828
855
|
};
|
|
829
856
|
out.value(json!({ "ok": true, "key": key, "value": shown }), || format!("{key}: {shown}"));
|
|
@@ -866,21 +893,40 @@ fn settings(
|
|
|
866
893
|
"bitwarden.unlock" => {
|
|
867
894
|
let mode = UnlockMode::parse(raw_value)
|
|
868
895
|
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — never, time or session")))?;
|
|
869
|
-
config.bitwarden.unlock = mode;
|
|
896
|
+
config.bitwarden.store.unlock = mode;
|
|
870
897
|
if let Some(hours) = hours {
|
|
871
|
-
config.bitwarden.hours = hours.clamp(1, 24 * 90);
|
|
898
|
+
config.bitwarden.store.hours = hours.clamp(1, 24 * 90);
|
|
872
899
|
}
|
|
873
900
|
config::save(paths, &config)?;
|
|
874
901
|
// Going back to per-session must not leave a key on disk.
|
|
875
902
|
if mode == UnlockMode::Session {
|
|
876
|
-
|
|
903
|
+
sealed::clear(paths, Kind::Bitwarden);
|
|
877
904
|
}
|
|
878
905
|
}
|
|
879
906
|
"bitwarden.hours" => {
|
|
880
907
|
let hours: i64 = raw_value
|
|
881
908
|
.parse()
|
|
882
909
|
.map_err(|_| Fault::Usage(format!("\"{raw_value}\" is not a number of hours")))?;
|
|
883
|
-
config.bitwarden.hours = hours.clamp(1, 24 * 90);
|
|
910
|
+
config.bitwarden.store.hours = hours.clamp(1, 24 * 90);
|
|
911
|
+
config::save(paths, &config)?;
|
|
912
|
+
}
|
|
913
|
+
"ente.unlock" => {
|
|
914
|
+
let mode = UnlockMode::parse(raw_value)
|
|
915
|
+
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — never, time or session")))?;
|
|
916
|
+
config.ente.unlock = mode;
|
|
917
|
+
if let Some(hours) = hours {
|
|
918
|
+
config.ente.hours = hours.clamp(1, 24 * 90);
|
|
919
|
+
}
|
|
920
|
+
config::save(paths, &config)?;
|
|
921
|
+
if mode == UnlockMode::Session {
|
|
922
|
+
sealed::clear(paths, Kind::Ente);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
"ente.hours" => {
|
|
926
|
+
let hours: i64 = raw_value
|
|
927
|
+
.parse()
|
|
928
|
+
.map_err(|_| Fault::Usage(format!("\"{raw_value}\" is not a number of hours")))?;
|
|
929
|
+
config.ente.hours = hours.clamp(1, 24 * 90);
|
|
884
930
|
config::save(paths, &config)?;
|
|
885
931
|
}
|
|
886
932
|
_ => {
|
|
@@ -913,12 +959,13 @@ fn mode(
|
|
|
913
959
|
}
|
|
914
960
|
}
|
|
915
961
|
|
|
916
|
-
fn
|
|
962
|
+
fn sealed_session(paths: &Paths, out: &Out, kind: Kind, action: &str) -> Outcome {
|
|
917
963
|
match action.trim() {
|
|
918
964
|
"" | "get" => {
|
|
919
|
-
let stored =
|
|
920
|
-
.ok_or_else(|| Fault::Auth("no persisted
|
|
921
|
-
let key =
|
|
965
|
+
let stored = sealed::load(paths, kind)?
|
|
966
|
+
.ok_or_else(|| Fault::Auth(format!("no persisted {} secret — save one first", kind_label(kind))))?;
|
|
967
|
+
let key = sealed::key(paths, kind)?
|
|
968
|
+
.ok_or_else(|| Fault::Auth(format!("no persisted {} secret", kind_label(kind))))?;
|
|
922
969
|
out.value(
|
|
923
970
|
json!({ "ok": true, "key": key, "savedAt": stored.saved_at, "expiresAt": stored.expires_at }),
|
|
924
971
|
|| key,
|
|
@@ -930,8 +977,8 @@ fn bw_session(paths: &Paths, out: &Out, action: &str) -> Outcome {
|
|
|
930
977
|
if key.is_empty() {
|
|
931
978
|
return Err(Fault::Usage("bw-session put expects the session key on stdin".into()));
|
|
932
979
|
}
|
|
933
|
-
let expires_at =
|
|
934
|
-
audit::append(&paths.audit(), "cli", "
|
|
980
|
+
let expires_at = sealed::save(paths, kind, &key)?;
|
|
981
|
+
audit::append(&paths.audit(), "cli", "sealed", None, Some(kind_label(kind)));
|
|
935
982
|
out.value(json!({ "ok": true, "expiresAt": expires_at }), || {
|
|
936
983
|
match expires_at {
|
|
937
984
|
Some(at) => format!("saved (expires in {} min)", ((at - session::now_ms()) / 60_000).max(0)),
|
|
@@ -941,14 +988,14 @@ fn bw_session(paths: &Paths, out: &Out, action: &str) -> Outcome {
|
|
|
941
988
|
Ok(())
|
|
942
989
|
}
|
|
943
990
|
"clear" | "delete" => {
|
|
944
|
-
let removed =
|
|
945
|
-
audit::append(&paths.audit(), "cli", "
|
|
991
|
+
let removed = sealed::clear(paths, kind);
|
|
992
|
+
audit::append(&paths.audit(), "cli", "sealed", None, Some(kind_label(kind)));
|
|
946
993
|
out.value(json!({ "ok": true, "removed": removed }), || {
|
|
947
994
|
if removed { "cleared".to_string() } else { "nothing stored".to_string() }
|
|
948
995
|
});
|
|
949
996
|
Ok(())
|
|
950
997
|
}
|
|
951
|
-
other => Err(Fault::Usage(format!("unknown
|
|
998
|
+
other => Err(Fault::Usage(format!("unknown session action \"{other}\" (get, put, clear)"))),
|
|
952
999
|
}
|
|
953
1000
|
}
|
|
954
1001
|
|
|
@@ -90,15 +90,28 @@ impl Default for UnlockPolicy {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/// How a third-party CLI's credential is kept on disk.
|
|
93
94
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
94
|
-
pub struct
|
|
95
|
-
/// `never` keeps the
|
|
96
|
-
///
|
|
97
|
-
/// deadline; `session` (default)
|
|
95
|
+
pub struct StorePolicy {
|
|
96
|
+
/// `never` keeps the credential in the store directory (sealed), so the
|
|
97
|
+
/// CLI needs no keychain or password; `time` does the same with a
|
|
98
|
+
/// deadline; `session` (default) leaves it in the CLI's own keychain.
|
|
98
99
|
#[serde(default = "default_bw_mode")]
|
|
99
100
|
pub unlock: UnlockMode,
|
|
100
101
|
#[serde(default = "default_hours")]
|
|
101
102
|
pub hours: i64,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
impl Default for StorePolicy {
|
|
106
|
+
fn default() -> Self {
|
|
107
|
+
StorePolicy { unlock: default_bw_mode(), hours: default_hours() }
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
112
|
+
pub struct BitwardenPolicy {
|
|
113
|
+
#[serde(flatten)]
|
|
114
|
+
pub store: StorePolicy,
|
|
102
115
|
#[serde(default = "default_import")]
|
|
103
116
|
pub import: ImportPolicy,
|
|
104
117
|
}
|
|
@@ -113,7 +126,7 @@ fn default_import() -> ImportPolicy {
|
|
|
113
126
|
|
|
114
127
|
impl Default for BitwardenPolicy {
|
|
115
128
|
fn default() -> Self {
|
|
116
|
-
BitwardenPolicy {
|
|
129
|
+
BitwardenPolicy { store: StorePolicy::default(), import: default_import() }
|
|
117
130
|
}
|
|
118
131
|
}
|
|
119
132
|
|
|
@@ -123,6 +136,10 @@ pub struct Config {
|
|
|
123
136
|
pub unlock: UnlockPolicy,
|
|
124
137
|
#[serde(default)]
|
|
125
138
|
pub bitwarden: BitwardenPolicy,
|
|
139
|
+
/// Ente's `ENTE_CLI_VAULT_KEY`: `session` leaves it to the OS keychain,
|
|
140
|
+
/// `never`/`time` keep it sealed next to the store.
|
|
141
|
+
#[serde(default)]
|
|
142
|
+
pub ente: StorePolicy,
|
|
126
143
|
}
|
|
127
144
|
|
|
128
145
|
/// A missing or unreadable config means the defaults. A broken file must not
|
|
@@ -156,7 +173,8 @@ mod tests {
|
|
|
156
173
|
fn defaults_are_the_cautious_ones() {
|
|
157
174
|
let config = Config::default();
|
|
158
175
|
assert_eq!(config.unlock.mode, UnlockMode::Session);
|
|
159
|
-
assert_eq!(config.bitwarden.unlock, UnlockMode::Session);
|
|
176
|
+
assert_eq!(config.bitwarden.store.unlock, UnlockMode::Session);
|
|
177
|
+
assert_eq!(config.ente.unlock, UnlockMode::Session);
|
|
160
178
|
assert_eq!(config.bitwarden.import, ImportPolicy::Ask);
|
|
161
179
|
}
|
|
162
180
|
|
|
@@ -166,6 +184,6 @@ mod tests {
|
|
|
166
184
|
let parsed: Config = serde_json::from_str(r#"{"unlock":{"mode":"time","hours":4}}"#).unwrap();
|
|
167
185
|
assert_eq!(parsed.unlock.mode, UnlockMode::Time);
|
|
168
186
|
assert_eq!(parsed.bitwarden.import, ImportPolicy::Ask);
|
|
169
|
-
assert_eq!(parsed.bitwarden.hours, 8);
|
|
187
|
+
assert_eq!(parsed.bitwarden.store.hours, 8);
|
|
170
188
|
}
|
|
171
189
|
}
|
|
@@ -42,12 +42,6 @@ impl Paths {
|
|
|
42
42
|
self.dir.join("agent.session.json")
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
/// The `bw` session key kept for `bitwarden.unlock = never | time`,
|
|
46
|
-
/// sealed under `wrap_key`.
|
|
47
|
-
pub fn bw_session(&self) -> PathBuf {
|
|
48
|
-
self.dir.join("bitwarden.session.json")
|
|
49
|
-
}
|
|
50
|
-
|
|
51
45
|
/// Random 256-bit key that seals the Bitwarden session at rest, so that
|
|
52
46
|
/// file alone is not a bearer token. Same exposure as the store itself.
|
|
53
47
|
pub fn wrap_key(&self) -> PathBuf {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
//!
|
|
1
|
+
//! Secrets kept on disk for `*.unlock = never | time`: the `bw` session key
|
|
2
|
+
//! and Ente's `ENTE_CLI_VAULT_KEY`.
|
|
2
3
|
//!
|
|
3
4
|
//! `bw unlock` hands back a bearer key for the *whole* Bitwarden vault, so the
|
|
4
5
|
//! file is sealed: ChaCha20-Poly1305 under a random 256-bit key in
|
|
@@ -17,6 +18,30 @@ use crate::crypto;
|
|
|
17
18
|
use crate::paths::Paths;
|
|
18
19
|
use crate::session::now_ms;
|
|
19
20
|
|
|
21
|
+
/// Which secret this file holds. They are sealed the same way but carry
|
|
22
|
+
/// different names and different policies.
|
|
23
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
24
|
+
pub enum Kind {
|
|
25
|
+
Bitwarden,
|
|
26
|
+
Ente,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
impl Kind {
|
|
30
|
+
fn file(self) -> &'static str {
|
|
31
|
+
match self {
|
|
32
|
+
Kind::Bitwarden => "bitwarden.session.json",
|
|
33
|
+
Kind::Ente => "ente.session.json",
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn policy(self, config: &config::Config) -> config::StorePolicy {
|
|
38
|
+
match self {
|
|
39
|
+
Kind::Bitwarden => config.bitwarden.store,
|
|
40
|
+
Kind::Ente => config.ente,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
20
45
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
21
46
|
pub struct Stored {
|
|
22
47
|
#[serde(rename = "savedAt")]
|
|
@@ -100,11 +125,18 @@ impl Stored {
|
|
|
100
125
|
|
|
101
126
|
/// Store a key, honouring the current `bitwarden.unlock` policy. `session`
|
|
102
127
|
/// means "do not persist": the caller keeps it in memory instead.
|
|
103
|
-
pub fn save(paths: &Paths,
|
|
104
|
-
let
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
128
|
+
pub fn save(paths: &Paths, kind: Kind, secret: &str) -> Result<Option<i64>, String> {
|
|
129
|
+
let config = config::load(paths);
|
|
130
|
+
let policy = kind.policy(&config);
|
|
131
|
+
if policy.unlock == UnlockMode::Session {
|
|
132
|
+
clear(paths, kind);
|
|
133
|
+
return Err(format!(
|
|
134
|
+
"{}.unlock is \"session\": nothing is kept on disk",
|
|
135
|
+
match kind {
|
|
136
|
+
Kind::Bitwarden => "bitwarden",
|
|
137
|
+
Kind::Ente => "ente",
|
|
138
|
+
}
|
|
139
|
+
));
|
|
108
140
|
}
|
|
109
141
|
let now = now_ms();
|
|
110
142
|
let expires_at = match policy.unlock {
|
|
@@ -113,15 +145,16 @@ pub fn save(paths: &Paths, key: &str) -> Result<Option<i64>, String> {
|
|
|
113
145
|
UnlockMode::Session => None,
|
|
114
146
|
};
|
|
115
147
|
let wrap = wrap_key(paths)?;
|
|
116
|
-
let stored = Stored { saved_at: now, expires_at, sealed: seal(&wrap,
|
|
148
|
+
let stored = Stored { saved_at: now, expires_at, sealed: seal(&wrap, secret)? };
|
|
117
149
|
let bytes = serde_json::to_vec(&stored).map_err(|e| format!("cannot serialize session: {e}"))?;
|
|
118
|
-
|
|
119
|
-
|
|
150
|
+
let path = paths.dir.join(kind.file());
|
|
151
|
+
crypto::write_atomic(&path, &bytes)?;
|
|
152
|
+
let _ = fs::set_permissions(&path, fs::Permissions::from_mode(0o600));
|
|
120
153
|
Ok(expires_at)
|
|
121
154
|
}
|
|
122
155
|
|
|
123
|
-
pub fn load(paths: &Paths) -> Result<Option<Stored>, String> {
|
|
124
|
-
let path = paths.
|
|
156
|
+
pub fn load(paths: &Paths, kind: Kind) -> Result<Option<Stored>, String> {
|
|
157
|
+
let path = paths.dir.join(kind.file());
|
|
125
158
|
let text = match fs::read_to_string(&path) {
|
|
126
159
|
Ok(text) => text,
|
|
127
160
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
|
@@ -142,8 +175,8 @@ pub fn load(paths: &Paths) -> Result<Option<Stored>, String> {
|
|
|
142
175
|
}
|
|
143
176
|
|
|
144
177
|
/// The session key itself, or an error naming why it cannot be opened.
|
|
145
|
-
pub fn key(paths: &Paths) -> Result<Option<String>, String> {
|
|
146
|
-
match load(paths)? {
|
|
178
|
+
pub fn key(paths: &Paths, kind: Kind) -> Result<Option<String>, String> {
|
|
179
|
+
match load(paths, kind)? {
|
|
147
180
|
None => Ok(None),
|
|
148
181
|
Some(stored) => {
|
|
149
182
|
let wrap = wrap_key(paths)?;
|
|
@@ -152,12 +185,6 @@ pub fn key(paths: &Paths) -> Result<Option<String>, String> {
|
|
|
152
185
|
}
|
|
153
186
|
}
|
|
154
187
|
|
|
155
|
-
pub fn clear(paths: &Paths) -> bool {
|
|
156
|
-
fs::remove_file(paths.
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
impl config::BitwardenPolicy {
|
|
160
|
-
pub fn mode_is_session(&self) -> bool {
|
|
161
|
-
self.unlock == UnlockMode::Session
|
|
162
|
-
}
|
|
188
|
+
pub fn clear(paths: &Paths, kind: Kind) -> bool {
|
|
189
|
+
fs::remove_file(paths.dir.join(kind.file())).is_ok()
|
|
163
190
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xiaolikt/ai-vault",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "One machine-wide encrypted secret store for AI coding agents: the ai-vault Rust CLI (npm-installable) plus the pi-ai-vault extension for pi and omp.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"optionalDependencies": {
|
|
36
|
-
"@xiaolikt/ai-vault-linux-x64": "0.
|
|
36
|
+
"@xiaolikt/ai-vault-linux-x64": "0.9.0"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"bin",
|
|
@@ -31,6 +31,8 @@ omp plugin install npm:pi-ai-vault # omp,同一个包
|
|
|
31
31
|
| `vault_import` | 把 Bitwarden 登录项抄进本地库(先确认) | 否 |
|
|
32
32
|
| `bw_list` / `bw_run` / `bw_clipboard` / `bw_reveal` | 在线 Bitwarden(`bw` CLI) | 否,`bw_reveal` 除外 |
|
|
33
33
|
| `bw_inject` | 注入 bash 环境(**仅 pi**,omp 不注册) | 否 |
|
|
34
|
+
| `ente_list` | Ente Auth 条目元数据 | 否 |
|
|
35
|
+
| `ente_code` | 当前 TOTP/Steam 验证码(每次确认) | 是,用户当场允许 |
|
|
34
36
|
|
|
35
37
|
## 命令
|
|
36
38
|
|
|
@@ -61,6 +63,12 @@ omp plugin install npm:pi-ai-vault # omp,同一个包
|
|
|
61
63
|
|
|
62
64
|
所以默认情况下 **只有你同意导入的密码才会进 ai-vault**。Bitwarden 自己的主密码请求频率也有单独设置(`bitwarden.unlock`:`never` / `time` / `session`),用 `/vault settings` 查看和修改。
|
|
63
65
|
|
|
66
|
+
## Ente
|
|
67
|
+
|
|
68
|
+
`ente` CLI 自己登录一次(`ente account login --app auth`),插件只是它的客户端:`ente_list` 只给元数据,`ente_code` 每次弹窗确认。
|
|
69
|
+
|
|
70
|
+
想完全免钥匙串:`ente vault key generate` → `/vault ente-key` 粘进去 → `/vault settings ente.unlock never`。密钥由 ai-vault 加密封存。详见仓库 README。
|
|
71
|
+
|
|
64
72
|
## 安全边界(一句话版)
|
|
65
73
|
|
|
66
74
|
模型的 `bash` 能找到会话令牌、能 `list`,但**签不出读取确认**(签名密钥只在插件进程内存里)、**改不了令牌文件**(带 HMAC,改了就作废)、**免费拿不到 `grant`**(要主密码)。详见仓库 README「安全」一节。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-ai-vault",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "pi extension for ai-vault: one machine-wide encrypted secret store plus live Bitwarden. Works in pi and in omp (the omp fork reads the same manifest).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"security"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@xiaolikt/ai-vault": "0.
|
|
28
|
+
"@xiaolikt/ai-vault": "0.9.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@earendil-works/pi-coding-agent": "^1.5.0",
|
|
@@ -354,7 +354,7 @@ const AUDIT_MAX_BYTES = 1024 * 1024;
|
|
|
354
354
|
let auditModeFixed = false;
|
|
355
355
|
|
|
356
356
|
/** One audit line per event. `session` is the binding's label, so a subagent's `bw_run` can be told from its parent's. */
|
|
357
|
-
async function
|
|
357
|
+
export async function auditEvent(event: string, detail: string, session?: string): Promise<void> {
|
|
358
358
|
try {
|
|
359
359
|
await fs.mkdir(STATE_DIR, { recursive: true, mode: 0o700 });
|
|
360
360
|
const log = path.join(STATE_DIR, "audit.log");
|
|
@@ -513,7 +513,7 @@ async function lock(st: BwState, reason: string): Promise<void> {
|
|
|
513
513
|
st.lockTimer = undefined;
|
|
514
514
|
}
|
|
515
515
|
if (hadSession) {
|
|
516
|
-
await
|
|
516
|
+
await auditEvent("lock", `reason=${reason}`, st.sessionLabel);
|
|
517
517
|
if (!releaseSessionKey()) return; // another binding still uses its own key
|
|
518
518
|
try {
|
|
519
519
|
const bin = await resolveBw();
|
|
@@ -581,7 +581,7 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
|
|
|
581
581
|
signal,
|
|
582
582
|
});
|
|
583
583
|
if (r.code !== 0) {
|
|
584
|
-
await
|
|
584
|
+
await auditEvent("unlock", "deny=bad-password", st.sessionLabel);
|
|
585
585
|
throw new Error(`Unlock failed (wrong master password or CLI error). ${tail(r.stderr, 120)}`);
|
|
586
586
|
}
|
|
587
587
|
const key = r.stdout.replace(/[\r\n]+$/, "");
|
|
@@ -599,7 +599,7 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
|
|
|
599
599
|
// from the bitwarden.hours policy.
|
|
600
600
|
await settingsCli().run(["bw-session", "put"], { stdin: key }).catch(() => undefined);
|
|
601
601
|
}
|
|
602
|
-
await
|
|
602
|
+
await auditEvent("unlock", "ok", st.sessionLabel);
|
|
603
603
|
// Refresh in the background: a full sync can take seconds on large vaults.
|
|
604
604
|
const lastSync = Date.parse(status?.lastSync ?? "");
|
|
605
605
|
if (!Number.isFinite(lastSync) || Date.now() - lastSync > 15 * 60_000) {
|
|
@@ -750,7 +750,7 @@ async function approveItem(
|
|
|
750
750
|
const user = item.username ? ` (user: ${item.username})` : "";
|
|
751
751
|
const ok = await confirmAction(ctx, title, `Item: ${name}${user}\nField: ${field}\n${detail}\n\nAllow?`, signal);
|
|
752
752
|
if (!ok) {
|
|
753
|
-
await
|
|
753
|
+
await auditEvent(event, `deny item="${name}" field=${field}`, st.sessionLabel);
|
|
754
754
|
return undefined;
|
|
755
755
|
}
|
|
756
756
|
return { session, item };
|
|
@@ -1116,6 +1116,9 @@ export function registerBitwarden(pi: ExtensionAPI, st: BwState, ompHost: boolea
|
|
|
1116
1116
|
spawnHook: ({ command, cwd, env }) => {
|
|
1117
1117
|
// A BW_SESSION exported in the user's shell must not reach bash.
|
|
1118
1118
|
for (const name of Object.keys(env)) if (name.startsWith("BW_")) delete env[name];
|
|
1119
|
+
// Same for Ente's unattended vault key / export password: the model's
|
|
1120
|
+
// shell must not be able to open Ente Auth with what the user exported.
|
|
1121
|
+
for (const name of ["ENTE_CLI_VAULT_KEY", "ENTE_CLI_PASSWORD"]) delete env[name];
|
|
1119
1122
|
for (const [name, rec] of st.injected) env[name] = rec.value;
|
|
1120
1123
|
return { command, cwd, env };
|
|
1121
1124
|
},
|
|
@@ -1230,7 +1233,7 @@ export function registerBitwarden(pi: ExtensionAPI, st: BwState, ompHost: boolea
|
|
|
1230
1233
|
const { value, itemId, itemName } = await secretOf(approved.session, approved.item, field, signal);
|
|
1231
1234
|
st.injected.set(envName, { value, item: itemName, field, at: Date.now() });
|
|
1232
1235
|
rememberScrubValue(envName, value);
|
|
1233
|
-
await
|
|
1236
|
+
await auditEvent("inject", `allow env=${envName} item="${itemName}" (${itemId}) field=${field}`, st.sessionLabel);
|
|
1234
1237
|
safeStatus(ctx, `🔓 bw: ${st.injected.size} env var(s)`);
|
|
1235
1238
|
const weak =
|
|
1236
1239
|
value.length < 6
|
|
@@ -1303,7 +1306,7 @@ export function registerBitwarden(pi: ExtensionAPI, st: BwState, ompHost: boolea
|
|
|
1303
1306
|
signal,
|
|
1304
1307
|
group: true,
|
|
1305
1308
|
});
|
|
1306
|
-
await
|
|
1309
|
+
await auditEvent("run", `allow env=${envName} item="${itemName}" field=${field} exit=${r.code}`, st.sessionLabel);
|
|
1307
1310
|
const raw = [r.stdout, r.stderr].filter((s) => s.length > 0).join("\n");
|
|
1308
1311
|
const scrubbed = scrubText(raw, scrubValues.values()).text;
|
|
1309
1312
|
const output =
|
|
@@ -1356,7 +1359,7 @@ export function registerBitwarden(pi: ExtensionAPI, st: BwState, ompHost: boolea
|
|
|
1356
1359
|
// so tool output carrying it must be scrubbed like an injected var.
|
|
1357
1360
|
rememberScrubValue("CLIPBOARD", value);
|
|
1358
1361
|
await copyToClipboard(value);
|
|
1359
|
-
await
|
|
1362
|
+
await auditEvent("clipboard", `allow item="${itemName}" field=${field}`, st.sessionLabel);
|
|
1360
1363
|
const clears =
|
|
1361
1364
|
CONFIG.clipboardClearSeconds > 0 ? `Clears in ${CONFIG.clipboardClearSeconds}s.` : "Auto-clear disabled.";
|
|
1362
1365
|
return {
|
|
@@ -1401,7 +1404,7 @@ export function registerBitwarden(pi: ExtensionAPI, st: BwState, ompHost: boolea
|
|
|
1401
1404
|
);
|
|
1402
1405
|
if (!approved) throw new Error("User declined the reveal.");
|
|
1403
1406
|
const { value, itemName } = await secretOf(approved.session, approved.item, field, signal);
|
|
1404
|
-
await
|
|
1407
|
+
await auditEvent("reveal", `allow item="${itemName}" field=${field}`, st.sessionLabel);
|
|
1405
1408
|
return {
|
|
1406
1409
|
content: [{ type: "text", text: `${itemName} (${field}):\n<secret>${value}</secret>` }],
|
|
1407
1410
|
details: { item: itemName, field },
|