@lsamien/yaoyao 0.4.76 → 0.4.78
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 +7 -5
- package/THIRD_PARTY_NOTICES.md +8 -0
- package/bin/hermes-yaoyao.mjs +15 -2
- package/build-info.json +35 -3
- package/desktop/main.mjs +12 -4
- package/dist/assets/{AboutDialog-fbm5CG09.js → AboutDialog-jvhTP8-J.js} +2 -2
- package/dist/assets/AboutDialog-jvhTP8-J.js.map +1 -0
- package/dist/assets/BotAutomationsView-ktoci79V.js +2 -0
- package/dist/assets/{BotAutomationsView-DPAAYyuy.js.map → BotAutomationsView-ktoci79V.js.map} +1 -1
- package/dist/assets/{BotPluginsDialog-MwGpwjgu.js → BotPluginsDialog-H85RI-7P.js} +2 -2
- package/dist/assets/{BotPluginsDialog-MwGpwjgu.js.map → BotPluginsDialog-H85RI-7P.js.map} +1 -1
- package/dist/assets/{BotSettingsDialog-DXtsSzi8.js → BotSettingsDialog-Ci_DZL4m.js} +5 -5
- package/dist/assets/{BotSettingsDialog-DXtsSzi8.js.map → BotSettingsDialog-Ci_DZL4m.js.map} +1 -1
- package/dist/assets/ChatView-6cdLMsuy.js +2 -0
- package/dist/assets/ChatView-6cdLMsuy.js.map +1 -0
- package/dist/assets/ChatView-C_7_7olu.css +1 -0
- package/dist/assets/{ComputerPanel-B0QyKQkF.js → ComputerPanel-Dbwgx5n8.js} +2 -2
- package/dist/assets/{ComputerPanel-B0QyKQkF.js.map → ComputerPanel-Dbwgx5n8.js.map} +1 -1
- package/dist/assets/{ComputerViewerView-CaOkTXpc.js → ComputerViewerView-csnhRedK.js} +2 -2
- package/dist/assets/{ComputerViewerView-CaOkTXpc.js.map → ComputerViewerView-csnhRedK.js.map} +1 -1
- package/dist/assets/ConversationsView-CEO2SVGT.js +6 -0
- package/dist/assets/ConversationsView-CEO2SVGT.js.map +1 -0
- package/dist/assets/{ConversationsView-C_VZl5g2.css → ConversationsView-CHLGKsJt.css} +1 -1
- package/dist/assets/FilesView-t5jA-H2-.js +2 -0
- package/dist/assets/{FilesView-CluNyIk8.js.map → FilesView-t5jA-H2-.js.map} +1 -1
- package/dist/assets/FloatingResourceSearch-3GOuDkuO.css +1 -0
- package/dist/assets/FloatingResourceSearch-BAYymuJV.js +9 -0
- package/dist/assets/FloatingResourceSearch-BAYymuJV.js.map +1 -0
- package/dist/assets/HistoryView-BqMSV9iC.js +2 -0
- package/dist/assets/HistoryView-BqMSV9iC.js.map +1 -0
- package/dist/assets/HistoryView-DlVfSP_p.css +1 -0
- package/dist/assets/KanbanView-CWwrwH9S.js +2 -0
- package/dist/assets/{KanbanView-D5AdRwqi.js.map → KanbanView-CWwrwH9S.js.map} +1 -1
- package/dist/assets/{LocalVmSettingsPanel-BILcGHmu.js → LocalVmSettingsPanel-BXvZwFQP.js} +2 -2
- package/dist/assets/{LocalVmSettingsPanel-BILcGHmu.js.map → LocalVmSettingsPanel-BXvZwFQP.js.map} +1 -1
- package/dist/assets/{RunnerSettingsPanel-IHLAFIh8.js → RunnerSettingsPanel-KkpIkkob.js} +2 -2
- package/dist/assets/{RunnerSettingsPanel-IHLAFIh8.js.map → RunnerSettingsPanel-KkpIkkob.js.map} +1 -1
- package/dist/assets/{StandaloneDialog-DdcL_wV2.js → StandaloneDialog-D4BQqjpB.js} +2 -2
- package/dist/assets/{StandaloneDialog-DdcL_wV2.js.map → StandaloneDialog-D4BQqjpB.js.map} +1 -1
- package/dist/assets/{UpdateCheckDialog-CJQezkXo.js → UpdateCheckDialog-Ct0bT6xU.js} +2 -2
- package/dist/assets/{UpdateCheckDialog-CJQezkXo.js.map → UpdateCheckDialog-Ct0bT6xU.js.map} +1 -1
- package/dist/assets/{WorkspaceKnowledgePanel-DS0kOQgg.js → WorkspaceKnowledgePanel-WBcPB49x.js} +2 -2
- package/dist/assets/{WorkspaceKnowledgePanel-DS0kOQgg.js.map → WorkspaceKnowledgePanel-WBcPB49x.js.map} +1 -1
- package/dist/assets/{WorkspaceRoutinesPanel-DpVAtnDX.js → WorkspaceRoutinesPanel-JubUwGsV.js} +2 -2
- package/dist/assets/{WorkspaceRoutinesPanel-DpVAtnDX.js.map → WorkspaceRoutinesPanel-JubUwGsV.js.map} +1 -1
- package/dist/assets/WorkspaceShell-0m4gg6AW.css +1 -0
- package/dist/assets/WorkspaceShell-Do2VAwIz.js +44 -0
- package/dist/assets/WorkspaceShell-Do2VAwIz.js.map +1 -0
- package/dist/assets/{WorkspaceView-Cco3Umfn.js → WorkspaceView-DTanioWW.js} +2 -2
- package/dist/assets/{WorkspaceView-Cco3Umfn.js.map → WorkspaceView-DTanioWW.js.map} +1 -1
- package/dist/assets/{docx-preview-C6qrP-g2.js → docx-preview-B1SjJgD_.js} +2 -2
- package/dist/assets/{docx-preview-C6qrP-g2.js.map → docx-preview-B1SjJgD_.js.map} +1 -1
- package/dist/assets/index-CWamgtSb.js +17 -0
- package/dist/assets/index-CWamgtSb.js.map +1 -0
- package/dist/assets/{pdf-G3DUsfMb.js → pdf-DVDddcFC.js} +2 -2
- package/dist/assets/{pdf-G3DUsfMb.js.map → pdf-DVDddcFC.js.map} +1 -1
- package/dist/assets/sessionOwnership-0hdKcwOs.js +4 -0
- package/dist/assets/sessionOwnership-0hdKcwOs.js.map +1 -0
- package/dist/assets/{sessionOwnership-BTz0Fyp5.css → sessionOwnership-CfO8CfmZ.css} +1 -1
- package/dist/assets/{viewModels-DfaKvI0x.js → viewModels-DKBqaDRg.js} +12 -12
- package/dist/assets/{viewModels-DfaKvI0x.js.map → viewModels-DKBqaDRg.js.map} +1 -1
- package/dist/index.html +1 -1
- package/dist-server/runner/agent.js +15 -4
- package/dist-server/runner/hermes-bots-bridge/README.md +4 -2
- package/dist-server/runner/hermes-bots-bridge/bridge_runtime.py +21 -2
- package/dist-server/runner/hermes-bots-bridge/plugin.yaml +1 -1
- package/dist-server/runner/worker/gateway.js +52 -22
- package/dist-server/runner/worker/profileSession.js +2 -12
- package/dist-server/server/app.js +15 -1
- package/dist-server/server/chatTranscriptApi.js +2 -2
- package/dist-server/server/chatTranscriptStore.js +16 -0
- package/dist-server/server/config.js +3 -0
- package/dist-server/server/credentialVault/broker.js +272 -0
- package/dist-server/server/credentialVault/coordinator.js +297 -0
- package/dist-server/server/credentialVault/executor.js +1 -0
- package/dist-server/server/credentialVault/fixtureWebsiteExecutor.js +51 -0
- package/dist-server/server/credentialVault/index.js +49 -0
- package/dist-server/server/credentialVault/isolation.js +37 -0
- package/dist-server/server/credentialVault/privateFiles.js +17 -0
- package/dist-server/server/credentialVault/protectedExecutor.js +21 -0
- package/dist-server/server/credentialVault/schema.js +45 -0
- package/dist-server/server/credentialVault/sshExecutor.js +135 -0
- package/dist-server/server/credentialVault/store.js +308 -0
- package/dist-server/server/credentialVault/transport.js +127 -0
- package/dist-server/server/credentialVault/websiteExecutor.js +102 -0
- package/dist-server/server/desktopEnvironments.js +2 -2
- package/dist-server/server/hermesRuntimeEnvironment.js +14 -0
- package/dist-server/server/localAuth.js +5 -0
- package/dist-server/server/openVikingMemoryProvider.js +29 -10
- package/dist-server/server/openVikingService.js +44 -14
- package/dist-server/server/openVikingSessionSync.js +148 -37
- package/dist-server/server/pushCoordinator.js +7 -4
- package/dist-server/server/realtimeApi.js +11 -2
- package/dist-server/server/realtimeBroker.js +1 -1
- package/dist-server/server/realtimeReceipts.js +39 -8
- package/dist-server/server/routes.js +18 -0
- package/dist-server/server/runnerHub.js +17 -4
- package/dist-server/server/sessionStop.js +19 -0
- package/dist-server/server/subaccountAccess.js +12 -0
- package/dist-server/server/taskCoordinator.js +3 -1
- package/dist-server/server/unreadCenter.js +61 -0
- package/dist-server/server/uploads.js +4 -1
- package/dist-server/server/workspaceAssets.js +56 -12
- package/dist-server/server/workspaceEnvironment.js +1 -0
- package/dist-server/server/workspaceMemorySynthesis.js +5 -1
- package/dist-server/server/workspacePrompt.js +5 -1
- package/dist-server/server/workspaceRoutes.js +26 -21
- package/dist-server/server/workspaceRuntime.js +89 -8
- package/dist-server/server/workspaceScheduler.js +5 -1
- package/dist-server/server/workspaceStore.js +125 -47
- package/dist-server/server/workspaceSync.js +4 -1
- package/dist-server/shared/botEnvironment.js +3 -3
- package/dist-server/shared/credentialVault.js +2 -0
- package/dist-server/shared/unread.js +6 -0
- package/dist-server/shared/uploadNames.js +14 -0
- package/licenses/libsodium.js/LICENSE +16 -0
- package/licenses/ssh2/LICENSE +19 -0
- package/package.json +8 -5
- package/release.json +3 -3
- package/dist/assets/AboutDialog-fbm5CG09.js.map +0 -1
- package/dist/assets/BotAutomationsView-DPAAYyuy.js +0 -2
- package/dist/assets/ChatView-BNqHa6Ma.js +0 -2
- package/dist/assets/ChatView-BNqHa6Ma.js.map +0 -1
- package/dist/assets/ChatView-Dx8YlcPr.css +0 -1
- package/dist/assets/ConversationsView-CYNc59hv.js +0 -6
- package/dist/assets/ConversationsView-CYNc59hv.js.map +0 -1
- package/dist/assets/FilesView-CluNyIk8.js +0 -2
- package/dist/assets/FloatingResourceSearch-BF2XOLd-.css +0 -1
- package/dist/assets/FloatingResourceSearch-Bg5rUZMy.js +0 -9
- package/dist/assets/FloatingResourceSearch-Bg5rUZMy.js.map +0 -1
- package/dist/assets/HistoryView-DSbSDzsB.js +0 -2
- package/dist/assets/HistoryView-DSbSDzsB.js.map +0 -1
- package/dist/assets/HistoryView-DvuCAjlo.css +0 -1
- package/dist/assets/KanbanView-D5AdRwqi.js +0 -2
- package/dist/assets/WorkspaceShell-7wn8iTne.css +0 -1
- package/dist/assets/WorkspaceShell-Bykfy3AR.js +0 -44
- package/dist/assets/WorkspaceShell-Bykfy3AR.js.map +0 -1
- package/dist/assets/index-CEqMqiJr.js +0 -17
- package/dist/assets/index-CEqMqiJr.js.map +0 -1
- package/dist/assets/sessionOwnership-DMfBRINe.js +0 -6
- package/dist/assets/sessionOwnership-DMfBRINe.js.map +0 -1
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants, openSync, closeSync, fsyncSync, fstatSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { HttpError } from '../errors.js';
|
|
6
|
+
import { CredentialVaultStore } from './store.js';
|
|
7
|
+
import { parse, leaseInput } from './schema.js';
|
|
8
|
+
export class CredentialVaultBroker {
|
|
9
|
+
store;
|
|
10
|
+
now;
|
|
11
|
+
executor;
|
|
12
|
+
sessions = new Map();
|
|
13
|
+
epochs = new Map();
|
|
14
|
+
leases = new Map();
|
|
15
|
+
controlEpoch = '';
|
|
16
|
+
timer;
|
|
17
|
+
constructor(store, now = Date.now, executor) {
|
|
18
|
+
this.store = store;
|
|
19
|
+
this.now = now;
|
|
20
|
+
this.executor = executor;
|
|
21
|
+
store.onLock = owner => this.revokeOwner(owner);
|
|
22
|
+
this.timer = setInterval(() => {
|
|
23
|
+
try {
|
|
24
|
+
this.executor?.assertAvailable?.();
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
this.disconnect();
|
|
28
|
+
}
|
|
29
|
+
for (const owner of this.sessions.keys()) {
|
|
30
|
+
try {
|
|
31
|
+
store.state(owner);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
store.lock(owner);
|
|
35
|
+
this.sessions.delete(owner);
|
|
36
|
+
this.revokeOwner(owner);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const [id, lease] of this.leases)
|
|
40
|
+
if (lease.expiresAt <= this.now())
|
|
41
|
+
this.revoke(id);
|
|
42
|
+
}, 1000);
|
|
43
|
+
this.timer.unref();
|
|
44
|
+
}
|
|
45
|
+
audit(owner, action, ref, outcome = 'ok') {
|
|
46
|
+
// No request body, username, target, secret, transport error or task output.
|
|
47
|
+
const row = { at: this.now(), owner, action, ...(ref ? { ref } : {}), outcome };
|
|
48
|
+
const path = join(this.store.root, 'audit.jsonl');
|
|
49
|
+
let fd;
|
|
50
|
+
try {
|
|
51
|
+
fd = openSync(path, constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW, 0o600);
|
|
52
|
+
const stat = fstatSync(fd);
|
|
53
|
+
if (!stat.isFile() || (stat.mode & 0o077) || stat.uid !== process.getuid?.())
|
|
54
|
+
throw new Error('密码库审计文件权限不安全');
|
|
55
|
+
writeFileSync(fd, JSON.stringify(row) + '\n');
|
|
56
|
+
fsyncSync(fd);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
this.store.lock(owner);
|
|
60
|
+
this.sessions.delete(owner);
|
|
61
|
+
this.revokeOwner(owner);
|
|
62
|
+
throw new HttpError(503, '密码库审计不可用,已锁定并撤销授权', 'vault_audit_unavailable');
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
if (fd !== undefined)
|
|
66
|
+
closeSync(fd);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
hello(epoch) {
|
|
70
|
+
parse(z.string().uuid(), epoch);
|
|
71
|
+
if (epoch !== this.controlEpoch) {
|
|
72
|
+
this.store.lockAll();
|
|
73
|
+
this.revokeAll();
|
|
74
|
+
this.sessions.clear();
|
|
75
|
+
this.controlEpoch = epoch;
|
|
76
|
+
}
|
|
77
|
+
return { protocol: 1, execution: this.execution().execution };
|
|
78
|
+
}
|
|
79
|
+
execution() {
|
|
80
|
+
if (!this.executor)
|
|
81
|
+
return { execution: 'disabled', reason: 'executor_not_enabled' };
|
|
82
|
+
try {
|
|
83
|
+
this.executor.assertAvailable?.();
|
|
84
|
+
return { execution: this.executor.mode ?? 'dummy-fixture', reason: this.executor.mode === 'protected-adapters' ? 'operator_approved' : 'dummy_fixture_only' };
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
this.disconnect();
|
|
88
|
+
return { execution: 'disabled', reason: 'isolation_required' };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
authorize(owner, session) {
|
|
92
|
+
if (this.sessions.get(owner) !== session || !this.store.state(owner).unlocked)
|
|
93
|
+
throw new HttpError(423, '请在本次登录的密码管理界面手动解锁', 'vault_locked');
|
|
94
|
+
}
|
|
95
|
+
status(owner, session) {
|
|
96
|
+
const execution = this.execution(), state = this.store.state(owner), own = this.sessions.get(owner) === session && state.unlocked;
|
|
97
|
+
return { protocol: 1, online: true, initialized: state.initialized, unlocked: own,
|
|
98
|
+
...(own ? { unlockExpiresAt: state.unlockExpiresAt } : {}), ...execution,
|
|
99
|
+
entries: own ? this.store.list(owner) : [], leases: own ? [...this.leases.values()].filter(l => l.owner === owner).map(({ id, credentialRef, agentId, workId, runnerId, operation, expiresAt }) => ({ id, credentialRef, agentId, workId, runnerId, operation, expiresAt })) : [] };
|
|
100
|
+
}
|
|
101
|
+
revokeOwner(owner) {
|
|
102
|
+
this.epochs.set(owner, (this.epochs.get(owner) ?? 0) + 1);
|
|
103
|
+
for (const [id, l] of this.leases)
|
|
104
|
+
if (l.owner === owner)
|
|
105
|
+
this.revoke(id);
|
|
106
|
+
}
|
|
107
|
+
revokeAll() { for (const id of [...this.leases.keys()])
|
|
108
|
+
this.revoke(id); }
|
|
109
|
+
revoke(id) { const l = this.leases.get(id); this.leases.delete(id); l?.controller.abort(); }
|
|
110
|
+
disconnect() { this.store.lockAll(); this.revokeAll(); this.sessions.clear(); }
|
|
111
|
+
close() { clearInterval(this.timer); this.disconnect(); }
|
|
112
|
+
dispatch(epoch, owner, session, command, value) {
|
|
113
|
+
parse(z.string().min(1).max(256), owner);
|
|
114
|
+
parse(z.string().regex(/^[a-f0-9]{64}$/), session);
|
|
115
|
+
if (epoch !== this.controlEpoch)
|
|
116
|
+
throw new HttpError(410, '控制连接已更换,旧授权失效', 'vault_control_changed');
|
|
117
|
+
if (command === 'status')
|
|
118
|
+
return this.status(owner, session);
|
|
119
|
+
if (command === 'initialize') {
|
|
120
|
+
const body = parse(z.object({ password: z.string() }).strict(), value);
|
|
121
|
+
this.store.initialize(owner, body.password);
|
|
122
|
+
this.audit(owner, 'initialize');
|
|
123
|
+
return this.status(owner, session);
|
|
124
|
+
}
|
|
125
|
+
if (command === 'restore') {
|
|
126
|
+
const b = parse(z.object({ archive: z.string(), password: z.string() }).strict(), value);
|
|
127
|
+
this.store.restore(owner, b.archive, b.password);
|
|
128
|
+
this.audit(owner, 'restore');
|
|
129
|
+
return this.status(owner, session);
|
|
130
|
+
}
|
|
131
|
+
if (command === 'unlock') {
|
|
132
|
+
const b = parse(z.object({ password: z.string(), seconds: z.number().int().min(30).max(900).default(300) }).strict(), value);
|
|
133
|
+
try {
|
|
134
|
+
this.store.unlock(owner, b.password, b.seconds);
|
|
135
|
+
this.sessions.set(owner, session);
|
|
136
|
+
this.audit(owner, 'unlock');
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
this.audit(owner, 'unlock', undefined, 'denied');
|
|
140
|
+
throw e;
|
|
141
|
+
}
|
|
142
|
+
return this.status(owner, session);
|
|
143
|
+
}
|
|
144
|
+
if (command === 'logout') {
|
|
145
|
+
if (this.sessions.get(owner) === session) {
|
|
146
|
+
this.store.lock(owner);
|
|
147
|
+
this.sessions.delete(owner);
|
|
148
|
+
this.audit(owner, 'logout');
|
|
149
|
+
}
|
|
150
|
+
;
|
|
151
|
+
return { ok: true };
|
|
152
|
+
}
|
|
153
|
+
if (command === 'lock') {
|
|
154
|
+
this.store.lock(owner);
|
|
155
|
+
this.sessions.delete(owner);
|
|
156
|
+
this.revokeOwner(owner);
|
|
157
|
+
this.audit(owner, 'lock');
|
|
158
|
+
return { ok: true };
|
|
159
|
+
}
|
|
160
|
+
if (command === 'revoke-work') {
|
|
161
|
+
const b = parse(z.object({ workId: z.string().uuid() }).strict(), value);
|
|
162
|
+
for (const [id, l] of this.leases)
|
|
163
|
+
if (l.owner === owner && l.workId === b.workId)
|
|
164
|
+
this.revoke(id);
|
|
165
|
+
this.audit(owner, 'revoke-work', b.workId);
|
|
166
|
+
return { ok: true };
|
|
167
|
+
}
|
|
168
|
+
this.authorize(owner, session);
|
|
169
|
+
if (command === 'add') {
|
|
170
|
+
const e = this.store.add(owner, value);
|
|
171
|
+
this.audit(owner, 'add', e.id);
|
|
172
|
+
return e;
|
|
173
|
+
}
|
|
174
|
+
if (command === 'update') {
|
|
175
|
+
const b = parse(z.object({ id: z.string().uuid(), entry: z.unknown() }).strict(), value);
|
|
176
|
+
const e = this.store.update(owner, b.id, b.entry);
|
|
177
|
+
this.audit(owner, 'update', e.id);
|
|
178
|
+
return e;
|
|
179
|
+
}
|
|
180
|
+
if (command === 'remove') {
|
|
181
|
+
const b = parse(z.object({ id: z.string().uuid() }).strict(), value);
|
|
182
|
+
this.store.remove(owner, b.id);
|
|
183
|
+
this.audit(owner, 'remove', b.id);
|
|
184
|
+
return { ok: true };
|
|
185
|
+
}
|
|
186
|
+
if (command === 'backup') {
|
|
187
|
+
this.audit(owner, 'backup');
|
|
188
|
+
return { archive: this.store.backup(owner) };
|
|
189
|
+
}
|
|
190
|
+
if (command === 'rotate') {
|
|
191
|
+
const b = parse(z.object({ password: z.string() }).strict(), value);
|
|
192
|
+
this.store.rotate(owner, b.password);
|
|
193
|
+
this.audit(owner, 'rotate');
|
|
194
|
+
return { ok: true };
|
|
195
|
+
}
|
|
196
|
+
if (command === 'grant') {
|
|
197
|
+
const b = parse(leaseInput, value);
|
|
198
|
+
const entry = this.store.list(owner).find(e => e.id === b.credentialRef);
|
|
199
|
+
if (!entry || JSON.stringify(entry.target) !== JSON.stringify(b.target) || (entry.target.kind === 'website') !== (b.operation === 'website.login'))
|
|
200
|
+
throw new HttpError(403, '凭据目标或操作不匹配', 'vault_target_forbidden');
|
|
201
|
+
if (entry.usage && (entry.usage.kind === 'website.form' ? 'website.login' : entry.usage.kind) !== b.operation)
|
|
202
|
+
throw new HttpError(403, '操作不属于用户批准的凭据使用计划', 'vault_usage_forbidden');
|
|
203
|
+
if ([...this.leases.values()].filter(l => l.owner === owner).length >= 64)
|
|
204
|
+
throw new HttpError(429, '任务授权数量已达到上限', 'vault_lease_limit');
|
|
205
|
+
const { seconds: _seconds, target: _target, runnerInstance: _instance, runnerEpoch: _epoch, ...publicInput } = b;
|
|
206
|
+
const result = { ...publicInput, id: randomUUID(), expiresAt: Math.min(this.now() + b.seconds * 1000, this.store.state(owner).unlockExpiresAt) };
|
|
207
|
+
this.leases.set(result.id, { ...result, owner, session, version: entry.revision, target: entry.target, runnerInstance: b.runnerInstance,
|
|
208
|
+
runnerEpoch: b.runnerEpoch, epoch: this.epochs.get(owner) ?? 0, consumed: false, controller: new AbortController() });
|
|
209
|
+
this.audit(owner, 'grant', result.id);
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
if (command === 'revoke') {
|
|
213
|
+
const b = parse(z.object({ id: z.string().uuid() }).strict(), value);
|
|
214
|
+
if (this.leases.get(b.id)?.owner !== owner)
|
|
215
|
+
throw new HttpError(404, '授权不存在', 'vault_lease_missing');
|
|
216
|
+
this.revoke(b.id);
|
|
217
|
+
this.audit(owner, 'revoke', b.id);
|
|
218
|
+
return { ok: true };
|
|
219
|
+
}
|
|
220
|
+
throw new HttpError(404, '密码库操作不存在', 'vault_command_unknown');
|
|
221
|
+
}
|
|
222
|
+
async execute(epoch, owner, session, id, input) {
|
|
223
|
+
const b = parse(leaseInput, input), l = this.leases.get(id);
|
|
224
|
+
const check = () => {
|
|
225
|
+
if (epoch !== this.controlEpoch || !l || this.leases.get(id) !== l || l.owner !== owner || l.epoch !== (this.epochs.get(owner) ?? 0)
|
|
226
|
+
|| l.expiresAt <= this.now() || l.controller.signal.aborted)
|
|
227
|
+
throw new HttpError(410, '凭据任务授权已失效', 'vault_lease_revoked');
|
|
228
|
+
this.authorize(owner, l.session);
|
|
229
|
+
if (session !== l.session)
|
|
230
|
+
throw new HttpError(403, '执行请求不属于解锁会话', 'vault_scope_forbidden');
|
|
231
|
+
if (['agentId', 'workId', 'runnerId', 'runnerInstance', 'runnerEpoch', 'credentialRef', 'operation'].some(k => l[k] !== b[k])
|
|
232
|
+
|| JSON.stringify(l.target) !== JSON.stringify(b.target))
|
|
233
|
+
throw new HttpError(403, '凭据任务范围不匹配', 'vault_scope_forbidden');
|
|
234
|
+
const entry = this.store.list(owner).find(e => e.id === l.credentialRef);
|
|
235
|
+
if (entry?.revision !== l.version)
|
|
236
|
+
throw new HttpError(410, '凭据版本已变化', 'vault_lease_revoked');
|
|
237
|
+
};
|
|
238
|
+
check();
|
|
239
|
+
if (!this.executor)
|
|
240
|
+
throw new HttpError(409, '受保护的浏览器与 SSH 执行器尚未启用,请人工接管', 'vault_executor_not_enabled');
|
|
241
|
+
this.executor.assertAvailable?.();
|
|
242
|
+
if (l.consumed)
|
|
243
|
+
throw new HttpError(409, '此授权已提交,不能重复执行', 'vault_operation_uncertain');
|
|
244
|
+
l.consumed = true;
|
|
245
|
+
this.audit(owner, 'execute-started', id);
|
|
246
|
+
try {
|
|
247
|
+
const result = await this.store.withSecret(owner, l.credentialRef, entry => this.executor.execute(entry, b, l.controller.signal));
|
|
248
|
+
check();
|
|
249
|
+
if (result === undefined && this.executor.mode === 'protected-adapters')
|
|
250
|
+
throw new Error('protected executor receipt missing');
|
|
251
|
+
const receipt = result === undefined ? { status: 'complete', operation: b.operation } : parse(z.union([
|
|
252
|
+
z.object({ status: z.literal('complete'), operation: leaseInput.shape.operation, exitCode: z.number().int().optional(), bytes: z.number().int().nonnegative().optional(), sha256: z.string().regex(/^[a-f0-9]{64}$/).optional() }).strict(),
|
|
253
|
+
z.object({ status: z.literal('manual_takeover_required'), reason: z.enum(['unsupported_form_flow', 'usage_policy_required']), submitted: z.boolean() }).strict(),
|
|
254
|
+
]), result);
|
|
255
|
+
if (receipt.status === 'complete' && receipt.operation !== b.operation)
|
|
256
|
+
throw new Error('receipt scope changed');
|
|
257
|
+
this.audit(owner, receipt.status === 'complete' ? 'execute-complete' : 'execute-manual', id);
|
|
258
|
+
return receipt; // No raw executor output, URL, exception, Cookie or secret.
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
this.audit(owner, 'execute-ended', id, 'not-confirmed');
|
|
262
|
+
if (error instanceof HttpError && ['vault_host_key_rejected', 'vault_usage_forbidden', 'vault_isolation_required'].includes(error.code))
|
|
263
|
+
throw error;
|
|
264
|
+
throw new HttpError(409, '操作结果未确认,授权不再允许重试,请人工核对', 'vault_operation_uncertain');
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
this.revoke(id);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
connectionClosed(owner, session, id) { const l = this.leases.get(id); if (l?.owner === owner && l.session === session)
|
|
271
|
+
this.revoke(id); }
|
|
272
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import Router from '@koa/router';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { HttpError } from '../errors.js';
|
|
6
|
+
import { CredentialVaultClient } from './transport.js';
|
|
7
|
+
import { parse, leaseInput } from './schema.js';
|
|
8
|
+
const toolSchema = { type: 'object', properties: { credentialRef: { type: 'string' }, operation: { enum: ['website.login', 'ssh.exec', 'sftp.read', 'sftp.write'] } }, required: ['credentialRef', 'operation'], additionalProperties: false };
|
|
9
|
+
export const CREDENTIAL_TOOLS = [
|
|
10
|
+
{ id: 'credential_refs', name: 'credential_refs', description: '列出用户授予当前 Bot/任务的凭据引用与允许目标,不包含账号密码或私钥。', inputSchema: { type: 'object', properties: {}, additionalProperties: false } },
|
|
11
|
+
{ id: 'credential_request', name: 'credential_request', description: '请求并使用当前任务的短凭据授权,只执行用户配置的标准网站表单、固定 SSH 命令或指定 SFTP 操作。锁定/离线时等待用户解锁与授权,隔离未批准或复杂网站转人工接管。不得索取或读取密码,不得降级 fill/shell;未知结果不能自动重试。', inputSchema: toolSchema },
|
|
12
|
+
];
|
|
13
|
+
/** The Web control plane never receives a read-secret response. Deployment must
|
|
14
|
+
* keep this process/IPC identity separate from Hermes' arbitrary shell identity. */
|
|
15
|
+
export class CredentialVaultCoordinator {
|
|
16
|
+
client;
|
|
17
|
+
store;
|
|
18
|
+
auth;
|
|
19
|
+
hub;
|
|
20
|
+
sessions = new Map();
|
|
21
|
+
grants = new Map();
|
|
22
|
+
pending = new Map();
|
|
23
|
+
generations = new Map();
|
|
24
|
+
logoutGenerations = new Map();
|
|
25
|
+
connectionGeneration = 0;
|
|
26
|
+
closed = false;
|
|
27
|
+
turnControllers = new Set();
|
|
28
|
+
timer;
|
|
29
|
+
constructor(client, store, auth, hub) {
|
|
30
|
+
this.client = client;
|
|
31
|
+
this.store = store;
|
|
32
|
+
this.auth = auth;
|
|
33
|
+
this.hub = hub;
|
|
34
|
+
client.onDisconnect = () => { this.connectionGeneration++; this.grants.clear(); this.sessions.clear(); };
|
|
35
|
+
this.timer = setInterval(() => {
|
|
36
|
+
for (const s of [...this.sessions.values()])
|
|
37
|
+
if (!this.valid(s))
|
|
38
|
+
void this.endSession(s);
|
|
39
|
+
for (const [id, g] of this.grants) {
|
|
40
|
+
try {
|
|
41
|
+
this.task(g.session.owner, g.summary.agentId, g.summary.workId);
|
|
42
|
+
const current = this.binding(g.session.owner, g.summary.agentId);
|
|
43
|
+
if (JSON.stringify(current) !== JSON.stringify(g.binding) || g.summary.expiresAt <= Date.now())
|
|
44
|
+
throw new Error();
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
this.grants.delete(id);
|
|
48
|
+
void this.client.call(g.session.owner, g.session.key, 'revoke', { id }).catch(() => { });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}, 1000);
|
|
52
|
+
this.timer.unref();
|
|
53
|
+
}
|
|
54
|
+
valid(s) { return this.auth.currentFromCookieHeader(s.cookie)?.id === s.owner && this.auth.isUserActive(s.owner) && this.auth.pushAuthorizationVersion(s.owner) === s.version; }
|
|
55
|
+
scope(ctx) {
|
|
56
|
+
if (this.closed)
|
|
57
|
+
throw new HttpError(503, '密码库连接已关闭', 'vault_offline');
|
|
58
|
+
const user = this.auth.require(ctx), cookie = ctx.get('cookie');
|
|
59
|
+
if (this.auth.currentFromCookieHeader(cookie)?.id !== user.id)
|
|
60
|
+
throw new HttpError(401, '当前会话已失效', 'vault_session_revoked');
|
|
61
|
+
const key = this.auth.sessionBinding(cookie);
|
|
62
|
+
if (!key)
|
|
63
|
+
throw new HttpError(401, '当前会话已失效', 'vault_session_revoked');
|
|
64
|
+
const s = { owner: user.id, key, cookie, version: this.auth.pushAuthorizationVersion(user.id) };
|
|
65
|
+
// Secrets may not traverse explicitly enabled insecure LAN HTTP.
|
|
66
|
+
const local = ['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(ctx.req.socket.remoteAddress ?? '');
|
|
67
|
+
if (!ctx.secure && !local)
|
|
68
|
+
throw new HttpError(403, '密码库管理需要 HTTPS 或本机回环连接', 'vault_https_required');
|
|
69
|
+
ctx.set('Cache-Control', 'no-store');
|
|
70
|
+
return s;
|
|
71
|
+
}
|
|
72
|
+
async call(s, command, value) {
|
|
73
|
+
if (!this.valid(s))
|
|
74
|
+
throw new HttpError(401, '当前会话已失效', 'vault_session_revoked');
|
|
75
|
+
const generation = this.generations.get(s.owner) ?? 0, logout = this.logoutGenerations.get(s.owner + ':' + s.key) ?? 0, connection = this.connectionGeneration;
|
|
76
|
+
const result = await this.client.call(s.owner, s.key, command, value);
|
|
77
|
+
if (!this.valid(s) || generation !== (this.generations.get(s.owner) ?? 0) || logout !== (this.logoutGenerations.get(s.owner + ':' + s.key) ?? 0) || connection !== this.connectionGeneration) {
|
|
78
|
+
await this.endSession(s);
|
|
79
|
+
throw new HttpError(401, '当前会话已失效', 'vault_session_revoked');
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
async endSession(s) {
|
|
84
|
+
if (this.sessions.get(s.owner)?.key === s.key)
|
|
85
|
+
this.sessions.delete(s.owner);
|
|
86
|
+
for (const [id, g] of this.grants)
|
|
87
|
+
if (g.session.key === s.key)
|
|
88
|
+
this.grants.delete(id);
|
|
89
|
+
await this.client.call(s.owner, s.key, 'logout').catch(() => { });
|
|
90
|
+
}
|
|
91
|
+
async logout(owner, cookie) {
|
|
92
|
+
const binding = this.auth.sessionBinding(cookie);
|
|
93
|
+
if (!binding)
|
|
94
|
+
return;
|
|
95
|
+
const key = owner + ':' + binding;
|
|
96
|
+
this.logoutGenerations.set(key, (this.logoutGenerations.get(key) ?? 0) + 1);
|
|
97
|
+
const s = this.sessions.get(owner);
|
|
98
|
+
if (s?.key === binding)
|
|
99
|
+
await this.endSession(s);
|
|
100
|
+
else
|
|
101
|
+
await this.client.call(owner, binding, 'logout').catch(() => { });
|
|
102
|
+
}
|
|
103
|
+
task(owner, agentId, workId) {
|
|
104
|
+
const agent = this.store.require(owner, 'agent', agentId), work = this.store.require(owner, 'turn', workId);
|
|
105
|
+
const run = this.store.require(owner, 'run', work.runId);
|
|
106
|
+
if (!this.auth.isUserActive(owner) || agent.archived || work.agentId !== agent.id || !['running', 'waiting'].includes(work.status) || work.cancelRequested || run.stopRequested
|
|
107
|
+
|| !this.auth.canUseSource(owner, agent.nodeId, agent.profile))
|
|
108
|
+
throw new HttpError(410, 'Bot 任务授权已结束', 'vault_task_revoked');
|
|
109
|
+
return { agent, work };
|
|
110
|
+
}
|
|
111
|
+
binding(owner, agentId) { return this.hub.credentialBinding(owner, this.store.require(owner, 'agent', agentId)); }
|
|
112
|
+
tasks(owner) {
|
|
113
|
+
return this.store.list(owner, 'turn').flatMap(w => {
|
|
114
|
+
try {
|
|
115
|
+
const { agent } = this.task(owner, w.agentId, w.id);
|
|
116
|
+
return [{ workId: w.id, agentId: agent.id, name: agent.name }];
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
offline() { return { protocol: 1, online: false, initialized: false, unlocked: false, execution: 'disabled', reason: 'vault_offline', entries: [], leases: [] }; }
|
|
124
|
+
async view(s) {
|
|
125
|
+
let status;
|
|
126
|
+
try {
|
|
127
|
+
status = await this.call(s, 'status');
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
if (!(e instanceof HttpError) || e.code !== 'vault_offline')
|
|
131
|
+
throw e;
|
|
132
|
+
status = this.offline();
|
|
133
|
+
}
|
|
134
|
+
return { ...status, tasks: this.tasks(s.owner), requests: [...this.pending.values()].filter(p => p.owner === s.owner).map(({ owner: _owner, ...p }) => p) };
|
|
135
|
+
}
|
|
136
|
+
router() {
|
|
137
|
+
const r = new Router(), root = '/api/app/vault';
|
|
138
|
+
r.get(root, async (ctx) => { ctx.body = await this.view(this.scope(ctx)); });
|
|
139
|
+
for (const command of ['initialize', 'unlock', 'lock', 'rotate', 'backup', 'restore'])
|
|
140
|
+
r.post(root + '/' + command, async (ctx) => {
|
|
141
|
+
const s = this.scope(ctx);
|
|
142
|
+
if (['unlock', 'lock', 'rotate'].includes(command))
|
|
143
|
+
this.generations.set(s.owner, (this.generations.get(s.owner) ?? 0) + 1);
|
|
144
|
+
if (['unlock', 'lock', 'rotate'].includes(command)) {
|
|
145
|
+
this.grants.forEach((g, id) => { if (g.session.owner === s.owner)
|
|
146
|
+
this.grants.delete(id); });
|
|
147
|
+
}
|
|
148
|
+
const result = await this.call(s, command, ctx.request.body ?? {});
|
|
149
|
+
if (command === 'unlock')
|
|
150
|
+
this.sessions.set(s.owner, s);
|
|
151
|
+
if (command === 'lock' || command === 'rotate')
|
|
152
|
+
this.sessions.delete(s.owner);
|
|
153
|
+
ctx.body = result;
|
|
154
|
+
});
|
|
155
|
+
r.post(root + '/entries', async (ctx) => { ctx.body = await this.call(this.scope(ctx), 'add', ctx.request.body); });
|
|
156
|
+
r.put(root + '/entries/:id', async (ctx) => {
|
|
157
|
+
const s = this.scope(ctx);
|
|
158
|
+
this.grants.forEach((g, id) => { if (g.summary.credentialRef === ctx.params.id && g.session.owner === s.owner)
|
|
159
|
+
this.grants.delete(id); });
|
|
160
|
+
ctx.body = await this.call(s, 'update', { id: ctx.params.id, entry: ctx.request.body });
|
|
161
|
+
});
|
|
162
|
+
r.delete(root + '/entries/:id', async (ctx) => {
|
|
163
|
+
const s = this.scope(ctx);
|
|
164
|
+
this.grants.forEach((g, id) => { if (g.summary.credentialRef === ctx.params.id && g.session.owner === s.owner)
|
|
165
|
+
this.grants.delete(id); });
|
|
166
|
+
ctx.body = await this.call(s, 'remove', { id: ctx.params.id });
|
|
167
|
+
});
|
|
168
|
+
r.post(root + '/leases', async (ctx) => {
|
|
169
|
+
const s = this.scope(ctx), body = parse(z.object({ credentialRef: z.string().uuid(), agentId: z.string().uuid(), workId: z.string().uuid(),
|
|
170
|
+
operation: leaseInput.shape.operation, seconds: leaseInput.shape.seconds }).strict(), ctx.request.body);
|
|
171
|
+
this.task(s.owner, body.agentId, body.workId);
|
|
172
|
+
const binding = this.binding(s.owner, body.agentId), state = await this.call(s, 'status'), entry = state.entries.find(e => e.id === body.credentialRef);
|
|
173
|
+
if (!entry)
|
|
174
|
+
throw new HttpError(403, '请解锁并选择当前账号的凭据', 'vault_credential_forbidden');
|
|
175
|
+
const summary = await this.call(s, 'grant', { ...body, ...binding, target: entry.target });
|
|
176
|
+
try {
|
|
177
|
+
this.task(s.owner, body.agentId, body.workId);
|
|
178
|
+
if (JSON.stringify(this.binding(s.owner, body.agentId)) !== JSON.stringify(binding))
|
|
179
|
+
throw new Error();
|
|
180
|
+
this.grants.set(summary.id, { summary, target: entry.target, usage: entry.usage, session: s, binding });
|
|
181
|
+
ctx.body = summary;
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
await this.client.call(s.owner, s.key, 'revoke', { id: summary.id }).catch(() => { });
|
|
185
|
+
throw new HttpError(410, '任务或执行节点已变化,授权未保留', 'vault_task_revoked');
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
r.delete(root + '/leases/:id', async (ctx) => {
|
|
189
|
+
const s = this.scope(ctx);
|
|
190
|
+
this.grants.delete(ctx.params.id);
|
|
191
|
+
ctx.body = await this.call(s, 'revoke', { id: ctx.params.id });
|
|
192
|
+
});
|
|
193
|
+
return r;
|
|
194
|
+
}
|
|
195
|
+
openTurn(owner, agentId, workId, externalSignal, authorize, waiting) {
|
|
196
|
+
const controller = new AbortController(), signal = AbortSignal.any([externalSignal, controller.signal]), pendingIds = new Set();
|
|
197
|
+
let waitingCalls = 0;
|
|
198
|
+
this.turnControllers.add(controller);
|
|
199
|
+
const check = () => { signal.throwIfAborted(); if (this.closed)
|
|
200
|
+
throw new HttpError(503, '密码库连接已关闭', 'vault_offline'); authorize(); this.task(owner, agentId, workId); };
|
|
201
|
+
const abort = () => {
|
|
202
|
+
this.turnControllers.delete(controller);
|
|
203
|
+
for (const id of pendingIds)
|
|
204
|
+
this.pending.delete(id);
|
|
205
|
+
for (const [key, g] of this.grants)
|
|
206
|
+
if (g.summary.workId === workId && g.session.owner === owner)
|
|
207
|
+
this.grants.delete(key);
|
|
208
|
+
const s = this.sessions.get(owner);
|
|
209
|
+
if (s)
|
|
210
|
+
void this.client.call(owner, s.key, 'revoke-work', { workId }).catch(() => { });
|
|
211
|
+
};
|
|
212
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
213
|
+
const refs = async () => {
|
|
214
|
+
const binding = this.binding(owner, agentId), entries = [];
|
|
215
|
+
for (const g of this.grants.values()) {
|
|
216
|
+
if (g.session.owner !== owner || g.summary.agentId !== agentId || g.summary.workId !== workId || g.summary.expiresAt <= Date.now()
|
|
217
|
+
|| !this.valid(g.session) || JSON.stringify(g.binding) !== JSON.stringify(binding))
|
|
218
|
+
continue;
|
|
219
|
+
const state = await this.call(g.session, 'status');
|
|
220
|
+
check();
|
|
221
|
+
if (state.unlocked && state.leases.some(l => l.id === g.summary.id))
|
|
222
|
+
entries.push({ credentialRef: g.summary.credentialRef, operation: g.summary.operation, allowedTarget: g.target, ...(g.usage ? { allowedUse: g.usage } : {}) });
|
|
223
|
+
}
|
|
224
|
+
return entries;
|
|
225
|
+
};
|
|
226
|
+
return { close: () => { controller.abort(); signal.removeEventListener('abort', abort); },
|
|
227
|
+
call: async (name, value) => {
|
|
228
|
+
check();
|
|
229
|
+
if (name === 'credential_refs') {
|
|
230
|
+
parse(z.object({}).strict(), value);
|
|
231
|
+
return { references: await refs() };
|
|
232
|
+
}
|
|
233
|
+
if (name !== 'credential_request')
|
|
234
|
+
throw new HttpError(404, '凭据工具不存在', 'vault_command_unknown');
|
|
235
|
+
const b = parse(z.object({ credentialRef: z.string().uuid(), operation: leaseInput.shape.operation }).strict(), value);
|
|
236
|
+
const id = randomUUID();
|
|
237
|
+
pendingIds.add(id);
|
|
238
|
+
this.pending.set(id, { owner, agentId, workId, ...b });
|
|
239
|
+
if (++waitingCalls === 1)
|
|
240
|
+
waiting(true);
|
|
241
|
+
try {
|
|
242
|
+
let initialBinding;
|
|
243
|
+
// Bounded waiting is a state, never permission to execute or replay.
|
|
244
|
+
const end = Date.now() + 120000;
|
|
245
|
+
while (Date.now() < end) {
|
|
246
|
+
check();
|
|
247
|
+
try {
|
|
248
|
+
const binding = this.binding(owner, agentId);
|
|
249
|
+
if (initialBinding && JSON.stringify(binding) !== JSON.stringify(initialBinding))
|
|
250
|
+
throw new HttpError(410, '节点重连后需要新的任务授权', 'vault_connection_revoked');
|
|
251
|
+
initialBinding ??= binding;
|
|
252
|
+
const g = [...this.grants.values()].find(g => g.session.owner === owner && g.summary.workId === workId && g.summary.credentialRef === b.credentialRef && g.summary.operation === b.operation);
|
|
253
|
+
if (g) {
|
|
254
|
+
if (!this.valid(g.session) || g.summary.expiresAt <= Date.now() || JSON.stringify(g.binding) !== JSON.stringify(binding))
|
|
255
|
+
throw new HttpError(410, '凭据任务授权已结束', 'vault_lease_revoked');
|
|
256
|
+
const state = await this.call(g.session, 'status');
|
|
257
|
+
check();
|
|
258
|
+
if (state.unlocked && state.leases.some(l => l.id === g.summary.id)) {
|
|
259
|
+
if (JSON.stringify(this.binding(owner, agentId)) !== JSON.stringify(binding))
|
|
260
|
+
throw new HttpError(410, '节点重连后需要新授权', 'vault_connection_revoked');
|
|
261
|
+
if (state.execution === 'dummy-fixture' || state.execution === 'protected-adapters') {
|
|
262
|
+
try {
|
|
263
|
+
const result = await this.call(g.session, 'execute', { id: g.summary.id, input: { ...b, agentId, workId, ...binding, target: g.target, seconds: 60 } });
|
|
264
|
+
check();
|
|
265
|
+
if (JSON.stringify(this.binding(owner, agentId)) !== JSON.stringify(binding))
|
|
266
|
+
throw new HttpError(409, '执行端在回执确认前变化,结果未知;不能自动重试', 'vault_operation_uncertain');
|
|
267
|
+
return result;
|
|
268
|
+
}
|
|
269
|
+
finally {
|
|
270
|
+
this.grants.delete(g.summary.id);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return { status: 'manual_takeover_required', reason: 'vault_executor_not_enabled', message: '正式网站/SSH 受保护执行器尚未启用;请人工接管,不能使用通用 fill 或 shell 替代。' };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch (e) {
|
|
278
|
+
if (!(e instanceof HttpError) || !['vault_offline', 'vault_runner_offline', 'vault_locked'].includes(e.code))
|
|
279
|
+
throw e;
|
|
280
|
+
}
|
|
281
|
+
await delay(1000, undefined, { signal });
|
|
282
|
+
}
|
|
283
|
+
return { status: 'waiting_for_user', reason: 'vault_locked_or_offline', message: '仍需用户连接、解锁和本任务授权;未执行登录。' };
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
this.pending.delete(id);
|
|
287
|
+
pendingIds.delete(id);
|
|
288
|
+
if (--waitingCalls === 0)
|
|
289
|
+
waiting(false);
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
close() { this.closed = true; clearInterval(this.timer); for (const c of this.turnControllers)
|
|
295
|
+
c.abort(); this.turnControllers.clear(); this.grants.clear(); this.pending.clear(); for (const s of this.sessions.values())
|
|
296
|
+
void this.endSession(s); this.sessions.clear(); }
|
|
297
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** A working, deliberately local-only HTTPS login adapter for dummy integration
|
|
2
|
+
* fixtures. Not wired into the production CLI and not a generic browser login.
|
|
3
|
+
* It never uses global fetch/proxies, redirects, shell, CDP or model form input. */
|
|
4
|
+
import { request } from 'node:https';
|
|
5
|
+
export class FixtureWebsiteLoginExecutor {
|
|
6
|
+
ca;
|
|
7
|
+
origin;
|
|
8
|
+
constructor(origin, ca) {
|
|
9
|
+
this.ca = ca;
|
|
10
|
+
const u = new URL(origin);
|
|
11
|
+
if (u.protocol !== 'https:' || u.hostname !== '127.0.0.1' || u.username || u.password || u.pathname !== '/' || u.search || u.hash || !u.port)
|
|
12
|
+
throw new Error('dummy executor requires exact loopback HTTPS origin');
|
|
13
|
+
this.origin = u.origin;
|
|
14
|
+
}
|
|
15
|
+
send(path, signal, body, cookie) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
signal.throwIfAborted();
|
|
18
|
+
const req = request(new URL(path, this.origin), { method: body ? 'POST' : 'GET', ca: this.ca, rejectUnauthorized: true, signal,
|
|
19
|
+
agent: false, headers: { ...(body ? { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(body) } : {}), ...(cookie ? { Cookie: cookie } : {}) } }, res => {
|
|
20
|
+
let size = 0;
|
|
21
|
+
const chunks = [];
|
|
22
|
+
res.on('data', chunk => { size += chunk.length; if (size > 8192)
|
|
23
|
+
req.destroy(new Error('dummy response limit'));
|
|
24
|
+
else
|
|
25
|
+
chunks.push(Buffer.from(chunk)); });
|
|
26
|
+
res.on('error', reject);
|
|
27
|
+
res.on('aborted', () => reject(new Error('dummy response interrupted')));
|
|
28
|
+
res.on('end', () => resolve({ status: res.statusCode ?? 0, cookies: res.headers['set-cookie'] ?? [], body: Buffer.concat(chunks).toString() }));
|
|
29
|
+
});
|
|
30
|
+
req.setTimeout(3000, () => req.destroy(new Error('dummy login timeout')));
|
|
31
|
+
req.on('error', reject);
|
|
32
|
+
req.end(body);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async execute(entry, input, signal) {
|
|
36
|
+
if (input.operation !== 'website.login' || entry.target.kind !== 'website' || input.target.kind !== 'website' || entry.target.origin !== this.origin || input.target.origin !== this.origin)
|
|
37
|
+
throw new Error('dummy login target forbidden');
|
|
38
|
+
const form = new URLSearchParams({ username: entry.username, password: entry.secret }).toString();
|
|
39
|
+
const login = await this.send('/login', signal, form);
|
|
40
|
+
if (login.status !== 200)
|
|
41
|
+
throw new Error('dummy login rejected'); // Never follow redirects.
|
|
42
|
+
const cookie = login.cookies.map(v => v.split(';')[0]).find(v => /^fixture_auth=[a-f0-9]{64}$/.test(v));
|
|
43
|
+
if (!cookie)
|
|
44
|
+
throw new Error('dummy authentication receipt missing');
|
|
45
|
+
signal.throwIfAborted();
|
|
46
|
+
const proof = await this.send('/protected', signal, undefined, cookie);
|
|
47
|
+
if (proof.status !== 200 || proof.body !== 'dummy-authenticated')
|
|
48
|
+
throw new Error('dummy authenticated session unverified');
|
|
49
|
+
signal.throwIfAborted(); // No cookies, body, username or password returned to the Bot.
|
|
50
|
+
}
|
|
51
|
+
}
|