@private.me/xbind 3.0.0 → 3.0.2
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 +55 -7
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
- package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
- package/dist-standalone/_deps/shared/cjs/index.js +463 -1
- package/dist-standalone/_deps/shared/cjs/types.js +315 -1
- package/dist-standalone/_deps/shared/errors.js +244 -1
- package/dist-standalone/_deps/shared/index.js +72 -1
- package/dist-standalone/_deps/shared/types.js +86 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +659 -1
- package/dist-standalone/agent-sdk.js +328 -1
- package/dist-standalone/agent.js +1800 -1
- package/dist-standalone/approval.js +193 -1
- package/dist-standalone/async-iterators.js +382 -1
- package/dist-standalone/auth.js +219 -1
- package/dist-standalone/auto-accept.js +229 -1
- package/dist-standalone/backup-config.js +201 -1
- package/dist-standalone/backup.js +326 -1
- package/dist-standalone/batch-operations.js +388 -1
- package/dist-standalone/cancellation.js +477 -1
- package/dist-standalone/checkpoint.js +186 -1
- package/dist-standalone/circuit-breaker.js +468 -1
- package/dist-standalone/cjs/agent-call.js +701 -1
- package/dist-standalone/cjs/agent-sdk.js +332 -1
- package/dist-standalone/cjs/agent.js +1837 -1
- package/dist-standalone/cjs/approval.js +199 -1
- package/dist-standalone/cjs/async-iterators.js +392 -1
- package/dist-standalone/cjs/auth.js +225 -1
- package/dist-standalone/cjs/auto-accept.js +233 -1
- package/dist-standalone/cjs/backup-config.js +207 -1
- package/dist-standalone/cjs/backup.js +330 -1
- package/dist-standalone/cjs/batch-operations.js +397 -1
- package/dist-standalone/cjs/cancellation.js +490 -1
- package/dist-standalone/cjs/checkpoint.js +193 -1
- package/dist-standalone/cjs/circuit-breaker.js +476 -1
- package/dist-standalone/cjs/cli/init.js +492 -1
- package/dist-standalone/cjs/config-validation.js +522 -1
- package/dist-standalone/cjs/connect.js +312 -1
- package/dist-standalone/cjs/connection-pool.js +506 -1
- package/dist-standalone/cjs/correlation-id.js +339 -1
- package/dist-standalone/cjs/crypto-utils.js +176 -1
- package/dist-standalone/cjs/debug-mode.js +534 -1
- package/dist-standalone/cjs/did-document.js +101 -1
- package/dist-standalone/cjs/did-privateme.js +130 -1
- package/dist-standalone/cjs/did-web.js +201 -1
- package/dist-standalone/cjs/discovery.js +462 -1
- package/dist-standalone/cjs/dual-mode.js +251 -1
- package/dist-standalone/cjs/email-templates.js +313 -1
- package/dist-standalone/cjs/email-transport.js +239 -1
- package/dist-standalone/cjs/envelope.js +538 -1
- package/dist-standalone/cjs/errors.js +913 -1
- package/dist-standalone/cjs/event-emitter.js +461 -1
- package/dist-standalone/cjs/gateway-state.js +55 -1
- package/dist-standalone/cjs/gateway-transport.js +120 -1
- package/dist-standalone/cjs/graceful-degradation.js +403 -1
- package/dist-standalone/cjs/guardrails.js +223 -1
- package/dist-standalone/cjs/health-check.js +336 -1
- package/dist-standalone/cjs/http-compat.js +272 -1
- package/dist-standalone/cjs/http-status-map.js +571 -1
- package/dist-standalone/cjs/identity.js +645 -1
- package/dist-standalone/cjs/index.js +406 -1
- package/dist-standalone/cjs/invitation.js +421 -1
- package/dist-standalone/cjs/invite.js +328 -1
- package/dist-standalone/cjs/key-agreement.js +335 -1
- package/dist-standalone/cjs/lazy-init.js +300 -1
- package/dist-standalone/cjs/logger.js +291 -1
- package/dist-standalone/cjs/mdns-discovery.js +202 -1
- package/dist-standalone/cjs/nonce-store.js +80 -1
- package/dist-standalone/cjs/pairing-manager.js +223 -1
- package/dist-standalone/cjs/plugin-system.js +264 -1
- package/dist-standalone/cjs/plugins/logging.js +168 -1
- package/dist-standalone/cjs/plugins/metrics.js +181 -1
- package/dist-standalone/cjs/plugins/validation.js +302 -1
- package/dist-standalone/cjs/policy.js +320 -1
- package/dist-standalone/cjs/progress-callbacks.js +583 -1
- package/dist-standalone/cjs/redis-nonce-store.js +76 -1
- package/dist-standalone/cjs/registry-middleware.js +50 -1
- package/dist-standalone/cjs/retry-strategies.js +544 -1
- package/dist-standalone/cjs/retry-transport.js +102 -1
- package/dist-standalone/cjs/runtime/browser.js +533 -1
- package/dist-standalone/cjs/runtime/edge.js +526 -1
- package/dist-standalone/cjs/runtime/react-native.js +394 -1
- package/dist-standalone/cjs/security-policy.js +245 -1
- package/dist-standalone/cjs/serialization.js +1040 -1
- package/dist-standalone/cjs/split-channel.js +225 -1
- package/dist-standalone/cjs/subscription-proof.js +230 -1
- package/dist-standalone/cjs/succession.js +148 -1
- package/dist-standalone/cjs/timeouts.js +412 -1
- package/dist-standalone/cjs/trace-context.js +424 -1
- package/dist-standalone/cjs/trace-spans.js +495 -1
- package/dist-standalone/cjs/transport.js +63 -1
- package/dist-standalone/cjs/trust-registry.js +991 -1
- package/dist-standalone/cjs/types/error-response.js +56 -1
- package/dist-standalone/cjs/vault-auth.js +178 -1
- package/dist-standalone/cjs/vault-store-loader.js +194 -1
- package/dist-standalone/cjs/verify.js +25 -1
- package/dist-standalone/cjs/version-info.js +543 -1
- package/dist-standalone/cjs/xfetch.js +340 -1
- package/dist-standalone/cli/init.js +455 -1
- package/dist-standalone/cli/setup.js +514 -1
- package/dist-standalone/cli/types.js +27 -1
- package/dist-standalone/cli/xbind.js +148 -1
- package/dist-standalone/config-validation.js +513 -1
- package/dist-standalone/connect.js +274 -1
- package/dist-standalone/connection-pool.js +500 -1
- package/dist-standalone/correlation-id.js +326 -1
- package/dist-standalone/crypto-utils.js +157 -1
- package/dist-standalone/debug-mode.js +510 -1
- package/dist-standalone/did-document.js +96 -1
- package/dist-standalone/did-privateme.js +121 -1
- package/dist-standalone/did-web.js +196 -1
- package/dist-standalone/discovery.js +458 -1
- package/dist-standalone/dual-mode.js +247 -1
- package/dist-standalone/email-templates.js +309 -1
- package/dist-standalone/email-transport.js +232 -1
- package/dist-standalone/envelope.js +525 -1
- package/dist-standalone/errors.js +896 -1
- package/dist-standalone/event-emitter.js +456 -1
- package/dist-standalone/gateway-state.js +51 -1
- package/dist-standalone/gateway-transport.js +116 -1
- package/dist-standalone/graceful-degradation.js +396 -1
- package/dist-standalone/guardrails.js +216 -1
- package/dist-standalone/health-check.js +332 -1
- package/dist-standalone/http-compat.js +267 -1
- package/dist-standalone/http-status-map.js +561 -1
- package/dist-standalone/identity.js +619 -1
- package/dist-standalone/index.js +78 -1
- package/dist-standalone/invitation.js +415 -1
- package/dist-standalone/invite.js +324 -1
- package/dist-standalone/key-agreement.js +325 -1
- package/dist-standalone/lazy-init.js +295 -1
- package/dist-standalone/logger.js +285 -1
- package/dist-standalone/mdns-discovery.js +195 -1
- package/dist-standalone/nonce-store.js +76 -1
- package/dist-standalone/pairing-manager.js +219 -1
- package/dist-standalone/plugin-system.js +257 -1
- package/dist-standalone/plugins/logging.d.ts +84 -0
- package/dist-standalone/plugins/logging.js +163 -0
- package/dist-standalone/plugins/metrics.d.ts +111 -0
- package/dist-standalone/plugins/metrics.js +176 -0
- package/dist-standalone/plugins/validation.d.ts +104 -0
- package/dist-standalone/plugins/validation.js +297 -0
- package/dist-standalone/policy.js +315 -1
- package/dist-standalone/progress-callbacks.js +576 -1
- package/dist-standalone/redis-nonce-store.js +72 -1
- package/dist-standalone/registry-middleware.js +47 -1
- package/dist-standalone/retry-strategies.js +534 -1
- package/dist-standalone/retry-transport.js +98 -1
- package/dist-standalone/runtime/browser.d.ts +311 -0
- package/dist-standalone/runtime/browser.js +516 -0
- package/dist-standalone/runtime/edge.d.ts +282 -0
- package/dist-standalone/runtime/edge.js +511 -0
- package/dist-standalone/runtime/react-native.d.ts +157 -0
- package/dist-standalone/runtime/react-native.js +383 -0
- package/dist-standalone/security-policy.js +239 -1
- package/dist-standalone/serialization.js +1031 -1
- package/dist-standalone/split-channel.js +219 -1
- package/dist-standalone/subscription-proof.js +224 -1
- package/dist-standalone/succession.js +142 -1
- package/dist-standalone/timeouts.js +398 -1
- package/dist-standalone/trace-context.js +414 -1
- package/dist-standalone/trace-spans.js +488 -1
- package/dist-standalone/transport.js +59 -1
- package/dist-standalone/trust-registry.js +950 -1
- package/dist-standalone/types/error-response.d.ts +209 -0
- package/dist-standalone/types/error-response.js +52 -0
- package/dist-standalone/vault-auth.js +174 -1
- package/dist-standalone/vault-store-loader.js +187 -1
- package/dist-standalone/verify.js +16 -1
- package/dist-standalone/version-info.js +530 -1
- package/dist-standalone/xfetch.js +335 -1
- package/package.json +4 -10
- package/share1.dat +0 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
- package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
- package/dist-standalone/_deps/shared/cjs/package.json +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
- package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
- package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
- package/dist-standalone/cjs/package.json +0 -3
- package/dist-standalone/package.json +0 -10
|
@@ -1 +1,302 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module plugins/validation
|
|
4
|
+
* Validation plugin for xBind plugin system
|
|
5
|
+
*
|
|
6
|
+
* Validates payloads, envelopes, and ensures data integrity before processing.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.CommonRules = exports.ValidationPlugin = void 0;
|
|
10
|
+
exports.createValidationPlugin = createValidationPlugin;
|
|
11
|
+
const shared_1 = require("../../_deps/shared/index.js");
|
|
12
|
+
/**
|
|
13
|
+
* Validation plugin implementation.
|
|
14
|
+
* Validates all data before processing to ensure integrity and compliance.
|
|
15
|
+
*/
|
|
16
|
+
class ValidationPlugin {
|
|
17
|
+
name = 'validation';
|
|
18
|
+
version = '1.0.0';
|
|
19
|
+
description = 'Validates payloads and envelopes before processing';
|
|
20
|
+
priority = 5; // Execute very early
|
|
21
|
+
options;
|
|
22
|
+
validationErrors = [];
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
this.options = {
|
|
25
|
+
maxPayloadSize: options.maxPayloadSize ?? 10 * 1024 * 1024, // 10MB
|
|
26
|
+
maxEnvelopeSize: options.maxEnvelopeSize ?? 15 * 1024 * 1024, // 15MB
|
|
27
|
+
allowedScopes: options.allowedScopes ?? [],
|
|
28
|
+
customRules: options.customRules ?? [],
|
|
29
|
+
strictMode: options.strictMode ?? false,
|
|
30
|
+
validateSchema: options.validateSchema ?? false,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
onInit() {
|
|
34
|
+
return (0, shared_1.ok)(undefined);
|
|
35
|
+
}
|
|
36
|
+
onDestroy() {
|
|
37
|
+
this.validationErrors = [];
|
|
38
|
+
return (0, shared_1.ok)(undefined);
|
|
39
|
+
}
|
|
40
|
+
onSend(payload, context) {
|
|
41
|
+
// Validate payload size
|
|
42
|
+
const sizeResult = this.validatePayloadSize(payload);
|
|
43
|
+
if (sizeResult.ok === false) {
|
|
44
|
+
this.recordError('payload_size', sizeResult.error);
|
|
45
|
+
return (0, shared_1.err)(sizeResult.error);
|
|
46
|
+
}
|
|
47
|
+
// Validate scope
|
|
48
|
+
if (context.scope) {
|
|
49
|
+
const scopeResult = this.validateScope(context.scope);
|
|
50
|
+
if (scopeResult.ok === false) {
|
|
51
|
+
this.recordError('scope', scopeResult.error);
|
|
52
|
+
return (0, shared_1.err)(scopeResult.error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Validate payload structure
|
|
56
|
+
const structureResult = this.validatePayloadStructure(payload);
|
|
57
|
+
if (structureResult.ok === false) {
|
|
58
|
+
this.recordError('payload_structure', structureResult.error);
|
|
59
|
+
return (0, shared_1.err)(structureResult.error);
|
|
60
|
+
}
|
|
61
|
+
// Apply custom rules
|
|
62
|
+
for (const rule of this.options.customRules) {
|
|
63
|
+
if (!rule.appliesTo || rule.appliesTo.includes('send')) {
|
|
64
|
+
const error = rule.validate(payload, context);
|
|
65
|
+
if (error) {
|
|
66
|
+
this.recordError(rule.name, error);
|
|
67
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:${rule.name}:${error}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return (0, shared_1.ok)({ payload });
|
|
72
|
+
}
|
|
73
|
+
onReceive(envelope, context) {
|
|
74
|
+
// Validate envelope size
|
|
75
|
+
const sizeResult = this.validateEnvelopeSize(envelope);
|
|
76
|
+
if (sizeResult.ok === false) {
|
|
77
|
+
this.recordError('envelope_size', sizeResult.error);
|
|
78
|
+
return (0, shared_1.err)(sizeResult.error);
|
|
79
|
+
}
|
|
80
|
+
// Validate envelope structure
|
|
81
|
+
const structureResult = this.validateEnvelopeStructure(envelope);
|
|
82
|
+
if (structureResult.ok === false) {
|
|
83
|
+
this.recordError('envelope_structure', structureResult.error);
|
|
84
|
+
return (0, shared_1.err)(structureResult.error);
|
|
85
|
+
}
|
|
86
|
+
// Apply custom rules
|
|
87
|
+
for (const rule of this.options.customRules) {
|
|
88
|
+
if (!rule.appliesTo || rule.appliesTo.includes('receive')) {
|
|
89
|
+
const error = rule.validate(envelope, context);
|
|
90
|
+
if (error) {
|
|
91
|
+
this.recordError(rule.name, error);
|
|
92
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:${rule.name}:${error}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return (0, shared_1.ok)({ payload: envelope });
|
|
97
|
+
}
|
|
98
|
+
onEncrypt(plaintext, context) {
|
|
99
|
+
// Validate plaintext is not empty
|
|
100
|
+
if (plaintext.length === 0) {
|
|
101
|
+
this.recordError('plaintext_empty', 'Plaintext cannot be empty');
|
|
102
|
+
return (0, shared_1.err)('VALIDATION_FAILED:PLAINTEXT_EMPTY');
|
|
103
|
+
}
|
|
104
|
+
// Validate plaintext size
|
|
105
|
+
if (plaintext.length > this.options.maxPayloadSize) {
|
|
106
|
+
this.recordError('plaintext_size', `Plaintext exceeds max size: ${plaintext.length} bytes`);
|
|
107
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:PLAINTEXT_TOO_LARGE:${plaintext.length}`);
|
|
108
|
+
}
|
|
109
|
+
// Apply custom rules
|
|
110
|
+
for (const rule of this.options.customRules) {
|
|
111
|
+
if (!rule.appliesTo || rule.appliesTo.includes('encrypt')) {
|
|
112
|
+
const error = rule.validate(plaintext, context);
|
|
113
|
+
if (error) {
|
|
114
|
+
this.recordError(rule.name, error);
|
|
115
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:${rule.name}:${error}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return (0, shared_1.ok)({ payload: plaintext });
|
|
120
|
+
}
|
|
121
|
+
onDecrypt(plaintext, context) {
|
|
122
|
+
// Validate plaintext is not empty
|
|
123
|
+
if (plaintext.length === 0) {
|
|
124
|
+
this.recordError('plaintext_empty', 'Decrypted plaintext is empty');
|
|
125
|
+
return (0, shared_1.err)('VALIDATION_FAILED:DECRYPTED_EMPTY');
|
|
126
|
+
}
|
|
127
|
+
// Apply custom rules
|
|
128
|
+
for (const rule of this.options.customRules) {
|
|
129
|
+
if (!rule.appliesTo || rule.appliesTo.includes('decrypt')) {
|
|
130
|
+
const error = rule.validate(plaintext, context);
|
|
131
|
+
if (error) {
|
|
132
|
+
this.recordError(rule.name, error);
|
|
133
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:${rule.name}:${error}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return (0, shared_1.ok)({ payload: plaintext });
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get all validation errors.
|
|
141
|
+
*/
|
|
142
|
+
getValidationErrors() {
|
|
143
|
+
return [...this.validationErrors];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Clear validation errors.
|
|
147
|
+
*/
|
|
148
|
+
clearErrors() {
|
|
149
|
+
this.validationErrors = [];
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Add a custom validation rule.
|
|
153
|
+
*/
|
|
154
|
+
addRule(rule) {
|
|
155
|
+
this.options.customRules.push(rule);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Remove a custom validation rule by name.
|
|
159
|
+
*/
|
|
160
|
+
removeRule(name) {
|
|
161
|
+
const index = this.options.customRules.findIndex((r) => r.name === name);
|
|
162
|
+
if (index === -1) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
this.options.customRules.splice(index, 1);
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
validatePayloadSize(payload) {
|
|
169
|
+
const size = this.estimateSize(payload);
|
|
170
|
+
if (size > this.options.maxPayloadSize) {
|
|
171
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:PAYLOAD_TOO_LARGE:${size}>${this.options.maxPayloadSize}`);
|
|
172
|
+
}
|
|
173
|
+
return (0, shared_1.ok)(undefined);
|
|
174
|
+
}
|
|
175
|
+
validateEnvelopeSize(envelope) {
|
|
176
|
+
const size = this.estimateSize(envelope);
|
|
177
|
+
if (size > this.options.maxEnvelopeSize) {
|
|
178
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:ENVELOPE_TOO_LARGE:${size}>${this.options.maxEnvelopeSize}`);
|
|
179
|
+
}
|
|
180
|
+
return (0, shared_1.ok)(undefined);
|
|
181
|
+
}
|
|
182
|
+
validateScope(scope) {
|
|
183
|
+
if (this.options.allowedScopes.length > 0 && !this.options.allowedScopes.includes(scope)) {
|
|
184
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:SCOPE_NOT_ALLOWED:${scope}`);
|
|
185
|
+
}
|
|
186
|
+
return (0, shared_1.ok)(undefined);
|
|
187
|
+
}
|
|
188
|
+
validatePayloadStructure(payload) {
|
|
189
|
+
if (payload === null || payload === undefined) {
|
|
190
|
+
return (0, shared_1.err)('VALIDATION_FAILED:PAYLOAD_NULL_OR_UNDEFINED');
|
|
191
|
+
}
|
|
192
|
+
// Validate can be serialized to JSON
|
|
193
|
+
try {
|
|
194
|
+
JSON.stringify(payload);
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
return (0, shared_1.err)(`VALIDATION_FAILED:PAYLOAD_NOT_SERIALIZABLE:${String(error)}`);
|
|
198
|
+
}
|
|
199
|
+
return (0, shared_1.ok)(undefined);
|
|
200
|
+
}
|
|
201
|
+
validateEnvelopeStructure(envelope) {
|
|
202
|
+
// Validate required fields
|
|
203
|
+
if (!('v' in envelope) && !('version' in envelope)) {
|
|
204
|
+
return (0, shared_1.err)('VALIDATION_FAILED:ENVELOPE_MISSING_VERSION');
|
|
205
|
+
}
|
|
206
|
+
if (!envelope.sender) {
|
|
207
|
+
return (0, shared_1.err)('VALIDATION_FAILED:ENVELOPE_MISSING_SENDER');
|
|
208
|
+
}
|
|
209
|
+
if (!envelope.recipient) {
|
|
210
|
+
return (0, shared_1.err)('VALIDATION_FAILED:ENVELOPE_MISSING_RECIPIENT');
|
|
211
|
+
}
|
|
212
|
+
if (!('payload' in envelope) && !('ciphertext' in envelope)) {
|
|
213
|
+
return (0, shared_1.err)('VALIDATION_FAILED:ENVELOPE_MISSING_CIPHERTEXT');
|
|
214
|
+
}
|
|
215
|
+
return (0, shared_1.ok)(undefined);
|
|
216
|
+
}
|
|
217
|
+
estimateSize(value) {
|
|
218
|
+
if (value instanceof Uint8Array) {
|
|
219
|
+
return value.length;
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
return JSON.stringify(value).length;
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
recordError(rule, error) {
|
|
229
|
+
this.validationErrors.push({
|
|
230
|
+
timestamp: Date.now(),
|
|
231
|
+
rule,
|
|
232
|
+
error,
|
|
233
|
+
});
|
|
234
|
+
// Keep only last 1000 errors
|
|
235
|
+
if (this.validationErrors.length > 1000) {
|
|
236
|
+
this.validationErrors.shift();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.ValidationPlugin = ValidationPlugin;
|
|
241
|
+
/**
|
|
242
|
+
* Create a validation plugin with options.
|
|
243
|
+
*/
|
|
244
|
+
function createValidationPlugin(options) {
|
|
245
|
+
return new ValidationPlugin(options);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Common validation rules.
|
|
249
|
+
*/
|
|
250
|
+
exports.CommonRules = {
|
|
251
|
+
/**
|
|
252
|
+
* Validate payload contains required fields.
|
|
253
|
+
*/
|
|
254
|
+
requireFields(fields) {
|
|
255
|
+
return {
|
|
256
|
+
name: 'require_fields',
|
|
257
|
+
appliesTo: ['send'],
|
|
258
|
+
validate: (value) => {
|
|
259
|
+
if (typeof value !== 'object' || value === null) {
|
|
260
|
+
return 'Payload must be an object';
|
|
261
|
+
}
|
|
262
|
+
const obj = value;
|
|
263
|
+
const missing = fields.filter((field) => !(field in obj));
|
|
264
|
+
if (missing.length > 0) {
|
|
265
|
+
return `Missing required fields: ${missing.join(', ')}`;
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
},
|
|
271
|
+
/**
|
|
272
|
+
* Validate DID format.
|
|
273
|
+
*/
|
|
274
|
+
validateDIDFormat() {
|
|
275
|
+
return {
|
|
276
|
+
name: 'did_format',
|
|
277
|
+
appliesTo: ['send', 'receive'],
|
|
278
|
+
validate: (value, context) => {
|
|
279
|
+
if (context.recipient && !context.recipient.startsWith('did:')) {
|
|
280
|
+
return `Invalid DID format: ${context.recipient}`;
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
},
|
|
286
|
+
/**
|
|
287
|
+
* Validate timestamp is recent.
|
|
288
|
+
*/
|
|
289
|
+
validateTimestamp(maxAgeMs = 300000) {
|
|
290
|
+
return {
|
|
291
|
+
name: 'timestamp_freshness',
|
|
292
|
+
appliesTo: ['receive'],
|
|
293
|
+
validate: (value, context) => {
|
|
294
|
+
const age = Date.now() - context.timestamp;
|
|
295
|
+
if (age > maxAgeMs) {
|
|
296
|
+
return `Timestamp too old: ${age}ms > ${maxAgeMs}ms`;
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
};
|
|
@@ -1 +1,320 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module policy
|
|
4
|
+
* PolicyEngine for agent.call() constraint enforcement
|
|
5
|
+
*
|
|
6
|
+
* Enforces spending limits, rate limits, scope restrictions, and data filters
|
|
7
|
+
* on agent tool calls. Separate from SecurityPolicy (risk classification).
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.PolicyEngine = void 0;
|
|
11
|
+
exports.getGlobalPolicyEngine = getGlobalPolicyEngine;
|
|
12
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
13
|
+
const agent_call_js_1 = require("./agent-call.js");
|
|
14
|
+
/**
|
|
15
|
+
* PolicyEngine - Enforces constraints on agent.call() requests
|
|
16
|
+
*
|
|
17
|
+
* Tracks:
|
|
18
|
+
* - Per-transaction spending limits
|
|
19
|
+
* - Daily spending limits
|
|
20
|
+
* - Rate limits (calls per minute)
|
|
21
|
+
* - Allowed tools/scopes
|
|
22
|
+
*
|
|
23
|
+
* Thread-safe for concurrent requests from the same agent.
|
|
24
|
+
*/
|
|
25
|
+
class PolicyEngine {
|
|
26
|
+
/** Rate limit tracker by agent DID */
|
|
27
|
+
rateLimits = new Map();
|
|
28
|
+
/** Daily spending tracker by agent DID */
|
|
29
|
+
dailySpending = new Map();
|
|
30
|
+
/** Monthly spending tracker by agent DID */
|
|
31
|
+
monthlySpending = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* Evaluate a policy against a request
|
|
34
|
+
*
|
|
35
|
+
* @param agentDID - Agent DID making the request
|
|
36
|
+
* @param tool - Tool being called (e.g., "stripe:createCharge")
|
|
37
|
+
* @param params - Tool parameters
|
|
38
|
+
* @param constraints - Policy constraints to enforce
|
|
39
|
+
* @returns Policy evaluation result
|
|
40
|
+
*/
|
|
41
|
+
evaluate(agentDID, tool, params, constraints) {
|
|
42
|
+
// Check allowed tools
|
|
43
|
+
if (constraints.allowedTools && constraints.allowedTools.length > 0) {
|
|
44
|
+
const [service] = tool.split(':');
|
|
45
|
+
const isAllowed = constraints.allowedTools.some((allowed) => allowed === tool || allowed === `${service}:*` || allowed === '*');
|
|
46
|
+
if (!isAllowed) {
|
|
47
|
+
const details = {
|
|
48
|
+
requested: tool,
|
|
49
|
+
allowed: constraints.allowedTools,
|
|
50
|
+
constraint: 'tool',
|
|
51
|
+
fix: `Update policy.allowedTools to include "${tool}" or "${service}:*" or request approval`,
|
|
52
|
+
};
|
|
53
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Tool "${tool}" is not allowed by policy`, details));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Check scopes
|
|
57
|
+
if (constraints.scopes && constraints.scopes.length > 0) {
|
|
58
|
+
// Extract scope from params if present
|
|
59
|
+
const scope = typeof params === 'object' && params !== null && 'scope' in params
|
|
60
|
+
? params.scope
|
|
61
|
+
: undefined;
|
|
62
|
+
if (scope && !constraints.scopes.includes(scope)) {
|
|
63
|
+
const details = {
|
|
64
|
+
requested: scope,
|
|
65
|
+
allowed: constraints.scopes,
|
|
66
|
+
constraint: 'scope',
|
|
67
|
+
fix: `Update policy.scopes to include "${scope}" or request additional permissions`,
|
|
68
|
+
};
|
|
69
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Scope "${scope}" is not allowed by policy`, details));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Check rate limits
|
|
73
|
+
if (constraints.limits?.callsPerMinute) {
|
|
74
|
+
const rateCheck = this.checkRateLimit(agentDID, constraints.limits.callsPerMinute);
|
|
75
|
+
if (!rateCheck.ok) {
|
|
76
|
+
return rateCheck;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Check spending limits (if amount is in params)
|
|
80
|
+
const amount = this.extractAmount(params);
|
|
81
|
+
if (amount !== null) {
|
|
82
|
+
// Check per-transaction limit
|
|
83
|
+
if (constraints.limits?.amountPerTxn && amount > constraints.limits.amountPerTxn) {
|
|
84
|
+
const details = {
|
|
85
|
+
requested: amount,
|
|
86
|
+
allowed: constraints.limits.amountPerTxn,
|
|
87
|
+
constraint: 'amountPerTxn',
|
|
88
|
+
fix: `Reduce amount to ${constraints.limits.amountPerTxn} or less, or update policy.limits.amountPerTxn`,
|
|
89
|
+
};
|
|
90
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Amount ${amount} exceeds per-transaction limit of ${constraints.limits.amountPerTxn}`, details));
|
|
91
|
+
}
|
|
92
|
+
// Check daily limit
|
|
93
|
+
if (constraints.limits?.dailyAmount) {
|
|
94
|
+
const dailyCheck = this.checkDailyLimit(agentDID, amount, constraints.limits.dailyAmount);
|
|
95
|
+
if (!dailyCheck.ok) {
|
|
96
|
+
return dailyCheck;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// Check monthly limit
|
|
100
|
+
if (constraints.limits?.monthlyAmount) {
|
|
101
|
+
const monthlyCheck = this.checkMonthlyLimit(agentDID, amount, constraints.limits.monthlyAmount);
|
|
102
|
+
if (!monthlyCheck.ok) {
|
|
103
|
+
return monthlyCheck;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// All checks passed
|
|
108
|
+
return (0, shared_1.ok)(undefined);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Record a successful call (for rate limiting)
|
|
112
|
+
*
|
|
113
|
+
* @param agentDID - Agent DID
|
|
114
|
+
*/
|
|
115
|
+
recordCall(agentDID) {
|
|
116
|
+
const now = Date.now();
|
|
117
|
+
const entry = this.rateLimits.get(agentDID) ?? {
|
|
118
|
+
calls: [],
|
|
119
|
+
windowStart: now,
|
|
120
|
+
};
|
|
121
|
+
entry.calls.push(now);
|
|
122
|
+
// Clean up old calls (older than 1 minute)
|
|
123
|
+
const oneMinuteAgo = now - 60000;
|
|
124
|
+
entry.calls = entry.calls.filter((timestamp) => timestamp > oneMinuteAgo);
|
|
125
|
+
this.rateLimits.set(agentDID, entry);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Record successful spending (for daily and monthly limits)
|
|
129
|
+
*
|
|
130
|
+
* @param agentDID - Agent DID
|
|
131
|
+
* @param amount - Amount spent
|
|
132
|
+
*/
|
|
133
|
+
recordSpending(agentDID, amount) {
|
|
134
|
+
const today = this.getCurrentDay();
|
|
135
|
+
const thisMonth = this.getCurrentMonth();
|
|
136
|
+
// Update daily spending
|
|
137
|
+
const dailyEntry = this.dailySpending.get(agentDID);
|
|
138
|
+
if (dailyEntry && dailyEntry.day === today) {
|
|
139
|
+
dailyEntry.amount += amount;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.dailySpending.set(agentDID, {
|
|
143
|
+
amount,
|
|
144
|
+
day: today,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
// Update monthly spending
|
|
148
|
+
const monthlyEntry = this.monthlySpending.get(agentDID);
|
|
149
|
+
if (monthlyEntry && monthlyEntry.month === thisMonth) {
|
|
150
|
+
monthlyEntry.amount += amount;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.monthlySpending.set(agentDID, {
|
|
154
|
+
amount,
|
|
155
|
+
month: thisMonth,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get current rate limit status for an agent
|
|
161
|
+
*
|
|
162
|
+
* @param agentDID - Agent DID
|
|
163
|
+
* @returns Calls in current minute
|
|
164
|
+
*/
|
|
165
|
+
getCurrentRateLimit(agentDID) {
|
|
166
|
+
const entry = this.rateLimits.get(agentDID);
|
|
167
|
+
if (!entry)
|
|
168
|
+
return 0;
|
|
169
|
+
const now = Date.now();
|
|
170
|
+
const oneMinuteAgo = now - 60000;
|
|
171
|
+
// Count calls in last minute
|
|
172
|
+
return entry.calls.filter((timestamp) => timestamp > oneMinuteAgo).length;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get current daily spending for an agent
|
|
176
|
+
*
|
|
177
|
+
* @param agentDID - Agent DID
|
|
178
|
+
* @returns Amount spent today
|
|
179
|
+
*/
|
|
180
|
+
getDailySpending(agentDID) {
|
|
181
|
+
const today = this.getCurrentDay();
|
|
182
|
+
const entry = this.dailySpending.get(agentDID);
|
|
183
|
+
if (!entry || entry.day !== today)
|
|
184
|
+
return 0;
|
|
185
|
+
return entry.amount;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Get current monthly spending for an agent
|
|
189
|
+
*
|
|
190
|
+
* @param agentDID - Agent DID
|
|
191
|
+
* @returns Amount spent this month
|
|
192
|
+
*/
|
|
193
|
+
getMonthlySpending(agentDID) {
|
|
194
|
+
const thisMonth = this.getCurrentMonth();
|
|
195
|
+
const entry = this.monthlySpending.get(agentDID);
|
|
196
|
+
if (!entry || entry.month !== thisMonth)
|
|
197
|
+
return 0;
|
|
198
|
+
return entry.amount;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Reset all limits for an agent (for testing)
|
|
202
|
+
*
|
|
203
|
+
* @param agentDID - Agent DID
|
|
204
|
+
*/
|
|
205
|
+
reset(agentDID) {
|
|
206
|
+
this.rateLimits.delete(agentDID);
|
|
207
|
+
this.dailySpending.delete(agentDID);
|
|
208
|
+
this.monthlySpending.delete(agentDID);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Check rate limit
|
|
212
|
+
*/
|
|
213
|
+
checkRateLimit(agentDID, limit) {
|
|
214
|
+
const current = this.getCurrentRateLimit(agentDID);
|
|
215
|
+
if (current >= limit) {
|
|
216
|
+
const details = {
|
|
217
|
+
requested: current + 1,
|
|
218
|
+
allowed: limit,
|
|
219
|
+
constraint: 'callsPerMinute',
|
|
220
|
+
current,
|
|
221
|
+
fix: `Wait before making additional calls, or update policy.limits.callsPerMinute to a higher value`,
|
|
222
|
+
};
|
|
223
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Rate limit exceeded: ${current}/${limit} calls per minute`, details));
|
|
224
|
+
}
|
|
225
|
+
return (0, shared_1.ok)(undefined);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Check daily spending limit
|
|
229
|
+
*/
|
|
230
|
+
checkDailyLimit(agentDID, amount, limit) {
|
|
231
|
+
const current = this.getDailySpending(agentDID);
|
|
232
|
+
const newTotal = current + amount;
|
|
233
|
+
if (newTotal > limit) {
|
|
234
|
+
const remaining = limit - current;
|
|
235
|
+
const details = {
|
|
236
|
+
requested: amount,
|
|
237
|
+
allowed: limit,
|
|
238
|
+
constraint: 'dailyAmount',
|
|
239
|
+
current,
|
|
240
|
+
newTotal,
|
|
241
|
+
fix: remaining > 0
|
|
242
|
+
? `Reduce amount to ${remaining} or less (remaining today), or update policy.limits.dailyAmount`
|
|
243
|
+
: `Daily limit already reached. Wait until tomorrow or update policy.limits.dailyAmount`,
|
|
244
|
+
};
|
|
245
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Amount ${amount} would exceed daily limit: ${newTotal}/${limit} (current: ${current})`, details));
|
|
246
|
+
}
|
|
247
|
+
return (0, shared_1.ok)(undefined);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Extract amount from params
|
|
251
|
+
*/
|
|
252
|
+
extractAmount(params) {
|
|
253
|
+
if (typeof params !== 'object' || params === null)
|
|
254
|
+
return null;
|
|
255
|
+
const obj = params;
|
|
256
|
+
// Check common amount field names
|
|
257
|
+
if (typeof obj.amount === 'number')
|
|
258
|
+
return obj.amount;
|
|
259
|
+
if (typeof obj.value === 'number')
|
|
260
|
+
return obj.value;
|
|
261
|
+
if (typeof obj.price === 'number')
|
|
262
|
+
return obj.price;
|
|
263
|
+
if (typeof obj.total === 'number')
|
|
264
|
+
return obj.total;
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Get current day identifier (YYYY-MM-DD in UTC)
|
|
269
|
+
*/
|
|
270
|
+
getCurrentDay() {
|
|
271
|
+
const now = new Date();
|
|
272
|
+
return now.toISOString().split('T')[0] ?? '';
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Get current month identifier (YYYY-MM in UTC)
|
|
276
|
+
*/
|
|
277
|
+
getCurrentMonth() {
|
|
278
|
+
const now = new Date();
|
|
279
|
+
const iso = now.toISOString();
|
|
280
|
+
return iso.substring(0, 7); // YYYY-MM
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Check monthly spending limit
|
|
284
|
+
*/
|
|
285
|
+
checkMonthlyLimit(agentDID, amount, limit) {
|
|
286
|
+
const current = this.getMonthlySpending(agentDID);
|
|
287
|
+
const newTotal = current + amount;
|
|
288
|
+
if (newTotal > limit) {
|
|
289
|
+
const remaining = limit - current;
|
|
290
|
+
const details = {
|
|
291
|
+
requested: amount,
|
|
292
|
+
allowed: limit,
|
|
293
|
+
constraint: 'dailyAmount', // Reuse constraint type (will add monthlyAmount later)
|
|
294
|
+
current,
|
|
295
|
+
newTotal,
|
|
296
|
+
fix: remaining > 0
|
|
297
|
+
? `Reduce amount to ${remaining} or less (remaining this month), or update policy.limits.monthlyAmount`
|
|
298
|
+
: `Monthly limit already reached. Wait until next month or update policy.limits.monthlyAmount`,
|
|
299
|
+
};
|
|
300
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Amount ${amount} would exceed monthly limit: ${newTotal}/${limit} (current: ${current})`, details));
|
|
301
|
+
}
|
|
302
|
+
return (0, shared_1.ok)(undefined);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
exports.PolicyEngine = PolicyEngine;
|
|
306
|
+
/**
|
|
307
|
+
* Global policy engine instance (singleton)
|
|
308
|
+
*/
|
|
309
|
+
let globalPolicyEngine = null;
|
|
310
|
+
/**
|
|
311
|
+
* Get the global policy engine instance
|
|
312
|
+
*
|
|
313
|
+
* @returns PolicyEngine instance
|
|
314
|
+
*/
|
|
315
|
+
function getGlobalPolicyEngine() {
|
|
316
|
+
if (!globalPolicyEngine) {
|
|
317
|
+
globalPolicyEngine = new PolicyEngine();
|
|
318
|
+
}
|
|
319
|
+
return globalPolicyEngine;
|
|
320
|
+
}
|