felix-mcp 2.0.133 → 2.0.136
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 +112 -33
- package/dist/credential-store.js +589 -0
- package/dist/getting-started-guide.js +15 -3
- package/dist/human-prompt.js +245 -0
- package/dist/index.js +1077 -116
- package/dist/key-backup.js +227 -159
- package/dist/market-stream-guide.js +14 -4
- package/dist/money-journal-identity.js +4 -3
- package/package.json +5 -2
- package/scripts/keychain-write.exp +15 -1
- package/server.json +3 -3
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
// CROSS_PLATFORM_KEYS_20261123: one credential store for macOS, Windows and Linux.
|
|
2
|
+
//
|
|
3
|
+
// macOS -> login Keychain (security(1); values written through scripts/keychain-write.exp, never argv).
|
|
4
|
+
// Windows -> per-user DPAPI (Windows PowerShell 5.1 ProtectedData, CurrentUser scope) sealing files under
|
|
5
|
+
// %APPDATA%\FelixTrade\credentials. Secrets travel on stdin/stdout only, never argv or env.
|
|
6
|
+
// Linux -> the desktop keyring through secret-tool (libsecret) when a Secret Service answers, otherwise
|
|
7
|
+
// AES-256-GCM files in ~/.config/felix-mcp/credentials (0600 in a 0700 directory) sealed with a random
|
|
8
|
+
// 32-byte key kept beside them (0600). That protects against other users on the machine, not against
|
|
9
|
+
// software running as this user or anyone holding a copy of the directory, so the status says so and
|
|
10
|
+
// onboarding asks the owner to keep the recovery code off this computer.
|
|
11
|
+
//
|
|
12
|
+
// QA r18 hardening:
|
|
13
|
+
// - A read returns undefined ONLY when the item definitely does not exist. Locked keyrings, denied access,
|
|
14
|
+
// timeouts, symlinks and unreadable files throw, so no caller can mistake "unreadable" for "absent" and
|
|
15
|
+
// create a replacement identity over a real one.
|
|
16
|
+
// - writeCredential(..., { createOnly: true }) refuses to replace an existing item (owner keys use it).
|
|
17
|
+
// - The Linux store choice is recorded on first use; a process without D-Bus that cannot tell whether keys live
|
|
18
|
+
// in the keyring refuses to create a new identity instead of silently starting a second one in files.
|
|
19
|
+
// - FELIX_CREDENTIAL_STORE is a deliberate Linux choice (secret-service|file). On macOS and Windows it is honoured
|
|
20
|
+
// only for tests (FELIX_TEST_CREDENTIAL_STORE=1), so a stray variable cannot move real keys.
|
|
21
|
+
import { spawnSync } from "node:child_process";
|
|
22
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
23
|
+
import { chmodSync, closeSync, constants as fsConstants, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync, writeSync, } from "node:fs";
|
|
24
|
+
import { homedir, userInfo } from "node:os";
|
|
25
|
+
import { delimiter, dirname, join, resolve } from "node:path";
|
|
26
|
+
import { fileURLToPath } from "node:url";
|
|
27
|
+
const SERVICE_RE = /^[A-Za-z0-9._-]{1,200}$/;
|
|
28
|
+
const MAX_SECRET_BYTES = 4096;
|
|
29
|
+
const SPAWN_TIMEOUT_MS = 20_000;
|
|
30
|
+
export class CredentialExistsError extends Error {
|
|
31
|
+
constructor() {
|
|
32
|
+
super("A Felix credential with this name already exists and was not replaced.");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function assertService(service) {
|
|
36
|
+
if (!SERVICE_RE.test(service))
|
|
37
|
+
throw new Error("Felix credential name is invalid.");
|
|
38
|
+
}
|
|
39
|
+
function username() {
|
|
40
|
+
try {
|
|
41
|
+
return userInfo().username;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return process.env.USER || process.env.USERNAME || "felix";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function credentialDirectory() {
|
|
48
|
+
const override = String(process.env.FELIX_CREDENTIAL_DIR || "").trim();
|
|
49
|
+
if (override)
|
|
50
|
+
return resolve(override);
|
|
51
|
+
if (process.platform === "win32") {
|
|
52
|
+
const appData = process.env.APPDATA || join(homedir(), "AppData", "Roaming");
|
|
53
|
+
return join(appData, "FelixTrade", "credentials");
|
|
54
|
+
}
|
|
55
|
+
const config = process.env.XDG_CONFIG_HOME && process.env.XDG_CONFIG_HOME.startsWith("/")
|
|
56
|
+
? process.env.XDG_CONFIG_HOME : join(homedir(), ".config");
|
|
57
|
+
return join(config, "felix-mcp", "credentials");
|
|
58
|
+
}
|
|
59
|
+
function ensurePrivateDirectory(directory) {
|
|
60
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
61
|
+
const status = lstatSync(directory);
|
|
62
|
+
if (!status.isDirectory() || status.isSymbolicLink()) {
|
|
63
|
+
throw new Error("The Felix credential directory is not a private directory.");
|
|
64
|
+
}
|
|
65
|
+
if (process.platform !== "win32") {
|
|
66
|
+
if (typeof process.geteuid === "function" && status.uid !== process.geteuid()) {
|
|
67
|
+
throw new Error("The Felix credential directory belongs to another user.");
|
|
68
|
+
}
|
|
69
|
+
if ((status.mode & 0o077) !== 0)
|
|
70
|
+
chmodSync(directory, 0o700);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const NOFOLLOW = typeof fsConstants.O_NOFOLLOW === "number" ? fsConstants.O_NOFOLLOW : 0;
|
|
74
|
+
/** undefined only when the path does not exist; anything odd (symlink, not a file, unreadable) throws. */
|
|
75
|
+
function readPrivateSmallFile(path) {
|
|
76
|
+
let status;
|
|
77
|
+
try {
|
|
78
|
+
status = lstatSync(path);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (error.code === "ENOENT")
|
|
82
|
+
return undefined;
|
|
83
|
+
throw new Error("A Felix credential file could not be checked.");
|
|
84
|
+
}
|
|
85
|
+
if (status.isSymbolicLink() || !status.isFile())
|
|
86
|
+
throw new Error("A Felix credential path is not a regular file.");
|
|
87
|
+
let descriptor;
|
|
88
|
+
try {
|
|
89
|
+
descriptor = openSync(path, fsConstants.O_RDONLY | NOFOLLOW);
|
|
90
|
+
const opened = fstatSync(descriptor);
|
|
91
|
+
if (!opened.isFile() || opened.size > 64 * 1024)
|
|
92
|
+
throw new Error("A Felix credential file is not valid.");
|
|
93
|
+
if (process.platform !== "win32" && typeof process.geteuid === "function" && opened.uid !== process.geteuid()) {
|
|
94
|
+
throw new Error("A Felix credential file belongs to another user.");
|
|
95
|
+
}
|
|
96
|
+
return readFileSync(descriptor, "utf8");
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (error instanceof Error && error.message.startsWith("A Felix credential"))
|
|
100
|
+
throw error;
|
|
101
|
+
throw new Error("A Felix credential file could not be read.");
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
if (descriptor !== undefined)
|
|
105
|
+
closeSync(descriptor);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function writePrivateFile(path, contents, options = {}) {
|
|
109
|
+
ensurePrivateDirectory(dirname(path));
|
|
110
|
+
if (options.createOnly) {
|
|
111
|
+
let descriptor;
|
|
112
|
+
try {
|
|
113
|
+
descriptor = openSync(path, fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL | NOFOLLOW, 0o600);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (error.code === "EEXIST")
|
|
117
|
+
throw new CredentialExistsError();
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
writeSync(descriptor, contents);
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
closeSync(descriptor);
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const temporary = `${path}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;
|
|
129
|
+
writeFileSync(temporary, contents, { mode: 0o600, flag: "wx" });
|
|
130
|
+
try {
|
|
131
|
+
renameSync(temporary, path);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
try {
|
|
135
|
+
unlinkSync(temporary);
|
|
136
|
+
}
|
|
137
|
+
catch { /* best effort */ }
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function removePrivateFile(path) {
|
|
142
|
+
try {
|
|
143
|
+
unlinkSync(path);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (error.code !== "ENOENT")
|
|
147
|
+
throw new Error("A Felix credential file could not be removed.");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function findExecutable(name, extraDirectories = []) {
|
|
151
|
+
const directories = [...extraDirectories, ...String(process.env.PATH || "").split(delimiter)]
|
|
152
|
+
.filter((directory) => directory && directory.startsWith("/"));
|
|
153
|
+
for (const directory of directories) {
|
|
154
|
+
const candidate = join(directory, name);
|
|
155
|
+
try {
|
|
156
|
+
if (lstatSync(candidate).isFile())
|
|
157
|
+
return candidate;
|
|
158
|
+
}
|
|
159
|
+
catch { /* not here */ }
|
|
160
|
+
}
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
// ---------------------------------------------------------------- macOS Keychain
|
|
164
|
+
const MACOS_ITEM_NOT_FOUND = 44;
|
|
165
|
+
function keychainWriteHelper() {
|
|
166
|
+
return resolve(dirname(fileURLToPath(import.meta.url)), "..", "scripts", "keychain-write.exp");
|
|
167
|
+
}
|
|
168
|
+
function macosExists(service) {
|
|
169
|
+
const result = spawnSync("/usr/bin/security", ["find-generic-password", "-a", username(), "-s", service], {
|
|
170
|
+
stdio: ["ignore", "ignore", "ignore"], timeout: SPAWN_TIMEOUT_MS,
|
|
171
|
+
});
|
|
172
|
+
if (result.status === 0)
|
|
173
|
+
return true;
|
|
174
|
+
if (result.status === MACOS_ITEM_NOT_FOUND)
|
|
175
|
+
return false;
|
|
176
|
+
throw new Error("macOS Keychain could not be checked (locked, denied or timed out). Nothing was changed.");
|
|
177
|
+
}
|
|
178
|
+
const macos = {
|
|
179
|
+
read(service) {
|
|
180
|
+
if (!macosExists(service))
|
|
181
|
+
return undefined;
|
|
182
|
+
const result = spawnSync("/usr/bin/security", [
|
|
183
|
+
"find-generic-password", "-a", username(), "-s", service, "-w",
|
|
184
|
+
], { encoding: "utf8", maxBuffer: 16 * 1024, stdio: ["ignore", "pipe", "ignore"], timeout: SPAWN_TIMEOUT_MS });
|
|
185
|
+
if (result.status !== 0) {
|
|
186
|
+
throw new Error("macOS Keychain has this Felix item but did not release it (denied or timed out). Nothing was changed.");
|
|
187
|
+
}
|
|
188
|
+
return String(result.stdout || "").replace(/\r?\n$/, "");
|
|
189
|
+
},
|
|
190
|
+
write(service, label, value, options) {
|
|
191
|
+
if (options.createOnly && macosExists(service))
|
|
192
|
+
throw new CredentialExistsError();
|
|
193
|
+
const result = spawnSync("/usr/bin/expect", [
|
|
194
|
+
keychainWriteHelper(), username(), service, label, ...(options.createOnly ? ["create-only"] : []),
|
|
195
|
+
], {
|
|
196
|
+
encoding: "utf8", input: value, maxBuffer: 16 * 1024, stdio: ["pipe", "ignore", "ignore"], timeout: SPAWN_TIMEOUT_MS,
|
|
197
|
+
});
|
|
198
|
+
if (result.status !== 0)
|
|
199
|
+
throw new Error("macOS Keychain write failed.");
|
|
200
|
+
},
|
|
201
|
+
remove(service) {
|
|
202
|
+
if (!macosExists(service))
|
|
203
|
+
return;
|
|
204
|
+
const result = spawnSync("/usr/bin/security", ["delete-generic-password", "-a", username(), "-s", service], {
|
|
205
|
+
stdio: ["ignore", "ignore", "ignore"], timeout: SPAWN_TIMEOUT_MS,
|
|
206
|
+
});
|
|
207
|
+
if (result.status !== 0)
|
|
208
|
+
throw new Error("macOS Keychain delete failed.");
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
// ---------------------------------------------------------------- Windows DPAPI
|
|
212
|
+
function windowsPowerShell() {
|
|
213
|
+
const root = process.env.SystemRoot || process.env.windir || "C:\\Windows";
|
|
214
|
+
return join(root, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
215
|
+
}
|
|
216
|
+
const DPAPI_ENTROPY = "felix-credential-v1";
|
|
217
|
+
const DPAPI_SCRIPT = (direction) => [
|
|
218
|
+
"$ErrorActionPreference = 'Stop'",
|
|
219
|
+
"if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') { [Console]::Error.Write('felix:constrained-language'); exit 3 }",
|
|
220
|
+
"Add-Type -AssemblyName System.Security",
|
|
221
|
+
"$reader = New-Object System.IO.StreamReader([Console]::OpenStandardInput(), (New-Object System.Text.UTF8Encoding($false)))",
|
|
222
|
+
"$data = $reader.ReadToEnd()",
|
|
223
|
+
`$entropy = [System.Text.Encoding]::UTF8.GetBytes('${DPAPI_ENTROPY}')`,
|
|
224
|
+
"$scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser",
|
|
225
|
+
direction === "protect"
|
|
226
|
+
? "$out = [Convert]::ToBase64String([System.Security.Cryptography.ProtectedData]::Protect([System.Text.Encoding]::UTF8.GetBytes($data), $entropy, $scope))"
|
|
227
|
+
: "$out = [System.Text.Encoding]::UTF8.GetString([System.Security.Cryptography.ProtectedData]::Unprotect([Convert]::FromBase64String($data.Trim()), $entropy, $scope))",
|
|
228
|
+
"$stdout = [Console]::OpenStandardOutput()",
|
|
229
|
+
"$bytes = [System.Text.Encoding]::UTF8.GetBytes($out)",
|
|
230
|
+
"$stdout.Write($bytes, 0, $bytes.Length)",
|
|
231
|
+
"$stdout.Flush()",
|
|
232
|
+
].join("\n");
|
|
233
|
+
function runDpapi(direction, input) {
|
|
234
|
+
const encoded = Buffer.from(DPAPI_SCRIPT(direction), "utf16le").toString("base64");
|
|
235
|
+
const result = spawnSync(windowsPowerShell(), [
|
|
236
|
+
"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand", encoded,
|
|
237
|
+
], {
|
|
238
|
+
input, encoding: "utf8", maxBuffer: 64 * 1024, stdio: ["pipe", "pipe", "pipe"], timeout: 60_000, windowsHide: true,
|
|
239
|
+
});
|
|
240
|
+
if (result.status === 0)
|
|
241
|
+
return String(result.stdout || "");
|
|
242
|
+
if (String(result.stderr || "").includes("felix:constrained-language")) {
|
|
243
|
+
throw new Error("Windows policy (Constrained Language Mode) blocks Windows PowerShell here, so Felix cannot use protected storage. Ask your administrator to allow it, or run Felix on another computer. Nothing was changed.");
|
|
244
|
+
}
|
|
245
|
+
if (result.error)
|
|
246
|
+
throw new Error("Windows PowerShell could not be started for protected storage. Nothing was changed.");
|
|
247
|
+
throw new Error(direction === "protect"
|
|
248
|
+
? "Windows protected storage write failed. Nothing was changed."
|
|
249
|
+
: `Windows could not unlock a Felix credential for this user (for example after a password reset). ${restoreAdvice()}`);
|
|
250
|
+
}
|
|
251
|
+
function sealedPath(service, extension) {
|
|
252
|
+
return join(credentialDirectory(), `${service}.${extension}`);
|
|
253
|
+
}
|
|
254
|
+
const windows = {
|
|
255
|
+
read(service) {
|
|
256
|
+
const sealed = readPrivateSmallFile(sealedPath(service, "dpapi"));
|
|
257
|
+
return sealed === undefined ? undefined : runDpapi("unprotect", sealed);
|
|
258
|
+
},
|
|
259
|
+
write(service, _label, value, options) {
|
|
260
|
+
const sealed = runDpapi("protect", value);
|
|
261
|
+
if (!sealed || !/^[A-Za-z0-9+/=]+$/.test(sealed))
|
|
262
|
+
throw new Error("Windows protected storage write failed.");
|
|
263
|
+
writePrivateFile(sealedPath(service, "dpapi"), sealed, options);
|
|
264
|
+
},
|
|
265
|
+
remove(service) {
|
|
266
|
+
removePrivateFile(sealedPath(service, "dpapi"));
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
// ---------------------------------------------------------------- Linux Secret Service (libsecret)
|
|
270
|
+
const SECRET_TOOL_DIRS = ["/usr/bin", "/bin", "/usr/local/bin"];
|
|
271
|
+
let secretServiceProbe;
|
|
272
|
+
function secretTool() {
|
|
273
|
+
return findExecutable("secret-tool", SECRET_TOOL_DIRS);
|
|
274
|
+
}
|
|
275
|
+
export function secretServiceAvailable() {
|
|
276
|
+
if (secretServiceProbe !== undefined)
|
|
277
|
+
return secretServiceProbe;
|
|
278
|
+
const tool = secretTool();
|
|
279
|
+
if (!tool || !process.env.DBUS_SESSION_BUS_ADDRESS)
|
|
280
|
+
return (secretServiceProbe = false);
|
|
281
|
+
// A lookup for an attribute nobody stores exits 1 with empty stderr when the service answers.
|
|
282
|
+
const result = spawnSync(tool, ["lookup", "felix-probe", "absent"], {
|
|
283
|
+
encoding: "utf8", stdio: ["ignore", "ignore", "pipe"], timeout: 8_000,
|
|
284
|
+
});
|
|
285
|
+
return (secretServiceProbe = result.error === undefined && (result.status === 0 || result.status === 1)
|
|
286
|
+
&& String(result.stderr || "").trim() === "");
|
|
287
|
+
}
|
|
288
|
+
function secretAttributes(service) {
|
|
289
|
+
return ["felix-service", service, "felix-user", username()];
|
|
290
|
+
}
|
|
291
|
+
export function secretToolSearchFoundItem(stdout, stderr) {
|
|
292
|
+
// secret-tool versions differ in which stream carries the item listing, so read both. Any item header,
|
|
293
|
+
// label or our attribute counts as "an item exists"; a false "exists" only ever refuses a write.
|
|
294
|
+
const text = `${stdout}\n${stderr}`;
|
|
295
|
+
return /\[\/org\/freedesktop\/secrets\//.test(text) || /felix-service/.test(text) || /^\s*label\s*=/m.test(text);
|
|
296
|
+
}
|
|
297
|
+
function secretServiceHasItem(tool, service) {
|
|
298
|
+
// CI r1: a successful lookup is definitive. search lists matching items even when the secret itself
|
|
299
|
+
// would need an unlock, so it covers the locked case.
|
|
300
|
+
const lookup = spawnSync(tool, ["lookup", ...secretAttributes(service)], {
|
|
301
|
+
encoding: "utf8", maxBuffer: 16 * 1024, stdio: ["ignore", "pipe", "pipe"], timeout: SPAWN_TIMEOUT_MS,
|
|
302
|
+
});
|
|
303
|
+
if (lookup.status === 0)
|
|
304
|
+
return true;
|
|
305
|
+
const result = spawnSync(tool, ["search", "--all", ...secretAttributes(service)], {
|
|
306
|
+
encoding: "utf8", maxBuffer: 64 * 1024, stdio: ["ignore", "pipe", "pipe"], timeout: SPAWN_TIMEOUT_MS,
|
|
307
|
+
});
|
|
308
|
+
if (result.error || (result.status !== 0 && result.status !== 1)) {
|
|
309
|
+
throw new Error("The system keyring did not answer. Unlock it, or run Felix from your desktop session. Nothing was changed.");
|
|
310
|
+
}
|
|
311
|
+
return secretToolSearchFoundItem(String(result.stdout || ""), String(result.stderr || ""));
|
|
312
|
+
}
|
|
313
|
+
const linuxSecretService = {
|
|
314
|
+
read(service) {
|
|
315
|
+
const tool = secretTool();
|
|
316
|
+
if (!tool)
|
|
317
|
+
throw new Error("secret-tool is not installed, so the system keyring cannot be read.");
|
|
318
|
+
const result = spawnSync(tool, ["lookup", ...secretAttributes(service)], {
|
|
319
|
+
encoding: "utf8", maxBuffer: 16 * 1024, stdio: ["ignore", "pipe", "pipe"], timeout: SPAWN_TIMEOUT_MS,
|
|
320
|
+
});
|
|
321
|
+
if (result.status === 0)
|
|
322
|
+
return String(result.stdout || "").replace(/\r?\n$/, "");
|
|
323
|
+
if (result.status === 1 && String(result.stderr || "").trim() === "") {
|
|
324
|
+
// Exit 1 also follows a dismissed unlock prompt. Only call it absent if the keyring lists no such item.
|
|
325
|
+
if (secretServiceHasItem(tool, service)) {
|
|
326
|
+
throw new Error("The system keyring is locked or the unlock was dismissed. Unlock it and retry. Nothing was changed.");
|
|
327
|
+
}
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
throw new Error("The system keyring did not answer. Unlock it, or run Felix from your desktop session. Nothing was changed.");
|
|
331
|
+
},
|
|
332
|
+
write(service, label, value, options) {
|
|
333
|
+
const tool = secretTool();
|
|
334
|
+
if (!tool)
|
|
335
|
+
throw new Error("secret-tool is not installed, so the system keyring cannot be written.");
|
|
336
|
+
if (options.createOnly && secretServiceHasItem(tool, service))
|
|
337
|
+
throw new CredentialExistsError();
|
|
338
|
+
const result = spawnSync(tool, ["store", `--label=${label}`, ...secretAttributes(service)], {
|
|
339
|
+
encoding: "utf8", input: value, stdio: ["pipe", "ignore", "ignore"], timeout: SPAWN_TIMEOUT_MS,
|
|
340
|
+
});
|
|
341
|
+
if (result.status !== 0)
|
|
342
|
+
throw new Error("System keyring write failed.");
|
|
343
|
+
},
|
|
344
|
+
remove(service) {
|
|
345
|
+
const tool = secretTool();
|
|
346
|
+
if (!tool)
|
|
347
|
+
throw new Error("secret-tool is not installed, so the system keyring cannot be changed.");
|
|
348
|
+
const result = spawnSync(tool, ["clear", ...secretAttributes(service)], {
|
|
349
|
+
stdio: ["ignore", "ignore", "ignore"], timeout: SPAWN_TIMEOUT_MS,
|
|
350
|
+
});
|
|
351
|
+
if (result.status !== 0 && result.status !== 1)
|
|
352
|
+
throw new Error("System keyring delete failed.");
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
// ---------------------------------------------------------------- sealed files (random local key)
|
|
356
|
+
const FILE_KEY_NAME = "file-store.key";
|
|
357
|
+
function fileKey(create) {
|
|
358
|
+
const path = join(credentialDirectory(), FILE_KEY_NAME);
|
|
359
|
+
const existing = readPrivateSmallFile(path);
|
|
360
|
+
if (existing !== undefined) {
|
|
361
|
+
const key = Buffer.from(existing.trim(), "base64");
|
|
362
|
+
if (key.length !== 32)
|
|
363
|
+
throw new Error(`The Felix sealed-file key is damaged. ${restoreAdvice()}`);
|
|
364
|
+
return key;
|
|
365
|
+
}
|
|
366
|
+
if (!create)
|
|
367
|
+
return undefined;
|
|
368
|
+
// QA r18 #2: never mint a new key beside sealed files that need the old one; that would hide the real cause.
|
|
369
|
+
if (sealedFilesPresent())
|
|
370
|
+
throw new Error(`The Felix sealed-file key is missing, so stored keys cannot be opened. ${restoreAdvice()}`);
|
|
371
|
+
const key = randomBytes(32);
|
|
372
|
+
try {
|
|
373
|
+
writePrivateFile(path, key.toString("base64"), { createOnly: true });
|
|
374
|
+
return key;
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
if (error instanceof CredentialExistsError)
|
|
378
|
+
return fileKey(false);
|
|
379
|
+
throw error;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const encryptedFile = {
|
|
383
|
+
read(service) {
|
|
384
|
+
const raw = readPrivateSmallFile(sealedPath(service, "cred"));
|
|
385
|
+
if (raw === undefined)
|
|
386
|
+
return undefined;
|
|
387
|
+
const key = fileKey(false);
|
|
388
|
+
if (!key)
|
|
389
|
+
throw new Error(`The Felix sealed-file key is missing, so stored keys cannot be opened. ${restoreAdvice()}`);
|
|
390
|
+
let parsed;
|
|
391
|
+
try {
|
|
392
|
+
parsed = JSON.parse(raw);
|
|
393
|
+
}
|
|
394
|
+
catch {
|
|
395
|
+
throw new Error("A Felix credential file is damaged.");
|
|
396
|
+
}
|
|
397
|
+
if (parsed.v !== 2 || typeof parsed.iv !== "string" || typeof parsed.tag !== "string" || typeof parsed.ct !== "string") {
|
|
398
|
+
throw new Error("A Felix credential file is damaged.");
|
|
399
|
+
}
|
|
400
|
+
try {
|
|
401
|
+
const decipher = createDecipheriv("aes-256-gcm", key, Buffer.from(parsed.iv, "base64"));
|
|
402
|
+
decipher.setAAD(Buffer.from(service));
|
|
403
|
+
decipher.setAuthTag(Buffer.from(parsed.tag, "base64"));
|
|
404
|
+
return Buffer.concat([decipher.update(Buffer.from(parsed.ct, "base64")), decipher.final()]).toString("utf8");
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
throw new Error(`A Felix credential file cannot be opened. ${restoreAdvice()}`);
|
|
408
|
+
}
|
|
409
|
+
},
|
|
410
|
+
write(service, _label, value, options) {
|
|
411
|
+
const key = fileKey(true);
|
|
412
|
+
const iv = randomBytes(12);
|
|
413
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
414
|
+
cipher.setAAD(Buffer.from(service));
|
|
415
|
+
const ct = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
|
|
416
|
+
writePrivateFile(sealedPath(service, "cred"), JSON.stringify({
|
|
417
|
+
v: 2, iv: iv.toString("base64"), tag: cipher.getAuthTag().toString("base64"), ct: ct.toString("base64"),
|
|
418
|
+
}), options);
|
|
419
|
+
},
|
|
420
|
+
remove(service) {
|
|
421
|
+
removePrivateFile(sealedPath(service, "cred"));
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
const BACKENDS = {
|
|
425
|
+
macos_keychain: macos,
|
|
426
|
+
windows_dpapi: windows,
|
|
427
|
+
linux_secret_service: linuxSecretService,
|
|
428
|
+
encrypted_file: encryptedFile,
|
|
429
|
+
};
|
|
430
|
+
function markerPath() {
|
|
431
|
+
return join(credentialDirectory(), "store.json");
|
|
432
|
+
}
|
|
433
|
+
function recordedLinuxStore() {
|
|
434
|
+
// QA r18 #4: an unreadable or damaged marker fails closed, like credential reads.
|
|
435
|
+
const raw = readPrivateSmallFile(markerPath());
|
|
436
|
+
if (!raw)
|
|
437
|
+
return undefined;
|
|
438
|
+
let kind;
|
|
439
|
+
try {
|
|
440
|
+
kind = JSON.parse(raw).kind;
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
kind = undefined;
|
|
444
|
+
}
|
|
445
|
+
if (kind === "linux_secret_service" || kind === "encrypted_file")
|
|
446
|
+
return kind;
|
|
447
|
+
throw new Error(`The Felix key-store marker ${markerPath()} is damaged. Fix or remove it only if you know which store holds your keys.`);
|
|
448
|
+
}
|
|
449
|
+
function restoreAdvice() {
|
|
450
|
+
return `Your encrypted backup still works: move the folder ${credentialDirectory()} aside (keep it), then run felix-keys restore --input <your backup file>.`;
|
|
451
|
+
}
|
|
452
|
+
function overrideKind() {
|
|
453
|
+
const override = String(process.env.FELIX_CREDENTIAL_STORE || "").trim().toLowerCase();
|
|
454
|
+
if (!override)
|
|
455
|
+
return undefined;
|
|
456
|
+
const map = {
|
|
457
|
+
keychain: "macos_keychain", dpapi: "windows_dpapi", "secret-service": "linux_secret_service", file: "encrypted_file",
|
|
458
|
+
};
|
|
459
|
+
const chosen = map[override];
|
|
460
|
+
if (!chosen)
|
|
461
|
+
throw new Error("FELIX_CREDENTIAL_STORE must be secret-service or file (Linux).");
|
|
462
|
+
const testing = process.env.FELIX_TEST_CREDENTIAL_STORE === "1";
|
|
463
|
+
if (process.platform === "darwin" || process.platform === "win32") {
|
|
464
|
+
if (!testing) {
|
|
465
|
+
throw new Error("FELIX_CREDENTIAL_STORE applies only on Linux. Unset it; Felix uses "
|
|
466
|
+
+ (process.platform === "darwin" ? "macOS Keychain." : "Windows protected storage."));
|
|
467
|
+
}
|
|
468
|
+
if ((chosen === "macos_keychain" && process.platform !== "darwin")
|
|
469
|
+
|| (chosen === "windows_dpapi" && process.platform !== "win32")
|
|
470
|
+
|| chosen === "linux_secret_service") {
|
|
471
|
+
throw new Error(`FELIX_CREDENTIAL_STORE=${override} is not available on this operating system.`);
|
|
472
|
+
}
|
|
473
|
+
return chosen;
|
|
474
|
+
}
|
|
475
|
+
if (chosen === "macos_keychain" || chosen === "windows_dpapi") {
|
|
476
|
+
throw new Error(`FELIX_CREDENTIAL_STORE=${override} is not available on this operating system.`);
|
|
477
|
+
}
|
|
478
|
+
return chosen;
|
|
479
|
+
}
|
|
480
|
+
export function credentialStoreKind() {
|
|
481
|
+
const override = overrideKind();
|
|
482
|
+
if (override)
|
|
483
|
+
return override;
|
|
484
|
+
if (process.platform === "darwin")
|
|
485
|
+
return "macos_keychain";
|
|
486
|
+
if (process.platform === "win32")
|
|
487
|
+
return "windows_dpapi";
|
|
488
|
+
return recordedLinuxStore() || (secretServiceAvailable() ? "linux_secret_service" : "encrypted_file");
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Linux only: no store has been recorded, no choice was made, and a keyring tool is installed but this process
|
|
492
|
+
* has no D-Bus session. Keys may exist in a keyring this process cannot see, so a NEW identity must not be
|
|
493
|
+
* created here.
|
|
494
|
+
*/
|
|
495
|
+
export function linuxKeyringMayBeHidden() {
|
|
496
|
+
return process.platform !== "darwin" && process.platform !== "win32"
|
|
497
|
+
&& !process.env.FELIX_CREDENTIAL_STORE && recordedLinuxStore() === undefined
|
|
498
|
+
&& secretTool() !== undefined && !secretServiceAvailable();
|
|
499
|
+
}
|
|
500
|
+
export function credentialStoreLabel(kind = credentialStoreKind()) {
|
|
501
|
+
return {
|
|
502
|
+
macos_keychain: "macOS Keychain",
|
|
503
|
+
windows_dpapi: "Windows protected storage",
|
|
504
|
+
linux_secret_service: "the system keyring",
|
|
505
|
+
encrypted_file: "sealed files in your Linux home folder",
|
|
506
|
+
}[kind];
|
|
507
|
+
}
|
|
508
|
+
export function credentialStoreDescription() {
|
|
509
|
+
const kind = credentialStoreKind();
|
|
510
|
+
return {
|
|
511
|
+
kind,
|
|
512
|
+
label: credentialStoreLabel(kind),
|
|
513
|
+
...(kind === "encrypted_file" ? {
|
|
514
|
+
location: credentialDirectory(),
|
|
515
|
+
protects_against: "other users on this computer",
|
|
516
|
+
does_not_protect_against: "software running as you, or anyone with a copy of this folder",
|
|
517
|
+
off_device_recovery_required: true,
|
|
518
|
+
note: "No system keyring answered, so keys are sealed in owner-only files. Keep your recovery code off this computer (felix-keys export-recovery-code). Install and unlock a Secret Service keyring (for example gnome-keyring) for stronger protection.",
|
|
519
|
+
} : {}),
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
/** Read a credential. undefined ONLY when it definitely does not exist; every other problem throws. */
|
|
523
|
+
export function readCredential(service) {
|
|
524
|
+
assertService(service);
|
|
525
|
+
const value = BACKENDS[credentialStoreKind()].read(service);
|
|
526
|
+
return value === undefined || value === "" ? undefined : value;
|
|
527
|
+
}
|
|
528
|
+
/** Read without throwing: "" when missing or unreadable (startup paths that must never crash, never create). */
|
|
529
|
+
export function readCredentialOrEmpty(service) {
|
|
530
|
+
try {
|
|
531
|
+
return readCredential(service) || "";
|
|
532
|
+
}
|
|
533
|
+
catch {
|
|
534
|
+
return "";
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
export function writeCredential(service, label, value, options = {}) {
|
|
538
|
+
assertService(service);
|
|
539
|
+
if (!value || Buffer.byteLength(value, "utf8") > MAX_SECRET_BYTES)
|
|
540
|
+
throw new Error("Felix credential value is invalid.");
|
|
541
|
+
const kind = credentialStoreKind();
|
|
542
|
+
BACKENDS[kind].write(service, label, value, options);
|
|
543
|
+
if (process.platform !== "darwin" && process.platform !== "win32" && recordedLinuxStore() !== kind) {
|
|
544
|
+
writePrivateFile(markerPath(), JSON.stringify({ kind, recorded_at: new Date().toISOString() }));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
export function deleteCredential(service) {
|
|
548
|
+
assertService(service);
|
|
549
|
+
BACKENDS[credentialStoreKind()].remove(service);
|
|
550
|
+
}
|
|
551
|
+
export function credentialExists(service) {
|
|
552
|
+
return readCredential(service) !== undefined;
|
|
553
|
+
}
|
|
554
|
+
/** True when sealed files exist (used to tell "never set up" from "key file lost"). */
|
|
555
|
+
export function sealedFilesPresent() {
|
|
556
|
+
try {
|
|
557
|
+
return readdirSync(credentialDirectory()).some((name) => name.endsWith(".cred") || name.endsWith(".dpapi"));
|
|
558
|
+
}
|
|
559
|
+
catch {
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
// Directory privacy on Windows is enforced by the per-user profile ACL; POSIX mode bits there are synthetic.
|
|
564
|
+
/**
|
|
565
|
+
* CI r3: open a file for reading without following a symlink, on every OS. POSIX refuses through O_NOFOLLOW;
|
|
566
|
+
* Windows has no such flag, so the path is lstat-ed first and the opened file must be the same file.
|
|
567
|
+
* A missing file still throws ENOENT so callers keep their "absent" handling.
|
|
568
|
+
*/
|
|
569
|
+
export function openReadNoFollow(path) {
|
|
570
|
+
const before = lstatSync(path);
|
|
571
|
+
if (before.isSymbolicLink()) {
|
|
572
|
+
throw Object.assign(new Error("Refusing to read a Felix file through a symbolic link."), { code: "ELOOP" });
|
|
573
|
+
}
|
|
574
|
+
const descriptor = openSync(path, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0));
|
|
575
|
+
try {
|
|
576
|
+
const after = fstatSync(descriptor);
|
|
577
|
+
if (after.ino !== before.ino || after.dev !== before.dev) {
|
|
578
|
+
throw Object.assign(new Error("A Felix file changed while it was being opened."), { code: "ELOOP" });
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
catch (error) {
|
|
582
|
+
closeSync(descriptor);
|
|
583
|
+
throw error;
|
|
584
|
+
}
|
|
585
|
+
return descriptor;
|
|
586
|
+
}
|
|
587
|
+
export function hasPrivateMode(mode) {
|
|
588
|
+
return process.platform === "win32" || (mode & 0o077) === 0;
|
|
589
|
+
}
|
|
@@ -18,7 +18,7 @@ then creates the identity and loads it into the session with no restart.
|
|
|
18
18
|
Or let your AI agent run this single command:
|
|
19
19
|
|
|
20
20
|
~~~bash
|
|
21
|
-
npx -y --package felix-mcp@2.0.
|
|
21
|
+
npx -y --package felix-mcp@2.0.136 felix-keys onboard --accept-terms --client claude
|
|
22
22
|
~~~
|
|
23
23
|
|
|
24
24
|
After the user reviews the terms and explicitly authorizes acceptance, the agent
|
|
@@ -265,8 +265,16 @@ allowlist, limit, or expiry before signing.
|
|
|
265
265
|
session. If it returns 'operation_state_unavailable', preserve local state
|
|
266
266
|
and restore the matching owner backup; never delete the journal or invent a
|
|
267
267
|
new idempotency key for an unresolved operation.
|
|
268
|
-
-
|
|
269
|
-
'felix
|
|
268
|
+
- Key rejected ('key_rejected: true'): the launcher key was revoked or
|
|
269
|
+
replaced and nothing was sent. Ask the user to run 'felix-keys renew-key'
|
|
270
|
+
(macOS confirmation, same owner and account), then call 'reload_launcher_key'.
|
|
271
|
+
For an account key, call 'use_child_account' with 'force_remint:true'.
|
|
272
|
+
- Outdated MCP: 'get_status.mcp.compatibility.update' and any failed call's
|
|
273
|
+
'mcp_update' give the exact update command. Update before money work.
|
|
274
|
+
- Interrupted money operation: 'list_recent_operations' lists every unfinished
|
|
275
|
+
workflow with its idempotency key and next step. Reuse that key and read
|
|
276
|
+
'felix://operations/recovery'. A request Felix refused outright does not
|
|
277
|
+
use up its key.
|
|
270
278
|
- Product or account issue: read 'felix://support/agent' and open an
|
|
271
279
|
account-scoped support ticket without secrets.
|
|
272
280
|
|
|
@@ -333,6 +341,10 @@ Web equivalents:
|
|
|
333
341
|
use place_order with paper:true.
|
|
334
342
|
7. get_situation, positions, and fills: reconcile before another action.
|
|
335
343
|
|
|
344
|
+
Cash already on one venue can move to another without going back through the
|
|
345
|
+
Arc home: preview_venue_transfer, then transfer_between_venues (Hyperliquid,
|
|
346
|
+
Polymarket, Lighter; open orders and positions keep their cash).
|
|
347
|
+
|
|
336
348
|
For a timeout or unknown order result, reuse the same idempotency key and
|
|
337
349
|
reconcile. Never create a replacement key until the original intent is known.
|
|
338
350
|
|