enseal 0.0.0-stage → 0.2.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/lib/prompt.mjs ADDED
@@ -0,0 +1,51 @@
1
+ import readline from "node:readline/promises";
2
+ import { CliError } from "./errors.mjs";
3
+
4
+ /** A visible question. */
5
+ export async function prompt(question) {
6
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
7
+ try {
8
+ return (await rl.question(question)).trim();
9
+ } finally {
10
+ rl.close();
11
+ }
12
+ }
13
+
14
+ /** A hidden question: nothing is echoed. Secrets come in through here or
15
+ * through stdin — never through argv, which lands in shell history and ps. */
16
+ export function promptHidden(question) {
17
+ if (!process.stdin.isTTY) {
18
+ throw new CliError("No terminal for a hidden prompt. In scripts, pipe the secret in with `-`.", 4);
19
+ }
20
+ return new Promise((resolve, reject) => {
21
+ process.stdout.write(question);
22
+ const stdin = process.stdin;
23
+ stdin.setRawMode(true);
24
+ stdin.resume();
25
+ stdin.setEncoding("utf8");
26
+ let value = "";
27
+ const done = (fn, arg) => {
28
+ stdin.setRawMode(false);
29
+ stdin.pause();
30
+ stdin.off("data", onData);
31
+ process.stdout.write("\n");
32
+ fn(arg);
33
+ };
34
+ const onData = (chunk) => {
35
+ for (const ch of chunk) {
36
+ if (ch === "\r" || ch === "\n") return done(resolve, value);
37
+ if (ch === "\u0003") return done(reject, new CliError("Cancelled.", 4)); // ^C
38
+ if (ch === "\u007f" || ch === "\b") value = value.slice(0, -1);
39
+ else value += ch;
40
+ }
41
+ };
42
+ stdin.on("data", onData);
43
+ });
44
+ }
45
+
46
+ /** Everything piped in on stdin, for `enseal send -`. */
47
+ export async function readStdin() {
48
+ const chunks = [];
49
+ for await (const chunk of process.stdin) chunks.push(chunk);
50
+ return Buffer.concat(chunks);
51
+ }
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Where a saved API token is kept: the system's own store when there is one,
3
+ * so the token is never a plain line in a file (backups, synced dotfiles, a
4
+ * copied home folder):
5
+ *
6
+ * macOS the login keychain (`security`)
7
+ * Windows encrypted for this Windows account with DPAPI, the protection
8
+ * Windows' own Credential Manager uses; the encrypted text sits
9
+ * in the config file and only this account on this computer can
10
+ * open it
11
+ * Linux the desktop keyring (Secret Service, `secret-tool`), when one
12
+ * is running
13
+ * anywhere else, or when the store refuses: the config file, readable
14
+ * only by its owner (0600), as before
15
+ *
16
+ * The token itself never appears in any command's arguments (which other
17
+ * programs can see in the process list): it always goes in through stdin.
18
+ * The PowerShell module reads and writes the same places in the same way,
19
+ * so either can use a sign-in made by the other.
20
+ *
21
+ * ENSEAL_SECRET_STORE=file forces the plain file (servers, CI, tests);
22
+ * ENSEAL_KEYCHAIN names a macOS keychain file to use instead of the login
23
+ * keychain (tests).
24
+ */
25
+ import { spawnSync } from "node:child_process";
26
+
27
+ export const SERVICE = "enseal";
28
+
29
+ export const defaultRun = (cmd, args, input, env) => {
30
+ try {
31
+ const r = spawnSync(cmd, args, { input: input ?? "", encoding: "utf8", stdio: ["pipe", "pipe", "pipe"], windowsHide: true, timeout: 20_000, ...(env ? { env } : {}) });
32
+ return { ok: r.status === 0 && !r.error, out: (r.stdout ?? "").trim(), err: (r.stderr ?? "").trim(), missing: r.error?.code === "ENOENT" };
33
+ } catch {
34
+ return { ok: false, out: "", err: "", missing: true };
35
+ }
36
+ };
37
+
38
+ /** For `security -i`, which reads commands from stdin: one quoted word. */
39
+ const q = (s) => `"${String(s).replace(/["\\]/g, (c) => `\\${c}`)}"`;
40
+
41
+ export const PS = ["-NoProfile", "-NonInteractive", "-Command"];
42
+ /** The environment for powershell.exe (Windows PowerShell 5.1). Run from a
43
+ * PowerShell 7 window (or anything started from one), it would inherit 7's
44
+ * PSModulePath, try to load 7's copy of its own security module, and fail
45
+ * ("the module could not be loaded"). Without PSModulePath it uses its own. */
46
+ export const ps51Env = (env = process.env) => Object.fromEntries(Object.entries(env).filter(([k]) => k.toLowerCase() !== "psmodulepath"));
47
+ // What was piped in. Windows PowerShell 5.1 (powershell.exe, which this
48
+ // calls) hands piped text to the script as $input and leaves the console's
49
+ // own input empty; PowerShell 7 offers both. Read whichever has it.
50
+ const STDIN = "$x=(@($input) -join [char]10).Trim(); if (-not $x) { $x=[Console]::In.ReadToEnd().Trim() }; ";
51
+ export const PROTECT = STDIN + "ConvertTo-SecureString $x -AsPlainText -Force | ConvertFrom-SecureString";
52
+ export const UNPROTECT =
53
+ STDIN + "$s=ConvertTo-SecureString $x; " +
54
+ "$p=[Runtime.InteropServices.Marshal]::SecureStringToBSTR($s); " +
55
+ "try { [Runtime.InteropServices.Marshal]::PtrToStringBSTR($p) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($p) }";
56
+
57
+ /**
58
+ * The store for this computer. Each method returns null when it could not
59
+ * do its job, and the caller falls back to the file.
60
+ * save(account, token) -> { kind, saved: object to merge into the config } | null
61
+ * load(account, cfg) -> token | null
62
+ * remove(account) -> void
63
+ */
64
+ export function secretStore({ platform = process.platform, env = process.env, run = defaultRun } = {}) {
65
+ if (env.ENSEAL_SECRET_STORE === "file") return null;
66
+
67
+ if (platform === "darwin") {
68
+ const kc = env.ENSEAL_KEYCHAIN ? [env.ENSEAL_KEYCHAIN] : [];
69
+ const kcq = env.ENSEAL_KEYCHAIN ? ` ${q(env.ENSEAL_KEYCHAIN)}` : "";
70
+ return {
71
+ kind: "keychain",
72
+ label: "your macOS keychain",
73
+ save(account, token) {
74
+ const r = run("security", ["-i"], `add-generic-password -U -s ${q(SERVICE)} -a ${q(account)} -l ${q("Enseal API token")} -w ${q(token)}${kcq}\n`);
75
+ if (r.missing) return null;
76
+ // `security -i` can report success for a command that failed: read it back
77
+ return this.load(account) === token ? { kind: "keychain", saved: {} } : null;
78
+ },
79
+ load(account) {
80
+ const r = run("security", ["find-generic-password", "-s", SERVICE, "-a", account, "-w", ...kc]);
81
+ return r.ok && r.out ? r.out : null;
82
+ },
83
+ remove(account) {
84
+ run("security", ["delete-generic-password", "-s", SERVICE, "-a", account, ...kc]);
85
+ },
86
+ };
87
+ }
88
+
89
+ if (platform === "win32") {
90
+ return {
91
+ kind: "dpapi",
92
+ label: "this Windows account (encrypted with DPAPI)",
93
+ save(_account, token) {
94
+ const r = run("powershell.exe", [...PS, PROTECT], token, ps51Env(env));
95
+ if (!r.ok || !/^[0-9a-f]{40,}$/i.test(r.out)) return null;
96
+ return this.load(_account, { tokenProtected: r.out }) === token ? { kind: "dpapi", saved: { tokenProtected: r.out } } : null;
97
+ },
98
+ load(_account, cfg) {
99
+ if (!cfg?.tokenProtected) return null;
100
+ const r = run("powershell.exe", [...PS, UNPROTECT], cfg.tokenProtected, ps51Env(env));
101
+ return r.ok && r.out ? r.out : null;
102
+ },
103
+ remove() {},
104
+ };
105
+ }
106
+
107
+ if (platform === "linux") {
108
+ return {
109
+ kind: "secret-service",
110
+ label: "your desktop keyring",
111
+ save(account, token) {
112
+ const r = run("secret-tool", ["store", "--label=Enseal API token", "service", SERVICE, "account", account], token);
113
+ if (!r.ok) return null;
114
+ return this.load(account) === token ? { kind: "secret-service", saved: {} } : null;
115
+ },
116
+ load(account) {
117
+ const r = run("secret-tool", ["lookup", "service", SERVICE, "account", account]);
118
+ return r.ok && r.out ? r.out : null;
119
+ },
120
+ remove(account) {
121
+ run("secret-tool", ["clear", "service", SERVICE, "account", account]);
122
+ },
123
+ };
124
+ }
125
+
126
+ return null;
127
+ }
package/package.json CHANGED
@@ -1,6 +1,19 @@
1
1
  {
2
2
  "name": "enseal",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Send secrets and files as one-time end-to-end-encrypted links from your terminal: the Enseal CLI.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "bin": { "enseal": "bin/enseal.mjs" },
8
+ "files": ["bin", "lib", "README.md", "LICENSE"],
9
+ "engines": { "node": ">=20" },
10
+ "scripts": {
11
+ "test": "node --test test/*.test.mjs",
12
+ "prepublishOnly": "node ../scripts/build-cli.mjs && node --test test/*.test.mjs"
13
+ },
14
+ "dependencies": {
15
+ "tweetnacl": "1.0.3"
16
+ },
17
+ "keywords": ["secrets", "one-time", "encryption", "end-to-end", "credentials", "secure-send"],
18
+ "homepage": "https://enseal.app"
19
+ }