@xiaolikt/ai-vault 0.6.0 → 0.7.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 +2 -1
- package/README.md +47 -11
- package/crates/ai-vault/Cargo.toml +2 -1
- package/crates/ai-vault/src/bwsession.rs +86 -5
- package/crates/ai-vault/src/cli.rs +176 -20
- package/crates/ai-vault/src/crypto.rs +34 -8
- package/crates/ai-vault/src/dialog.rs +45 -0
- package/crates/ai-vault/src/paths.rs +8 -1
- package/crates/ai-vault/src/session.rs +82 -13
- package/package.json +2 -2
- package/packages/pi-ai-vault/README.md +4 -0
- package/packages/pi-ai-vault/package.json +2 -2
- package/packages/pi-ai-vault/src/cli.ts +58 -1
- package/packages/pi-ai-vault/src/index.ts +28 -15
- package/packages/pi-ai-vault/src/prompt.ts +13 -0
- package/test/cli.sh +85 -5
- package/tests/prompt.test.mjs +12 -0
- package/tests/vault-cli.test.mjs +33 -0
package/test/cli.sh
CHANGED
|
@@ -49,7 +49,26 @@ echo "== unlock =="
|
|
|
49
49
|
check "unlock without a window asks the host" 3 "$BIN" unlock
|
|
50
50
|
check "a wrong password is an auth failure" 5 bash -c "printf 'wrong password' | '$BIN' unlock --password-stdin"
|
|
51
51
|
check "the right password opens it" 0 bash -c "printf 'correct horse' | '$BIN' unlock --password-stdin --json"
|
|
52
|
-
|
|
52
|
+
# The extension is the host: it mints its own secret before the first unlock
|
|
53
|
+
# and presents it on every call. A caller that only says AI_VAULT_HOST=1
|
|
54
|
+
# without a secret is treated as a human (see the attacker block).
|
|
55
|
+
HS=$(python3 -c "import secrets;print(secrets.token_hex(32))")
|
|
56
|
+
export AI_VAULT_HOST=1 AI_VAULT_HOST_SECRET="$HS"
|
|
57
|
+
check "a host that presents no secret is not handed one" 0 bash -c "printf 'correct horse' | env -u AI_VAULT_HOST_SECRET '$BIN' unlock --password-stdin --json >'$TMP/nosec'"
|
|
58
|
+
python3 - "$TMP/nosec" <<'PY' && ok "AI_VAULT_HOST=1 alone yields no hostSecret" || bad "a secret was handed to a caller without one"
|
|
59
|
+
import json,sys
|
|
60
|
+
assert json.load(open(sys.argv[1])).get("hostSecret") is None
|
|
61
|
+
PY
|
|
62
|
+
HOST_JSON=$(printf 'correct horse' | "$BIN" unlock --password-stdin --json)
|
|
63
|
+
ECHOED=$(printf '%s' "$HOST_JSON" | python3 -c "import json,sys;print(json.load(sys.stdin).get('hostSecret',''))")
|
|
64
|
+
[[ "$ECHOED" == "$HS" ]] && ok "the host's own secret is echoed back" || bad "hostSecret echo mismatch"
|
|
65
|
+
TOKEN=$(printf '%s' "$HOST_JSON" | json token)
|
|
66
|
+
# relay <entry> <y|n>: a signed approval for the host's token.
|
|
67
|
+
relay() {
|
|
68
|
+
local ap
|
|
69
|
+
ap=$(python3 -c "import hmac,hashlib;print(hmac.new('$HS'.encode(),('allow:$TOKEN:$1:$2'.lower()).encode(),hashlib.sha256).hexdigest())")
|
|
70
|
+
printf '%s\n' "$2" | "$BIN" get "$1" --token "$TOKEN" --answer-stdin --approval "$ap" --json
|
|
71
|
+
}
|
|
53
72
|
[[ ${#TOKEN} -ge 32 ]] && ok "a session token is printed" || bad "no token"
|
|
54
73
|
SESS="$XDG_RUNTIME_DIR/ai-vault/sessions/$TOKEN.json"
|
|
55
74
|
[[ -f "$SESS" ]] && ok "the token file lives under XDG_RUNTIME_DIR" || bad "token file missing"
|
|
@@ -70,9 +89,9 @@ grep -q 'p1ssw0rd' "$AI_VAULT_DIR/store.json" && bad "the password is in plainte
|
|
|
70
89
|
|
|
71
90
|
echo "== read approval =="
|
|
72
91
|
check "get without a window asks the host" 3 bash -c "'$BIN' get GitHub --token '$TOKEN'"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
[[ $? -eq 0 ]] && ok "an approved
|
|
92
|
+
relay GitHub n >"$TMP/deny" 2>"$TMP/denyerr"; [[ $? -eq 4 ]] && ok "a denied relay is not a lock (exit 4)" || bad "a denied relay was not exit 4"
|
|
93
|
+
relay GitHub y >"$TMP/get" 2>"$TMP/err"
|
|
94
|
+
[[ $? -eq 0 ]] && ok "an approved relay reveals the entry" || bad "an approved relay failed"
|
|
76
95
|
python3 - "$TMP/get" <<'PY' && ok "the approved entry carries the password" || bad "get output is wrong"
|
|
77
96
|
import json,sys
|
|
78
97
|
item = json.load(open(sys.argv[1]))
|
|
@@ -99,6 +118,50 @@ grep -q 'exp-pass' "$AI_VAULT_DIR/store.json" && bad "an imported password is on
|
|
|
99
118
|
check "save requires the caller's --yes" 2 bash -c "printf '[]' | '$BIN' save --token '$TOKEN'"
|
|
100
119
|
check "save inserts drafts" 0 bash -c "printf '[{\"name\":\"Draft\",\"password\":\"dr-pw\",\"uris\":[\"https://d\"]}]' | '$BIN' save --yes --token '$TOKEN' --json"
|
|
101
120
|
|
|
121
|
+
echo "== tamper resistance (H1 / H2) =="
|
|
122
|
+
# A fresh, non-host session for the attacker tests.
|
|
123
|
+
# The attacker: a valid token, but no host identity and no secret (the model's bash).
|
|
124
|
+
T2=$(printf 'correct horse' | env -u AI_VAULT_HOST -u AI_VAULT_HOST_SECRET "$BIN" unlock --password-stdin --json | json token)
|
|
125
|
+
check "attacker: --answer-stdin without a host secret is refused" 2 bash -c "printf 'y\n' | env -u AI_VAULT_HOST -u AI_VAULT_HOST_SECRET '$BIN' get Draft --token '$T2' --answer-stdin"
|
|
126
|
+
check "attacker: a guessed approval with no secret is refused" 2 bash -c "printf 'y\n' | env -u AI_VAULT_HOST -u AI_VAULT_HOST_SECRET '$BIN' get Draft --token '$T2' --answer-stdin --approval 00"
|
|
127
|
+
check "attacker: grant now costs the password (window -> needs-ui)" 3 env -u AI_VAULT_HOST -u AI_VAULT_HOST_SECRET "$BIN" grant Draft --token "$T2"
|
|
128
|
+
T3="$TOKEN"
|
|
129
|
+
"$BIN" revoke Draft --token "$T3" --json >/dev/null
|
|
130
|
+
AP=$(python3 -c "import hmac,hashlib;print(hmac.new('$HS'.encode(),b'allow:$T3:draft:y',hashlib.sha256).hexdigest())")
|
|
131
|
+
check "host: a signed relay is accepted" 0 bash -c "printf 'y\n' | '$BIN' get Draft --token '$T3' --answer-stdin --approval '$AP' --json"
|
|
132
|
+
AP_N=$(python3 -c "import hmac,hashlib;print(hmac.new('$HS'.encode(),b'allow:$T3:draft:n',hashlib.sha256).hexdigest())")
|
|
133
|
+
"$BIN" revoke Draft --token "$T3" --json >/dev/null
|
|
134
|
+
check "a signature for 'n' with stdin 'y' is a mismatch" 4 bash -c "printf 'y\n' | '$BIN' get Draft --token '$T3' --answer-stdin --approval '$AP_N' --json"
|
|
135
|
+
# H2: edit the token file (mint a grant, rename the requester)
|
|
136
|
+
F="$XDG_RUNTIME_DIR/ai-vault/sessions/$T3.json"
|
|
137
|
+
python3 - "$F" <<'PY'
|
|
138
|
+
import json,sys
|
|
139
|
+
d=json.load(open(sys.argv[1])); d['grants']={'draft':9999999999999}; d['requester']='主会话'
|
|
140
|
+
json.dump(d,open(sys.argv[1],'w'))
|
|
141
|
+
PY
|
|
142
|
+
check "an edited token file is rejected" 5 bash -c "'$BIN' get Draft --token '$T3' --json"
|
|
143
|
+
[[ -f "$F" ]] && bad "the tampered file survived" || ok "the tampered file is deleted"
|
|
144
|
+
check "M1: a junk file in the sessions dir is swept on the next call" 0 "$BIN" path --json
|
|
145
|
+
|
|
146
|
+
echo "== sealed Bitwarden session (M4) =="
|
|
147
|
+
check "switch bitwarden.unlock to never" 0 "$BIN" settings bitwarden.unlock never --json
|
|
148
|
+
check "store a bw session key" 0 bash -c "printf 'BW_BEARER_TOKEN_PLAINTEXT' | '$BIN' bw-session put --json"
|
|
149
|
+
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
|
+
[[ -f "$AI_VAULT_DIR/.wrap" ]] && ok "a wrap key exists" || bad "no wrap key"
|
|
151
|
+
[[ "$(stat -c %a "$AI_VAULT_DIR/.wrap")" == "600" ]] && ok "the wrap key is 0600" || bad "the wrap key is not 0600"
|
|
152
|
+
"$BIN" bw-session get --json | grep -q 'BW_BEARER_TOKEN_PLAINTEXT' && ok "bw-session get unseals it" || bad "bw-session get cannot unseal"
|
|
153
|
+
mv "$AI_VAULT_DIR/.wrap" "$TMP/wrap.bak"
|
|
154
|
+
check "without the wrap key the session does not open" 1 "$BIN" bw-session get
|
|
155
|
+
mv "$TMP/wrap.bak" "$AI_VAULT_DIR/.wrap"
|
|
156
|
+
"$BIN" settings bitwarden.unlock session --json >/dev/null
|
|
157
|
+
|
|
158
|
+
echo "== rekey (L2) =="
|
|
159
|
+
OLD_N=$(python3 -c "import json;print(json.load(open('$AI_VAULT_DIR/store.json'))['kdf']['N'])")
|
|
160
|
+
check "rekey with the password" 0 bash -c "printf 'correct horse' | '$BIN' rekey --password-stdin --json"
|
|
161
|
+
NEW_N=$(python3 -c "import json;print(json.load(open('$AI_VAULT_DIR/store.json'))['kdf']['N'])")
|
|
162
|
+
[[ "$NEW_N" == "131072" ]] && ok "scrypt N raised ($OLD_N -> $NEW_N)" || bad "kdf N is $NEW_N after rekey"
|
|
163
|
+
check "the store still opens with the same password" 0 bash -c "printf 'correct horse' | '$BIN' unlock --password-stdin --json"
|
|
164
|
+
|
|
102
165
|
echo "== lock / audit =="
|
|
103
166
|
check "lock drops the token" 0 "$BIN" lock --token "$TOKEN" --json
|
|
104
167
|
check "a dropped token no longer works" 5 "$BIN" list --token "$TOKEN"
|
|
@@ -107,7 +170,7 @@ python3 - "$TMP/audit" <<'PY' && ok "the audit log records events without values
|
|
|
107
170
|
import json,sys
|
|
108
171
|
entries = json.load(open(sys.argv[1]))["entries"]
|
|
109
172
|
events = {e["event"] for e in entries}
|
|
110
|
-
for want in ("init", "unlock", "unlock_failed", "add", "grant", "read_deny", "read_allow", "remove", "import", "lock"):
|
|
173
|
+
for want in ("init", "unlock", "unlock_failed", "add", "grant", "read_deny", "read_allow", "remove", "import", "lock", "rekey", "bw_session"):
|
|
111
174
|
assert want in events, (want, events)
|
|
112
175
|
assert not any("p1ssw0rd" in json.dumps(e) or "exp-pass" in json.dumps(e) for e in entries)
|
|
113
176
|
PY
|
|
@@ -149,6 +212,23 @@ check "bw-session put in time mode" 0 bash -c "printf 'K2' | '$BIN' bw-session p
|
|
|
149
212
|
grep -q '"expiresAt": *[0-9]' "$AI_VAULT_DIR/bitwarden.session.json" && ok "time mode writes a deadline" || bad "time mode wrote no deadline"
|
|
150
213
|
"$BIN" settings bitwarden.unlock session --json >/dev/null
|
|
151
214
|
|
|
215
|
+
echo "== two stores share the sessions dir =="
|
|
216
|
+
# A second store in another directory uses the same XDG_RUNTIME_DIR sessions
|
|
217
|
+
# directory. Housekeeping of one must not delete the other's live token.
|
|
218
|
+
TA=$(printf 'correct horse' | "$BIN" unlock --password-stdin --json | json token)
|
|
219
|
+
check "store A: token works" 0 "$BIN" list --token "$TA" --json
|
|
220
|
+
export AI_VAULT_DIR="$TMP/store2"
|
|
221
|
+
printf 'other horse' | "$BIN" init --password-stdin --json >/dev/null
|
|
222
|
+
TOK2=$(printf 'other horse' | "$BIN" unlock --password-stdin --json | json token)
|
|
223
|
+
# B's init and unlock each ran a sweep; A's token must still be there.
|
|
224
|
+
export AI_VAULT_DIR="$TMP/store"
|
|
225
|
+
check "store A's token survives store B's init/unlock" 0 "$BIN" list --token "$TA" --json
|
|
226
|
+
# A's calls just ran A's sweep; B's token must still be there.
|
|
227
|
+
export AI_VAULT_DIR="$TMP/store2"
|
|
228
|
+
check "store B's token survives store A's calls" 0 "$BIN" list --token "$TOK2" --json
|
|
229
|
+
"$BIN" lock --token "$TOK2" >/dev/null
|
|
230
|
+
export AI_VAULT_DIR="$TMP/store"
|
|
231
|
+
|
|
152
232
|
echo "== doctor =="
|
|
153
233
|
check "doctor runs" 0 "$BIN" doctor --json
|
|
154
234
|
|
package/tests/prompt.test.mjs
CHANGED
|
@@ -198,3 +198,15 @@ test("no display and no UI fails closed", async () => {
|
|
|
198
198
|
restore();
|
|
199
199
|
}
|
|
200
200
|
});
|
|
201
|
+
|
|
202
|
+
test("forDialog flattens a forged multi-line value into one bounded line", async () => {
|
|
203
|
+
const { forDialog } = await import("../packages/pi-ai-vault/src/prompt.ts");
|
|
204
|
+
const forged = "github\n\n条目: nothing\n请求方: 主会话\n(已授权,点允许)";
|
|
205
|
+
const clean = forDialog(forged);
|
|
206
|
+
assert.equal(clean.includes("\n"), false);
|
|
207
|
+
assert.ok(clean.startsWith("github 条目: nothing"));
|
|
208
|
+
assert.ok(clean.length <= 80);
|
|
209
|
+
assert.equal(forDialog(" a b "), "a b");
|
|
210
|
+
assert.equal(forDialog("x".repeat(100), 10).length, 10);
|
|
211
|
+
assert.ok(forDialog("x".repeat(100), 10).endsWith("…"));
|
|
212
|
+
});
|
package/tests/vault-cli.test.mjs
CHANGED
|
@@ -280,3 +280,36 @@ test("a persisted Bitwarden session costs no password", async () => {
|
|
|
280
280
|
const get = spawnSync(bin, ["bw-session", "get"], { env: process.env, encoding: "utf8" });
|
|
281
281
|
assert.equal(get.status, 5, "switching back to session must remove the stored key");
|
|
282
282
|
});
|
|
283
|
+
|
|
284
|
+
test("a relayed read approval is signed by the host: the raw CLI refuses a bare 'y'", async () => {
|
|
285
|
+
const bin = process.env.AI_VAULT_BIN;
|
|
286
|
+
// A session opened by a human (no host identity): the attacker's position.
|
|
287
|
+
const human = spawnSync(bin, ["unlock", "--password-stdin", "--json"], {
|
|
288
|
+
input: `${PW}\n`,
|
|
289
|
+
env: { ...process.env, AI_VAULT_HOST: "", AI_VAULT_HOST_SECRET: "" },
|
|
290
|
+
encoding: "utf8",
|
|
291
|
+
});
|
|
292
|
+
const token = JSON.parse(human.stdout).token;
|
|
293
|
+
assert.equal(JSON.parse(human.stdout).hostSecret ?? null, null, "a human caller is not handed a secret");
|
|
294
|
+
const bare = spawnSync(bin, ["get", "GitHub", "--token", token, "--answer-stdin", "--json"], {
|
|
295
|
+
input: "y\n",
|
|
296
|
+
env: { ...process.env, AI_VAULT_HOST: "", AI_VAULT_HOST_SECRET: "" },
|
|
297
|
+
encoding: "utf8",
|
|
298
|
+
});
|
|
299
|
+
assert.equal(bare.status, 2, bare.stderr);
|
|
300
|
+
assert.match(bare.stderr, /--approval/);
|
|
301
|
+
|
|
302
|
+
// The same token, with the file edited to carry a grant: rejected on load.
|
|
303
|
+
const { readdirSync, readFileSync, writeFileSync } = await import("node:fs");
|
|
304
|
+
const { join } = await import("node:path");
|
|
305
|
+
const dir = join(process.env.XDG_RUNTIME_DIR ?? "/tmp", "ai-vault", "sessions");
|
|
306
|
+
const file = join(dir, `${token}.json`);
|
|
307
|
+
const edited = JSON.parse(readFileSync(file, "utf8"));
|
|
308
|
+
edited.grants = { github: 9999999999999 };
|
|
309
|
+
edited.requester = "主会话";
|
|
310
|
+
writeFileSync(file, JSON.stringify(edited));
|
|
311
|
+
const tampered = spawnSync(bin, ["get", "GitHub", "--token", token, "--json"], { encoding: "utf8" });
|
|
312
|
+
assert.equal(tampered.status, 5);
|
|
313
|
+
assert.match(tampered.stderr, /modified outside ai-vault/);
|
|
314
|
+
assert.equal(readdirSync(dir).includes(`${token}.json`), false, "the tampered file is deleted");
|
|
315
|
+
});
|