clikcode 1.0.3 → 1.0.5
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/README.md +25 -14
- package/dist/chunk-23TDEBGZ.js +71 -0
- package/dist/chunk-25T3GVS7.js +425 -0
- package/dist/chunk-2BBELGHY.js +11 -0
- package/dist/chunk-2EOL5PZL.js +208 -0
- package/dist/chunk-2GAB36IE.js +26 -0
- package/dist/chunk-2OLS7V66.js +2440 -0
- package/dist/chunk-2QFO2URB.js +179 -0
- package/dist/chunk-2QWPQRSU.js +254 -0
- package/dist/chunk-2YPAPAR3.js +184 -0
- package/dist/chunk-2ZAG5372.js +33 -0
- package/dist/chunk-32IKAIZC.js +12507 -0
- package/dist/chunk-34AZEGYW.js +95 -0
- package/dist/chunk-34JQYQCA.js +175 -0
- package/dist/chunk-34ZISPAO.js +67 -0
- package/dist/chunk-3AAAARE6.js +46 -0
- package/dist/chunk-3AJ6Q3OY.js +510 -0
- package/dist/chunk-3B2CHX7M.js +884 -0
- package/dist/chunk-3BUFORRX.js +59 -0
- package/dist/chunk-3QLHYYFK.js +30 -0
- package/dist/chunk-3RXBMYKZ.js +140 -0
- package/dist/chunk-3TFNIJWS.js +52 -0
- package/dist/chunk-3W3IPDFP.js +119 -0
- package/dist/chunk-3WMS7SEE.js +417 -0
- package/dist/chunk-435ZNSZ3.js +23 -0
- package/dist/chunk-4AQ5NINV.js +39 -0
- package/dist/chunk-4GVEJ5FK.js +168 -0
- package/dist/chunk-4GXAO3UY.js +262 -0
- package/dist/chunk-4L2ZZM4Q.js +332 -0
- package/dist/chunk-4LPDG57B.js +17 -0
- package/dist/chunk-4NN3XQQT.js +223 -0
- package/dist/chunk-4QZEZME7.js +54 -0
- package/dist/chunk-4R6CSC3S.js +38 -0
- package/dist/chunk-4RHXTL3W.js +514 -0
- package/dist/chunk-4SJMPTVQ.js +18 -0
- package/dist/chunk-4VY7G6TX.js +114 -0
- package/dist/chunk-5642PBF7.js +124 -0
- package/dist/chunk-5B6TVO4A.js +33 -0
- package/dist/chunk-5BJLDUPL.js +62 -0
- package/dist/chunk-5HMFFJWU.js +19 -0
- package/dist/chunk-5IKLK33T.js +69 -0
- package/dist/chunk-5NQ5FFMX.js +44 -0
- package/dist/chunk-5QNNW5L6.js +176 -0
- package/dist/chunk-5SM5UAWN.js +239 -0
- package/dist/chunk-5WYJTA6O.js +74 -0
- package/dist/chunk-5ZSR2Y5T.js +227 -0
- package/dist/chunk-5ZWUM5GH.js +39 -0
- package/dist/chunk-64E7D4HY.js +364 -0
- package/dist/chunk-6CK466BC.js +99 -0
- package/dist/chunk-6ES45HHO.js +138 -0
- package/dist/chunk-6HYEZFGL.js +277 -0
- package/dist/chunk-6Y7HLXGW.js +29 -0
- package/dist/chunk-6YKK4FYR.js +258 -0
- package/dist/chunk-74WNMLMS.js +117 -0
- package/dist/chunk-7AULZ7OD.js +55 -0
- package/dist/chunk-7DOKGSUN.js +101 -0
- package/dist/chunk-7IH5LTIZ.js +195 -0
- package/dist/chunk-7ISTS5NS.js +21 -0
- package/dist/chunk-7NMUTGOW.js +527 -0
- package/dist/chunk-7PNHUZEL.js +153 -0
- package/dist/chunk-7TSQLONI.js +88 -0
- package/dist/chunk-AE7TOLD5.js +30 -0
- package/dist/chunk-AEMHSXKA.js +130 -0
- package/dist/chunk-AKRYTS2D.js +211 -0
- package/dist/chunk-AMNSE36L.js +59 -0
- package/dist/chunk-AZVD74ZV.js +23 -0
- package/dist/chunk-B35DVOTK.js +1981 -0
- package/dist/chunk-B4HNJRAH.js +626 -0
- package/dist/chunk-B5WYPPHS.js +13 -0
- package/dist/chunk-BBOVDXQM.js +59 -0
- package/dist/chunk-BD3ECZLK.js +62 -0
- package/dist/chunk-BDGSOT74.js +4578 -0
- package/dist/chunk-BF5VEOPK.js +31 -0
- package/dist/chunk-BFFRW4XH.js +47 -0
- package/dist/chunk-BFPC6PEW.js +208 -0
- package/dist/chunk-BGEMY2JJ.js +376 -0
- package/dist/chunk-BISGNHKD.js +35 -0
- package/dist/chunk-BMRAEBRY.js +21 -0
- package/dist/chunk-BNTXAZXW.js +127 -0
- package/dist/chunk-BOQBXGSN.js +75 -0
- package/dist/chunk-BTJDSTQP.js +213 -0
- package/dist/chunk-BUOPJQP5.js +218 -0
- package/dist/chunk-BXFJS65Q.js +52 -0
- package/dist/chunk-BXPQFSX5.js +59 -0
- package/dist/chunk-C57LIWNY.js +1269 -0
- package/dist/chunk-C6YVCCWR.js +74 -0
- package/dist/chunk-CAADDF6M.js +167 -0
- package/dist/chunk-CBANJFBO.js +69 -0
- package/dist/chunk-CERF3RIB.js +597 -0
- package/dist/chunk-COAS5ETR.js +46 -0
- package/dist/chunk-CQ7J6UQ7.js +78 -0
- package/dist/chunk-CRUVYGO2.js +26 -0
- package/dist/chunk-CVFVFRGN.js +58 -0
- package/dist/chunk-D2UK66HM.js +37 -0
- package/dist/chunk-D2W7SA4R.js +18 -0
- package/dist/chunk-DA44ZENT.js +501 -0
- package/dist/chunk-DAJ76TOC.js +99 -0
- package/dist/chunk-DCZ6CIUM.js +147 -0
- package/dist/chunk-DF35KMXM.js +1314 -0
- package/dist/chunk-DGASQT26.js +151 -0
- package/dist/chunk-DJT57C5P.js +55 -0
- package/dist/chunk-DKDPZTD3.js +44 -0
- package/dist/chunk-DQYVQ2N7.js +77 -0
- package/dist/chunk-DRTP2RTY.js +266 -0
- package/dist/chunk-DW2BMUKY.js +61 -0
- package/dist/chunk-DW5T5ZVU.js +307 -0
- package/dist/chunk-DXKNDNFC.js +164 -0
- package/dist/chunk-DZEQ7UYR.js +291 -0
- package/dist/chunk-E4F4OUAC.js +463 -0
- package/dist/chunk-EE4B6A7W.js +69 -0
- package/dist/chunk-EISKIYWG.js +23 -0
- package/dist/chunk-EKAQK7UQ.js +235 -0
- package/dist/chunk-EOBTJV32.js +123 -0
- package/dist/chunk-ESF3DIZY.js +325 -0
- package/dist/chunk-EVOHOZ4U.js +45 -0
- package/dist/chunk-EXN4KHLB.js +110 -0
- package/dist/chunk-F252T6Y3.js +78 -0
- package/dist/chunk-F3DP4ZA2.js +40 -0
- package/dist/chunk-F6F47QAY.js +58 -0
- package/dist/chunk-FA3Y23ZW.js +57 -0
- package/dist/chunk-FBKXE4EJ.js +640 -0
- package/dist/chunk-FCGEB3H6.js +23 -0
- package/dist/chunk-FF7ZVYJF.js +3732 -0
- package/dist/chunk-FFGKN6L3.js +39 -0
- package/dist/chunk-FFGU4GCC.js +94 -0
- package/dist/chunk-FSFDAOVP.js +74 -0
- package/dist/chunk-FT5TX7OX.js +1573 -0
- package/dist/chunk-G3QP67Z7.js +232 -0
- package/dist/chunk-G5ORJKV2.js +179 -0
- package/dist/chunk-GFNC62PE.js +111 -0
- package/dist/chunk-GGK43Q3N.js +67 -0
- package/dist/chunk-GGYKY4GF.js +11537 -0
- package/dist/chunk-GGYNHGNJ.js +55 -0
- package/dist/chunk-GLY7RM3C.js +354 -0
- package/dist/chunk-GQBZU6EO.js +20 -0
- package/dist/chunk-GSXENUZL.js +9 -0
- package/dist/chunk-GTOAT5D3.js +40 -0
- package/dist/chunk-GWCMDPQU.js +1007 -0
- package/dist/chunk-GXYSMYHJ.js +152 -0
- package/dist/chunk-GYMAI5NY.js +14 -0
- package/dist/chunk-H2VZMKPE.js +30 -0
- package/dist/chunk-H4THUNBM.js +2091 -0
- package/dist/chunk-HGRVSNUZ.js +361 -0
- package/dist/chunk-HI4VY473.js +27 -0
- package/dist/chunk-HOJRSD3Q.js +447 -0
- package/dist/chunk-HRX25W35.js +82 -0
- package/dist/chunk-HSE4AQYV.js +55 -0
- package/dist/chunk-HU5SSUFX.js +383 -0
- package/dist/chunk-I3GDBJLH.js +196 -0
- package/dist/chunk-I75EDU75.js +684 -0
- package/dist/chunk-IBA7DMIO.js +20 -0
- package/dist/chunk-IC6N4NZA.js +231 -0
- package/dist/chunk-IF43HIGV.js +284 -0
- package/dist/chunk-IGZ6C2MC.js +439 -0
- package/dist/chunk-IMLTL2MY.js +192 -0
- package/dist/chunk-IMZL6SGR.js +33 -0
- package/dist/chunk-IS4USJPY.js +516 -0
- package/dist/chunk-ISFSPN2F.js +82 -0
- package/dist/chunk-IXE7GTFI.js +55 -0
- package/dist/chunk-IZ77QJQG.js +36 -0
- package/dist/chunk-J2PACWRQ.js +34 -0
- package/dist/chunk-J6EGFVQX.js +180 -0
- package/dist/chunk-JEKOD43D.js +22 -0
- package/dist/chunk-JGIWATSG.js +30 -0
- package/dist/chunk-JYBONKD4.js +43 -0
- package/dist/chunk-K2NLG2HK.js +454 -0
- package/dist/chunk-KCQ4TBLS.js +164 -0
- package/dist/chunk-KD5KIVMS.js +176 -0
- package/dist/chunk-KHW6TX6I.js +63 -0
- package/dist/chunk-KI5GZFCE.js +520 -0
- package/dist/chunk-KN34B7NO.js +105 -0
- package/dist/chunk-KYUHIFP3.js +136 -0
- package/dist/chunk-KZL5VS4B.js +300 -0
- package/dist/chunk-L3VW3QIH.js +48 -0
- package/dist/chunk-LA6MG4ET.js +2110 -0
- package/dist/chunk-LAVFOA3S.js +66 -0
- package/dist/chunk-LB4OA77N.js +13 -0
- package/dist/chunk-LBFRF2K4.js +196 -0
- package/dist/chunk-LM6EIA4B.js +93 -0
- package/dist/chunk-LPCNRGJ7.js +692 -0
- package/dist/chunk-LSLNL5T3.js +168 -0
- package/dist/chunk-LVRRWP4W.js +568 -0
- package/dist/chunk-LWCFBRHL.js +33 -0
- package/dist/chunk-LXFTMGZZ.js +166 -0
- package/dist/chunk-M33LVA4M.js +151 -0
- package/dist/chunk-M3FO5UMK.js +4944 -0
- package/dist/chunk-M5QNDBAY.js +66 -0
- package/dist/chunk-M6ND6KBE.js +1291 -0
- package/dist/chunk-M7H3WQTE.js +76 -0
- package/dist/chunk-MCBRVPA7.js +191 -0
- package/dist/chunk-MCSDWZET.js +91 -0
- package/dist/chunk-MDT5XDXL.js +289 -0
- package/dist/chunk-MJG3IQY4.js +91 -0
- package/dist/chunk-MOU3N2UL.js +44 -0
- package/dist/chunk-MP4KZOQC.js +3451 -0
- package/dist/chunk-MPAJ446F.js +66 -0
- package/dist/chunk-MVJZZ6AL.js +16 -0
- package/dist/chunk-MXS6S2PY.js +253 -0
- package/dist/chunk-N3SFDYM7.js +291 -0
- package/dist/chunk-NAOX3JUB.js +13 -0
- package/dist/chunk-NEKKIBCX.js +283 -0
- package/dist/chunk-NKDATMEM.js +133 -0
- package/dist/chunk-NR4FMC4K.js +395 -0
- package/dist/chunk-NSOHOJBR.js +221 -0
- package/dist/chunk-NTDB6QKF.js +29 -0
- package/dist/chunk-NWEYRBWK.js +25 -0
- package/dist/chunk-O2YSNYWD.js +23 -0
- package/dist/chunk-O3EW7LAY.js +54 -0
- package/dist/chunk-O7BNJIXU.js +366 -0
- package/dist/chunk-OARL2NXZ.js +116 -0
- package/dist/chunk-OHKZUEJW.js +351 -0
- package/dist/chunk-OKU45BXA.js +647 -0
- package/dist/chunk-OL4JBD3J.js +59 -0
- package/dist/chunk-OLUO5DDR.js +192 -0
- package/dist/chunk-OQ245JLA.js +196 -0
- package/dist/chunk-P72UZAQI.js +289 -0
- package/dist/chunk-PAL2ENB6.js +20 -0
- package/dist/chunk-PAXYQZH7.js +53 -0
- package/dist/chunk-PDPEM5VW.js +51 -0
- package/dist/chunk-PEWCDI2C.js +668 -0
- package/dist/chunk-PGZ54EDX.js +27 -0
- package/dist/chunk-PRWVLDZF.js +73 -0
- package/dist/chunk-PSQQ75VJ.js +52 -0
- package/dist/chunk-PUOVIFHW.js +118 -0
- package/dist/chunk-PWBF2X3V.js +107 -0
- package/dist/chunk-PWXII6HF.js +43 -0
- package/dist/chunk-Q35VLJRT.js +80 -0
- package/dist/chunk-QJP7EYGL.js +128 -0
- package/dist/chunk-QPQKX2MK.js +8836 -0
- package/dist/chunk-QQRLPY25.js +1053 -0
- package/dist/chunk-QUS4ER2W.js +407 -0
- package/dist/chunk-QYHI3YET.js +29 -0
- package/dist/chunk-QYRU2UQI.js +32 -0
- package/dist/chunk-R6XVVQGC.js +13 -0
- package/dist/chunk-R7QN6ONS.js +483 -0
- package/dist/chunk-RD23MPGC.js +221 -0
- package/dist/chunk-RDDWQBEM.js +185 -0
- package/dist/chunk-RDLLDYFO.js +104 -0
- package/dist/chunk-RE4TGORR.js +84 -0
- package/dist/chunk-RJLHVYQY.js +217 -0
- package/dist/chunk-RJLPTWHW.js +905 -0
- package/dist/chunk-RKUVLRZI.js +295 -0
- package/dist/chunk-ROGXPGYR.js +37 -0
- package/dist/chunk-RSZGEL5U.js +163 -0
- package/dist/chunk-RVUOIJXN.js +909 -0
- package/dist/chunk-RW7F4JRP.js +11 -0
- package/dist/chunk-S4UO3AO3.js +53 -0
- package/dist/chunk-S5JAAF6S.js +585 -0
- package/dist/chunk-SNU37TQI.js +56 -0
- package/dist/chunk-SQVUDO2O.js +31 -0
- package/dist/chunk-SYVHIYQT.js +66 -0
- package/dist/chunk-SZBQMTG2.js +85 -0
- package/dist/chunk-T5VCXA3J.js +25 -0
- package/dist/chunk-TK6NSP56.js +30 -0
- package/dist/chunk-TKWVMA6L.js +22 -0
- package/dist/chunk-TQDKJHWX.js +59 -0
- package/dist/chunk-TQDM7S5N.js +127 -0
- package/dist/chunk-TULAXS77.js +23 -0
- package/dist/chunk-TVT45XNH.js +4625 -0
- package/dist/chunk-TWCVV2JO.js +467 -0
- package/dist/chunk-U4DKAD6S.js +47 -0
- package/dist/chunk-UA5K2BTI.js +564 -0
- package/dist/chunk-UAMKCMOK.js +40 -0
- package/dist/chunk-UBMVADBF.js +51 -0
- package/dist/chunk-UFQ5A5PW.js +645 -0
- package/dist/chunk-UOD2SOLL.js +1301 -0
- package/dist/chunk-UTSB4LOW.js +156 -0
- package/dist/chunk-UVXNIUTN.js +412 -0
- package/dist/chunk-UXVPXYPE.js +90 -0
- package/dist/chunk-UYEEMIWR.js +374 -0
- package/dist/chunk-VBGM5ZBW.js +54 -0
- package/dist/chunk-VEWPCZPG.js +762 -0
- package/dist/chunk-VIRDLZ5E.js +108 -0
- package/dist/chunk-VMPO7WKN.js +52 -0
- package/dist/chunk-W2NVSDO5.js +5122 -0
- package/dist/chunk-W3YYPBEA.js +176 -0
- package/dist/chunk-W5AYOICN.js +280 -0
- package/dist/chunk-W5GZOIUF.js +27 -0
- package/dist/chunk-WHKNLZ3C.js +76 -0
- package/dist/chunk-WKBEPPQ5.js +1409 -0
- package/dist/chunk-WM3Z6QKQ.js +151 -0
- package/dist/chunk-WMI2TPMA.js +31 -0
- package/dist/chunk-WNX5VD7S.js +150 -0
- package/dist/chunk-WOVTYST3.js +60 -0
- package/dist/chunk-WPB5WEZJ.js +546 -0
- package/dist/chunk-WUO5EOAT.js +44 -0
- package/dist/chunk-WWLJMP6D.js +133 -0
- package/dist/chunk-WZMLH3XP.js +58 -0
- package/dist/chunk-WZYVLUVQ.js +79 -0
- package/dist/chunk-X2OCPVU5.js +135 -0
- package/dist/chunk-X3CL6DTR.js +95 -0
- package/dist/chunk-X6WF7JEV.js +354 -0
- package/dist/chunk-XAHGBPWT.js +44 -0
- package/dist/chunk-XCKQ5BM2.js +228 -0
- package/dist/chunk-XIBYREEY.js +106 -0
- package/dist/chunk-XKW5NDMR.js +378 -0
- package/dist/chunk-XRDALAO3.js +100 -0
- package/dist/chunk-XUP22EAS.js +41 -0
- package/dist/chunk-XWAC3MKW.js +243 -0
- package/dist/chunk-Y3R7JMXB.js +59 -0
- package/dist/chunk-Y54LTP4W.js +82 -0
- package/dist/chunk-Y7UECI4M.js +99 -0
- package/dist/chunk-YACGDFHC.js +47 -0
- package/dist/chunk-YHNIQPKY.js +71 -0
- package/dist/chunk-YMBR6HYP.js +111 -0
- package/dist/chunk-YMCH3L2S.js +26 -0
- package/dist/chunk-Z4ZU6T7V.js +99 -0
- package/dist/chunk-ZDPF67ZZ.js +173 -0
- package/dist/chunk-ZFC2Q67K.js +101 -0
- package/dist/chunk-ZGHNNSCT.js +114 -0
- package/dist/chunk-ZGX3ERAT.js +173 -0
- package/dist/chunk-ZI3YHEBY.js +37 -0
- package/dist/chunk-ZN7G6JLS.js +639 -0
- package/dist/chunk-ZNPFQMXT.js +47 -0
- package/dist/chunk-ZOU3LV4U.js +63 -0
- package/dist/chunk-ZT7B4H2V.js +117 -0
- package/dist/chunk-ZZCBDNZI.js +58 -0
- package/dist/cli.js +498 -112593
- package/dist/computer-tools.js +36642 -21258
- package/dist/harness-catalog.cjs +88 -43
- package/dist/index.js +1 -1
- package/dist/local-model-supervisor.cjs +555 -0
- package/package.json +3 -4
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
withFileLock
|
|
5
|
+
} from "./chunk-4L2ZZM4Q.js";
|
|
6
|
+
import {
|
|
7
|
+
atomicWriteFile
|
|
8
|
+
} from "./chunk-F6F47QAY.js";
|
|
9
|
+
import {
|
|
10
|
+
quiet,
|
|
11
|
+
swallowed
|
|
12
|
+
} from "./chunk-RDDWQBEM.js";
|
|
13
|
+
|
|
14
|
+
// src/agent/computer/credential-vault.ts
|
|
15
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
16
|
+
import fs from "node:fs/promises";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
var SESSION_MS = 10 * 6e4;
|
|
19
|
+
var sessions = /* @__PURE__ */ new Map();
|
|
20
|
+
var CredentialError = class extends Error {
|
|
21
|
+
};
|
|
22
|
+
function vaultDir(stateDir) {
|
|
23
|
+
return path.join(stateDir, "computer", "vault");
|
|
24
|
+
}
|
|
25
|
+
function keyFile(stateDir) {
|
|
26
|
+
return path.join(vaultDir(stateDir), "vault.key");
|
|
27
|
+
}
|
|
28
|
+
function siteOrigin(url) {
|
|
29
|
+
try {
|
|
30
|
+
const value = new URL(url);
|
|
31
|
+
if (value.protocol === "https:" && !value.username && !value.password) return value.origin;
|
|
32
|
+
} catch {
|
|
33
|
+
}
|
|
34
|
+
throw new CredentialError("Private credentials require an HTTPS website.");
|
|
35
|
+
}
|
|
36
|
+
var REGION_LABEL = /^[a-z]{2}(?:-[a-z]+)+-\d+\./;
|
|
37
|
+
var WWW_LABEL = /^www\./;
|
|
38
|
+
function siteKey(url) {
|
|
39
|
+
const { protocol, hostname, port } = new URL(siteOrigin(url));
|
|
40
|
+
return `${protocol}//${hostname.replace(REGION_LABEL, "").replace(WWW_LABEL, "")}${port ? `:${port}` : ""}`;
|
|
41
|
+
}
|
|
42
|
+
function sameSite(a, b) {
|
|
43
|
+
try {
|
|
44
|
+
return siteKey(a) === siteKey(b);
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function withVaultLock(stateDir, run) {
|
|
50
|
+
await fs.mkdir(vaultDir(stateDir), { recursive: true, mode: 448 });
|
|
51
|
+
return withFileLock(path.join(vaultDir(stateDir), "write.lock"), run);
|
|
52
|
+
}
|
|
53
|
+
async function writeSealed(file, data, options = {}) {
|
|
54
|
+
if (options.exclusive && await fs.lstat(file).then(() => true, () => false)) throw Object.assign(new Error(`${path.basename(file)} exists`), { code: "EEXIST" });
|
|
55
|
+
await atomicWriteFile(file, data, { mode: 384 });
|
|
56
|
+
}
|
|
57
|
+
var isRecordFile = (file) => file.endsWith(".json");
|
|
58
|
+
function seal(key, aad, plain) {
|
|
59
|
+
const iv = randomBytes(12);
|
|
60
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
61
|
+
cipher.setAAD(Buffer.from(aad));
|
|
62
|
+
return Buffer.concat([iv, cipher.update(plain, "utf8"), cipher.final(), cipher.getAuthTag()]).toString("base64url");
|
|
63
|
+
}
|
|
64
|
+
function open(key, aad, packet) {
|
|
65
|
+
try {
|
|
66
|
+
const bytes = Buffer.from(packet, "base64url");
|
|
67
|
+
if (bytes.length < 29) throw new Error();
|
|
68
|
+
const decipher = createDecipheriv("aes-256-gcm", key, bytes.subarray(0, 12));
|
|
69
|
+
decipher.setAAD(Buffer.from(aad));
|
|
70
|
+
decipher.setAuthTag(bytes.subarray(-16));
|
|
71
|
+
return Buffer.concat([decipher.update(bytes.subarray(12, -16)), decipher.final()]).toString("utf8");
|
|
72
|
+
} catch {
|
|
73
|
+
throw new CredentialError("The saved login could not be decrypted.");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function cache(stateDir, key) {
|
|
77
|
+
lockCredentialVault(stateDir);
|
|
78
|
+
sessions.set(stateDir, { key, expires: Date.now() + SESSION_MS });
|
|
79
|
+
}
|
|
80
|
+
function currentKey(stateDir) {
|
|
81
|
+
const session = sessions.get(stateDir);
|
|
82
|
+
if (!session) return void 0;
|
|
83
|
+
if (session.expires <= Date.now()) {
|
|
84
|
+
lockCredentialVault(stateDir);
|
|
85
|
+
return void 0;
|
|
86
|
+
}
|
|
87
|
+
return session.key;
|
|
88
|
+
}
|
|
89
|
+
function lockCredentialVault(stateDir) {
|
|
90
|
+
sessions.get(stateDir)?.key.fill(0);
|
|
91
|
+
sessions.delete(stateDir);
|
|
92
|
+
}
|
|
93
|
+
async function vaultKey(stateDir, create) {
|
|
94
|
+
const cached = currentKey(stateDir);
|
|
95
|
+
if (cached) return cached;
|
|
96
|
+
const dir = vaultDir(stateDir);
|
|
97
|
+
if (create) {
|
|
98
|
+
await fs.mkdir(dir, { recursive: true, mode: 448 });
|
|
99
|
+
const generated = randomBytes(32);
|
|
100
|
+
try {
|
|
101
|
+
await fs.writeFile(keyFile(stateDir), generated, { mode: 384, flag: "wx" });
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error.code !== "EEXIST") throw new CredentialError("The local login vault could not be opened.");
|
|
104
|
+
} finally {
|
|
105
|
+
generated.fill(0);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
let key;
|
|
109
|
+
try {
|
|
110
|
+
const info = await fs.lstat(keyFile(stateDir));
|
|
111
|
+
if (!info.isFile() || process.platform !== "win32" && (info.mode & 63) !== 0) throw new Error();
|
|
112
|
+
key = await fs.readFile(keyFile(stateDir));
|
|
113
|
+
} catch {
|
|
114
|
+
throw new CredentialError("The local login vault key is missing or not private.");
|
|
115
|
+
}
|
|
116
|
+
if (key.length !== 32) throw new CredentialError("The local login vault key is invalid.");
|
|
117
|
+
cache(stateDir, key);
|
|
118
|
+
return key;
|
|
119
|
+
}
|
|
120
|
+
function itemFields(item) {
|
|
121
|
+
return { ...item.fields };
|
|
122
|
+
}
|
|
123
|
+
var TWO_PART_TLD = /^(?:co|com|org|net|gov|ac|edu)\.[a-z]{2}$/;
|
|
124
|
+
function siteName(urlOrOrigin) {
|
|
125
|
+
let host = "";
|
|
126
|
+
try {
|
|
127
|
+
host = new URL(urlOrOrigin).hostname.replace(/^www\./, "");
|
|
128
|
+
} catch {
|
|
129
|
+
return urlOrOrigin;
|
|
130
|
+
}
|
|
131
|
+
const labels = host.split(".");
|
|
132
|
+
const tail = labels.slice(-2).join(".");
|
|
133
|
+
const own = labels.length >= 3 && TWO_PART_TLD.test(tail) ? labels.at(-3) : labels.length >= 2 ? labels.at(-2) : labels[0];
|
|
134
|
+
return own.charAt(0).toUpperCase() + own.slice(1);
|
|
135
|
+
}
|
|
136
|
+
function loginRecord(id, item) {
|
|
137
|
+
const fields = itemFields(item);
|
|
138
|
+
const method = item.method ?? (fields.password ? "password" : "unknown");
|
|
139
|
+
const identified = Boolean(fields.email || fields.username || fields.accountId);
|
|
140
|
+
return { id, name: item.name ?? siteName(item.origin), site: item.origin, method, account: loginLabel(fields), complete: identified && (method !== "password" || Boolean(fields.password)), rejected: item.rejected === true };
|
|
141
|
+
}
|
|
142
|
+
async function savedLoginFields(stateDir, url, id) {
|
|
143
|
+
return itemFields(await readCredential(stateDir, url, id));
|
|
144
|
+
}
|
|
145
|
+
var UNKNOWN_ACCOUNT = "(email not captured)";
|
|
146
|
+
async function saveLoginFields(stateDir, url, fields, id, extra = {}) {
|
|
147
|
+
const origin = siteOrigin(url);
|
|
148
|
+
if (!Object.keys(fields).length || Object.values(fields).some((value) => !value)) throw new CredentialError("Complete the visible login fields.");
|
|
149
|
+
return withVaultLock(stateDir, () => saveLoginFieldsHeld(stateDir, url, origin, fields, id, extra));
|
|
150
|
+
}
|
|
151
|
+
async function saveLoginFieldsHeld(stateDir, url, origin, fields, id, extra) {
|
|
152
|
+
const key = await vaultKey(stateDir, true);
|
|
153
|
+
const username = fields.username?.trim().toLowerCase();
|
|
154
|
+
id ??= username && username !== UNKNOWN_ACCOUNT ? (await readItems(stateDir)).find(({ item: item2 }) => sameSite(item2.origin, url) && item2.fields?.username?.trim().toLowerCase() === username)?.id : void 0;
|
|
155
|
+
const item = id ? await readCredential(stateDir, url, id) : { origin };
|
|
156
|
+
const method = extra.method ?? item.method ?? (fields.password || item.fields?.password ? "password" : void 0);
|
|
157
|
+
const itemId = id ?? randomUUID();
|
|
158
|
+
const dir = path.join(vaultDir(stateDir), "items");
|
|
159
|
+
await fs.mkdir(dir, { recursive: true, mode: 448 });
|
|
160
|
+
const { rejected: _worked, ...kept } = item;
|
|
161
|
+
await writeSealed(path.join(dir, `${itemId}.json`), seal(key, `item:${itemId}`, JSON.stringify({ ...kept, name: extra.name ?? kept.name ?? siteName(origin), ...method ? { method } : {}, fields: { ...item.fields, ...fields } })), { exclusive: !id });
|
|
162
|
+
return itemId;
|
|
163
|
+
}
|
|
164
|
+
async function setLoginRejected(stateDir, url, id, rejected) {
|
|
165
|
+
await withVaultLock(stateDir, async () => {
|
|
166
|
+
const key = await vaultKey(stateDir, false);
|
|
167
|
+
const { rejected: _was, ...item } = await readCredential(stateDir, url, id);
|
|
168
|
+
await writeSealed(path.join(vaultDir(stateDir), "items", `${id}.json`), seal(key, `item:${id}`, JSON.stringify(rejected ? { ...item, rejected: true } : item)));
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
async function readCredential(stateDir, url, id) {
|
|
172
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)) throw new CredentialError("Invalid saved login reference.");
|
|
173
|
+
const key = await vaultKey(stateDir, false);
|
|
174
|
+
let item;
|
|
175
|
+
try {
|
|
176
|
+
item = JSON.parse(open(key, `item:${id}`, await fs.readFile(path.join(vaultDir(stateDir), "items", `${id}.json`), "utf8")));
|
|
177
|
+
} catch {
|
|
178
|
+
throw new CredentialError("That saved login could not be opened.");
|
|
179
|
+
}
|
|
180
|
+
if (!sameSite(item.origin, url)) throw new CredentialError("That saved login does not match this website.");
|
|
181
|
+
return item;
|
|
182
|
+
}
|
|
183
|
+
function loginLabel(fields) {
|
|
184
|
+
const order = ["accountId", "email", "username"];
|
|
185
|
+
const keys = [...order.filter((key) => fields[key]), ...Object.keys(fields).filter((key) => !order.includes(key) && !/password/i.test(key) && fields[key])];
|
|
186
|
+
const values = [...new Set(keys.map((key) => fields[key]))];
|
|
187
|
+
return values.join(" \xB7 ") || "Saved login";
|
|
188
|
+
}
|
|
189
|
+
var unreadableItems = /* @__PURE__ */ new Map();
|
|
190
|
+
var unreadableLoginIds = (stateDir) => [...unreadableItems.get(stateDir) ?? []].sort();
|
|
191
|
+
async function readItems(stateDir) {
|
|
192
|
+
const dir = path.join(vaultDir(stateDir), "items");
|
|
193
|
+
let files;
|
|
194
|
+
try {
|
|
195
|
+
files = await fs.readdir(dir);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (error.code === "ENOENT") return [];
|
|
198
|
+
throw new CredentialError("Saved logins could not be read.");
|
|
199
|
+
}
|
|
200
|
+
if (files.length === 0) return [];
|
|
201
|
+
const key = await vaultKey(stateDir, false);
|
|
202
|
+
const items = [];
|
|
203
|
+
const unreadable = /* @__PURE__ */ new Set();
|
|
204
|
+
for (const file of files) {
|
|
205
|
+
const id = file.replace(/\.json$/, "");
|
|
206
|
+
if (!/^[0-9a-f-]{36}$/i.test(id) || file !== `${id}.json`) continue;
|
|
207
|
+
try {
|
|
208
|
+
items.push({ id, item: JSON.parse(open(key, `item:${id}`, await fs.readFile(path.join(dir, file), "utf8"))) });
|
|
209
|
+
} catch (error) {
|
|
210
|
+
unreadable.add(id);
|
|
211
|
+
swallowed("agent/computer/credential-vault", error);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (unreadable.size) unreadableItems.set(stateDir, unreadable);
|
|
215
|
+
else unreadableItems.delete(stateDir);
|
|
216
|
+
return items;
|
|
217
|
+
}
|
|
218
|
+
var byLabel = (a, b) => a.label.localeCompare(b.label) || a.id.localeCompare(b.id);
|
|
219
|
+
async function savedLoginsForSite(stateDir, url) {
|
|
220
|
+
siteOrigin(url);
|
|
221
|
+
return (await readItems(stateDir)).filter(({ item }) => sameSite(item.origin, url)).map(({ id, item }) => ({ id, label: loginLabel(itemFields(item)), rejected: item.rejected === true, usable: Boolean(itemFields(item).password) })).sort(byLabel);
|
|
222
|
+
}
|
|
223
|
+
async function allSavedLogins(stateDir) {
|
|
224
|
+
return (await readItems(stateDir)).map(({ id, item }) => {
|
|
225
|
+
const record = loginRecord(id, item);
|
|
226
|
+
return { id, site: new URL(item.origin).hostname, label: record.account, name: record.name, baseUrl: record.site, method: record.method, complete: record.complete };
|
|
227
|
+
}).sort((a, b) => a.site.localeCompare(b.site) || byLabel(a, b));
|
|
228
|
+
}
|
|
229
|
+
async function saveSignInMethod(stateDir, url, method, account, name) {
|
|
230
|
+
if (!/^[a-z][a-z0-9-]{1,30}$/.test(method) || method === "password") throw new CredentialError("Name the provider the sign-in went through, such as google or github.");
|
|
231
|
+
if (!account.trim()) throw new CredentialError("Say which account was used.");
|
|
232
|
+
return saveLoginFields(stateDir, url, { email: account.trim() }, void 0, { method, ...name ? { name } : {} });
|
|
233
|
+
}
|
|
234
|
+
function siteUrl(site) {
|
|
235
|
+
const value = site.trim();
|
|
236
|
+
if (!value) throw new CredentialError("Name the site whose saved login to remove.");
|
|
237
|
+
return /^https?:\/\//i.test(value) ? value : `https://${value}`;
|
|
238
|
+
}
|
|
239
|
+
async function forgetSavedLogins(stateDir, site, label) {
|
|
240
|
+
const url = siteUrl(site);
|
|
241
|
+
siteOrigin(url);
|
|
242
|
+
const wanted = label?.trim().toLowerCase();
|
|
243
|
+
let removed = 0;
|
|
244
|
+
for (const login of await savedLoginsForSite(stateDir, url)) {
|
|
245
|
+
if (wanted && login.label.toLowerCase() !== wanted && !login.label.toLowerCase().split(" \xB7 ").includes(wanted)) continue;
|
|
246
|
+
if (await removeSavedLogin(stateDir, login.id)) removed++;
|
|
247
|
+
}
|
|
248
|
+
return removed;
|
|
249
|
+
}
|
|
250
|
+
async function removeSavedLogin(stateDir, id) {
|
|
251
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)) throw new CredentialError("Invalid saved login reference.");
|
|
252
|
+
try {
|
|
253
|
+
await fs.unlink(path.join(vaultDir(stateDir), "items", `${id}.json`));
|
|
254
|
+
return true;
|
|
255
|
+
} catch (error) {
|
|
256
|
+
if (error.code === "ENOENT") return false;
|
|
257
|
+
throw new CredentialError("The saved login could not be removed.");
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
var SECRET_NAME = /^[a-z0-9][a-z0-9._/-]{0,63}$/i;
|
|
261
|
+
function secretFile(stateDir, name) {
|
|
262
|
+
if (!SECRET_NAME.test(name)) throw new CredentialError("A secret name uses letters, digits, dot, dash, underscore or slash, up to 64 characters.");
|
|
263
|
+
const hash = createHash("sha256").update(name.toLowerCase()).digest("hex").slice(0, 32);
|
|
264
|
+
return { file: path.join(vaultDir(stateDir), "secrets", `${hash}.json`), aad: `secret:${hash}` };
|
|
265
|
+
}
|
|
266
|
+
var KIND_ALIASES = {
|
|
267
|
+
"api-key": "key",
|
|
268
|
+
apikey: "key",
|
|
269
|
+
"api_key": "key",
|
|
270
|
+
"secret-key": "key",
|
|
271
|
+
"secret": "key",
|
|
272
|
+
"api-token": "token",
|
|
273
|
+
"access-token": "token",
|
|
274
|
+
"auth-token": "token",
|
|
275
|
+
clientid: "client-id",
|
|
276
|
+
"client_id": "client-id",
|
|
277
|
+
clientsecret: "client-secret",
|
|
278
|
+
"client_secret": "client-secret"
|
|
279
|
+
};
|
|
280
|
+
function canonicalSecretName(name) {
|
|
281
|
+
const lower = name.trim().toLowerCase();
|
|
282
|
+
const slash = lower.indexOf("/");
|
|
283
|
+
if (slash < 0) return lower;
|
|
284
|
+
const vendor = lower.slice(0, slash).replace(/^www\./, "");
|
|
285
|
+
const kind = lower.slice(slash + 1);
|
|
286
|
+
return `${vendor}/${KIND_ALIASES[kind] ?? kind}`;
|
|
287
|
+
}
|
|
288
|
+
async function savedNameFor(stateDir, name) {
|
|
289
|
+
const names = await namedSecretNames(stateDir).catch(() => []);
|
|
290
|
+
const exact = names.find((saved) => saved.toLowerCase() === name.trim().toLowerCase());
|
|
291
|
+
if (exact) return exact;
|
|
292
|
+
const same = names.filter((saved) => canonicalSecretName(saved) === canonicalSecretName(name));
|
|
293
|
+
return same.length === 1 ? same[0] : void 0;
|
|
294
|
+
}
|
|
295
|
+
async function nearNames(stateDir, name) {
|
|
296
|
+
const vendor = canonicalSecretName(name).split("/")[0];
|
|
297
|
+
return (await namedSecretNames(stateDir).catch(() => [])).filter((saved) => canonicalSecretName(saved).split("/")[0] === vendor).slice(0, 8);
|
|
298
|
+
}
|
|
299
|
+
function webOrigin(value) {
|
|
300
|
+
try {
|
|
301
|
+
const url = new URL(value ?? "");
|
|
302
|
+
return (url.protocol === "https:" || url.protocol === "http:") && !url.username && !url.password ? url.origin : void 0;
|
|
303
|
+
} catch {
|
|
304
|
+
return void 0;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
var INTERNAL_SECRET = /^(?:profile|email)\//i;
|
|
308
|
+
async function baseUrlFor(stateDir, name, given, before) {
|
|
309
|
+
for (const candidate of [given, before]) {
|
|
310
|
+
const origin = webOrigin(candidate);
|
|
311
|
+
if (origin) return origin;
|
|
312
|
+
}
|
|
313
|
+
const first = name.split("/")[0];
|
|
314
|
+
if (/^[a-z0-9-]+(\.[a-z0-9-]+)+$/i.test(first)) return `https://${first.toLowerCase()}`;
|
|
315
|
+
const vendor = first.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
316
|
+
if (vendor.length < 3) return void 0;
|
|
317
|
+
const sites = new Set((await readItems(stateDir).catch(() => [])).map(({ item }) => item.origin).filter((origin) => new URL(origin).hostname.toLowerCase().replace(/[^a-z0-9]/g, "").includes(vendor)));
|
|
318
|
+
return sites.size === 1 ? [...sites][0] : void 0;
|
|
319
|
+
}
|
|
320
|
+
async function saveNamedSecret(stateDir, name, value, meta = {}, options = {}) {
|
|
321
|
+
if (!value) throw new CredentialError("The secret is empty.");
|
|
322
|
+
await withVaultLock(stateDir, () => saveNamedSecretHeld(stateDir, name, value, meta, options));
|
|
323
|
+
}
|
|
324
|
+
async function saveNamedSecretHeld(stateDir, name, value, meta, options) {
|
|
325
|
+
name = await savedNameFor(stateDir, name) ?? name;
|
|
326
|
+
const { file, aad } = secretFile(stateDir, name);
|
|
327
|
+
const key = await vaultKey(stateDir, true);
|
|
328
|
+
let before = {};
|
|
329
|
+
try {
|
|
330
|
+
before = JSON.parse(open(key, aad, await fs.readFile(file, "utf8")));
|
|
331
|
+
} catch {
|
|
332
|
+
}
|
|
333
|
+
if (before.value !== void 0 && !before.meta) {
|
|
334
|
+
const made = await fs.stat(file).then((info) => info.mtime.toISOString(), () => void 0);
|
|
335
|
+
if (made) before.meta = { createdAt: made };
|
|
336
|
+
}
|
|
337
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
338
|
+
const kept = (field) => {
|
|
339
|
+
const v = meta[field]?.trim() || before.meta?.[field];
|
|
340
|
+
return v ? { [field]: v } : {};
|
|
341
|
+
};
|
|
342
|
+
const url = INTERNAL_SECRET.test(name) ? void 0 : await baseUrlFor(stateDir, name, meta.url, before.meta?.url);
|
|
343
|
+
if (!url && !INTERNAL_SECRET.test(name) && !options.legacy) throw new CredentialError(`A saved key must say which site it is for: give its base URL (https://example.com) when saving "${name}".`);
|
|
344
|
+
const saved = { ...kept("vendor"), ...kept("owner"), ...kept("env"), ...kept("page"), ...url ? { url } : {}, createdAt: before.meta?.createdAt ?? now, ...before.value !== void 0 && before.value !== value ? { rotatedAt: now } : before.meta?.rotatedAt ? { rotatedAt: before.meta.rotatedAt } : {} };
|
|
345
|
+
await fs.mkdir(path.dirname(file), { recursive: true, mode: 448 });
|
|
346
|
+
const pin = options.pin ?? (before.pin && before.value === value ? before.pin : void 0);
|
|
347
|
+
await writeSealed(file, seal(key, aad, JSON.stringify({ name, value, meta: saved, ...pin ? { pin } : {} })));
|
|
348
|
+
}
|
|
349
|
+
var pinMac = (key, name, origin, value) => createHmac("sha256", key).update(`token-pin:v1\0${name.toLowerCase()}\0${origin}\0${createHash("sha256").update(value).digest("hex")}`).digest("base64url");
|
|
350
|
+
async function saveGeneratedToken(stateDir, name, value, origin, meta) {
|
|
351
|
+
const url = webOrigin(origin);
|
|
352
|
+
if (!value || !url || !url.startsWith("https://")) throw new CredentialError("A generated token needs an https origin.");
|
|
353
|
+
await withVaultLock(stateDir, async () => {
|
|
354
|
+
const key = await vaultKey(stateDir, true);
|
|
355
|
+
const saved = await savedNameFor(stateDir, name) ?? name;
|
|
356
|
+
await saveNamedSecretHeld(stateDir, saved, value, { url, vendor: meta.vendor }, { pin: { origin: url, mac: pinMac(key, saved, url, value) } });
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
async function pinEarlierToken(stateDir, name, origin, generatedVendor) {
|
|
360
|
+
const url = webOrigin(origin);
|
|
361
|
+
if (!url || !url.startsWith("https://")) throw new CredentialError("Pin a token to an https origin.");
|
|
362
|
+
return withVaultLock(stateDir, async () => {
|
|
363
|
+
const key = await vaultKey(stateDir, false);
|
|
364
|
+
const { file, aad } = secretFile(stateDir, name);
|
|
365
|
+
let record;
|
|
366
|
+
try {
|
|
367
|
+
record = JSON.parse(open(key, aad, await fs.readFile(file, "utf8")));
|
|
368
|
+
} catch {
|
|
369
|
+
throw new CredentialError(`No saved token is called "${name}".`);
|
|
370
|
+
}
|
|
371
|
+
const value = record.value;
|
|
372
|
+
if (typeof value !== "string" || !value) throw new CredentialError(`"${name}" holds no token.`);
|
|
373
|
+
const current = await pinnedToken(stateDir, name);
|
|
374
|
+
if (current) {
|
|
375
|
+
if (current.origin !== url) throw new CredentialError(`"${name}" is already pinned to ${current.origin}.`);
|
|
376
|
+
return current.origin;
|
|
377
|
+
}
|
|
378
|
+
if (record.meta?.vendor !== generatedVendor) throw new CredentialError(`"${name}" was not made by \`clikcode computer secrets generate\`, so it is not pinned.`);
|
|
379
|
+
if (webOrigin(record.meta?.url ?? "") !== url) throw new CredentialError(`"${name}" is recorded for ${record.meta?.url ?? "no site"}, not ${url}; it is not pinned.`);
|
|
380
|
+
const saved = record.name ?? name;
|
|
381
|
+
await writeSealed(file, seal(key, aad, JSON.stringify({ ...record, pin: { origin: url, mac: pinMac(key, saved, url, value) } })));
|
|
382
|
+
return url;
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
async function pinnedToken(stateDir, name) {
|
|
386
|
+
const { file, aad } = secretFile(stateDir, name);
|
|
387
|
+
const key = await vaultKey(stateDir, false);
|
|
388
|
+
let record;
|
|
389
|
+
try {
|
|
390
|
+
record = JSON.parse(open(key, aad, await fs.readFile(file, "utf8")));
|
|
391
|
+
} catch {
|
|
392
|
+
return void 0;
|
|
393
|
+
}
|
|
394
|
+
const { pin, value } = record;
|
|
395
|
+
if (!pin || typeof pin.origin !== "string" || typeof pin.mac !== "string" || typeof value !== "string" || !value) return void 0;
|
|
396
|
+
const want = Buffer.from(pinMac(key, record.name ?? name, pin.origin, value));
|
|
397
|
+
const have = Buffer.from(pin.mac);
|
|
398
|
+
return want.length === have.length && timingSafeEqual(want, have) ? { origin: pin.origin, value } : void 0;
|
|
399
|
+
}
|
|
400
|
+
async function namedSecretInfo(stateDir) {
|
|
401
|
+
const dir = path.join(vaultDir(stateDir), "secrets");
|
|
402
|
+
let files;
|
|
403
|
+
try {
|
|
404
|
+
files = await fs.readdir(dir);
|
|
405
|
+
} catch {
|
|
406
|
+
return [];
|
|
407
|
+
}
|
|
408
|
+
const key = await vaultKey(stateDir, false);
|
|
409
|
+
const out = [];
|
|
410
|
+
for (const file of files.filter(isRecordFile)) {
|
|
411
|
+
try {
|
|
412
|
+
const { name, meta } = JSON.parse(open(key, `secret:${file.replace(/\.json$/, "")}`, await fs.readFile(path.join(dir, file), "utf8")));
|
|
413
|
+
out.push({ name, ...meta ? { meta } : {} });
|
|
414
|
+
} catch (error) {
|
|
415
|
+
swallowed("agent/computer/credential-vault", error);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return out.sort((a, b) => a.name.localeCompare(b.name));
|
|
419
|
+
}
|
|
420
|
+
async function readNamedSecret(stateDir, name) {
|
|
421
|
+
const read = async (wanted) => {
|
|
422
|
+
const { file, aad } = secretFile(stateDir, wanted);
|
|
423
|
+
const key = await vaultKey(stateDir, false);
|
|
424
|
+
return JSON.parse(open(key, aad, await fs.readFile(file, "utf8"))).value;
|
|
425
|
+
};
|
|
426
|
+
try {
|
|
427
|
+
return await read(name);
|
|
428
|
+
} catch {
|
|
429
|
+
}
|
|
430
|
+
const saved = await savedNameFor(stateDir, name);
|
|
431
|
+
if (saved) {
|
|
432
|
+
try {
|
|
433
|
+
return await read(saved);
|
|
434
|
+
} catch {
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const near = await nearNames(stateDir, name);
|
|
438
|
+
throw new CredentialError(`No secret named "${name}" is saved.${near.length ? ` Saved for that vendor: ${near.join(", ")}.` : ""}`);
|
|
439
|
+
}
|
|
440
|
+
async function namedSecretNames(stateDir) {
|
|
441
|
+
const dir = path.join(vaultDir(stateDir), "secrets");
|
|
442
|
+
let files;
|
|
443
|
+
try {
|
|
444
|
+
files = await fs.readdir(dir);
|
|
445
|
+
} catch {
|
|
446
|
+
return [];
|
|
447
|
+
}
|
|
448
|
+
const key = await vaultKey(stateDir, false);
|
|
449
|
+
const names = [];
|
|
450
|
+
for (const file of files.filter(isRecordFile)) {
|
|
451
|
+
try {
|
|
452
|
+
names.push(JSON.parse(open(key, `secret:${file.replace(/\.json$/, "")}`, await fs.readFile(path.join(dir, file), "utf8"))).name);
|
|
453
|
+
} catch (error) {
|
|
454
|
+
swallowed("agent/computer/credential-vault", error);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return names.sort();
|
|
458
|
+
}
|
|
459
|
+
async function deleteNamedSecret(stateDir, name) {
|
|
460
|
+
secretFile(stateDir, name);
|
|
461
|
+
return withVaultLock(stateDir, async () => {
|
|
462
|
+
const saved = await savedNameFor(stateDir, name);
|
|
463
|
+
if (!saved) return false;
|
|
464
|
+
try {
|
|
465
|
+
await fs.unlink(secretFile(stateDir, saved).file);
|
|
466
|
+
return true;
|
|
467
|
+
} catch (error) {
|
|
468
|
+
if (error.code === "ENOENT") return false;
|
|
469
|
+
throw new CredentialError("The secret could not be removed.");
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
var RECORD_FOLDER = /^[a-z][a-z-]{1,30}$/;
|
|
474
|
+
async function writeVaultRecord(stateDir, folder, id, value) {
|
|
475
|
+
if (!RECORD_FOLDER.test(folder) || !/^[0-9a-f-]{36}$/i.test(id)) throw new CredentialError("Invalid vault record.");
|
|
476
|
+
const key = await vaultKey(stateDir, true);
|
|
477
|
+
const dir = path.join(vaultDir(stateDir), folder);
|
|
478
|
+
await fs.mkdir(dir, { recursive: true, mode: 448 });
|
|
479
|
+
await withVaultLock(stateDir, () => writeSealed(path.join(dir, `${id}.json`), seal(key, `${folder}:${id}`, JSON.stringify(value))));
|
|
480
|
+
}
|
|
481
|
+
async function readVaultRecords(stateDir, folder) {
|
|
482
|
+
if (!RECORD_FOLDER.test(folder)) throw new CredentialError("Invalid vault record.");
|
|
483
|
+
const dir = path.join(vaultDir(stateDir), folder);
|
|
484
|
+
let files;
|
|
485
|
+
try {
|
|
486
|
+
files = await fs.readdir(dir);
|
|
487
|
+
} catch {
|
|
488
|
+
return [];
|
|
489
|
+
}
|
|
490
|
+
if (!files.length) return [];
|
|
491
|
+
const key = await vaultKey(stateDir, false);
|
|
492
|
+
const out = [];
|
|
493
|
+
for (const file of files) {
|
|
494
|
+
const id = file.replace(/\.json$/, "");
|
|
495
|
+
if (!/^[0-9a-f-]{36}$/i.test(id) || file !== `${id}.json`) continue;
|
|
496
|
+
try {
|
|
497
|
+
out.push({ id, value: JSON.parse(open(key, `${folder}:${id}`, await fs.readFile(path.join(dir, file), "utf8"))) });
|
|
498
|
+
} catch (error) {
|
|
499
|
+
swallowed("agent/computer/credential-vault", error);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return out;
|
|
503
|
+
}
|
|
504
|
+
async function removeVaultRecord(stateDir, folder, id) {
|
|
505
|
+
if (!RECORD_FOLDER.test(folder) || !/^[0-9a-f-]{36}$/i.test(id)) throw new CredentialError("Invalid vault record.");
|
|
506
|
+
try {
|
|
507
|
+
await fs.unlink(path.join(vaultDir(stateDir), folder, `${id}.json`));
|
|
508
|
+
return true;
|
|
509
|
+
} catch (error) {
|
|
510
|
+
if (error.code === "ENOENT") return false;
|
|
511
|
+
throw new CredentialError("The record could not be removed.");
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
async function backfillSecretMeta(stateDir, owner) {
|
|
515
|
+
return withVaultLock(stateDir, () => backfillSecretMetaHeld(stateDir, owner));
|
|
516
|
+
}
|
|
517
|
+
async function backfillSecretMetaHeld(stateDir, owner) {
|
|
518
|
+
const done = [];
|
|
519
|
+
for (const { name, meta } of await namedSecretInfo(stateDir)) {
|
|
520
|
+
const vendor = name.includes("/") ? name.split("/")[0] : void 0;
|
|
521
|
+
const url = INTERNAL_SECRET.test(name) || meta?.url ? void 0 : await baseUrlFor(stateDir, name, void 0, void 0);
|
|
522
|
+
if ((meta?.vendor || !vendor) && (meta?.owner || !owner) && !url) continue;
|
|
523
|
+
await saveNamedSecretHeld(stateDir, name, await readNamedSecret(stateDir, name), { ...vendor && !meta?.vendor ? { vendor } : {}, ...owner && !meta?.owner ? { owner } : {}, ...url ? { url } : {} }, { legacy: true });
|
|
524
|
+
done.push(name);
|
|
525
|
+
}
|
|
526
|
+
return done;
|
|
527
|
+
}
|
|
528
|
+
async function auditVault(stateDir) {
|
|
529
|
+
const info = await namedSecretInfo(stateDir);
|
|
530
|
+
const groups = /* @__PURE__ */ new Map();
|
|
531
|
+
for (const { name } of info) groups.set(canonicalSecretName(name), [...groups.get(canonicalSecretName(name)) ?? [], name]);
|
|
532
|
+
const odd = /\d{4}-\d{2}-\d{2}|-m\d+$|\btry-|-\d$/;
|
|
533
|
+
const items = await readItems(stateDir);
|
|
534
|
+
return {
|
|
535
|
+
unreadableLogins: unreadableLoginIds(stateDir),
|
|
536
|
+
twins: [...groups.values()].filter((names) => names.length > 1),
|
|
537
|
+
oddNames: info.map(({ name }) => name).filter((name) => !name.includes("/") || odd.test(name)),
|
|
538
|
+
noOwner: info.filter(({ meta }) => !meta?.owner).map(({ name }) => name),
|
|
539
|
+
noUrl: info.filter(({ name, meta }) => !meta?.url && !INTERNAL_SECRET.test(name)).map(({ name }) => name),
|
|
540
|
+
incompleteLogins: items.map(({ id, item }) => loginRecord(id, item)).filter((record) => !record.complete).map((record) => ({ site: new URL(record.site).hostname, label: record.account, method: record.method })),
|
|
541
|
+
rejectedLogins: items.filter(({ item }) => item.rejected).map(({ item }) => ({ site: new URL(item.origin).hostname, label: loginLabel(itemFields(item)) }))
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
async function mergeVault(stateDir, snapshot) {
|
|
545
|
+
if (snapshot?.v !== 1 || !Array.isArray(snapshot.items) || !Array.isArray(snapshot.secrets) || !Array.isArray(snapshot.records)) throw new CredentialError("That is not a ClikCode vault snapshot.");
|
|
546
|
+
return withVaultLock(stateDir, () => mergeVaultHeld(stateDir, snapshot));
|
|
547
|
+
}
|
|
548
|
+
async function mergeVaultHeld(stateDir, snapshot) {
|
|
549
|
+
const mine = new Map((await namedSecretInfo(stateDir)).map((entry) => [canonicalSecretName(entry.name), entry]));
|
|
550
|
+
const stamp = (meta) => meta?.rotatedAt ?? meta?.createdAt ?? "";
|
|
551
|
+
let updated = 0, same = 0;
|
|
552
|
+
const forLogins = { v: 1, items: snapshot.items, secrets: [], records: snapshot.records };
|
|
553
|
+
const logins = await restoreVaultHeld(stateDir, forLogins, {});
|
|
554
|
+
let added = logins.added;
|
|
555
|
+
same += logins.skipped;
|
|
556
|
+
for (const secret of snapshot.secrets) {
|
|
557
|
+
const have = mine.get(canonicalSecretName(secret.name));
|
|
558
|
+
if (!have) {
|
|
559
|
+
await saveNamedSecretHeld(stateDir, secret.name, secret.value, secret.meta ?? {}, { legacy: true });
|
|
560
|
+
await stampMeta(stateDir, secret.name, secret.meta);
|
|
561
|
+
added++;
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
if (stamp(secret.meta) > stamp(have.meta) && await readNamedSecret(stateDir, have.name) !== secret.value) {
|
|
565
|
+
await saveNamedSecretHeld(stateDir, have.name, secret.value, secret.meta ?? {}, { legacy: true });
|
|
566
|
+
await stampMeta(stateDir, have.name, secret.meta);
|
|
567
|
+
updated++;
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
const fill = ["url", "owner", "vendor"].filter((field) => secret.meta?.[field] && !have.meta?.[field]);
|
|
571
|
+
if (fill.length) {
|
|
572
|
+
await saveNamedSecretHeld(stateDir, have.name, await readNamedSecret(stateDir, have.name), Object.fromEntries(fill.map((field) => [field, secret.meta[field]])), { legacy: true });
|
|
573
|
+
updated++;
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
same++;
|
|
577
|
+
}
|
|
578
|
+
return { added, updated, same };
|
|
579
|
+
}
|
|
580
|
+
async function stampMeta(stateDir, name, meta) {
|
|
581
|
+
if (!meta?.createdAt) return;
|
|
582
|
+
const { file, aad } = secretFile(stateDir, name);
|
|
583
|
+
const key = await vaultKey(stateDir, false);
|
|
584
|
+
const record = JSON.parse(open(key, aad, await fs.readFile(file, "utf8")));
|
|
585
|
+
record.meta = { ...record.meta, ...meta };
|
|
586
|
+
await writeSealed(file, seal(key, aad, JSON.stringify(record)));
|
|
587
|
+
}
|
|
588
|
+
async function snapshotVault(stateDir) {
|
|
589
|
+
const items = await readItems(stateDir);
|
|
590
|
+
const key = await vaultKey(stateDir, false).catch(quiet("agent/computer/credential-vault"));
|
|
591
|
+
const secrets = [];
|
|
592
|
+
const dir = path.join(vaultDir(stateDir), "secrets");
|
|
593
|
+
for (const file of key ? (await fs.readdir(dir).catch(() => [])).filter(isRecordFile) : []) {
|
|
594
|
+
try {
|
|
595
|
+
secrets.push(JSON.parse(open(key, `secret:${file.replace(/\.json$/, "")}`, await fs.readFile(path.join(dir, file), "utf8"))));
|
|
596
|
+
} catch (error) {
|
|
597
|
+
swallowed("agent/computer/credential-vault", error);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
const records = [];
|
|
601
|
+
for (const entry of await fs.readdir(vaultDir(stateDir), { withFileTypes: true }).catch(() => [])) {
|
|
602
|
+
if (!entry.isDirectory() || entry.name === "items" || entry.name === "secrets" || !RECORD_FOLDER.test(entry.name)) continue;
|
|
603
|
+
for (const { id, value } of await readVaultRecords(stateDir, entry.name)) records.push({ folder: entry.name, id, value });
|
|
604
|
+
}
|
|
605
|
+
return { v: 1, items, secrets, records };
|
|
606
|
+
}
|
|
607
|
+
async function restoreVault(stateDir, snapshot, options = {}) {
|
|
608
|
+
if (snapshot?.v !== 1 || !Array.isArray(snapshot.items) || !Array.isArray(snapshot.secrets) || !Array.isArray(snapshot.records)) throw new CredentialError("That is not a ClikCode vault backup.");
|
|
609
|
+
return withVaultLock(stateDir, () => restoreVaultHeld(stateDir, snapshot, options));
|
|
610
|
+
}
|
|
611
|
+
async function restoreVaultHeld(stateDir, snapshot, options) {
|
|
612
|
+
const key = await vaultKey(stateDir, true);
|
|
613
|
+
let added = 0;
|
|
614
|
+
let skipped = 0;
|
|
615
|
+
const put = async (file, aad, value) => {
|
|
616
|
+
await fs.mkdir(path.dirname(file), { recursive: true, mode: 448 });
|
|
617
|
+
try {
|
|
618
|
+
await writeSealed(file, seal(key, aad, JSON.stringify(value)), { exclusive: !options.overwrite });
|
|
619
|
+
added++;
|
|
620
|
+
} catch (error) {
|
|
621
|
+
if (error.code === "EEXIST") skipped++;
|
|
622
|
+
else throw error;
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
for (const { id, item } of snapshot.items) {
|
|
626
|
+
if (!/^[0-9a-f-]{36}$/i.test(id)) continue;
|
|
627
|
+
await put(path.join(vaultDir(stateDir), "items", `${id}.json`), `item:${id}`, item);
|
|
628
|
+
}
|
|
629
|
+
for (const secret of snapshot.secrets) {
|
|
630
|
+
const { file, aad } = secretFile(stateDir, secret.name);
|
|
631
|
+
await put(file, aad, secret);
|
|
632
|
+
}
|
|
633
|
+
for (const { folder, id, value } of snapshot.records) {
|
|
634
|
+
if (!RECORD_FOLDER.test(folder) || folder === "items" || folder === "secrets" || !/^[0-9a-f-]{36}$/i.test(id)) continue;
|
|
635
|
+
await put(path.join(vaultDir(stateDir), folder, `${id}.json`), `${folder}:${id}`, value);
|
|
636
|
+
}
|
|
637
|
+
return { added, skipped };
|
|
638
|
+
}
|
|
639
|
+
async function setSecretUrl(stateDir, name, url) {
|
|
640
|
+
const origin = webOrigin(url);
|
|
641
|
+
if (!origin) throw new CredentialError("Give the site's base URL, such as https://example.com.");
|
|
642
|
+
await withVaultLock(stateDir, async () => {
|
|
643
|
+
const saved = await savedNameFor(stateDir, name);
|
|
644
|
+
if (!saved) throw new CredentialError(`No secret named "${name}" is saved.`);
|
|
645
|
+
await saveNamedSecretHeld(stateDir, saved, await readNamedSecret(stateDir, saved), { url: origin }, { legacy: true });
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
export {
|
|
650
|
+
CredentialError,
|
|
651
|
+
siteKey,
|
|
652
|
+
sameSite,
|
|
653
|
+
lockCredentialVault,
|
|
654
|
+
siteName,
|
|
655
|
+
savedLoginFields,
|
|
656
|
+
UNKNOWN_ACCOUNT,
|
|
657
|
+
saveLoginFields,
|
|
658
|
+
setLoginRejected,
|
|
659
|
+
loginLabel,
|
|
660
|
+
unreadableLoginIds,
|
|
661
|
+
savedLoginsForSite,
|
|
662
|
+
allSavedLogins,
|
|
663
|
+
saveSignInMethod,
|
|
664
|
+
forgetSavedLogins,
|
|
665
|
+
removeSavedLogin,
|
|
666
|
+
canonicalSecretName,
|
|
667
|
+
saveNamedSecret,
|
|
668
|
+
saveGeneratedToken,
|
|
669
|
+
pinEarlierToken,
|
|
670
|
+
pinnedToken,
|
|
671
|
+
namedSecretInfo,
|
|
672
|
+
readNamedSecret,
|
|
673
|
+
namedSecretNames,
|
|
674
|
+
deleteNamedSecret,
|
|
675
|
+
writeVaultRecord,
|
|
676
|
+
readVaultRecords,
|
|
677
|
+
removeVaultRecord,
|
|
678
|
+
backfillSecretMeta,
|
|
679
|
+
auditVault,
|
|
680
|
+
mergeVault,
|
|
681
|
+
snapshotVault,
|
|
682
|
+
restoreVault,
|
|
683
|
+
setSecretUrl
|
|
684
|
+
};
|