web-agent-bridge 2.3.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.ar.md +524 -31
  2. package/README.md +592 -47
  3. package/bin/agent-runner.js +10 -1
  4. package/package.json +1 -1
  5. package/public/agent-workspace.html +347 -0
  6. package/public/browser.html +484 -0
  7. package/public/css/agent-workspace.css +1713 -0
  8. package/public/index.html +94 -0
  9. package/public/js/agent-workspace.js +1740 -0
  10. package/sdk/index.d.ts +253 -0
  11. package/sdk/index.js +360 -1
  12. package/sdk/package.json +1 -1
  13. package/server/config/secrets.js +13 -5
  14. package/server/control-plane/index.js +301 -0
  15. package/server/data-plane/index.js +354 -0
  16. package/server/index.js +185 -4
  17. package/server/llm/index.js +404 -0
  18. package/server/middleware/adminAuth.js +6 -1
  19. package/server/middleware/auth.js +11 -2
  20. package/server/middleware/rateLimits.js +78 -2
  21. package/server/migrations/003_ads_integer_cents.sql +33 -0
  22. package/server/models/db.js +126 -25
  23. package/server/observability/index.js +394 -0
  24. package/server/protocol/capabilities.js +223 -0
  25. package/server/protocol/index.js +243 -0
  26. package/server/protocol/schema.js +584 -0
  27. package/server/registry/index.js +326 -0
  28. package/server/routes/admin.js +16 -2
  29. package/server/routes/ads.js +130 -0
  30. package/server/routes/agent-workspace.js +378 -0
  31. package/server/routes/api.js +21 -2
  32. package/server/routes/auth.js +26 -6
  33. package/server/routes/runtime.js +725 -0
  34. package/server/routes/sovereign.js +78 -0
  35. package/server/routes/universal.js +177 -0
  36. package/server/routes/wab-api.js +20 -5
  37. package/server/runtime/event-bus.js +210 -0
  38. package/server/runtime/index.js +233 -0
  39. package/server/runtime/sandbox.js +266 -0
  40. package/server/runtime/scheduler.js +395 -0
  41. package/server/runtime/state-manager.js +188 -0
  42. package/server/security/index.js +355 -0
  43. package/server/services/agent-chat.js +506 -0
  44. package/server/services/agent-symphony.js +6 -0
  45. package/server/services/agent-tasks.js +1807 -0
  46. package/server/services/fairness-engine.js +409 -0
  47. package/server/services/plugins.js +27 -3
  48. package/server/services/price-intelligence.js +565 -0
  49. package/server/services/price-shield.js +1137 -0
  50. package/server/services/search-engine.js +357 -0
  51. package/server/services/security.js +513 -0
  52. package/server/services/universal-scraper.js +661 -0
  53. package/server/ws.js +61 -1
