@causyne/agent 1.20.120 → 1.20.122
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 +3 -3
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +81 -26
- package/src/acp/agentCatalog.ts +73 -29
- package/src/acp/client.js +210 -53
- package/src/acp/client.ts +202 -52
- package/src/acp/peerProfiles.js +55 -22
- package/src/acp/peerProfiles.ts +51 -22
- package/src/acp/protocol.js +37 -8
- package/src/acp/protocol.ts +30 -8
- package/src/acp/server.js +123 -25
- package/src/acp/server.ts +124 -24
- package/src/agent/errorClassifier.js +117 -0
- package/src/agent/errorClassifier.ts +165 -0
- package/src/agent/promptIntent.js +200 -0
- package/src/agent/promptIntent.ts +203 -0
- package/src/agent/toolArgValidator.js +97 -0
- package/src/agent/toolArgValidator.ts +112 -0
- package/src/agentLoop.js +33 -344
- package/src/agentLoop.ts +51 -352
- package/src/approvalGrants.js +19 -7
- package/src/approvalGrants.ts +20 -7
- package/src/approvalPolicy.js +48 -15
- package/src/approvalPolicy.ts +46 -15
- package/src/autoUpdate.js +116 -10
- package/src/autoUpdate.ts +149 -10
- package/src/browser/causyneBrowser.js +3 -2
- package/src/browser/causyneBrowser.ts +3 -2
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/cliMode.js +16 -2
- package/src/commands.js +10 -9
- package/src/config.js +288 -272
- package/src/config.ts +342 -0
- package/src/configVault.js +271 -0
- package/src/configVault.ts +273 -0
- package/src/continuum/compactor.js +2 -1
- package/src/continuum/compactor.ts +2 -1
- package/src/continuum/sanitizer.js +10 -3
- package/src/continuum/sanitizer.ts +9 -3
- package/src/continuum/ui/continuumPickerView.js +3 -2
- package/src/continuum/ui/continuumPickerView.ts +3 -2
- package/src/core/contextCompactor.js +70 -11
- package/src/core/contextCompactor.ts +83 -11
- package/src/cortex/vectorDb.js +26 -15
- package/src/cortex/vectorDb.ts +26 -16
- package/src/creative/liveBridge.js +7 -5
- package/src/creative/liveBridge.ts +7 -5
- package/src/creative/studioScripts.js +16 -5
- package/src/creative/studioScripts.ts +14 -5
- package/src/decisionPolicy.js +16 -4
- package/src/decisionPolicy.ts +14 -4
- package/src/documentEngine/antiSlopAuditor.js +10 -5
- package/src/documentEngine/antiSlopAuditor.ts +11 -5
- package/src/documentEngine/documentForge.js +47 -15
- package/src/documentEngine/documentForge.ts +43 -14
- package/src/dynamicCatalogue.js +42 -73
- package/src/dynamicCatalogue.ts +74 -60
- package/src/engines/design.js +2 -1
- package/src/engines/design.ts +2 -1
- package/src/engines/film.js +4 -4
- package/src/engines/film.ts +2 -2
- package/src/engines/light.js +2 -1
- package/src/engines/light.ts +2 -1
- package/src/engines/registry.js +1 -1
- package/src/engines/registry.ts +1 -1
- package/src/engines/reverseHarness.js +4 -3
- package/src/engines/reverseHarness.ts +4 -3
- package/src/entrypoint.js +77 -4
- package/src/headlessCli.js +305 -0
- package/src/headlessCli.ts +381 -0
- package/src/historyPruner.js +3 -2
- package/src/historyPruner.ts +3 -2
- package/src/hooks.js +3 -0
- package/src/hooks.ts +3 -0
- package/src/implementationPlan.js +21 -11
- package/src/implementationPlan.ts +21 -11
- package/src/infra/atomicWriter.js +4 -1
- package/src/infra/atomicWriter.ts +4 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/mcp/mcpClient.js +38 -11
- package/src/mcp/mcpClient.ts +44 -11
- package/src/mcp/mcpRegistry.js +2 -5
- package/src/mcp/mcpRegistry.ts +2 -5
- package/src/mcp/mcpSecurity.js +189 -0
- package/src/mcp/mcpSecurity.ts +190 -0
- package/src/memory/scopedFacts.js +1 -1
- package/src/memory/scopedFacts.ts +1 -1
- package/src/memory/symbolFolder.js +1 -1
- package/src/memory/symbolFolder.ts +1 -1
- package/src/modelRouter.js +1 -1
- package/src/modelRouter.ts +1 -1
- package/src/observer/evidence.js +10 -4
- package/src/observer/evidence.ts +10 -4
- package/src/osKeychain.js +263 -0
- package/src/osKeychain.ts +287 -0
- package/src/perception.js +3 -2
- package/src/perception.ts +3 -2
- package/src/planCache.js +2 -2
- package/src/planCache.ts +2 -2
- package/src/pluginsFactory/pluginsFactory.js +19 -11
- package/src/pluginsFactory/pluginsFactory.ts +18 -11
- package/src/promptMentions.js +2 -2
- package/src/promptMentions.ts +2 -2
- package/src/providers/acp.js +1 -1
- package/src/providers/acp.ts +1 -1
- package/src/providers/anthropic.js +328 -385
- package/src/providers/anthropic.ts +386 -0
- package/src/providers/antigravity.js +10 -4
- package/src/providers/antigravity.ts +10 -4
- package/src/providers/base.js +221 -319
- package/src/providers/base.ts +340 -0
- package/src/providers/byokHttp.js +397 -0
- package/src/providers/byokHttp.ts +497 -0
- package/src/providers/byokStream.js +748 -0
- package/src/providers/byokStream.ts +863 -0
- package/src/providers/dashscope-byok.js +98 -81
- package/src/providers/dashscope-byok.ts +114 -0
- package/src/providers/deepseek.js +74 -64
- package/src/providers/deepseek.ts +99 -0
- package/src/providers/factory.js +35 -28
- package/src/providers/factory.ts +40 -0
- package/src/providers/gemini.js +229 -253
- package/src/providers/gemini.ts +261 -0
- package/src/providers/loopGuard.js +9 -5
- package/src/providers/loopGuard.ts +10 -5
- package/src/providers/moonshot.js +71 -64
- package/src/providers/moonshot.ts +93 -0
- package/src/providers/muse.js +82 -152
- package/src/providers/muse.ts +122 -0
- package/src/providers/ollama.js +116 -93
- package/src/providers/ollama.ts +135 -0
- package/src/providers/openai-compatible.js +114 -65
- package/src/providers/openai-compatible.ts +139 -0
- package/src/providers/openai.js +22 -15
- package/src/providers/openai.ts +30 -0
- package/src/providers/openrouter.js +194 -98
- package/src/providers/openrouter.ts +202 -97
- package/src/providers/pi.js +6 -0
- package/src/providers/pi.ts +6 -0
- package/src/providers/qwen-byok.js +97 -71
- package/src/providers/qwen-byok.ts +113 -0
- package/src/providers/toolContracts.js +40 -14
- package/src/providers/toolContracts.ts +35 -14
- package/src/providers/wireNormalizer.js +78 -11
- package/src/providers/wireNormalizer.ts +68 -12
- package/src/providers/xai.js +74 -64
- package/src/providers/xai.ts +97 -0
- package/src/reliability/assetEvidence.js +4 -2
- package/src/reliability/assetEvidence.ts +4 -2
- package/src/sandbox.js +486 -530
- package/src/sandbox.ts +552 -0
- package/src/sandboxPolicy.js +1287 -0
- package/src/sandboxPolicy.ts +1381 -0
- package/src/sessionJournal.js +55 -3
- package/src/sessionJournal.ts +58 -3
- package/src/skills/skillResourceSafety.js +19 -8
- package/src/skills/skillResourceSafety.ts +16 -7
- package/src/skillsManager.js +3 -3
- package/src/skillsManager.ts +3 -3
- package/src/smartToolsUse.js +28 -4
- package/src/smartToolsUse.ts +29 -7
- package/src/stallGuard.js +45 -22
- package/src/stallGuard.ts +46 -22
- package/src/subagentJournal.js +6 -4
- package/src/subagentJournal.ts +6 -4
- package/src/textDiff.js +6 -1
- package/src/textDiff.ts +6 -1
- package/src/timeMachine.js +7 -1
- package/src/timeMachine.ts +7 -1
- package/src/tokenTracker.js +43 -28
- package/src/tokenTracker.ts +44 -30
- package/src/toolPacks.js +3 -1
- package/src/toolPacks.ts +3 -1
- package/src/tools/archive.js +245 -35
- package/src/tools/archive.ts +244 -35
- package/src/tools/computerUse.js +7 -3
- package/src/tools/computerUse.ts +7 -3
- package/src/tools/creativeApps.js +13 -8
- package/src/tools/creativeApps.ts +22 -9
- package/src/tools/deferredMcpSearch.js +26 -14
- package/src/tools/deferredMcpSearch.ts +26 -14
- package/src/tools/desktopOpen.js +69 -10
- package/src/tools/desktopOpen.ts +70 -11
- package/src/tools/documentForge.js +167 -82
- package/src/tools/documentForge.ts +169 -84
- package/src/tools/fsEdit.js +101 -30
- package/src/tools/fsEdit.ts +105 -32
- package/src/tools/fsFind.js +8 -5
- package/src/tools/fsFind.ts +7 -4
- package/src/tools/fsRead.js +119 -13
- package/src/tools/fsRead.ts +124 -13
- package/src/tools/fsWrite.js +17 -5
- package/src/tools/fsWrite.ts +17 -7
- package/src/tools/generateMediaViaApi.js +82 -19
- package/src/tools/generateMediaViaApi.ts +80 -19
- package/src/tools/memoryRecall.js +62 -11
- package/src/tools/memoryRecall.ts +70 -13
- package/src/tools/memoryWrite.js +27 -14
- package/src/tools/memoryWrite.ts +30 -14
- package/src/tools/multiEdit.js +100 -30
- package/src/tools/multiEdit.ts +104 -32
- package/src/tools/nativeEngine.js +33 -18
- package/src/tools/nativeEngine.ts +31 -18
- package/src/tools/openSourceToolProvisioner.js +49 -14
- package/src/tools/openSourceToolProvisioner.ts +54 -15
- package/src/tools/osSandbox.js +513 -0
- package/src/tools/osSandbox.ts +588 -0
- package/src/tools/payloadSanitizer.js +40 -8
- package/src/tools/payloadSanitizer.ts +39 -8
- package/src/tools/peerAgent.js +96 -34
- package/src/tools/peerAgent.ts +114 -37
- package/src/tools/persistentShell.js +8 -3
- package/src/tools/pluginsFactory.js +38 -6
- package/src/tools/pluginsFactory.ts +42 -6
- package/src/tools/readNativeSkill.js +10 -8
- package/src/tools/readNativeSkill.ts +10 -8
- package/src/tools/requestDecision.js +10 -3
- package/src/tools/requestDecision.ts +10 -3
- package/src/tools/resolveAutomator.js +91 -17
- package/src/tools/resolveAutomator.ts +102 -17
- package/src/tools/reverseEngineering.js +168 -41
- package/src/tools/reverseEngineering.ts +161 -41
- package/src/tools/subprocessEnv.js +197 -0
- package/src/tools/subprocessEnv.ts +212 -0
- package/src/tools/sysRun.js +108 -35
- package/src/tools/sysRun.ts +119 -31
- package/src/tools/toolDispatch.js +24 -1
- package/src/tools/toolDispatch.ts +21 -1
- package/src/tools/toolOutputStore.js +62 -23
- package/src/tools/toolOutputStore.ts +56 -23
- package/src/tools/toolScanner.js +13 -10
- package/src/tools/toolScanner.ts +13 -10
- package/src/tools/toolSelfRepair.js +94 -28
- package/src/tools/toolSelfRepair.ts +96 -28
- package/src/tools/uiForge.js +71 -6
- package/src/tools/uiForge.ts +74 -6
- package/src/tools/viewImage.js +62 -17
- package/src/tools/viewImage.ts +57 -12
- package/src/tools/visionInspector.js +15 -3
- package/src/tools/visionInspector.ts +15 -3
- package/src/tools/walkerSafety.js +2 -0
- package/src/tools/walkerSafety.ts +1 -0
- package/src/tools/webFetch.js +105 -10
- package/src/tools/webFetch.ts +105 -11
- package/src/tui/banner.js +3 -2
- package/src/tui/banner.ts +3 -2
- package/src/tui/consoleGuard.js +12 -4
- package/src/tui/consoleGuard.ts +9 -4
- package/src/tui/messageInteraction.js +2 -3
- package/src/tui/messageInteraction.ts +2 -3
- package/src/tui/modelPicker.js +1 -0
- package/src/tui/modelPicker.ts +1 -0
- package/src/tui/overlay.js +3 -1
- package/src/tui/overlay.ts +3 -1
- package/src/tui/sessionPicker.js +3 -2
- package/src/tui/sessionPicker.ts +3 -2
- package/src/tui/sessionPickerView.js +7 -1
- package/src/tui/sessionPickerView.ts +6 -1
- package/src/tui/toolOutputNormalizer.js +2 -3
- package/src/tui/toolOutputNormalizer.ts +4 -3
- package/src/tui/viewport.js +4 -4
- package/src/tui/viewport.ts +5 -5
- package/src/tui/workspaceIdentity.js +2 -1
- package/src/tui/workspaceIdentity.ts +2 -1
- package/src/uncertainty.js +8 -3
- package/src/uncertainty.ts +8 -3
- package/src/utils/paths.js +8 -5
- package/src/utils/paths.ts +9 -7
- package/src/vfeTracker.js +9 -3
- package/src/vfeTracker.ts +9 -3
- package/src/config.d.ts +0 -2
- package/src/providers/base.d.ts +0 -94
- package/src/providers/factory.d.ts +0 -1
- package/src/sandbox.d.ts +0 -1
package/src/modelRouter.ts
CHANGED
|
@@ -101,7 +101,7 @@ export function resolveRoute(config: RouterConfig | undefined | null, kind: Rout
|
|
|
101
101
|
};
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
const cheap = UTILITY_MODEL_BY_PROVIDER[provider];
|
|
104
|
+
const cheap = Object.hasOwn(UTILITY_MODEL_BY_PROVIDER, provider) ? UTILITY_MODEL_BY_PROVIDER[provider] : undefined;
|
|
105
105
|
if (!cheap) {
|
|
106
106
|
return { ...stay, reason: `no published utility tier for ${provider}; paying full rate` };
|
|
107
107
|
}
|
package/src/observer/evidence.js
CHANGED
|
@@ -275,7 +275,9 @@ function scanWrite(toolName, args, workspaceRoot, evidence) {
|
|
|
275
275
|
const resolved = resolveTarget(workspaceRoot, rawTarget);
|
|
276
276
|
const base = path.basename(resolved.physical);
|
|
277
277
|
if (!resolved.inside) {
|
|
278
|
-
const
|
|
278
|
+
const tmpRoot = safeRealpath(os.tmpdir());
|
|
279
|
+
const tmpRel = path.relative(tmpRoot, resolved.physical);
|
|
280
|
+
const temporary = tmpRel === '' || (tmpRel !== '..' && !tmpRel.startsWith(`..${path.sep}`) && !path.isAbsolute(tmpRel));
|
|
279
281
|
add(evidence, temporary ? 'write-temporary' : 'write-outside-workspace', temporary ? 'low' : 'high', `${toolName} writes outside the workspace: ${rawTarget}`, 0.82);
|
|
280
282
|
}
|
|
281
283
|
if (resolved.physical.includes(`${path.sep}.git${path.sep}`) || base === '.git') {
|
|
@@ -289,7 +291,7 @@ function scanWrite(toolName, args, workspaceRoot, evidence) {
|
|
|
289
291
|
}
|
|
290
292
|
const originalSize = readSize(resolved.physical);
|
|
291
293
|
let replacementSize = null;
|
|
292
|
-
if (toolName === 'fs_write') {
|
|
294
|
+
if (toolName === 'fs_write' || toolName === 'write_file' || toolName === 'write_to_file' || toolName === 'write') {
|
|
293
295
|
const content = typeof args.content === 'string' ? args.content : typeof args.text === 'string' ? args.text : '';
|
|
294
296
|
replacementSize = Buffer.byteLength(content, 'utf8');
|
|
295
297
|
if (replacementSize === 0 && originalSize !== null && originalSize > 0) {
|
|
@@ -393,7 +395,7 @@ function scanMove(args, workspaceRoot, evidence) {
|
|
|
393
395
|
export function scanMutation(toolName, args, context) {
|
|
394
396
|
const evidence = [];
|
|
395
397
|
const workspaceRoot = context.workspaceRoot || process.cwd();
|
|
396
|
-
if (['sysRun', 'sysRunBg', 'terminal_persistent_shell'].includes(toolName)) {
|
|
398
|
+
if (['sysRun', 'sysRunBg', 'terminal_persistent_shell', 'bash', 'execute'].includes(toolName)) {
|
|
397
399
|
const command = typeof args.command === 'string'
|
|
398
400
|
? args.command
|
|
399
401
|
: typeof args.cmd === 'string' ? args.cmd : '';
|
|
@@ -403,7 +405,11 @@ export function scanMutation(toolName, args, context) {
|
|
|
403
405
|
const command = typeof args.command === 'string' ? `git ${args.command}` : '';
|
|
404
406
|
scanShell(command, workspaceRoot, evidence);
|
|
405
407
|
}
|
|
406
|
-
else if (
|
|
408
|
+
else if ([
|
|
409
|
+
'fs_write', 'fs_edit', 'MultiEdit',
|
|
410
|
+
'write_file', 'write_to_file', 'write',
|
|
411
|
+
'edit_file', 'replace_file_content', 'multi_replace_file_content', 'edit', 'multiedit'
|
|
412
|
+
].includes(toolName)) {
|
|
407
413
|
scanWrite(toolName, args, workspaceRoot, evidence);
|
|
408
414
|
}
|
|
409
415
|
else if (toolName === 'fs_delete') {
|
package/src/observer/evidence.ts
CHANGED
|
@@ -333,7 +333,9 @@ function scanWrite(toolName: string, args: Record<string, unknown>, workspaceRoo
|
|
|
333
333
|
const base = path.basename(resolved.physical);
|
|
334
334
|
|
|
335
335
|
if (!resolved.inside) {
|
|
336
|
-
const
|
|
336
|
+
const tmpRoot = safeRealpath(os.tmpdir());
|
|
337
|
+
const tmpRel = path.relative(tmpRoot, resolved.physical);
|
|
338
|
+
const temporary = tmpRel === '' || (tmpRel !== '..' && !tmpRel.startsWith(`..${path.sep}`) && !path.isAbsolute(tmpRel));
|
|
337
339
|
add(
|
|
338
340
|
evidence,
|
|
339
341
|
temporary ? 'write-temporary' : 'write-outside-workspace',
|
|
@@ -357,7 +359,7 @@ function scanWrite(toolName: string, args: Record<string, unknown>, workspaceRoo
|
|
|
357
359
|
|
|
358
360
|
const originalSize = readSize(resolved.physical);
|
|
359
361
|
let replacementSize: number | null = null;
|
|
360
|
-
if (toolName === 'fs_write') {
|
|
362
|
+
if (toolName === 'fs_write' || toolName === 'write_file' || toolName === 'write_to_file' || toolName === 'write') {
|
|
361
363
|
const content = typeof args.content === 'string' ? args.content : typeof args.text === 'string' ? args.text : '';
|
|
362
364
|
replacementSize = Buffer.byteLength(content, 'utf8');
|
|
363
365
|
if (replacementSize === 0 && originalSize !== null && originalSize > 0) {
|
|
@@ -464,7 +466,7 @@ export function scanMutation(toolName: string, args: Record<string, unknown>, co
|
|
|
464
466
|
const evidence: RiskEvidence[] = [];
|
|
465
467
|
const workspaceRoot = context.workspaceRoot || process.cwd();
|
|
466
468
|
|
|
467
|
-
if (['sysRun', 'sysRunBg', 'terminal_persistent_shell'].includes(toolName)) {
|
|
469
|
+
if (['sysRun', 'sysRunBg', 'terminal_persistent_shell', 'bash', 'execute'].includes(toolName)) {
|
|
468
470
|
const command = typeof args.command === 'string'
|
|
469
471
|
? args.command
|
|
470
472
|
: typeof args.cmd === 'string' ? args.cmd : '';
|
|
@@ -472,7 +474,11 @@ export function scanMutation(toolName: string, args: Record<string, unknown>, co
|
|
|
472
474
|
} else if (toolName === 'gitOp') {
|
|
473
475
|
const command = typeof args.command === 'string' ? `git ${args.command}` : '';
|
|
474
476
|
scanShell(command, workspaceRoot, evidence);
|
|
475
|
-
} else if (
|
|
477
|
+
} else if ([
|
|
478
|
+
'fs_write', 'fs_edit', 'MultiEdit',
|
|
479
|
+
'write_file', 'write_to_file', 'write',
|
|
480
|
+
'edit_file', 'replace_file_content', 'multi_replace_file_content', 'edit', 'multiedit'
|
|
481
|
+
].includes(toolName)) {
|
|
476
482
|
scanWrite(toolName, args, workspaceRoot, evidence);
|
|
477
483
|
} else if (toolName === 'fs_delete') {
|
|
478
484
|
scanDelete(args, workspaceRoot, evidence);
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — Optional OS Keychain Master-Key Backend (Sector 6C)
|
|
3
|
+
*
|
|
4
|
+
* Stores and retrieves the 32-byte AES-256-GCM vault master key in the host
|
|
5
|
+
* operating system's credential store when enabled via `CAUSYNE_KEYCHAIN=on|auto|strict`:
|
|
6
|
+
* - macOS (`darwin`): Keychain Services (`/usr/bin/security -i` via stdin, never argv)
|
|
7
|
+
* - Linux (`linux`): libsecret (`secret-tool` via stdin, never argv)
|
|
8
|
+
* - Windows (`win32`): DPAPI `CurrentUser` encrypted blob (`.vault_key.dpapi` via stdin)
|
|
9
|
+
*
|
|
10
|
+
* Defaults to `'off'` so headless CI runners, containers, and existing `.vault_key`
|
|
11
|
+
* users experience zero unexpected OS prompts.
|
|
12
|
+
*/
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import crypto from 'node:crypto';
|
|
15
|
+
import fs from 'node:fs';
|
|
16
|
+
import path from 'node:path';
|
|
17
|
+
const KEYCHAIN_SERVICE = 'causyne-agent';
|
|
18
|
+
const EXPECTED_KEY_BYTES = 32;
|
|
19
|
+
const EXPECTED_KEY_HEX_LEN = EXPECTED_KEY_BYTES * 2;
|
|
20
|
+
const MACOS_SECURITY_BIN = '/usr/bin/security';
|
|
21
|
+
export function resolveKeychainMode(env = process.env) {
|
|
22
|
+
const raw = typeof env.CAUSYNE_KEYCHAIN === 'string' ? env.CAUSYNE_KEYCHAIN.trim().toLowerCase() : '';
|
|
23
|
+
if (raw === 'strict')
|
|
24
|
+
return 'strict';
|
|
25
|
+
if (raw === 'on' || raw === '1' || raw === 'true')
|
|
26
|
+
return 'on';
|
|
27
|
+
if (raw === 'auto')
|
|
28
|
+
return 'auto';
|
|
29
|
+
return 'off';
|
|
30
|
+
}
|
|
31
|
+
export function keychainAccountForVaultDir(vaultDir) {
|
|
32
|
+
const digest = crypto.createHash('sha256').update(path.resolve(vaultDir)).digest('hex').slice(0, 12);
|
|
33
|
+
return `vault-key-${digest}`;
|
|
34
|
+
}
|
|
35
|
+
function isValidHexKey(raw) {
|
|
36
|
+
return raw.length === EXPECTED_KEY_HEX_LEN && /^[0-9a-fA-F]+$/.test(raw);
|
|
37
|
+
}
|
|
38
|
+
function findInPath(binary, env = process.env) {
|
|
39
|
+
if (!binary || binary.includes('..') || binary.includes('/') || binary.includes('\\') || /[\r\n\0]/.test(binary)) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const pathVal = env.PATH || env.Path || '';
|
|
43
|
+
for (const rawDir of pathVal.split(path.delimiter)) {
|
|
44
|
+
const dir = rawDir.trim();
|
|
45
|
+
if (!dir || !path.isAbsolute(dir))
|
|
46
|
+
continue;
|
|
47
|
+
const full = path.join(dir, binary);
|
|
48
|
+
try {
|
|
49
|
+
fs.accessSync(full, fs.constants.X_OK);
|
|
50
|
+
if (fs.statSync(full).isFile())
|
|
51
|
+
return full;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
export function probeOsKeychain(options = {}) {
|
|
60
|
+
const platform = options.platform ?? process.platform;
|
|
61
|
+
const env = options.env ?? process.env;
|
|
62
|
+
if (platform === 'darwin') {
|
|
63
|
+
const exists = fs.existsSync(MACOS_SECURITY_BIN);
|
|
64
|
+
return {
|
|
65
|
+
backend: exists ? 'macos-keychain' : 'none',
|
|
66
|
+
platform,
|
|
67
|
+
available: exists,
|
|
68
|
+
reason: exists ? 'macOS Keychain Services (/usr/bin/security) available.' : 'macOS security CLI not found.'
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (platform === 'linux') {
|
|
72
|
+
const secretTool = findInPath('secret-tool', env);
|
|
73
|
+
return {
|
|
74
|
+
backend: secretTool ? 'linux-secret-service' : 'none',
|
|
75
|
+
platform,
|
|
76
|
+
available: Boolean(secretTool),
|
|
77
|
+
reason: secretTool ? 'Linux Secret Service (secret-tool) available.' : 'secret-tool not installed.'
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (platform === 'win32') {
|
|
81
|
+
return {
|
|
82
|
+
backend: 'windows-dpapi',
|
|
83
|
+
platform,
|
|
84
|
+
available: true,
|
|
85
|
+
reason: 'Windows DPAPI (CurrentUser scope) available.'
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
backend: 'none',
|
|
90
|
+
platform,
|
|
91
|
+
available: false,
|
|
92
|
+
reason: `No OS keychain backend implemented for platform "${platform}".`
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function dpapiBlobPath(vaultDir) {
|
|
96
|
+
return path.join(path.resolve(vaultDir), '.vault_key.dpapi');
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Loads the 32-byte vault key from the OS credential store, or returns `null` if absent/unavailable.
|
|
100
|
+
*/
|
|
101
|
+
const SAFE_ACCOUNT_PATTERN = /^vault-key-[0-9a-f]{12}$/;
|
|
102
|
+
export function loadVaultKeyFromKeychain(vaultDir, options = {}) {
|
|
103
|
+
if (typeof vaultDir !== 'string' || !vaultDir.trim() || vaultDir.includes('\0'))
|
|
104
|
+
return null;
|
|
105
|
+
const platform = options.platform ?? process.platform;
|
|
106
|
+
const runner = options.spawnSyncImpl ?? spawnSync;
|
|
107
|
+
const account = keychainAccountForVaultDir(vaultDir);
|
|
108
|
+
if (platform === 'darwin') {
|
|
109
|
+
const res = runner(MACOS_SECURITY_BIN, ['find-generic-password', '-s', KEYCHAIN_SERVICE, '-a', account, '-w'], {
|
|
110
|
+
encoding: 'utf8',
|
|
111
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
112
|
+
timeout: 5000,
|
|
113
|
+
maxBuffer: 1024 * 1024,
|
|
114
|
+
windowsHide: true,
|
|
115
|
+
shell: false
|
|
116
|
+
});
|
|
117
|
+
if (!res || res.error || res.status !== 0 || typeof res.stdout !== 'string')
|
|
118
|
+
return null;
|
|
119
|
+
const hex = res.stdout.trim();
|
|
120
|
+
return isValidHexKey(hex) ? Buffer.from(hex, 'hex') : null;
|
|
121
|
+
}
|
|
122
|
+
if (platform === 'linux') {
|
|
123
|
+
const res = runner('secret-tool', ['lookup', 'service', KEYCHAIN_SERVICE, 'account', account], {
|
|
124
|
+
encoding: 'utf8',
|
|
125
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
126
|
+
timeout: 5000,
|
|
127
|
+
maxBuffer: 1024 * 1024,
|
|
128
|
+
windowsHide: true,
|
|
129
|
+
shell: false
|
|
130
|
+
});
|
|
131
|
+
if (res.status !== 0 || typeof res.stdout !== 'string')
|
|
132
|
+
return null;
|
|
133
|
+
const hex = res.stdout.trim();
|
|
134
|
+
return isValidHexKey(hex) ? Buffer.from(hex, 'hex') : null;
|
|
135
|
+
}
|
|
136
|
+
if (platform === 'win32') {
|
|
137
|
+
const blobFile = dpapiBlobPath(vaultDir);
|
|
138
|
+
if (!fs.existsSync(blobFile))
|
|
139
|
+
return null;
|
|
140
|
+
try {
|
|
141
|
+
const stat = fs.lstatSync(blobFile);
|
|
142
|
+
if (stat.isSymbolicLink() || !stat.isFile() || stat.size === 0 || stat.size > 65_536)
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
const protectedBase64 = fs.readFileSync(blobFile, 'utf8').trim();
|
|
149
|
+
if (!protectedBase64 || !/^[A-Za-z0-9+/=]+$/.test(protectedBase64))
|
|
150
|
+
return null;
|
|
151
|
+
const script = [
|
|
152
|
+
'Add-Type -AssemblyName System.Security',
|
|
153
|
+
'$b64 = [Console]::In.ReadToEnd().Trim()',
|
|
154
|
+
'$enc = [Convert]::FromBase64String($b64)',
|
|
155
|
+
'$plain = [System.Security.Cryptography.ProtectedData]::Unprotect($enc, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)',
|
|
156
|
+
'[Console]::Out.Write([System.Text.Encoding]::UTF8.GetString($plain))'
|
|
157
|
+
].join('; ');
|
|
158
|
+
const res = runner('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
159
|
+
input: protectedBase64,
|
|
160
|
+
encoding: 'utf8',
|
|
161
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
162
|
+
timeout: 8000,
|
|
163
|
+
maxBuffer: 1024 * 1024,
|
|
164
|
+
windowsHide: true,
|
|
165
|
+
shell: false
|
|
166
|
+
});
|
|
167
|
+
if (res.status !== 0 || typeof res.stdout !== 'string')
|
|
168
|
+
return null;
|
|
169
|
+
const hex = res.stdout.trim();
|
|
170
|
+
return isValidHexKey(hex) ? Buffer.from(hex, 'hex') : null;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Persists a 32-byte vault key into the OS credential store without passing
|
|
176
|
+
* secret material on command-line argv (always delivered via stdin).
|
|
177
|
+
*/
|
|
178
|
+
export function saveVaultKeyToKeychain(vaultDir, key, options = {}) {
|
|
179
|
+
if (typeof vaultDir !== 'string' || !vaultDir.trim() || vaultDir.includes('\0'))
|
|
180
|
+
return false;
|
|
181
|
+
if (!Buffer.isBuffer(key) || key.length !== EXPECTED_KEY_BYTES)
|
|
182
|
+
return false;
|
|
183
|
+
const platform = options.platform ?? process.platform;
|
|
184
|
+
const runner = options.spawnSyncImpl ?? spawnSync;
|
|
185
|
+
const account = keychainAccountForVaultDir(vaultDir);
|
|
186
|
+
const hexKey = key.toString('hex');
|
|
187
|
+
if (!SAFE_ACCOUNT_PATTERN.test(account) || !isValidHexKey(hexKey))
|
|
188
|
+
return false;
|
|
189
|
+
if (platform === 'darwin') {
|
|
190
|
+
// Use interactive `-i` stdin mode so the hex key never appears in process argv (`ps`)
|
|
191
|
+
const stdinCmd = `add-generic-password -U -s "${KEYCHAIN_SERVICE}" -a "${account}" -w "${hexKey}"\n`;
|
|
192
|
+
const res = runner(MACOS_SECURITY_BIN, ['-i'], {
|
|
193
|
+
input: stdinCmd,
|
|
194
|
+
encoding: 'utf8',
|
|
195
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
196
|
+
timeout: 5000,
|
|
197
|
+
maxBuffer: 1024 * 1024,
|
|
198
|
+
windowsHide: true,
|
|
199
|
+
shell: false
|
|
200
|
+
});
|
|
201
|
+
return Boolean(res && !res.error && res.status === 0);
|
|
202
|
+
}
|
|
203
|
+
if (platform === 'linux') {
|
|
204
|
+
const res = runner('secret-tool', ['store', '--label', 'Causyne Agent Vault Key', 'service', KEYCHAIN_SERVICE, 'account', account], {
|
|
205
|
+
input: hexKey,
|
|
206
|
+
encoding: 'utf8',
|
|
207
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
208
|
+
timeout: 5000,
|
|
209
|
+
maxBuffer: 1024 * 1024,
|
|
210
|
+
windowsHide: true,
|
|
211
|
+
shell: false
|
|
212
|
+
});
|
|
213
|
+
return Boolean(res && !res.error && res.status === 0);
|
|
214
|
+
}
|
|
215
|
+
if (platform === 'win32') {
|
|
216
|
+
const script = [
|
|
217
|
+
'Add-Type -AssemblyName System.Security',
|
|
218
|
+
'$hex = [Console]::In.ReadToEnd().Trim()',
|
|
219
|
+
'$bytes = [System.Text.Encoding]::UTF8.GetBytes($hex)',
|
|
220
|
+
'$enc = [System.Security.Cryptography.ProtectedData]::Protect($bytes, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)',
|
|
221
|
+
'[Console]::Out.Write([Convert]::ToBase64String($enc))'
|
|
222
|
+
].join('; ');
|
|
223
|
+
const res = runner('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
224
|
+
input: hexKey,
|
|
225
|
+
encoding: 'utf8',
|
|
226
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
227
|
+
timeout: 8000,
|
|
228
|
+
maxBuffer: 1024 * 1024,
|
|
229
|
+
windowsHide: true,
|
|
230
|
+
shell: false
|
|
231
|
+
});
|
|
232
|
+
if (res.status !== 0 || typeof res.stdout !== 'string' || !res.stdout.trim())
|
|
233
|
+
return false;
|
|
234
|
+
const blobFile = dpapiBlobPath(vaultDir);
|
|
235
|
+
const dir = path.dirname(blobFile);
|
|
236
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
237
|
+
try {
|
|
238
|
+
const existing = fs.lstatSync(blobFile);
|
|
239
|
+
if (existing.isSymbolicLink() || !existing.isFile())
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
catch (err) {
|
|
243
|
+
const code = typeof err === 'object' && err !== null && 'code' in err ? String(err.code) : '';
|
|
244
|
+
if (code !== 'ENOENT')
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
const tempFile = path.join(dir, `.vault_key.dpapi.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`);
|
|
248
|
+
try {
|
|
249
|
+
fs.writeFileSync(tempFile, `${res.stdout.trim()}\n`, { encoding: 'utf8', mode: 0o600, flag: 'wx' });
|
|
250
|
+
fs.renameSync(tempFile, blobFile);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
try {
|
|
254
|
+
if (fs.existsSync(tempFile))
|
|
255
|
+
fs.unlinkSync(tempFile);
|
|
256
|
+
}
|
|
257
|
+
catch { /* ignore */ }
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — Optional OS Keychain Master-Key Backend (Sector 6C)
|
|
3
|
+
*
|
|
4
|
+
* Stores and retrieves the 32-byte AES-256-GCM vault master key in the host
|
|
5
|
+
* operating system's credential store when enabled via `CAUSYNE_KEYCHAIN=on|auto|strict`:
|
|
6
|
+
* - macOS (`darwin`): Keychain Services (`/usr/bin/security -i` via stdin, never argv)
|
|
7
|
+
* - Linux (`linux`): libsecret (`secret-tool` via stdin, never argv)
|
|
8
|
+
* - Windows (`win32`): DPAPI `CurrentUser` encrypted blob (`.vault_key.dpapi` via stdin)
|
|
9
|
+
*
|
|
10
|
+
* Defaults to `'off'` so headless CI runners, containers, and existing `.vault_key`
|
|
11
|
+
* users experience zero unexpected OS prompts.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { spawnSync, type SpawnSyncReturns } from 'node:child_process';
|
|
15
|
+
import crypto from 'node:crypto';
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
|
|
19
|
+
export type OsKeychainMode = 'off' | 'auto' | 'on' | 'strict';
|
|
20
|
+
export type OsKeychainBackend = 'macos-keychain' | 'linux-secret-service' | 'windows-dpapi' | 'none';
|
|
21
|
+
|
|
22
|
+
export interface OsKeychainCapabilities {
|
|
23
|
+
backend: OsKeychainBackend;
|
|
24
|
+
platform: NodeJS.Platform;
|
|
25
|
+
available: boolean;
|
|
26
|
+
reason: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface KeychainSpawnOptions {
|
|
30
|
+
platform?: NodeJS.Platform | undefined;
|
|
31
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
32
|
+
spawnSyncImpl?: ((command: string, args: readonly string[], options: Record<string, unknown>) => SpawnSyncReturns<string>) | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const KEYCHAIN_SERVICE = 'causyne-agent';
|
|
36
|
+
const EXPECTED_KEY_BYTES = 32;
|
|
37
|
+
const EXPECTED_KEY_HEX_LEN = EXPECTED_KEY_BYTES * 2;
|
|
38
|
+
const MACOS_SECURITY_BIN = '/usr/bin/security';
|
|
39
|
+
|
|
40
|
+
export function resolveKeychainMode(env: NodeJS.ProcessEnv = process.env): OsKeychainMode {
|
|
41
|
+
const raw = typeof env.CAUSYNE_KEYCHAIN === 'string' ? env.CAUSYNE_KEYCHAIN.trim().toLowerCase() : '';
|
|
42
|
+
if (raw === 'strict') return 'strict';
|
|
43
|
+
if (raw === 'on' || raw === '1' || raw === 'true') return 'on';
|
|
44
|
+
if (raw === 'auto') return 'auto';
|
|
45
|
+
return 'off';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function keychainAccountForVaultDir(vaultDir: string): string {
|
|
49
|
+
const digest = crypto.createHash('sha256').update(path.resolve(vaultDir)).digest('hex').slice(0, 12);
|
|
50
|
+
return `vault-key-${digest}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isValidHexKey(raw: string): boolean {
|
|
54
|
+
return raw.length === EXPECTED_KEY_HEX_LEN && /^[0-9a-fA-F]+$/.test(raw);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function findInPath(binary: string, env: NodeJS.ProcessEnv = process.env): string | null {
|
|
58
|
+
if (!binary || binary.includes('..') || binary.includes('/') || binary.includes('\\') || /[\r\n\0]/.test(binary)) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const pathVal = env.PATH || env.Path || '';
|
|
62
|
+
for (const rawDir of pathVal.split(path.delimiter)) {
|
|
63
|
+
const dir = rawDir.trim();
|
|
64
|
+
if (!dir || !path.isAbsolute(dir)) continue;
|
|
65
|
+
const full = path.join(dir, binary);
|
|
66
|
+
try {
|
|
67
|
+
fs.accessSync(full, fs.constants.X_OK);
|
|
68
|
+
if (fs.statSync(full).isFile()) return full;
|
|
69
|
+
} catch {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function probeOsKeychain(options: KeychainSpawnOptions = {}): OsKeychainCapabilities {
|
|
77
|
+
const platform = options.platform ?? process.platform;
|
|
78
|
+
const env = options.env ?? process.env;
|
|
79
|
+
|
|
80
|
+
if (platform === 'darwin') {
|
|
81
|
+
const exists = fs.existsSync(MACOS_SECURITY_BIN);
|
|
82
|
+
return {
|
|
83
|
+
backend: exists ? 'macos-keychain' : 'none',
|
|
84
|
+
platform,
|
|
85
|
+
available: exists,
|
|
86
|
+
reason: exists ? 'macOS Keychain Services (/usr/bin/security) available.' : 'macOS security CLI not found.'
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (platform === 'linux') {
|
|
91
|
+
const secretTool = findInPath('secret-tool', env);
|
|
92
|
+
return {
|
|
93
|
+
backend: secretTool ? 'linux-secret-service' : 'none',
|
|
94
|
+
platform,
|
|
95
|
+
available: Boolean(secretTool),
|
|
96
|
+
reason: secretTool ? 'Linux Secret Service (secret-tool) available.' : 'secret-tool not installed.'
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (platform === 'win32') {
|
|
101
|
+
return {
|
|
102
|
+
backend: 'windows-dpapi',
|
|
103
|
+
platform,
|
|
104
|
+
available: true,
|
|
105
|
+
reason: 'Windows DPAPI (CurrentUser scope) available.'
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
backend: 'none',
|
|
111
|
+
platform,
|
|
112
|
+
available: false,
|
|
113
|
+
reason: `No OS keychain backend implemented for platform "${platform}".`
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function dpapiBlobPath(vaultDir: string): string {
|
|
118
|
+
return path.join(path.resolve(vaultDir), '.vault_key.dpapi');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Loads the 32-byte vault key from the OS credential store, or returns `null` if absent/unavailable.
|
|
123
|
+
*/
|
|
124
|
+
const SAFE_ACCOUNT_PATTERN = /^vault-key-[0-9a-f]{12}$/;
|
|
125
|
+
|
|
126
|
+
export function loadVaultKeyFromKeychain(
|
|
127
|
+
vaultDir: string,
|
|
128
|
+
options: KeychainSpawnOptions = {}
|
|
129
|
+
): Buffer | null {
|
|
130
|
+
if (typeof vaultDir !== 'string' || !vaultDir.trim() || vaultDir.includes('\0')) return null;
|
|
131
|
+
const platform = options.platform ?? process.platform;
|
|
132
|
+
const runner = options.spawnSyncImpl ?? (spawnSync as unknown as NonNullable<KeychainSpawnOptions['spawnSyncImpl']>);
|
|
133
|
+
const account = keychainAccountForVaultDir(vaultDir);
|
|
134
|
+
|
|
135
|
+
if (platform === 'darwin') {
|
|
136
|
+
const res = runner(MACOS_SECURITY_BIN, ['find-generic-password', '-s', KEYCHAIN_SERVICE, '-a', account, '-w'], {
|
|
137
|
+
encoding: 'utf8',
|
|
138
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
139
|
+
timeout: 5000,
|
|
140
|
+
maxBuffer: 1024 * 1024,
|
|
141
|
+
windowsHide: true,
|
|
142
|
+
shell: false
|
|
143
|
+
});
|
|
144
|
+
if (!res || res.error || res.status !== 0 || typeof res.stdout !== 'string') return null;
|
|
145
|
+
const hex = res.stdout.trim();
|
|
146
|
+
return isValidHexKey(hex) ? Buffer.from(hex, 'hex') : null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (platform === 'linux') {
|
|
150
|
+
const res = runner('secret-tool', ['lookup', 'service', KEYCHAIN_SERVICE, 'account', account], {
|
|
151
|
+
encoding: 'utf8',
|
|
152
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
153
|
+
timeout: 5000,
|
|
154
|
+
maxBuffer: 1024 * 1024,
|
|
155
|
+
windowsHide: true,
|
|
156
|
+
shell: false
|
|
157
|
+
});
|
|
158
|
+
if (res.status !== 0 || typeof res.stdout !== 'string') return null;
|
|
159
|
+
const hex = res.stdout.trim();
|
|
160
|
+
return isValidHexKey(hex) ? Buffer.from(hex, 'hex') : null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (platform === 'win32') {
|
|
164
|
+
const blobFile = dpapiBlobPath(vaultDir);
|
|
165
|
+
if (!fs.existsSync(blobFile)) return null;
|
|
166
|
+
try {
|
|
167
|
+
const stat = fs.lstatSync(blobFile);
|
|
168
|
+
if (stat.isSymbolicLink() || !stat.isFile() || stat.size === 0 || stat.size > 65_536) return null;
|
|
169
|
+
} catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
const protectedBase64 = fs.readFileSync(blobFile, 'utf8').trim();
|
|
173
|
+
if (!protectedBase64 || !/^[A-Za-z0-9+/=]+$/.test(protectedBase64)) return null;
|
|
174
|
+
const script = [
|
|
175
|
+
'Add-Type -AssemblyName System.Security',
|
|
176
|
+
'$b64 = [Console]::In.ReadToEnd().Trim()',
|
|
177
|
+
'$enc = [Convert]::FromBase64String($b64)',
|
|
178
|
+
'$plain = [System.Security.Cryptography.ProtectedData]::Unprotect($enc, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)',
|
|
179
|
+
'[Console]::Out.Write([System.Text.Encoding]::UTF8.GetString($plain))'
|
|
180
|
+
].join('; ');
|
|
181
|
+
const res = runner('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
182
|
+
input: protectedBase64,
|
|
183
|
+
encoding: 'utf8',
|
|
184
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
185
|
+
timeout: 8000,
|
|
186
|
+
maxBuffer: 1024 * 1024,
|
|
187
|
+
windowsHide: true,
|
|
188
|
+
shell: false
|
|
189
|
+
});
|
|
190
|
+
if (res.status !== 0 || typeof res.stdout !== 'string') return null;
|
|
191
|
+
const hex = res.stdout.trim();
|
|
192
|
+
return isValidHexKey(hex) ? Buffer.from(hex, 'hex') : null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Persists a 32-byte vault key into the OS credential store without passing
|
|
200
|
+
* secret material on command-line argv (always delivered via stdin).
|
|
201
|
+
*/
|
|
202
|
+
export function saveVaultKeyToKeychain(
|
|
203
|
+
vaultDir: string,
|
|
204
|
+
key: Buffer,
|
|
205
|
+
options: KeychainSpawnOptions = {}
|
|
206
|
+
): boolean {
|
|
207
|
+
if (typeof vaultDir !== 'string' || !vaultDir.trim() || vaultDir.includes('\0')) return false;
|
|
208
|
+
if (!Buffer.isBuffer(key) || key.length !== EXPECTED_KEY_BYTES) return false;
|
|
209
|
+
const platform = options.platform ?? process.platform;
|
|
210
|
+
const runner = options.spawnSyncImpl ?? (spawnSync as unknown as NonNullable<KeychainSpawnOptions['spawnSyncImpl']>);
|
|
211
|
+
const account = keychainAccountForVaultDir(vaultDir);
|
|
212
|
+
const hexKey = key.toString('hex');
|
|
213
|
+
if (!SAFE_ACCOUNT_PATTERN.test(account) || !isValidHexKey(hexKey)) return false;
|
|
214
|
+
|
|
215
|
+
if (platform === 'darwin') {
|
|
216
|
+
// Use interactive `-i` stdin mode so the hex key never appears in process argv (`ps`)
|
|
217
|
+
const stdinCmd = `add-generic-password -U -s "${KEYCHAIN_SERVICE}" -a "${account}" -w "${hexKey}"\n`;
|
|
218
|
+
const res = runner(MACOS_SECURITY_BIN, ['-i'], {
|
|
219
|
+
input: stdinCmd,
|
|
220
|
+
encoding: 'utf8',
|
|
221
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
222
|
+
timeout: 5000,
|
|
223
|
+
maxBuffer: 1024 * 1024,
|
|
224
|
+
windowsHide: true,
|
|
225
|
+
shell: false
|
|
226
|
+
});
|
|
227
|
+
return Boolean(res && !res.error && res.status === 0);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (platform === 'linux') {
|
|
231
|
+
const res = runner(
|
|
232
|
+
'secret-tool',
|
|
233
|
+
['store', '--label', 'Causyne Agent Vault Key', 'service', KEYCHAIN_SERVICE, 'account', account],
|
|
234
|
+
{
|
|
235
|
+
input: hexKey,
|
|
236
|
+
encoding: 'utf8',
|
|
237
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
238
|
+
timeout: 5000,
|
|
239
|
+
maxBuffer: 1024 * 1024,
|
|
240
|
+
windowsHide: true,
|
|
241
|
+
shell: false
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
return Boolean(res && !res.error && res.status === 0);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (platform === 'win32') {
|
|
248
|
+
const script = [
|
|
249
|
+
'Add-Type -AssemblyName System.Security',
|
|
250
|
+
'$hex = [Console]::In.ReadToEnd().Trim()',
|
|
251
|
+
'$bytes = [System.Text.Encoding]::UTF8.GetBytes($hex)',
|
|
252
|
+
'$enc = [System.Security.Cryptography.ProtectedData]::Protect($bytes, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)',
|
|
253
|
+
'[Console]::Out.Write([Convert]::ToBase64String($enc))'
|
|
254
|
+
].join('; ');
|
|
255
|
+
const res = runner('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
256
|
+
input: hexKey,
|
|
257
|
+
encoding: 'utf8',
|
|
258
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
259
|
+
timeout: 8000,
|
|
260
|
+
maxBuffer: 1024 * 1024,
|
|
261
|
+
windowsHide: true,
|
|
262
|
+
shell: false
|
|
263
|
+
});
|
|
264
|
+
if (res.status !== 0 || typeof res.stdout !== 'string' || !res.stdout.trim()) return false;
|
|
265
|
+
const blobFile = dpapiBlobPath(vaultDir);
|
|
266
|
+
const dir = path.dirname(blobFile);
|
|
267
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
268
|
+
try {
|
|
269
|
+
const existing = fs.lstatSync(blobFile);
|
|
270
|
+
if (existing.isSymbolicLink() || !existing.isFile()) return false;
|
|
271
|
+
} catch (err: unknown) {
|
|
272
|
+
const code = typeof err === 'object' && err !== null && 'code' in err ? String(err.code) : '';
|
|
273
|
+
if (code !== 'ENOENT') return false;
|
|
274
|
+
}
|
|
275
|
+
const tempFile = path.join(dir, `.vault_key.dpapi.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`);
|
|
276
|
+
try {
|
|
277
|
+
fs.writeFileSync(tempFile, `${res.stdout.trim()}\n`, { encoding: 'utf8', mode: 0o600, flag: 'wx' });
|
|
278
|
+
fs.renameSync(tempFile, blobFile);
|
|
279
|
+
} catch {
|
|
280
|
+
try { if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile); } catch { /* ignore */ }
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return false;
|
|
287
|
+
}
|
package/src/perception.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import os from 'node:os';
|
|
4
|
+
import { resolveUserPath } from './utils/paths.js';
|
|
4
5
|
const SKIP_DIRECTORIES = new Set([
|
|
5
6
|
'node_modules', '.git', 'dist', 'build', 'out', 'target',
|
|
6
7
|
'.next', '.nuxt', '.venv', 'venv', '__pycache__', '.gradle',
|
|
@@ -8,7 +9,7 @@ const SKIP_DIRECTORIES = new Set([
|
|
|
8
9
|
]);
|
|
9
10
|
export class WorkspacePerception {
|
|
10
11
|
async buildContext(workspacePath) {
|
|
11
|
-
const resolvedPath = workspacePath
|
|
12
|
+
const resolvedPath = resolveUserPath(workspacePath);
|
|
12
13
|
const context = {
|
|
13
14
|
cwd: resolvedPath,
|
|
14
15
|
projectName: path.basename(resolvedPath)
|
|
@@ -363,7 +364,7 @@ export class WorkspacePerception {
|
|
|
363
364
|
return null;
|
|
364
365
|
}
|
|
365
366
|
async detectGitInfo(dir) {
|
|
366
|
-
let current = dir;
|
|
367
|
+
let current = path.resolve(dir);
|
|
367
368
|
for (let depth = 0; depth < 5; depth++) {
|
|
368
369
|
const gitDir = path.join(current, '.git');
|
|
369
370
|
try {
|