AnyBot/docs/restructure/notes/b04i-secrets-vault.md

45 lines
8.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# b04i:secrets.rs(109 行)+ vault_store.rs(1,248 行)+ vault.rs(913 行)— 金鑰遮罩與保險庫
## secrets.rs(對話里的金鑰遮罩)
MASK="••••"。規則表(OnceLock Regex,實讀逐條):GitLab `glpat-`、GitHub `ghp_`/`github_pat_`、`sk-`、`xai-`、AWS `AKIA`/`ASIA`、Slack `xox`、Google `AIza`、JWT `eyJ`、Bearer、PRIVATE KEY 區塊;+「NAME=value」型(NAME 含 TOKEN/SECRET/PASSWORD/API_KEY)。
- **不在 oplog 上用**(實讀註記:重放要真值——遮罩只給模型看/寫進聊天)。
## vault_store.rs(保險庫本體)
- 檔:`<data>/vault.bin` = **12-byte nonce + AES-256-GCM(JSON)**;金鑰隨機、存**系統金鑰圈**(macOS Keychain / Linux Secret Service),沒有就退 **0600 金鑰檔**(Windows 現行無金鑰圈=走檔案——實讀 416)。
- 環境逃生口:`ANYBOT_VAULT_DIR`(339)、`ANYBOT_VAULT_KEY_FILE`(361,測試用)。
- ring aead:`AES_256_GCM` UnboundKey;密封aad=`b"anybot-state-v1"`;`SEALED = "enc:v1:"`(447)=**chatgpt token 也用保險庫金鑰封**(nonce+hex 內嵌,465–480 seal/open 逐字)。金鑰讀一次快取(449:問金鑰圈會起程式)。
- 常數:MAX_SITES=10、MAX_NOTES=2000、MAX_TITLE=200、MAX_USERNAME=512、MAX_PASSWORD_BYTES=4096、MAX_ROWS=10(一次匯入)、MAX_IMPORT_BYTES=5MB。
- `Kind{Login,Card,Contact}`;Item 欄位(sites,username,notes,brand,last4,exp_month/exp_year,number,csc,holder,billing,name,address{line1,line2,city,region,postal,country},phones,emails,label/title,imported_keychain)。
- `registrable_domain(host)`(249)=註冊域(比對網站用;vault 互印)。
- `put_login/put_card/put_contact`(Input struct 各一)、`get`、`list()`=**秘密欄全部清空**、`logins_for(site)`、`add_site(id, site)`、`login_secret(id)/card_secret(id)`=**只有填入那一步才讀到真值**。
- CSV 匯入:`CsvFormat{830}`=Chrome/Edge、Safari/Apple Passwords、Firefox、LastPass、1Password 五種(偵測文案逐條),表頭判格式(900–902);`RowStatus{Ready,AlreadySaved,Error,Skipped}`;上限 10 列/5MB。`import_keychain_once()`(583)=macOS 舊 AnyBot 鑰匙圈登入一次匯入。
## vault.rs(填入動作——Bot 永遠看不到密碼)
常數/工具名:TOOL="browser_fill_login"、LIST_TOOL="vault_list"、CARD_TOOL="browser_fill_card"、CONTACT_TOOL="browser_fill_contact"、**REMOTE_TOOL="browser_vault_fill"**(唯一帶秘密的指令,走帳號加密 relay 到有小鍵盤那台;**永不給模型**=AnyBot→AnyBot)、LABEL="AnyBot 登入"(鑰匙圈標籤)。
- `Offer{login,card,contact}`+`from_kinds(kinds, logins_elsewhere)`/`any()`/`kinds()`/`offer()`=macOS 或有 `op` → login 可來源在別處。**空保險庫不給填入工具**(實讀註記:Threads 巡逻 bot 被硬塞三工具,小模型一回合「_accidentally_'」連打 171 次失敗填入)。
- `op_installed()`=在 cloudbox::tool_path 逐目錄找 op/op.exe。
- `specs_for(offer)`=vault_list+按 Offer 的填入工具(描述全文逐字:「你看不到密碼」「每次都要使用者核准」「付款前一定先問」)。
- `check_ids/check_ids_in`=**動瀏覽器之前**先答「保險庫沒有這筆」(措辭指向 browser action=type——它多半是要打字)。空 id 的 login 放過(按站查)。
- `list_for_bot(args)`=kind/query 過濾;Login→sites/username/notes;Card→brand/last4/exp MM/YY/holder/billing.one_line;Contact→地址全欄——**無密碼/卡號/安全碼**。
- `host_of(url)`/`candidates(host)`(accounts.google.com→[全,google.com])。
- 鑰匙圈流:`Login{username,password,source}`。macOS `security find-internet-password -s host -g`(password 在 **stderr**,`parse_g_password`:`"text"` 或 `0xHEX "escaped"`;-w 分不出故不用);acct 從 stdout `"acct"` 行。Linux **secret-tool**(LC_ALL=C.UTF-8 必設——中文標籤)`search --all --unlock application anybot server <host>`,`parse_secret_search` 逐 block(attribute.* + secret;stdout 或 stderr 舊版)。1Password:`op item list --categories Login --format json` → urls.href **完全等于或 `.subdomain` 後綴**(notgoogle.com 不答 google.com——防仿冒逐字)→ `op item get --fields label=username,label=password --reveal`。`find(host)`=鑰匙圈 candidates 全跑 → 1Password candidates 全跑。
- 索引:`vault-index.json`(AnyBot 存過的 site,account 名單;寫用 httpd::write_private)+`vault-index-imported` 單次標記(舊鑰匙圈 AnyBot 登入匯入一次);`legacy_keychain_logins()`=security dump-keychain 逐 block(LABEL 文字或 hex 大寫兩種都認);`keychain_has`(不讀密碼=不彈窗);`saved_sites()`=macOS 名單 prune(被人手刪的掉掉)/Linux secret-tool search application=anybot。`cfg!(test)` 一律不讀真密碼(逐條)。
- 登入 JS `fill_script_for_host(u,p,submit,expected_host)`:**host 守門**(location.hostname≠want → {wrong_host},等核准期間換頁=釣魚取消);值走 HTMLInputElement prototype setter(React 認得);密碼欄上 `data-anybot-vault-secret`+`window.__anybot_vault_tainted`(污點旗標);帳號欄=密碼欄之前那個 email/text/username 欄;送出走 form 的 submit 钮,没有 form 找最近可見的「登入|log in|sign in|繼續|下一步|next|continue」钮(距離排序)。
- `LoginPlan{host,profile,item,matched,other}`+`question()`:matched→直白;**未 matched→仿冒警告全文**(「只有在它與已儲存的網站屬於同一服務時才允許。仿冒網站可能會竊取你的密碼。」逐字)。
- `plan_login/plan_login_on(tab,v)`:**打開的分頁就是站**(問什麼/記「一律允許」/比對已存,全用分頁 host;`site` 只挑哪筆登入——舊版頁面可騙 site=google.com 在已准 google.com 時把 Google 密碼填進仿冒頁,實讀修復註)。site≠分頁→筆在 vault 照給(帶警告、不記准);vault 無→鑰匙圈/1Password **只准在它自己的站**;「一律允許」新站→`add_site`(只有人選「一律允許」才記,一次核准不記——login_fill_text 的 remember_site 逐字)。
- `login_secret_of(plan)`=(username,password,來源)=**唯一出口**;`fill_login_with`→eval_current→`login_fill_text`(wrong_host→取消文案;無欄位→「先按登入/先填帳號按下一步」指引;成功文案含「你看不到密碼」逐字)。
- 表單 JS `form_fill_script_for_host(values, expected_host, require_https)`:host 守門+**https 守門**(insecure 回 {insecure},127.0.0.1/localhost 放行);autocomplete 角色優先,無 autocomplete 才走 regex 規則表(cc-number/cc-exp(-month/-year)/cc-csc/cc-name/name/address-line1/2/level1/2/postal-code/country/tel/email,中英繁体字樣逐條);SELECT 用 value/text 比對;**cc-number/cc-csc 上污點旗標**;回 {filled, frames}。
- 卡:`plan_card_at`=**非 HTTPS 拒填**(127.0.0.1/localhost 例外);`card_values`=秘密欄(cc-* 全套);`card_fill_text`=「已填入 {卡名} •••• {last4} 的…(你看不到卡號和安全碼)。送出付款前一定要先問使用者。」逐字;frames>0→「付款欄在付款服務的框架裡,填不進去,請使用者自己填」;wrong_host/insecure→取消。
- 聯絡:`plan_contact_at`(id→Contact);`contact_values`(name/address-*/tel=第一通/email=第一組);contact_fill_text(wrong_host→取消;無欄→Err;成功列角色名)。
- `role_name`/`filled_summary`(角色→中文名、dedup)。
- `run_remote(args)` kind=host/login/form=小鍵盤那台的本地執行端(eval_current);「不懂的保險庫填入種類」Err 逐字。
## 測試鎖(節選)
offer(空庫=零工具;只有有的;鑰匙圈來源)、check_ids(先於分頁、指路 action=type)、host_of/candidates/parse_secret_search、parse_g_password(hex+escaped+空)、`the_script_keeps_the_password_in_a_string`(密碼只以 JSON 字串進 JS)、`fill_live` #[ignore](真瀏覽器 127.0.0.1 假站全流:login/card/contact 填入後 eval 驗值、模型可见文本无秘密)。
## 平台差
macOS:security CLI + Keychain 標籤 hex 難搜→靠 index 檔。Linux:secret-tool(Secret Service,application=anybot 屬性)。Windows:**無金鑰圈→金鑰走 0600 檔**。1Password:op CLI(PATH 用 cloudbox::tool_path)。
## 迁移
vault_store.rs → **core::vault**(加密/条目/匯入;ring aead 紅線)。vault.rs 拆:JS 腳本+Offer+specs+plan → **browser crate**(吃 botbrowser::eval_current);鑰匙圈/secret-tool/op CLI → **platform**(mac/linux 各自);REMOTE_TOOL 中繼 → channels/net(b05 互印)。紅線:仿冒警告、host 守門、污點旗標、付款前必問——逐字保留。