@xiaolikt/ai-vault 0.9.0 → 0.9.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 +122 -4
- package/README.md +28 -0
- package/bin/ai-vault.cjs +6 -0
- package/crates/ai-vault/Cargo.toml +2 -3
- package/crates/ai-vault/src/audit.rs +4 -6
- package/crates/ai-vault/src/cli.rs +1 -2
- package/crates/ai-vault/src/crypto.rs +4 -11
- package/crates/ai-vault/src/dialog.rs +285 -7
- package/crates/ai-vault/src/main.rs +1 -0
- package/crates/ai-vault/src/paths.rs +30 -7
- package/crates/ai-vault/src/perms.rs +54 -0
- package/crates/ai-vault/src/sealed.rs +3 -4
- package/crates/ai-vault/src/session.rs +9 -21
- package/package.json +4 -2
- package/packages/pi-ai-vault/package.json +2 -2
- package/packages/pi-ai-vault/src/cli.ts +15 -6
- package/test/cli.sh +6 -4
- package/test/npm.sh +14 -1
package/Cargo.lock
CHANGED
|
@@ -14,15 +14,15 @@ dependencies = [
|
|
|
14
14
|
|
|
15
15
|
[[package]]
|
|
16
16
|
name = "ai-vault"
|
|
17
|
-
version = "0.9.
|
|
17
|
+
version = "0.9.1"
|
|
18
18
|
dependencies = [
|
|
19
19
|
"base64",
|
|
20
20
|
"chacha20poly1305",
|
|
21
21
|
"chrono",
|
|
22
22
|
"clap",
|
|
23
|
+
"fs4",
|
|
23
24
|
"getrandom",
|
|
24
25
|
"hmac",
|
|
25
|
-
"libc",
|
|
26
26
|
"scrypt",
|
|
27
27
|
"serde",
|
|
28
28
|
"serde_json",
|
|
@@ -75,7 +75,7 @@ version = "1.1.5"
|
|
|
75
75
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
76
76
|
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
|
77
77
|
dependencies = [
|
|
78
|
-
"windows-sys",
|
|
78
|
+
"windows-sys 0.61.2",
|
|
79
79
|
]
|
|
80
80
|
|
|
81
81
|
[[package]]
|
|
@@ -86,7 +86,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
|
|
86
86
|
dependencies = [
|
|
87
87
|
"anstyle",
|
|
88
88
|
"once_cell_polyfill",
|
|
89
|
-
"windows-sys",
|
|
89
|
+
"windows-sys 0.61.2",
|
|
90
90
|
]
|
|
91
91
|
|
|
92
92
|
[[package]]
|
|
@@ -107,6 +107,12 @@ version = "1.8.3"
|
|
|
107
107
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
108
108
|
checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
|
|
109
109
|
|
|
110
|
+
[[package]]
|
|
111
|
+
name = "bitflags"
|
|
112
|
+
version = "2.13.2"
|
|
113
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
114
|
+
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
|
|
115
|
+
|
|
110
116
|
[[package]]
|
|
111
117
|
name = "block-buffer"
|
|
112
118
|
version = "0.10.4"
|
|
@@ -267,12 +273,32 @@ dependencies = [
|
|
|
267
273
|
"subtle",
|
|
268
274
|
]
|
|
269
275
|
|
|
276
|
+
[[package]]
|
|
277
|
+
name = "errno"
|
|
278
|
+
version = "0.3.15"
|
|
279
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
280
|
+
checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776"
|
|
281
|
+
dependencies = [
|
|
282
|
+
"libc",
|
|
283
|
+
"windows-sys 0.61.2",
|
|
284
|
+
]
|
|
285
|
+
|
|
270
286
|
[[package]]
|
|
271
287
|
name = "find-msvc-tools"
|
|
272
288
|
version = "0.1.16"
|
|
273
289
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
274
290
|
checksum = "3e44f110d1777dc6559a51e7d45083945ded4f1bf3305494350f693b6d20b9c8"
|
|
275
291
|
|
|
292
|
+
[[package]]
|
|
293
|
+
name = "fs4"
|
|
294
|
+
version = "0.13.1"
|
|
295
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
296
|
+
checksum = "8640e34b88f7652208ce9e88b1a37a2ae95227d84abec377ccd3c5cfeb141ed4"
|
|
297
|
+
dependencies = [
|
|
298
|
+
"rustix",
|
|
299
|
+
"windows-sys 0.59.0",
|
|
300
|
+
]
|
|
301
|
+
|
|
276
302
|
[[package]]
|
|
277
303
|
name = "futures-core"
|
|
278
304
|
version = "0.3.34"
|
|
@@ -395,6 +421,12 @@ version = "0.2.191"
|
|
|
395
421
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
396
422
|
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
397
423
|
|
|
424
|
+
[[package]]
|
|
425
|
+
name = "linux-raw-sys"
|
|
426
|
+
version = "0.12.1"
|
|
427
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
428
|
+
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
|
429
|
+
|
|
398
430
|
[[package]]
|
|
399
431
|
name = "log"
|
|
400
432
|
version = "0.4.35"
|
|
@@ -493,6 +525,19 @@ dependencies = [
|
|
|
493
525
|
"getrandom",
|
|
494
526
|
]
|
|
495
527
|
|
|
528
|
+
[[package]]
|
|
529
|
+
name = "rustix"
|
|
530
|
+
version = "1.1.6"
|
|
531
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
532
|
+
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
|
|
533
|
+
dependencies = [
|
|
534
|
+
"bitflags",
|
|
535
|
+
"errno",
|
|
536
|
+
"libc",
|
|
537
|
+
"linux-raw-sys",
|
|
538
|
+
"windows-sys 0.61.2",
|
|
539
|
+
]
|
|
540
|
+
|
|
496
541
|
[[package]]
|
|
497
542
|
name = "rustversion"
|
|
498
543
|
version = "1.0.23"
|
|
@@ -763,6 +808,15 @@ dependencies = [
|
|
|
763
808
|
"windows-link",
|
|
764
809
|
]
|
|
765
810
|
|
|
811
|
+
[[package]]
|
|
812
|
+
name = "windows-sys"
|
|
813
|
+
version = "0.59.0"
|
|
814
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
815
|
+
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
|
816
|
+
dependencies = [
|
|
817
|
+
"windows-targets",
|
|
818
|
+
]
|
|
819
|
+
|
|
766
820
|
[[package]]
|
|
767
821
|
name = "windows-sys"
|
|
768
822
|
version = "0.61.2"
|
|
@@ -772,6 +826,70 @@ dependencies = [
|
|
|
772
826
|
"windows-link",
|
|
773
827
|
]
|
|
774
828
|
|
|
829
|
+
[[package]]
|
|
830
|
+
name = "windows-targets"
|
|
831
|
+
version = "0.52.6"
|
|
832
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
833
|
+
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
|
834
|
+
dependencies = [
|
|
835
|
+
"windows_aarch64_gnullvm",
|
|
836
|
+
"windows_aarch64_msvc",
|
|
837
|
+
"windows_i686_gnu",
|
|
838
|
+
"windows_i686_gnullvm",
|
|
839
|
+
"windows_i686_msvc",
|
|
840
|
+
"windows_x86_64_gnu",
|
|
841
|
+
"windows_x86_64_gnullvm",
|
|
842
|
+
"windows_x86_64_msvc",
|
|
843
|
+
]
|
|
844
|
+
|
|
845
|
+
[[package]]
|
|
846
|
+
name = "windows_aarch64_gnullvm"
|
|
847
|
+
version = "0.52.6"
|
|
848
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
849
|
+
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
|
850
|
+
|
|
851
|
+
[[package]]
|
|
852
|
+
name = "windows_aarch64_msvc"
|
|
853
|
+
version = "0.52.6"
|
|
854
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
855
|
+
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
|
856
|
+
|
|
857
|
+
[[package]]
|
|
858
|
+
name = "windows_i686_gnu"
|
|
859
|
+
version = "0.52.6"
|
|
860
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
861
|
+
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
|
862
|
+
|
|
863
|
+
[[package]]
|
|
864
|
+
name = "windows_i686_gnullvm"
|
|
865
|
+
version = "0.52.6"
|
|
866
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
867
|
+
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
|
868
|
+
|
|
869
|
+
[[package]]
|
|
870
|
+
name = "windows_i686_msvc"
|
|
871
|
+
version = "0.52.6"
|
|
872
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
873
|
+
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
|
874
|
+
|
|
875
|
+
[[package]]
|
|
876
|
+
name = "windows_x86_64_gnu"
|
|
877
|
+
version = "0.52.6"
|
|
878
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
879
|
+
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
|
880
|
+
|
|
881
|
+
[[package]]
|
|
882
|
+
name = "windows_x86_64_gnullvm"
|
|
883
|
+
version = "0.52.6"
|
|
884
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
885
|
+
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
|
886
|
+
|
|
887
|
+
[[package]]
|
|
888
|
+
name = "windows_x86_64_msvc"
|
|
889
|
+
version = "0.52.6"
|
|
890
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
891
|
+
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
|
892
|
+
|
|
775
893
|
[[package]]
|
|
776
894
|
name = "zeroize"
|
|
777
895
|
version = "1.9.2"
|
package/README.md
CHANGED
|
@@ -58,6 +58,34 @@ printf '//registry.npmjs.org/:_authToken=%s\n' "<token>" > ~/.npmrc && chmod 600
|
|
|
58
58
|
|
|
59
59
|
别的平台要有自己的二进制包:`./scripts/stage-npm.sh darwin-arm64 --from <交叉编译产物>`,发布后把对应名字加进 `optionalDependencies`。没有匹配的平台包时,shim 会明确报错并提示 `cargo install --path crates/ai-vault`。
|
|
60
60
|
|
|
61
|
+
### 平台
|
|
62
|
+
|
|
63
|
+
| 平台 | 状态 |
|
|
64
|
+
|---|---|
|
|
65
|
+
| Linux x64 / arm64 | ✅ 已发布(`@xiaolikt/ai-vault-linux-x64` / `-linux-arm64`) |
|
|
66
|
+
| Windows x64 | ✅ 已发布(`@xiaolikt/ai-vault-win32-x64`),对话框走 PowerShell/WinForms |
|
|
67
|
+
| macOS arm64 / x64 | 🔧 代码已可编译(`cargo check --target aarch64-apple-darwin` 通过),二进制需要 Apple SDK —— 由 `.github/workflows/build.yml` 的 macOS runner 产出 |
|
|
68
|
+
|
|
69
|
+
跨平台的部分:
|
|
70
|
+
- **加密/存储**:纯 Rust(chacha20poly1305、scrypt、hmac、sha2、base64、zeroize)
|
|
71
|
+
- **文件锁**:`fs4`(Linux/macOS 用 flock,Windows 用 LockFileEx)
|
|
72
|
+
- **权限**:Unix 设 0600/0700;Windows 没有位权限,靠 `%USERPROFILE%\.ai-vault` 的 ACL
|
|
73
|
+
- **会话目录**:Linux `$XDG_RUNTIME_DIR`、macOS `$TMPDIR`、Windows `%TEMP%`(都是每人一份、可被系统清理)
|
|
74
|
+
- **弹窗**:Linux zenity / macOS osascript / Windows PowerShell+WinForms,同一套 `Answer` 契约(`NoUi` 才回退到宿主输入框)
|
|
75
|
+
|
|
76
|
+
自己交叉编译(不需要 Mac/Windows 机器):
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
cargo install cargo-zigbuild
|
|
80
|
+
rustup target add aarch64-unknown-linux-gnu x86_64-pc-windows-gnu
|
|
81
|
+
cargo zigbuild --release --target aarch64-unknown-linux-gnu
|
|
82
|
+
cargo zigbuild --release --target x86_64-pc-windows-gnu
|
|
83
|
+
./scripts/stage-npm.sh linux-arm64 --from target/aarch64-unknown-linux-gnu/release/ai-vault
|
|
84
|
+
./scripts/stage-npm.sh win32-x64 --from target/x86_64-pc-windows-gnu/release/ai-vault.exe
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`stage-npm.sh` 两种写法都收:npm 平台名(`darwin-arm64`)或 Rust triple(`aarch64-apple-darwin`)。macOS 的二进制只能用 Apple SDK 链,所以留给 CI(tag push 时自动构建 + 发布)。
|
|
88
|
+
|
|
61
89
|
### 从源码 / git
|
|
62
90
|
|
|
63
91
|
```bash
|
package/bin/ai-vault.cjs
CHANGED
|
@@ -29,6 +29,12 @@ function candidates() {
|
|
|
29
29
|
// A source checkout: package root/target/{release,debug}/ai-vault.
|
|
30
30
|
const root = join(__dirname, "..");
|
|
31
31
|
found.push(join(root, "target", "release", EXE), join(root, "target", "debug", EXE));
|
|
32
|
+
// Windows has no exec bit and npm links .cmd/.ps1 wrappers: try the whole set.
|
|
33
|
+
if (process.platform === "win32") {
|
|
34
|
+
for (const name of ["ai-vault.exe", "ai-vault.cmd", "ai-vault.ps1"]) {
|
|
35
|
+
found.push(join(root, "target", "release", name), join(root, "target", "debug", name));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
32
38
|
return found.filter((path) => path && path !== __filename);
|
|
33
39
|
}
|
|
34
40
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "ai-vault"
|
|
3
|
-
version = "0.9.
|
|
3
|
+
version = "0.9.1"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
description = "Machine-wide encrypted secret store shared by AI coding agents"
|
|
6
6
|
license = "MIT"
|
|
@@ -17,11 +17,10 @@ chrono = { version = "0.4", default-features = false, features = ["clock", "std"
|
|
|
17
17
|
clap = { version = "4", features = ["derive", "env"] }
|
|
18
18
|
getrandom = "0.2"
|
|
19
19
|
hmac = "0.12"
|
|
20
|
+
fs4 = { version = "0.13", features = ["sync"] }
|
|
20
21
|
scrypt = "0.11"
|
|
21
22
|
serde = { version = "1", features = ["derive"] }
|
|
22
23
|
serde_json = "1"
|
|
23
24
|
sha2 = "0.10"
|
|
24
25
|
zeroize = "1"
|
|
25
26
|
|
|
26
|
-
[target.'cfg(unix)'.dependencies]
|
|
27
|
-
libc = "0.2"
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
//! Records who asked for what, never a value. A full or read-only disk is not
|
|
4
4
|
//! allowed to break the operation being audited.
|
|
5
5
|
|
|
6
|
-
use std::fs
|
|
6
|
+
use std::fs;
|
|
7
7
|
use std::io::Write;
|
|
8
|
-
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
|
9
8
|
use std::path::Path;
|
|
10
9
|
|
|
11
10
|
use serde::{Deserialize, Serialize};
|
|
@@ -37,20 +36,19 @@ pub fn append(path: &Path, session: &str, event: &str, item: Option<&str>, reaso
|
|
|
37
36
|
};
|
|
38
37
|
if let Some(dir) = path.parent() {
|
|
39
38
|
let _ = fs::create_dir_all(dir);
|
|
40
|
-
|
|
39
|
+
crate::perms::restrict_dir(dir);
|
|
41
40
|
}
|
|
42
41
|
if let Ok(meta) = fs::metadata(path) {
|
|
43
42
|
if meta.len() > MAX_BYTES {
|
|
44
43
|
let _ = fs::rename(path, path.with_extension("log.1"));
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
|
-
let result = OpenOptions::new()
|
|
46
|
+
let result = std::fs::OpenOptions::new()
|
|
48
47
|
.create(true)
|
|
49
48
|
.append(true)
|
|
50
|
-
.mode(0o600)
|
|
51
49
|
.open(path)
|
|
52
50
|
.and_then(|mut file| {
|
|
53
|
-
|
|
51
|
+
crate::perms::restrict_file(path);
|
|
54
52
|
file.write_all(format!("{line}\n").as_bytes())
|
|
55
53
|
});
|
|
56
54
|
result.is_ok()
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
//! these commands and never links against the encryption itself.
|
|
4
4
|
|
|
5
5
|
use std::io::{self, BufRead, Read};
|
|
6
|
-
use std::os::unix::fs::PermissionsExt;
|
|
7
6
|
use std::path::PathBuf;
|
|
8
7
|
|
|
9
8
|
use clap::{Parser, Subcommand};
|
|
@@ -296,7 +295,7 @@ fn migrate_legacy(paths: &Paths) {
|
|
|
296
295
|
if std::fs::copy(&legacy_store, paths.store()).is_err() {
|
|
297
296
|
return;
|
|
298
297
|
}
|
|
299
|
-
|
|
298
|
+
crate::perms::restrict_file(&paths.store());
|
|
300
299
|
if legacy.join("audit.log").exists() && !paths.audit().exists() {
|
|
301
300
|
let _ = std::fs::copy(legacy.join("audit.log"), paths.audit());
|
|
302
301
|
}
|
|
@@ -8,9 +8,8 @@
|
|
|
8
8
|
//! Identical to the format the TypeScript release used, so an existing store
|
|
9
9
|
//! opens without a migration step.
|
|
10
10
|
|
|
11
|
-
use std::fs
|
|
11
|
+
use std::fs;
|
|
12
12
|
use std::io::Write;
|
|
13
|
-
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
|
14
13
|
use std::path::Path;
|
|
15
14
|
|
|
16
15
|
use base64::engine::general_purpose::STANDARD as B64;
|
|
@@ -184,7 +183,7 @@ pub fn read_store(path: &Path) -> Result<Option<StoreFile>, String> {
|
|
|
184
183
|
pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
|
|
185
184
|
let dir = path.parent().unwrap_or(Path::new("."));
|
|
186
185
|
fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
|
187
|
-
|
|
186
|
+
crate::perms::restrict_dir(dir);
|
|
188
187
|
|
|
189
188
|
let tmp = dir.join(format!(
|
|
190
189
|
".{}.{}.{}.tmp",
|
|
@@ -192,11 +191,7 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
|
|
|
192
191
|
std::process::id(),
|
|
193
192
|
B64.encode(random_bytes::<6>()).replace(['/', '+', '='], "")
|
|
194
193
|
));
|
|
195
|
-
let mut file =
|
|
196
|
-
.write(true)
|
|
197
|
-
.create_new(true)
|
|
198
|
-
.mode(0o600)
|
|
199
|
-
.open(&tmp)
|
|
194
|
+
let mut file = crate::perms::open_private(&tmp)
|
|
200
195
|
.map_err(|e| format!("cannot create a temp file next to {}: {e}", path.display()))?;
|
|
201
196
|
let outcome = (|| -> std::io::Result<()> {
|
|
202
197
|
file.write_all(bytes)?;
|
|
@@ -208,9 +203,7 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
|
|
|
208
203
|
let _ = fs::remove_file(&tmp);
|
|
209
204
|
return Err(format!("cannot write {}: {e}", path.display()));
|
|
210
205
|
}
|
|
211
|
-
|
|
212
|
-
let _ = dir_file.sync_all();
|
|
213
|
-
}
|
|
206
|
+
crate::perms::sync_dir(dir);
|
|
214
207
|
Ok(())
|
|
215
208
|
}
|
|
216
209
|
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
//! Desktop dialogs
|
|
1
|
+
//! Desktop dialogs.
|
|
2
2
|
//!
|
|
3
|
-
//!
|
|
4
|
-
//!
|
|
5
|
-
//!
|
|
3
|
+
//! Linux zenity (X11 or Wayland)
|
|
4
|
+
//! macOS osascript (a native dialog, hidden answer for secrets)
|
|
5
|
+
//! Windows PowerShell + WinForms (a native dialog, PasswordChar for secrets)
|
|
6
|
+
//!
|
|
7
|
+
//! When no window can open at all, the caller gets `NoUi` and the host UI
|
|
8
|
+
//! (pi/omp prompt) takes over. Cancel and our own timeout end the prompt here:
|
|
9
|
+
//! they never turn into a second prompt.
|
|
6
10
|
|
|
11
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
7
12
|
use std::env;
|
|
13
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
8
14
|
use std::path::Path;
|
|
15
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
9
16
|
use std::process::{Command, Stdio};
|
|
10
17
|
|
|
11
18
|
const DIALOG_SECONDS: u32 = 600;
|
|
@@ -20,11 +27,233 @@ pub enum Answer<T> {
|
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
pub fn has_display() -> bool {
|
|
23
|
-
[
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
#[cfg(target_os = "linux")]
|
|
31
|
+
{
|
|
32
|
+
return ["DISPLAY", "WAYLAND_DISPLAY"]
|
|
33
|
+
.iter()
|
|
34
|
+
.any(|name| env::var_os(name).is_some_and(|v| !v.is_empty()));
|
|
35
|
+
}
|
|
36
|
+
// macOS and Windows always have a window server for an interactive user.
|
|
37
|
+
#[cfg(not(target_os = "linux"))]
|
|
38
|
+
{
|
|
39
|
+
true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ---------------------------------------------------------------- macOS
|
|
44
|
+
|
|
45
|
+
#[cfg(target_os = "macos")]
|
|
46
|
+
mod mac {
|
|
47
|
+
use super::{Answer, DIALOG_SECONDS};
|
|
48
|
+
use std::process::{Command, Stdio};
|
|
49
|
+
|
|
50
|
+
/// Escape a value for AppleScript's double-quoted string literals.
|
|
51
|
+
fn quote(text: &str) -> String {
|
|
52
|
+
text.replace('\\', "\\\\").replace('"', "\\\"")
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
struct Out {
|
|
56
|
+
spawned: bool,
|
|
57
|
+
code: i32,
|
|
58
|
+
stdout: String,
|
|
59
|
+
stderr: String,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fn osascript(script: &str) -> Out {
|
|
63
|
+
match Command::new("osascript")
|
|
64
|
+
.args(["-e", script])
|
|
65
|
+
.stdin(Stdio::null())
|
|
66
|
+
.stdout(Stdio::piped())
|
|
67
|
+
.stderr(Stdio::piped())
|
|
68
|
+
.output()
|
|
69
|
+
{
|
|
70
|
+
Ok(out) => Out {
|
|
71
|
+
spawned: true,
|
|
72
|
+
code: out.status.code().unwrap_or(-1),
|
|
73
|
+
stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
|
|
74
|
+
stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
|
|
75
|
+
},
|
|
76
|
+
Err(_) => Out { spawned: false, code: 127, stdout: String::new(), stderr: String::new() },
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn failed(out: &Out) -> bool {
|
|
81
|
+
!out.spawned
|
|
82
|
+
|| {
|
|
83
|
+
let e = out.stderr.to_lowercase();
|
|
84
|
+
e.contains("not allowed") || e.contains("application isn")
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
89
|
+
let script = format!(
|
|
90
|
+
"tell application \"System Events\" to display dialog \"{}\" with title \"{}\" default answer \"\" with hidden answer buttons {{\"取消\", \"OK\"}} default button \"OK\" giving up after {}",
|
|
91
|
+
quote(label),
|
|
92
|
+
quote(title),
|
|
93
|
+
DIALOG_SECONDS
|
|
94
|
+
);
|
|
95
|
+
let out = osascript(&script);
|
|
96
|
+
if failed(&out) {
|
|
97
|
+
return Answer::NoUi;
|
|
98
|
+
}
|
|
99
|
+
if !out.stdout.contains("text returned:") {
|
|
100
|
+
return Answer::Cancelled;
|
|
101
|
+
}
|
|
102
|
+
let value = out
|
|
103
|
+
.stdout
|
|
104
|
+
.split("text returned:")
|
|
105
|
+
.nth(1)
|
|
106
|
+
.unwrap_or("")
|
|
107
|
+
.trim()
|
|
108
|
+
.to_string();
|
|
109
|
+
if value.is_empty() {
|
|
110
|
+
Answer::Cancelled
|
|
111
|
+
} else {
|
|
112
|
+
Answer::Value(value)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
117
|
+
let script = format!(
|
|
118
|
+
"tell application \"System Events\" to display dialog \"{}\" with title \"{}\" default answer \"\" buttons {{\"取消\", \"OK\"}} default button \"OK\" giving up after {}",
|
|
119
|
+
quote(label),
|
|
120
|
+
quote(title),
|
|
121
|
+
DIALOG_SECONDS
|
|
122
|
+
);
|
|
123
|
+
let out = osascript(&script);
|
|
124
|
+
if failed(&out) {
|
|
125
|
+
return Answer::NoUi;
|
|
126
|
+
}
|
|
127
|
+
if !out.stdout.contains("text returned:") {
|
|
128
|
+
return Answer::Cancelled;
|
|
129
|
+
}
|
|
130
|
+
Answer::Value(
|
|
131
|
+
out.stdout
|
|
132
|
+
.split("text returned:")
|
|
133
|
+
.nth(1)
|
|
134
|
+
.unwrap_or("")
|
|
135
|
+
.trim()
|
|
136
|
+
.to_string(),
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
141
|
+
let script = format!(
|
|
142
|
+
"tell application \"System Events\" to display dialog \"{}\" with title \"{}\" buttons {{\"拒绝\", \"允许\"}} default button \"拒绝\" giving up after {}",
|
|
143
|
+
quote(message),
|
|
144
|
+
quote(title),
|
|
145
|
+
DIALOG_SECONDS
|
|
146
|
+
);
|
|
147
|
+
let out = osascript(&script);
|
|
148
|
+
if failed(&out) {
|
|
149
|
+
return Answer::NoUi;
|
|
150
|
+
}
|
|
151
|
+
Answer::Value(out.code == 0 && out.stdout.contains("button returned:允许"))
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// -------------------------------------------------------------- Windows
|
|
156
|
+
|
|
157
|
+
#[cfg(target_os = "windows")]
|
|
158
|
+
mod win {
|
|
159
|
+
use super::{Answer, DIALOG_SECONDS};
|
|
160
|
+
use std::process::{Command, Stdio};
|
|
161
|
+
|
|
162
|
+
// WinForms from PowerShell: no extra dependency, and PasswordChar can hide
|
|
163
|
+
// the input. Exit 0 = OK (value on stdout), 1 = cancel, 2 = no window.
|
|
164
|
+
const PREAMBLE: &str = r#"
|
|
165
|
+
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
|
|
166
|
+
$f = New-Object System.Windows.Forms.Form
|
|
167
|
+
$f.Text = $env:AI_VAULT_DLG_TITLE
|
|
168
|
+
$f.Width = 480; $f.Height = 200; $f.StartPosition = 'CenterScreen'
|
|
169
|
+
$f.TopMost = $true; $f.FormBorderStyle = 'FixedDialog'; $f.MaximizeBox = $false
|
|
170
|
+
$lbl = New-Object System.Windows.Forms.Label
|
|
171
|
+
$lbl.Text = $env:AI_VAULT_DLG_TEXT; $lbl.Left = 12; $lbl.Top = 12; $lbl.Width = 440; $lbl.Height = 60
|
|
172
|
+
$f.Controls.Add($lbl)
|
|
173
|
+
$box = New-Object System.Windows.Forms.TextBox
|
|
174
|
+
$box.Left = 12; $box.Top = 80; $box.Width = 440
|
|
175
|
+
if ($env:AI_VAULT_DLG_SECRET -eq '1') { $box.UseSystemPasswordChar = $true }
|
|
176
|
+
$f.Controls.Add($box)
|
|
177
|
+
$ok = New-Object System.Windows.Forms.Button
|
|
178
|
+
$ok.Text = 'OK'; $ok.DialogResult = 'OK'; $ok.Left = 280; $ok.Top = 115; $f.AcceptButton = $ok
|
|
179
|
+
$cancel = New-Object System.Windows.Forms.Button
|
|
180
|
+
$cancel.Text = '取消'; $cancel.DialogResult = 'Cancel'; $cancel.Left = 370; $cancel.Top = 115; $f.CancelButton = $cancel
|
|
181
|
+
$f.Controls.Add($ok); $f.Controls.Add($cancel)
|
|
182
|
+
$f.Add_Shown({ $box.Focus() })
|
|
183
|
+
if ($f.ShowDialog() -eq 'OK') { [Console]::Out.Write($box.Text); exit 0 } else { exit 1 }
|
|
184
|
+
"#;
|
|
185
|
+
|
|
186
|
+
const CONFIRM: &str = r#"
|
|
187
|
+
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
|
|
188
|
+
$msg = $env:AI_VAULT_DLG_TEXT
|
|
189
|
+
$r = [System.Windows.Forms.MessageBox]::Show($msg, $env:AI_VAULT_DLG_TITLE, 'YesNo', 'Warning', 'Button2')
|
|
190
|
+
if ($r -eq 'Yes') { exit 0 } else { exit 1 }
|
|
191
|
+
"#;
|
|
192
|
+
|
|
193
|
+
struct Out {
|
|
194
|
+
spawned: bool,
|
|
195
|
+
code: i32,
|
|
196
|
+
stdout: String,
|
|
197
|
+
stderr: String,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
fn powershell(script: &str, title: &str, text: &str, secret: bool) -> Out {
|
|
201
|
+
let mut cmd = Command::new("powershell");
|
|
202
|
+
cmd.args(["-NoProfile", "-NonInteractive", "-STA", "-Command", script])
|
|
203
|
+
.env("AI_VAULT_DLG_TITLE", title)
|
|
204
|
+
.env("AI_VAULT_DLG_TEXT", text)
|
|
205
|
+
.env("AI_VAULT_DLG_SECRET", if secret { "1" } else { "0" })
|
|
206
|
+
.stdin(Stdio::null())
|
|
207
|
+
.stdout(Stdio::piped())
|
|
208
|
+
.stderr(Stdio::piped());
|
|
209
|
+
let _ = DIALOG_SECONDS; // the dialog is modal for the user, not us
|
|
210
|
+
match cmd.output() {
|
|
211
|
+
Ok(out) => Out {
|
|
212
|
+
spawned: true,
|
|
213
|
+
code: out.status.code().unwrap_or(-1),
|
|
214
|
+
stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
|
|
215
|
+
stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
|
|
216
|
+
},
|
|
217
|
+
Err(_) => Out { spawned: false, code: 127, stdout: String::new(), stderr: String::new() },
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
fn failed(out: &Out) -> bool {
|
|
222
|
+
!out.spawned || (out.code != 0 && out.code != 1)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
226
|
+
let out = powershell(PREAMBLE, title, label, true);
|
|
227
|
+
if failed(&out) {
|
|
228
|
+
return Answer::NoUi;
|
|
229
|
+
}
|
|
230
|
+
if out.code != 0 || out.stdout.is_empty() {
|
|
231
|
+
return Answer::Cancelled;
|
|
232
|
+
}
|
|
233
|
+
Answer::Value(out.stdout)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
237
|
+
let out = powershell(PREAMBLE, title, label, false);
|
|
238
|
+
if failed(&out) {
|
|
239
|
+
return Answer::NoUi;
|
|
240
|
+
}
|
|
241
|
+
if out.code != 0 {
|
|
242
|
+
return Answer::Cancelled;
|
|
243
|
+
}
|
|
244
|
+
Answer::Value(out.stdout)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
248
|
+
let out = powershell(CONFIRM, title, message, false);
|
|
249
|
+
if failed(&out) {
|
|
250
|
+
return Answer::NoUi;
|
|
251
|
+
}
|
|
252
|
+
Answer::Value(out.code == 0)
|
|
253
|
+
}
|
|
26
254
|
}
|
|
27
255
|
|
|
256
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
28
257
|
struct Zenity {
|
|
29
258
|
/// False when the process could not be started at all (zenity missing).
|
|
30
259
|
spawned: bool,
|
|
@@ -33,6 +262,7 @@ struct Zenity {
|
|
|
33
262
|
stderr: String,
|
|
34
263
|
}
|
|
35
264
|
|
|
265
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
36
266
|
fn zenity(args: &[String]) -> Zenity {
|
|
37
267
|
match Command::new("zenity")
|
|
38
268
|
.args(args)
|
|
@@ -51,14 +281,17 @@ fn zenity(args: &[String]) -> Zenity {
|
|
|
51
281
|
}
|
|
52
282
|
}
|
|
53
283
|
|
|
284
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
54
285
|
fn arg(text: impl Into<String>) -> String {
|
|
55
286
|
text.into()
|
|
56
287
|
}
|
|
57
288
|
|
|
289
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
58
290
|
fn timeout_arg() -> String {
|
|
59
291
|
format!("--timeout={DIALOG_SECONDS}")
|
|
60
292
|
}
|
|
61
293
|
|
|
294
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
62
295
|
fn trim_newline(text: &str) -> String {
|
|
63
296
|
text.trim_end_matches(['\r', '\n']).to_string()
|
|
64
297
|
}
|
|
@@ -66,6 +299,7 @@ fn trim_newline(text: &str) -> String {
|
|
|
66
299
|
/// True when the configured display looks usable. Only consulted when zenity
|
|
67
300
|
/// exits 1 with no output at all: a cancel on a live display, or a silent
|
|
68
301
|
/// start failure on a dead one.
|
|
302
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
69
303
|
fn display_reachable() -> bool {
|
|
70
304
|
if let Some(wl) = env::var_os("WAYLAND_DISPLAY").filter(|v| !v.is_empty()) {
|
|
71
305
|
let runtime = match env::var_os("XDG_RUNTIME_DIR") {
|
|
@@ -94,6 +328,7 @@ fn display_reachable() -> bool {
|
|
|
94
328
|
/// and a failed start both use it), so stderr decides: GTK's own diagnostics
|
|
95
329
|
/// carry a `(zenity:PID): Gtk-WARNING` prefix; any other line, or a display
|
|
96
330
|
/// failure, means the window never opened.
|
|
331
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
97
332
|
fn failed_to_open(z: &Zenity) -> bool {
|
|
98
333
|
if !z.spawned {
|
|
99
334
|
return true;
|
|
@@ -118,6 +353,7 @@ fn failed_to_open(z: &Zenity) -> bool {
|
|
|
118
353
|
.any(|line| !is_gtk_diagnostic(line))
|
|
119
354
|
}
|
|
120
355
|
|
|
356
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
121
357
|
fn is_gtk_diagnostic(line: &str) -> bool {
|
|
122
358
|
// (zenity:1234): Gtk-WARNING **: ...
|
|
123
359
|
let line = line.trim_start_matches('*');
|
|
@@ -163,6 +399,20 @@ pub fn for_dialog(value: &str, max: usize) -> String {
|
|
|
163
399
|
|
|
164
400
|
/// Secret entry. Empty and cancel are both "no value".
|
|
165
401
|
pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
402
|
+
#[cfg(target_os = "macos")]
|
|
403
|
+
{
|
|
404
|
+
return mac::prompt_secret(title, label);
|
|
405
|
+
}
|
|
406
|
+
#[cfg(target_os = "windows")]
|
|
407
|
+
{
|
|
408
|
+
return win::prompt_secret(title, label);
|
|
409
|
+
}
|
|
410
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
411
|
+
prompt_secret_zenity(title, label)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
415
|
+
fn prompt_secret_zenity(title: &str, label: &str) -> Answer<String> {
|
|
166
416
|
if !has_display() {
|
|
167
417
|
return Answer::NoUi;
|
|
168
418
|
}
|
|
@@ -185,6 +435,20 @@ pub fn prompt_secret(title: &str, label: &str) -> Answer<String> {
|
|
|
185
435
|
|
|
186
436
|
/// Plain text entry. Cancel is `Cancelled`; confirming an empty field is `""`.
|
|
187
437
|
pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
438
|
+
#[cfg(target_os = "macos")]
|
|
439
|
+
{
|
|
440
|
+
return mac::prompt_text(title, label);
|
|
441
|
+
}
|
|
442
|
+
#[cfg(target_os = "windows")]
|
|
443
|
+
{
|
|
444
|
+
return win::prompt_text(title, label);
|
|
445
|
+
}
|
|
446
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
447
|
+
prompt_text_zenity(title, label)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
451
|
+
fn prompt_text_zenity(title: &str, label: &str) -> Answer<String> {
|
|
188
452
|
if !has_display() {
|
|
189
453
|
return Answer::NoUi;
|
|
190
454
|
}
|
|
@@ -208,6 +472,20 @@ pub fn prompt_text(title: &str, label: &str) -> Answer<String> {
|
|
|
208
472
|
|
|
209
473
|
/// Yes/no window. Deny and timeout are false and do not fall through.
|
|
210
474
|
pub fn confirm(title: &str, message: &str) -> Answer<bool> {
|
|
475
|
+
#[cfg(target_os = "macos")]
|
|
476
|
+
{
|
|
477
|
+
return mac::confirm(title, message);
|
|
478
|
+
}
|
|
479
|
+
#[cfg(target_os = "windows")]
|
|
480
|
+
{
|
|
481
|
+
return win::confirm(title, message);
|
|
482
|
+
}
|
|
483
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
484
|
+
confirm_zenity(title, message)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
488
|
+
fn confirm_zenity(title: &str, message: &str) -> Answer<bool> {
|
|
211
489
|
if !has_display() {
|
|
212
490
|
return Answer::NoUi;
|
|
213
491
|
}
|
|
@@ -53,14 +53,30 @@ impl Paths {
|
|
|
53
53
|
self.dir.join("store.lock")
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/// Session tokens hold key material, so they prefer tmpfs when there is
|
|
57
|
-
/// they vanish at logout instead of lying on disk.
|
|
56
|
+
/// Session tokens hold key material, so they prefer a tmpfs when there is
|
|
57
|
+
/// one: they vanish at logout instead of lying on disk.
|
|
58
|
+
///
|
|
59
|
+
/// Linux $XDG_RUNTIME_DIR (a tmpfs)
|
|
60
|
+
/// macOS $TMPDIR (per-user, cleaned by the OS)
|
|
61
|
+
/// Windows %TEMP% (per-user)
|
|
58
62
|
pub fn sessions(&self) -> PathBuf {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
if self.dir != Paths::resolve(None).dir {
|
|
64
|
+
return self.dir.join("sessions");
|
|
65
|
+
}
|
|
66
|
+
#[cfg(target_os = "linux")]
|
|
67
|
+
{
|
|
68
|
+
if let Some(runtime) = env::var_os("XDG_RUNTIME_DIR").filter(|v| !v.is_empty()) {
|
|
69
|
+
return PathBuf::from(runtime).join("ai-vault").join("sessions");
|
|
62
70
|
}
|
|
63
|
-
|
|
71
|
+
return self.dir.join("sessions");
|
|
72
|
+
}
|
|
73
|
+
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
|
74
|
+
{
|
|
75
|
+
env::temp_dir().join("ai-vault").join("sessions")
|
|
76
|
+
}
|
|
77
|
+
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
|
|
78
|
+
{
|
|
79
|
+
self.dir.join("sessions")
|
|
64
80
|
}
|
|
65
81
|
}
|
|
66
82
|
|
|
@@ -70,5 +86,12 @@ impl Paths {
|
|
|
70
86
|
}
|
|
71
87
|
|
|
72
88
|
pub fn home() -> PathBuf {
|
|
73
|
-
|
|
89
|
+
#[cfg(windows)]
|
|
90
|
+
let key = "USERPROFILE";
|
|
91
|
+
#[cfg(not(windows))]
|
|
92
|
+
let key = "HOME";
|
|
93
|
+
env::var_os(key)
|
|
94
|
+
.map(PathBuf::from)
|
|
95
|
+
.or_else(|| env::var_os("HOME").map(PathBuf::from))
|
|
96
|
+
.unwrap_or_else(|| Path::new(".").to_path_buf())
|
|
74
97
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//! File and directory permissions, per platform.
|
|
2
|
+
//!
|
|
3
|
+
//! On Unix a secret file is 0600 and its directory 0700. Windows has no mode
|
|
4
|
+
//! bits: a file under the user's profile is protected by the profile's ACL,
|
|
5
|
+
//! which is why the store lives in `%USERPROFILE%\.ai-vault` and nothing else
|
|
6
|
+
//! is done there. The helpers are no-ops that keep the call sites honest.
|
|
7
|
+
|
|
8
|
+
use std::fs::OpenOptions;
|
|
9
|
+
use std::path::Path;
|
|
10
|
+
|
|
11
|
+
/// Open a new file that only this user can read, where the OS has such a thing.
|
|
12
|
+
pub fn open_private(path: &Path) -> std::io::Result<std::fs::File> {
|
|
13
|
+
let mut options = OpenOptions::new();
|
|
14
|
+
options.write(true).create_new(true);
|
|
15
|
+
#[cfg(unix)]
|
|
16
|
+
{
|
|
17
|
+
use std::os::unix::fs::OpenOptionsExt;
|
|
18
|
+
options.mode(0o600);
|
|
19
|
+
}
|
|
20
|
+
options.open(path)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Narrow an existing file to the owner, best effort.
|
|
24
|
+
pub fn restrict_file(path: &Path) {
|
|
25
|
+
#[cfg(unix)]
|
|
26
|
+
{
|
|
27
|
+
use std::os::unix::fs::PermissionsExt;
|
|
28
|
+
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600));
|
|
29
|
+
}
|
|
30
|
+
#[cfg(not(unix))]
|
|
31
|
+
{
|
|
32
|
+
let _ = path;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// Narrow a directory to the owner, best effort.
|
|
37
|
+
pub fn restrict_dir(path: &Path) {
|
|
38
|
+
#[cfg(unix)]
|
|
39
|
+
{
|
|
40
|
+
use std::os::unix::fs::PermissionsExt;
|
|
41
|
+
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700));
|
|
42
|
+
}
|
|
43
|
+
#[cfg(not(unix))]
|
|
44
|
+
{
|
|
45
|
+
let _ = path;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Best-effort fsync of a directory entry, where the platform allows it.
|
|
50
|
+
pub fn sync_dir(dir: &Path) {
|
|
51
|
+
if let Ok(handle) = std::fs::File::open(dir) {
|
|
52
|
+
let _ = handle.sync_all();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
//! unless `bw-session get` is asked for it.
|
|
10
10
|
|
|
11
11
|
use std::fs;
|
|
12
|
-
use std::os::unix::fs::PermissionsExt;
|
|
13
12
|
|
|
14
13
|
use serde::{Deserialize, Serialize};
|
|
15
14
|
|
|
@@ -72,9 +71,9 @@ fn wrap_key(paths: &Paths) -> Result<crypto::Key, String> {
|
|
|
72
71
|
}
|
|
73
72
|
let fresh = crypto::random_bytes::<32>();
|
|
74
73
|
fs::create_dir_all(&paths.dir).map_err(|e| format!("cannot create {}: {e}", paths.dir.display()))?;
|
|
75
|
-
|
|
74
|
+
crate::perms::restrict_dir(&paths.dir);
|
|
76
75
|
crypto::write_atomic(&path, &fresh)?;
|
|
77
|
-
|
|
76
|
+
crate::perms::restrict_file(&path);
|
|
78
77
|
Ok(crypto::Key::new(fresh))
|
|
79
78
|
}
|
|
80
79
|
|
|
@@ -149,7 +148,7 @@ pub fn save(paths: &Paths, kind: Kind, secret: &str) -> Result<Option<i64>, Stri
|
|
|
149
148
|
let bytes = serde_json::to_vec(&stored).map_err(|e| format!("cannot serialize session: {e}"))?;
|
|
150
149
|
let path = paths.dir.join(kind.file());
|
|
151
150
|
crypto::write_atomic(&path, &bytes)?;
|
|
152
|
-
|
|
151
|
+
crate::perms::restrict_file(&path);
|
|
153
152
|
Ok(expires_at)
|
|
154
153
|
}
|
|
155
154
|
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
|
|
17
17
|
use std::collections::BTreeMap;
|
|
18
18
|
use std::fs;
|
|
19
|
-
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
|
20
19
|
|
|
21
20
|
use base64::engine::general_purpose::STANDARD as B64;
|
|
22
21
|
use base64::Engine as _;
|
|
@@ -158,7 +157,7 @@ impl Session {
|
|
|
158
157
|
signed.sign(host_secret);
|
|
159
158
|
let dir = paths.sessions();
|
|
160
159
|
fs::create_dir_all(&dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
|
161
|
-
|
|
160
|
+
crate::perms::restrict_dir(&dir);
|
|
162
161
|
let text = serde_json::to_vec(&signed).map_err(|e| format!("cannot serialize session: {e}"))?;
|
|
163
162
|
crypto::write_atomic(&paths.session_file(&signed.token), &text)
|
|
164
163
|
}
|
|
@@ -277,7 +276,7 @@ pub fn load_auto(paths: &Paths) -> Result<Option<Session>, String> {
|
|
|
277
276
|
|
|
278
277
|
pub fn save_auto(paths: &Paths, session: &Session) -> Result<(), String> {
|
|
279
278
|
fs::create_dir_all(&paths.dir).map_err(|e| format!("cannot create {}: {e}", paths.dir.display()))?;
|
|
280
|
-
|
|
279
|
+
crate::perms::restrict_dir(&paths.dir);
|
|
281
280
|
let bytes = serde_json::to_vec(session).map_err(|e| format!("cannot serialize session: {e}"))?;
|
|
282
281
|
crypto::write_atomic(&paths.auto_session(), &bytes)
|
|
283
282
|
}
|
|
@@ -294,8 +293,8 @@ pub fn ensure_session_dir(paths: &Paths) -> Result<(), String> {
|
|
|
294
293
|
fs::create_dir_all(&paths.dir).map_err(|e| format!("cannot create {}: {e}", paths.dir.display()))?;
|
|
295
294
|
let dir = paths.sessions();
|
|
296
295
|
fs::create_dir_all(&dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
|
297
|
-
|
|
298
|
-
|
|
296
|
+
crate::perms::restrict_dir(&dir);
|
|
297
|
+
crate::perms::restrict_dir(&paths.dir);
|
|
299
298
|
Ok(())
|
|
300
299
|
}
|
|
301
300
|
|
|
@@ -314,30 +313,19 @@ impl StoreLock {
|
|
|
314
313
|
.create(true)
|
|
315
314
|
.read(true)
|
|
316
315
|
.write(true)
|
|
317
|
-
.mode(0o600)
|
|
318
316
|
.open(&path)
|
|
319
317
|
.map_err(|e| format!("cannot open {}: {e}", path.display()))?;
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if rc != 0 {
|
|
325
|
-
return Err(format!("cannot lock {}: {}", path.display(), std::io::Error::last_os_error()));
|
|
326
|
-
}
|
|
327
|
-
}
|
|
318
|
+
crate::perms::restrict_file(&path);
|
|
319
|
+
// fs4 takes an OS lock on Unix (flock), Windows (LockFileEx) and macOS.
|
|
320
|
+
<fs::File as fs4::fs_std::FileExt>::lock_exclusive(&file)
|
|
321
|
+
.map_err(|e| format!("cannot lock {}: {e}", path.display()))?;
|
|
328
322
|
Ok(StoreLock { file, path })
|
|
329
323
|
}
|
|
330
324
|
}
|
|
331
325
|
|
|
332
326
|
impl Drop for StoreLock {
|
|
333
327
|
fn drop(&mut self) {
|
|
334
|
-
|
|
335
|
-
{
|
|
336
|
-
use std::os::unix::io::AsRawFd;
|
|
337
|
-
unsafe {
|
|
338
|
-
libc::flock(self.file.as_raw_fd(), libc::LOCK_UN);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
328
|
+
let _ = <fs::File as fs4::fs_std::FileExt>::unlock(&self.file);
|
|
341
329
|
let _ = &self.path;
|
|
342
330
|
}
|
|
343
331
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xiaolikt/ai-vault",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
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,9 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"optionalDependencies": {
|
|
36
|
-
"@xiaolikt/ai-vault-linux-x64": "0.9.
|
|
36
|
+
"@xiaolikt/ai-vault-linux-x64": "0.9.1",
|
|
37
|
+
"@xiaolikt/ai-vault-linux-arm64": "0.9.1",
|
|
38
|
+
"@xiaolikt/ai-vault-win32-x64": "0.9.1"
|
|
37
39
|
},
|
|
38
40
|
"files": [
|
|
39
41
|
"bin",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-ai-vault",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
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.9.
|
|
28
|
+
"@xiaolikt/ai-vault": "0.9.1"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@earendil-works/pi-coding-agent": "^1.5.0",
|
|
@@ -64,20 +64,29 @@ export function resolveBinary(): string | undefined {
|
|
|
64
64
|
// pip/npm layouts hoist differently: walk up from packages/pi-ai-vault/src.
|
|
65
65
|
let dir = dirname(fileURLToPath(import.meta.url));
|
|
66
66
|
for (let depth = 0; depth < 5; depth++) {
|
|
67
|
-
const
|
|
68
|
-
|
|
67
|
+
for (const name of process.platform === "win32" ? ["ai-vault.exe", "ai-vault.cmd", "ai-vault.ps1"] : ["ai-vault"]) {
|
|
68
|
+
const bundled = join(dir, "node_modules", ".bin", name);
|
|
69
|
+
if (existsSync(bundled)) return bundled;
|
|
70
|
+
}
|
|
69
71
|
const parent = dirname(dir);
|
|
70
72
|
if (parent === dir) break;
|
|
71
73
|
dir = parent;
|
|
72
74
|
}
|
|
73
75
|
|
|
76
|
+
// Windows has no execute bit and npm links .cmd/.ps1 wrappers beside the exe.
|
|
77
|
+
const names = process.platform === "win32" ? ["ai-vault.exe", "ai-vault.cmd", "ai-vault.ps1", "ai-vault"] : ["ai-vault"];
|
|
74
78
|
for (const entry of (process.env.PATH ?? "").split(delimiter)) {
|
|
75
79
|
if (!entry) continue;
|
|
76
|
-
const
|
|
77
|
-
|
|
80
|
+
for (const name of names) {
|
|
81
|
+
const candidate = join(entry, name);
|
|
82
|
+
if (existsSync(candidate)) return candidate;
|
|
83
|
+
}
|
|
78
84
|
}
|
|
79
|
-
for (const
|
|
80
|
-
|
|
85
|
+
for (const base of [join(homedir(), ".local", "bin"), join(homedir(), ".cargo", "bin"), join(homedir(), ".ai-vault", "bin")]) {
|
|
86
|
+
for (const name of names) {
|
|
87
|
+
const candidate = join(base, name);
|
|
88
|
+
if (existsSync(candidate)) return candidate;
|
|
89
|
+
}
|
|
81
90
|
}
|
|
82
91
|
return undefined;
|
|
83
92
|
}
|
package/test/cli.sh
CHANGED
|
@@ -22,6 +22,8 @@ mkdir -p "$HOME"
|
|
|
22
22
|
PASS=0
|
|
23
23
|
FAIL=0
|
|
24
24
|
ok() { printf ' PASS %s\n' "$*"; PASS=$((PASS + 1)); }
|
|
25
|
+
# GNU stat (-c) vs BSD stat (-f): the mode, in octal, on either.
|
|
26
|
+
mode_of() { stat -c %a "$1" 2>/dev/null || stat -f %Lp "$1"; }
|
|
25
27
|
bad() { printf ' FAIL %s\n' "$*"; FAIL=$((FAIL + 1)); }
|
|
26
28
|
# check <description> <expected-exit> <command...>
|
|
27
29
|
check() {
|
|
@@ -43,7 +45,7 @@ grep -q 'needs-ui: password' "$TMP/err" && ok "the needs-ui step is named" || ba
|
|
|
43
45
|
check "a short password is a usage error" 2 bash -c "printf 'short' | '$BIN' init --password-stdin"
|
|
44
46
|
check "init creates the store" 0 bash -c "printf 'correct horse' | '$BIN' init --password-stdin --requester cli"
|
|
45
47
|
[[ -f "$AI_VAULT_DIR/store.json" ]] && ok "store.json exists" || bad "store.json missing"
|
|
46
|
-
[[ "$(
|
|
48
|
+
[[ "$(mode_of "$AI_VAULT_DIR/store.json")" == "600" ]] && ok "store.json is 0600" || bad "store.json is not 0600"
|
|
47
49
|
|
|
48
50
|
echo "== unlock =="
|
|
49
51
|
check "unlock without a window asks the host" 3 "$BIN" unlock
|
|
@@ -148,7 +150,7 @@ check "switch bitwarden.unlock to never" 0 "$BIN" settings bitwarden.unlock neve
|
|
|
148
150
|
check "store a bw session key" 0 bash -c "printf 'BW_BEARER_TOKEN_PLAINTEXT' | '$BIN' bw-session put --json"
|
|
149
151
|
grep -q 'BW_BEARER_TOKEN_PLAINTEXT' "$AI_VAULT_DIR/bitwarden.session.json" && bad "the bw key is plaintext on disk" || ok "the bw key is not plaintext on disk"
|
|
150
152
|
[[ -f "$AI_VAULT_DIR/.wrap" ]] && ok "a wrap key exists" || bad "no wrap key"
|
|
151
|
-
[[ "$(
|
|
153
|
+
[[ "$(mode_of "$AI_VAULT_DIR/.wrap")" == "600" ]] && ok "the wrap key is 0600" || bad "the wrap key is not 0600"
|
|
152
154
|
"$BIN" bw-session get --json | grep -q 'BW_BEARER_TOKEN_PLAINTEXT' && ok "bw-session get unseals it" || bad "bw-session get cannot unseal"
|
|
153
155
|
mv "$AI_VAULT_DIR/.wrap" "$TMP/wrap.bak"
|
|
154
156
|
check "without the wrap key the session does not open" 1 "$BIN" bw-session get
|
|
@@ -161,7 +163,7 @@ check "ente-session put refused in session mode" 1 bash -c "printf 'K' | '$BIN'
|
|
|
161
163
|
check "switch ente.unlock to never" 0 "$BIN" settings ente.unlock never --json
|
|
162
164
|
check "store the Ente vault key" 0 bash -c "printf 'ENTE_KEY_PLAINTEXT' | '$BIN' ente-session put --json"
|
|
163
165
|
grep -q 'ENTE_KEY_PLAINTEXT' "$AI_VAULT_DIR/ente.session.json" && bad "the ente key is plaintext on disk" || ok "the ente key is sealed at rest"
|
|
164
|
-
[[ "$(
|
|
166
|
+
[[ "$(mode_of "$AI_VAULT_DIR/ente.session.json")" == "600" ]] && ok "ente.session.json is 0600" || bad "ente.session.json is not 0600"
|
|
165
167
|
"$BIN" ente-session get --json | grep -q 'ENTE_KEY_PLAINTEXT' && ok "ente-session get unseals it" || bad "ente-session get cannot unseal"
|
|
166
168
|
"$BIN" settings ente.unlock session --json >/dev/null
|
|
167
169
|
check "switching back to session clears the ente key" 5 "$BIN" ente-session get
|
|
@@ -214,7 +216,7 @@ check "an unknown setting is a usage error" 2 "$BIN" settings nope.value 1
|
|
|
214
216
|
check "bw-session put is refused in session mode" 1 bash -c "printf 'K' | '$BIN' bw-session put"
|
|
215
217
|
check "switch bitwarden.unlock to never" 0 "$BIN" settings bitwarden.unlock never --json
|
|
216
218
|
check "bw-session put stores the key" 0 bash -c "printf 'GOOD_SESSION' | '$BIN' bw-session put --json"
|
|
217
|
-
[[ "$(
|
|
219
|
+
[[ "$(mode_of "$AI_VAULT_DIR/bitwarden.session.json")" == "600" ]] && ok "the bw session file is 0600" || bad "the bw session file is not 0600"
|
|
218
220
|
check "bw-session get returns it" 0 "$BIN" bw-session get --json
|
|
219
221
|
check "switch back to session" 0 "$BIN" settings bitwarden.unlock session --json
|
|
220
222
|
check "the stored key is gone" 5 "$BIN" bw-session get
|
package/test/npm.sh
CHANGED
|
@@ -17,7 +17,20 @@ bad() { printf ' FAIL %s\n' "$*"; FAIL=$((FAIL + 1)); }
|
|
|
17
17
|
|
|
18
18
|
echo "== stage the platform package =="
|
|
19
19
|
bash "$ROOT/scripts/stage-npm.sh" >"$TMP/stage" 2>&1 || { cat "$TMP/stage"; exit 1; }
|
|
20
|
-
|
|
20
|
+
# The one that matches this machine: the others are for other OSes and npm
|
|
21
|
+
# refuses to install a tarball whose os/cpu does not match.
|
|
22
|
+
case "$(uname -s)" in
|
|
23
|
+
Linux) OS=linux ;;
|
|
24
|
+
Darwin) OS=darwin ;;
|
|
25
|
+
*) OS=win32 ;;
|
|
26
|
+
esac
|
|
27
|
+
case "$(uname -m)" in
|
|
28
|
+
x86_64|amd64) ARCH=x64 ;;
|
|
29
|
+
aarch64|arm64) ARCH=arm64 ;;
|
|
30
|
+
*) ARCH="$(uname -m)" ;;
|
|
31
|
+
esac
|
|
32
|
+
PLATFORM="$OS-$ARCH"
|
|
33
|
+
[[ -d "$ROOT/npm/$PLATFORM" ]] || PLATFORM="$(cd "$ROOT/npm" && ls -d */ | head -1 | tr -d /)"
|
|
21
34
|
[[ -f "$ROOT/npm/$PLATFORM/bin/ai-vault" ]] && ok "staged $PLATFORM" || bad "nothing staged"
|
|
22
35
|
|
|
23
36
|
echo "== pack =="
|