@xiaolikt/ai-vault 0.8.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 +41 -1
- 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 +72 -26
- package/crates/ai-vault/src/config.rs +25 -7
- 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 +2 -1
- package/crates/ai-vault/src/paths.rs +30 -13
- package/crates/ai-vault/src/perms.rs +54 -0
- package/crates/ai-vault/src/{bwsession.rs → sealed.rs} +50 -24
- package/crates/ai-vault/src/session.rs +9 -21
- package/package.json +4 -2
- package/packages/pi-ai-vault/README.md +3 -1
- package/packages/pi-ai-vault/package.json +2 -2
- package/packages/pi-ai-vault/src/cli.ts +15 -6
- package/packages/pi-ai-vault/src/ente.ts +27 -1
- package/packages/pi-ai-vault/src/index.ts +23 -1
- package/packages/pi-ai-vault/src/settings.ts +3 -0
- package/test/cli.sh +17 -4
- package/test/npm.sh +14 -1
- package/tests/ente.test.mjs +51 -0
- package/tests/helpers.mjs +3 -0
- package/tests/mocks/bin/ente +7 -0
|
@@ -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
|
}
|
|
@@ -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 {
|
|
@@ -59,14 +53,30 @@ impl Paths {
|
|
|
59
53
|
self.dir.join("store.lock")
|
|
60
54
|
}
|
|
61
55
|
|
|
62
|
-
/// Session tokens hold key material, so they prefer tmpfs when there is
|
|
63
|
-
/// 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)
|
|
64
62
|
pub fn sessions(&self) -> PathBuf {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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");
|
|
68
70
|
}
|
|
69
|
-
|
|
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")
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
|
|
@@ -76,5 +86,12 @@ impl Paths {
|
|
|
76
86
|
}
|
|
77
87
|
|
|
78
88
|
pub fn home() -> PathBuf {
|
|
79
|
-
|
|
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())
|
|
80
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
|
+
}
|
|
@@ -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
|
|
@@ -8,7 +9,6 @@
|
|
|
8
9
|
//! unless `bw-session get` is asked for it.
|
|
9
10
|
|
|
10
11
|
use std::fs;
|
|
11
|
-
use std::os::unix::fs::PermissionsExt;
|
|
12
12
|
|
|
13
13
|
use serde::{Deserialize, Serialize};
|
|
14
14
|
|
|
@@ -17,6 +17,30 @@ use crate::crypto;
|
|
|
17
17
|
use crate::paths::Paths;
|
|
18
18
|
use crate::session::now_ms;
|
|
19
19
|
|
|
20
|
+
/// Which secret this file holds. They are sealed the same way but carry
|
|
21
|
+
/// different names and different policies.
|
|
22
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
23
|
+
pub enum Kind {
|
|
24
|
+
Bitwarden,
|
|
25
|
+
Ente,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl Kind {
|
|
29
|
+
fn file(self) -> &'static str {
|
|
30
|
+
match self {
|
|
31
|
+
Kind::Bitwarden => "bitwarden.session.json",
|
|
32
|
+
Kind::Ente => "ente.session.json",
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fn policy(self, config: &config::Config) -> config::StorePolicy {
|
|
37
|
+
match self {
|
|
38
|
+
Kind::Bitwarden => config.bitwarden.store,
|
|
39
|
+
Kind::Ente => config.ente,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
20
44
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
21
45
|
pub struct Stored {
|
|
22
46
|
#[serde(rename = "savedAt")]
|
|
@@ -47,9 +71,9 @@ fn wrap_key(paths: &Paths) -> Result<crypto::Key, String> {
|
|
|
47
71
|
}
|
|
48
72
|
let fresh = crypto::random_bytes::<32>();
|
|
49
73
|
fs::create_dir_all(&paths.dir).map_err(|e| format!("cannot create {}: {e}", paths.dir.display()))?;
|
|
50
|
-
|
|
74
|
+
crate::perms::restrict_dir(&paths.dir);
|
|
51
75
|
crypto::write_atomic(&path, &fresh)?;
|
|
52
|
-
|
|
76
|
+
crate::perms::restrict_file(&path);
|
|
53
77
|
Ok(crypto::Key::new(fresh))
|
|
54
78
|
}
|
|
55
79
|
|
|
@@ -100,11 +124,18 @@ impl Stored {
|
|
|
100
124
|
|
|
101
125
|
/// Store a key, honouring the current `bitwarden.unlock` policy. `session`
|
|
102
126
|
/// means "do not persist": the caller keeps it in memory instead.
|
|
103
|
-
pub fn save(paths: &Paths,
|
|
104
|
-
let
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
127
|
+
pub fn save(paths: &Paths, kind: Kind, secret: &str) -> Result<Option<i64>, String> {
|
|
128
|
+
let config = config::load(paths);
|
|
129
|
+
let policy = kind.policy(&config);
|
|
130
|
+
if policy.unlock == UnlockMode::Session {
|
|
131
|
+
clear(paths, kind);
|
|
132
|
+
return Err(format!(
|
|
133
|
+
"{}.unlock is \"session\": nothing is kept on disk",
|
|
134
|
+
match kind {
|
|
135
|
+
Kind::Bitwarden => "bitwarden",
|
|
136
|
+
Kind::Ente => "ente",
|
|
137
|
+
}
|
|
138
|
+
));
|
|
108
139
|
}
|
|
109
140
|
let now = now_ms();
|
|
110
141
|
let expires_at = match policy.unlock {
|
|
@@ -113,15 +144,16 @@ pub fn save(paths: &Paths, key: &str) -> Result<Option<i64>, String> {
|
|
|
113
144
|
UnlockMode::Session => None,
|
|
114
145
|
};
|
|
115
146
|
let wrap = wrap_key(paths)?;
|
|
116
|
-
let stored = Stored { saved_at: now, expires_at, sealed: seal(&wrap,
|
|
147
|
+
let stored = Stored { saved_at: now, expires_at, sealed: seal(&wrap, secret)? };
|
|
117
148
|
let bytes = serde_json::to_vec(&stored).map_err(|e| format!("cannot serialize session: {e}"))?;
|
|
118
|
-
|
|
119
|
-
|
|
149
|
+
let path = paths.dir.join(kind.file());
|
|
150
|
+
crypto::write_atomic(&path, &bytes)?;
|
|
151
|
+
crate::perms::restrict_file(&path);
|
|
120
152
|
Ok(expires_at)
|
|
121
153
|
}
|
|
122
154
|
|
|
123
|
-
pub fn load(paths: &Paths) -> Result<Option<Stored>, String> {
|
|
124
|
-
let path = paths.
|
|
155
|
+
pub fn load(paths: &Paths, kind: Kind) -> Result<Option<Stored>, String> {
|
|
156
|
+
let path = paths.dir.join(kind.file());
|
|
125
157
|
let text = match fs::read_to_string(&path) {
|
|
126
158
|
Ok(text) => text,
|
|
127
159
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
|
@@ -142,8 +174,8 @@ pub fn load(paths: &Paths) -> Result<Option<Stored>, String> {
|
|
|
142
174
|
}
|
|
143
175
|
|
|
144
176
|
/// 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)? {
|
|
177
|
+
pub fn key(paths: &Paths, kind: Kind) -> Result<Option<String>, String> {
|
|
178
|
+
match load(paths, kind)? {
|
|
147
179
|
None => Ok(None),
|
|
148
180
|
Some(stored) => {
|
|
149
181
|
let wrap = wrap_key(paths)?;
|
|
@@ -152,12 +184,6 @@ pub fn key(paths: &Paths) -> Result<Option<String>, String> {
|
|
|
152
184
|
}
|
|
153
185
|
}
|
|
154
186
|
|
|
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
|
-
}
|
|
187
|
+
pub fn clear(paths: &Paths, kind: Kind) -> bool {
|
|
188
|
+
fs::remove_file(paths.dir.join(kind.file())).is_ok()
|
|
163
189
|
}
|