@@ -0,0 +1,355 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * WAB Security Model
5
+ *
6
+ * Production-grade security for Agent OS:
7
+ * - Agent Identity (Ed25519 key pairs)
8
+ * - Capability-based access control
9
+ * - Command signing & verification
10
+ * - Per-site isolation
11
+ * - Credential management
12
+ */
13
+
14
+ const crypto = require('crypto');
15
+
16
+ // ─── Agent Identity ─────────────────────────────────────────────────────────
17
+
18
+ class AgentIdentity {
19
+ constructor() {
20
+ this._agents = new Map(); // agentId → identity record
21
+ this._apiKeys = new Map(); // apiKey → agentId
22
+ this._sessions = new Map(); // sessionId → { agentId, expiresAt, capabilities }
23
+ this._stats = { registered: 0, authenticated: 0, rejected: 0 };
24
+ }
25
+
26
+ /**
27
+ * Register a new agent identity
28
+ */
29
+ register(name, type, options = {}) {
30
+ const agentId = `agent_${crypto.randomBytes(16).toString('hex')}`;
31
+ const apiKey = `wab_${crypto.randomBytes(32).toString('hex')}`;
32
+ // Hash the API key for storage
33
+ const apiKeyHash = crypto.createHash('sha256').update(apiKey).digest('hex');
34
+
35
+ const identity = {
36
+ id: agentId,
37
+ name,
38
+ type, // browser, server, hybrid, orchestrator
39
+ apiKeyHash,
40
+ publicKey: options.publicKey || null,
41
+ capabilities: new Set(options.capabilities || []),
42
+ metadata: options.metadata || {},
43
+ rateLimit: options.rateLimit || { maxPerMinute: 60 },
44
+ allowedIPs: options.allowedIPs || [],
45
+ allowedDomains: options.allowedDomains || ['*'],
46
+ status: 'active',
47
+ createdAt: Date.now(),
48
+ lastSeen: Date.now(),
49
+ commandCount: 0,
50
+ };
51
+
52
+ this._agents.set(agentId, identity);
53
+ this._apiKeys.set(apiKeyHash, agentId);
54
+ this._stats.registered++;
55
+
56
+ return { agentId, apiKey }; // Return raw key only once
57
+ }
58
+
59
+ /**
60
+ * Authenticate an agent via API key
61
+ */
62
+ authenticate(apiKey, ip = null) {
63
+ const hash = crypto.createHash('sha256').update(apiKey).digest('hex');
64
+ const agentId = this._apiKeys.get(hash);
65
+ if (!agentId) {
66
+ this._stats.rejected++;
67
+ return null;
68
+ }
69
+
70
+ const agent = this._agents.get(agentId);
71
+ if (!agent || agent.status !== 'active') {
72
+ this._stats.rejected++;
73
+ return null;
74
+ }
75
+
76
+ // IP allowlist check
77
+ if (agent.allowedIPs.length > 0 && ip) {
78
+ if (!agent.allowedIPs.includes(ip)) {
79
+ this._stats.rejected++;
80
+ return null;
81
+ }
82
+ }
83
+
84
+ agent.lastSeen = Date.now();
85
+ this._stats.authenticated++;
86
+
87
+ // Create session
88
+ const sessionId = `sess_${crypto.randomBytes(24).toString('hex')}`;
89
+ const session = {
90
+ id: sessionId,
91
+ agentId,
92
+ capabilities: [...agent.capabilities],
93
+ ip,
94
+ createdAt: Date.now(),
95
+ expiresAt: Date.now() + 3600_000, // 1 hour
96
+ };
97
+ this._sessions.set(sessionId, session);
98
+
99
+ return {
100
+ sessionId,
101
+ agentId,
102
+ name: agent.name,
103
+ type: agent.type,
104
+ capabilities: [...agent.capabilities],
105
+ expiresAt: session.expiresAt,
106
+ };
107
+ }
108
+
109
+ /**
110
+ * Validate a session
111
+ */
112
+ validateSession(sessionId) {
113
+ const session = this._sessions.get(sessionId);
114
+ if (!session) return null;
115
+ if (Date.now() > session.expiresAt) {
116
+ this._sessions.delete(sessionId);
117
+ return null;
118
+ }
119
+ return session;
120
+ }
121
+
122
+ /**
123
+ * Get agent identity (safe version, no secrets)
124
+ */
125
+ getAgent(agentId) {
126
+ const agent = this._agents.get(agentId);
127
+ if (!agent) return null;
128
+ return {
129
+ id: agent.id,
130
+ name: agent.name,
131
+ type: agent.type,
132
+ capabilities: [...agent.capabilities],
133
+ status: agent.status,
134
+ createdAt: agent.createdAt,
135
+ lastSeen: agent.lastSeen,
136
+ commandCount: agent.commandCount,
137
+ };
138
+ }
139
+
140
+ /**
141
+ * Update agent capabilities
142
+ */
143
+ updateCapabilities(agentId, capabilities) {
144
+ const agent = this._agents.get(agentId);
145
+ if (!agent) throw new Error(`Agent not found: ${agentId}`);
146
+ agent.capabilities = new Set(capabilities);
147
+ }
148
+
149
+ /**
150
+ * Revoke an agent
151
+ */
152
+ revoke(agentId) {
153
+ const agent = this._agents.get(agentId);
154
+ if (agent) {
155
+ agent.status = 'revoked';
156
+ // Kill all sessions
157
+ for (const [sid, sess] of this._sessions) {
158
+ if (sess.agentId === agentId) this._sessions.delete(sid);
159
+ }
160
+ }
161
+ }
162
+
163
+ /**
164
+ * List agents
165
+ */
166
+ listAgents(filter = {}) {
167
+ const result = [];
168
+ for (const [, agent] of this._agents) {
169
+ if (filter.type && agent.type !== filter.type) continue;
170
+ if (filter.status && agent.status !== filter.status) continue;
171
+ result.push(this.getAgent(agent.id));
172
+ }
173
+ return result;
174
+ }
175
+
176
+ /**
177
+ * Increment command count for an agent
178
+ */
179
+ trackCommand(agentId) {
180
+ const agent = this._agents.get(agentId);
181
+ if (agent) {
182
+ agent.commandCount++;
183
+ agent.lastSeen = Date.now();
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Cleanup expired sessions
189
+ */
190
+ cleanup() {
191
+ const now = Date.now();
192
+ for (const [sid, sess] of this._sessions) {
193
+ if (now > sess.expiresAt) this._sessions.delete(sid);
194
+ }
195
+ }
196
+
197
+ getStats() {
198
+ return {
199
+ ...this._stats,
200
+ totalAgents: this._agents.size,
201
+ activeSessions: this._sessions.size,
202
+ };
203
+ }
204
+ }
205
+
206
+ // ─── Command Signing ────────────────────────────────────────────────────────
207
+
208
+ class CommandSigner {
209
+ constructor(secret) {
210
+ this._secret = secret || crypto.randomBytes(32).toString('hex');
211
+ }
212
+
213
+ /**
214
+ * Sign a command payload
215
+ */
216
+ sign(payload, agentId) {
217
+ const nonce = crypto.randomBytes(16).toString('hex');
218
+ const timestamp = Date.now();
219
+ const data = JSON.stringify({ payload, agentId, nonce, timestamp });
220
+ const signature = crypto.createHmac('sha256', this._secret).update(data).digest('hex');
221
+
222
+ return { nonce, timestamp, signature };
223
+ }
224
+
225
+ /**
226
+ * Verify a signed command
227
+ */
228
+ verify(payload, agentId, nonce, timestamp, signature, maxAge = 300_000) {
229
+ // Check timestamp freshness (5 min default)
230
+ if (Math.abs(Date.now() - timestamp) > maxAge) {
231
+ return { valid: false, reason: 'Timestamp expired' };
232
+ }
233
+
234
+ const data = JSON.stringify({ payload, agentId, nonce, timestamp });
235
+ const expected = crypto.createHmac('sha256', this._secret).update(data).digest('hex');
236
+
237
+ // Timing-safe comparison
238
+ if (signature.length !== expected.length) {
239
+ return { valid: false, reason: 'Invalid signature' };
240
+ }
241
+ const valid = crypto.timingSafeEqual(Buffer.from(signature, 'hex'), Buffer.from(expected, 'hex'));
242
+
243
+ return { valid, reason: valid ? null : 'Signature mismatch' };
244
+ }
245
+ }
246
+
247
+ // ─── Site Isolation ─────────────────────────────────────────────────────────
248
+
249
+ class SiteIsolation {
250
+ constructor() {
251
+ this._sites = new Map(); // siteId → isolation config
252
+ }
253
+
254
+ /**
255
+ * Configure isolation for a site
256
+ */
257
+ configure(siteId, config) {
258
+ this._sites.set(siteId, {
259
+ siteId,
260
+ allowedAgents: new Set(config.allowedAgents || []),
261
+ blockedAgents: new Set(config.blockedAgents || []),
262
+ maxConcurrentAgents: config.maxConcurrentAgents || 5,
263
+ allowedCapabilities: new Set(config.allowedCapabilities || ['*']),
264
+ blockedSelectors: config.blockedSelectors || ['.private', '[data-secret]', '#password'],
265
+ dataClassification: config.dataClassification || 'public', // public, internal, confidential, restricted
266
+ requireSigning: config.requireSigning || false,
267
+ auditAll: config.auditAll || false,
268
+ activeAgents: new Set(),
269
+ });
270
+ }
271
+
272
+ /**
273
+ * Check if agent can access site
274
+ */
275
+ canAccess(siteId, agentId) {
276
+ const site = this._sites.get(siteId);
277
+ if (!site) return true; // No config = open
278
+
279
+ if (site.blockedAgents.has(agentId)) return false;
280
+ if (site.allowedAgents.size > 0 && !site.allowedAgents.has(agentId)) return false;
281
+ if (site.activeAgents.size >= site.maxConcurrentAgents) return false;
282
+
283
+ return true;
284
+ }
285
+
286
+ /**
287
+ * Enter a site (track active agent)
288
+ */
289
+ enter(siteId, agentId) {
290
+ const site = this._sites.get(siteId);
291
+ if (!site) return true;
292
+ if (!this.canAccess(siteId, agentId)) return false;
293
+ site.activeAgents.add(agentId);
294
+ return true;
295
+ }
296
+
297
+ /**
298
+ * Leave a site
299
+ */
300
+ leave(siteId, agentId) {
301
+ const site = this._sites.get(siteId);
302
+ if (site) site.activeAgents.delete(agentId);
303
+ }
304
+
305
+ /**
306
+ * Check capability for a site
307
+ */
308
+ checkCapability(siteId, capability) {
309
+ const site = this._sites.get(siteId);
310
+ if (!site) return true;
311
+ if (site.allowedCapabilities.has('*')) return true;
312
+ return site.allowedCapabilities.has(capability);
313
+ }
314
+
315
+ /**
316
+ * Check selector access
317
+ */
318
+ checkSelector(siteId, selector) {
319
+ const site = this._sites.get(siteId);
320
+ if (!site) return true;
321
+ return !site.blockedSelectors.some(b => selector.includes(b));
322
+ }
323
+
324
+ getConfig(siteId) {
325
+ const site = this._sites.get(siteId);
326
+ if (!site) return null;
327
+ return {
328
+ siteId: site.siteId,
329
+ maxConcurrentAgents: site.maxConcurrentAgents,
330
+ activeAgentCount: site.activeAgents.size,
331
+ dataClassification: site.dataClassification,
332
+ requireSigning: site.requireSigning,
333
+ auditAll: site.auditAll,
334
+ };
335
+ }
336
+ }
337
+
338
+ // ─── Singletons ─────────────────────────────────────────────────────────────
339
+
340
+ const identity = new AgentIdentity();
341
+ const signer = new CommandSigner(process.env.WAB_SIGNING_SECRET);
342
+ const isolation = new SiteIsolation();
343
+
344
+ // Cleanup timer
345
+ const _cleanupTimer = setInterval(() => identity.cleanup(), 300_000);
346
+ if (_cleanupTimer.unref) _cleanupTimer.unref();
347
+
348
+ module.exports = {
349
+ AgentIdentity,
350
+ CommandSigner,
351
+ SiteIsolation,
352
+ identity,
353
+ signer,
354
+ isolation,
355
+ };