@lightdash-tools/common 0.13.1 → 0.13.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/dist/agentops/extract-yaml-project.d.ts +0 -2
- package/dist/agentops/extract-yaml-project.js +0 -9
- package/dist/argument-validation.d.ts +2 -16
- package/dist/argument-validation.js +2 -106
- package/dist/argument-validation.test.js +1 -71
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -7
- package/dist/safety.d.ts +0 -19
- package/dist/safety.js +0 -30
- package/dist/safety.test.js +0 -52
- package/package.json +1 -1
- package/src/agentops/extract-yaml-project.ts +0 -9
- package/src/argument-validation.test.ts +2 -108
- package/src/argument-validation.ts +2 -137
- package/src/index.ts +2 -9
- package/src/safety.test.ts +0 -76
- package/src/safety.ts +0 -47
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -9
- package/src/index.test.ts +0 -9
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/** True when MCP tool args carry AgentOps YAML documents that imply a project scope. */
|
|
2
|
-
export declare function hasYamlProjectDocumentArgs(args: unknown): boolean;
|
|
3
1
|
/**
|
|
4
2
|
* Extracts project UUIDs from MCP tool args, including YAML document fields
|
|
5
3
|
* (`bundleYaml`, `gateYaml`) used by AgentOps composite tools.
|
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasYamlProjectDocumentArgs = hasYamlProjectDocumentArgs;
|
|
4
3
|
exports.extractProjectUuidsFromToolArgs = extractProjectUuidsFromToolArgs;
|
|
5
4
|
const types_1 = require("./types");
|
|
6
|
-
/** True when MCP tool args carry AgentOps YAML documents that imply a project scope. */
|
|
7
|
-
function hasYamlProjectDocumentArgs(args) {
|
|
8
|
-
if (typeof args !== 'object' || args === null || Array.isArray(args)) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
const record = args;
|
|
12
|
-
return typeof record.bundleYaml === 'string' || typeof record.gateYaml === 'string';
|
|
13
|
-
}
|
|
14
5
|
/**
|
|
15
6
|
* Extracts project UUIDs from MCP tool args, including YAML document fields
|
|
16
7
|
* (`bundleYaml`, `gateYaml`) used by AgentOps composite tools.
|
|
@@ -1,23 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Validates known resource identifier keys
|
|
2
|
+
* Resource-identifier validation for CLI and MCP tools.
|
|
3
|
+
* Validates known resource identifier keys when walking tool/command argument objects.
|
|
4
4
|
*/
|
|
5
5
|
/** Object keys whose string values are treated as resource identifiers. */
|
|
6
6
|
export declare const RESOURCE_ID_KEYS: Set<string>;
|
|
7
|
-
export type ArgumentSource = 'body' | 'option' | 'positional';
|
|
8
|
-
export type ArgumentSemanticType = 'boolean' | 'fingerprint' | 'free-text' | 'json' | 'number' | 'slug' | 'uuid-or-slug' | 'uuid';
|
|
9
|
-
/** Per-argument validation metadata supplied by CLI/MCP command definitions. */
|
|
10
|
-
export interface ArgumentDescriptor {
|
|
11
|
-
name: string;
|
|
12
|
-
source: ArgumentSource;
|
|
13
|
-
semanticType: ArgumentSemanticType;
|
|
14
|
-
required?: boolean;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Validates tool arguments against explicit descriptors.
|
|
18
|
-
* Identifier semantic types (`uuid`, `slug`, `fingerprint`) receive strict validation.
|
|
19
|
-
*/
|
|
20
|
-
export declare function validateArguments(args: Record<string, unknown>, descriptors: ArgumentDescriptor[]): void;
|
|
21
7
|
/**
|
|
22
8
|
* Recursively walks an object (and nested arrays/objects) and validates any
|
|
23
9
|
* string value under a key in {@link RESOURCE_ID_KEYS}.
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* Validates known resource identifier keys
|
|
3
|
+
* Resource-identifier validation for CLI and MCP tools.
|
|
4
|
+
* Validates known resource identifier keys when walking tool/command argument objects.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.RESOURCE_ID_KEYS = void 0;
|
|
8
|
-
exports.validateArguments = validateArguments;
|
|
9
8
|
exports.validateResourceIdsInObject = validateResourceIdsInObject;
|
|
10
9
|
const input_validation_1 = require("./input-validation");
|
|
11
10
|
/** Object keys whose string values are treated as resource identifiers. */
|
|
@@ -58,86 +57,6 @@ const UUID_OR_SLUG_KEYS = new Set([
|
|
|
58
57
|
'sourceChartUuidOrSlug',
|
|
59
58
|
'contentUuidOrSlug',
|
|
60
59
|
]);
|
|
61
|
-
function isMissing(value) {
|
|
62
|
-
return value === undefined || value === null;
|
|
63
|
-
}
|
|
64
|
-
function requireString(value, name, label) {
|
|
65
|
-
if (typeof value !== 'string') {
|
|
66
|
-
throw new Error(`Argument '${name}' must be a string ${label}`);
|
|
67
|
-
}
|
|
68
|
-
return value;
|
|
69
|
-
}
|
|
70
|
-
function validateUuidArgument(value, name) {
|
|
71
|
-
(0, input_validation_1.validateUuid)(requireString(value, name, 'UUID'));
|
|
72
|
-
}
|
|
73
|
-
function validateSlugArgument(value, name) {
|
|
74
|
-
(0, input_validation_1.validateSlug)(requireString(value, name, 'slug'));
|
|
75
|
-
}
|
|
76
|
-
function validateFingerprintArgument(value, name) {
|
|
77
|
-
(0, input_validation_1.validateFingerprint)(requireString(value, name, 'fingerprint'));
|
|
78
|
-
}
|
|
79
|
-
function validateFreeTextArgument(value, _name) {
|
|
80
|
-
if (typeof value === 'string') {
|
|
81
|
-
(0, input_validation_1.rejectControlChars)(value);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function validateJsonArgument(value, name) {
|
|
85
|
-
if (typeof value !== 'string') {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
(0, input_validation_1.rejectControlChars)(value);
|
|
89
|
-
try {
|
|
90
|
-
JSON.parse(value);
|
|
91
|
-
}
|
|
92
|
-
catch (_a) {
|
|
93
|
-
throw new Error(`Argument '${name}' must be valid JSON`);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
function validateBooleanArgument(value, name) {
|
|
97
|
-
if (typeof value !== 'boolean') {
|
|
98
|
-
throw new Error(`Argument '${name}' must be a boolean`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
function validateNumberArgument(value, name) {
|
|
102
|
-
if (typeof value !== 'number' || Number.isNaN(value)) {
|
|
103
|
-
throw new Error(`Argument '${name}' must be a number`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
function validateUuidOrSlugArgument(value, name) {
|
|
107
|
-
(0, input_validation_1.validateUuidOrSlug)(requireString(value, name, 'UUID or slug'));
|
|
108
|
-
}
|
|
109
|
-
function validateBySemanticType(value, semanticType, name) {
|
|
110
|
-
switch (semanticType) {
|
|
111
|
-
case 'boolean':
|
|
112
|
-
validateBooleanArgument(value, name);
|
|
113
|
-
break;
|
|
114
|
-
case 'fingerprint':
|
|
115
|
-
validateFingerprintArgument(value, name);
|
|
116
|
-
break;
|
|
117
|
-
case 'free-text':
|
|
118
|
-
validateFreeTextArgument(value, name);
|
|
119
|
-
break;
|
|
120
|
-
case 'json':
|
|
121
|
-
validateJsonArgument(value, name);
|
|
122
|
-
break;
|
|
123
|
-
case 'number':
|
|
124
|
-
validateNumberArgument(value, name);
|
|
125
|
-
break;
|
|
126
|
-
case 'slug':
|
|
127
|
-
validateSlugArgument(value, name);
|
|
128
|
-
break;
|
|
129
|
-
case 'uuid':
|
|
130
|
-
validateUuidArgument(value, name);
|
|
131
|
-
break;
|
|
132
|
-
case 'uuid-or-slug':
|
|
133
|
-
validateUuidOrSlugArgument(value, name);
|
|
134
|
-
break;
|
|
135
|
-
default: {
|
|
136
|
-
const exhaustive = semanticType;
|
|
137
|
-
throw new Error(`Unknown semantic type: ${exhaustive}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
60
|
function validateResourceIdValue(key, value) {
|
|
142
61
|
if (key === 'slug' || key === 'newSlug') {
|
|
143
62
|
(0, input_validation_1.validateSlug)(value);
|
|
@@ -167,29 +86,6 @@ function validateResourceIdValues(key, value) {
|
|
|
167
86
|
}
|
|
168
87
|
}
|
|
169
88
|
}
|
|
170
|
-
/**
|
|
171
|
-
* Validates tool arguments against explicit descriptors.
|
|
172
|
-
* Identifier semantic types (`uuid`, `slug`, `fingerprint`) receive strict validation.
|
|
173
|
-
*/
|
|
174
|
-
function validateArguments(args, descriptors) {
|
|
175
|
-
for (const descriptor of descriptors) {
|
|
176
|
-
const value = args[descriptor.name];
|
|
177
|
-
if (isMissing(value)) {
|
|
178
|
-
if (descriptor.required) {
|
|
179
|
-
throw new Error(`Required argument '${descriptor.name}' is missing`);
|
|
180
|
-
}
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
if (Array.isArray(value)) {
|
|
184
|
-
for (const item of value) {
|
|
185
|
-
validateBySemanticType(item, descriptor.semanticType, descriptor.name);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
validateBySemanticType(value, descriptor.semanticType, descriptor.name);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
89
|
/**
|
|
194
90
|
* Recursively walks an object (and nested arrays/objects) and validates any
|
|
195
91
|
* string value under a key in {@link RESOURCE_ID_KEYS}.
|
|
@@ -5,7 +5,7 @@ const argument_validation_1 = require("./argument-validation");
|
|
|
5
5
|
const VALID_UUID = '550e8400-e29b-41d4-a716-446655440000';
|
|
6
6
|
(0, vitest_1.describe)('argument-validation', () => {
|
|
7
7
|
(0, vitest_1.describe)('RESOURCE_ID_KEYS', () => {
|
|
8
|
-
(0, vitest_1.it)('contains
|
|
8
|
+
(0, vitest_1.it)('contains known resource identifier keys', () => {
|
|
9
9
|
(0, vitest_1.expect)([...argument_validation_1.RESOURCE_ID_KEYS].sort()).toEqual([
|
|
10
10
|
'agentUuid',
|
|
11
11
|
'artifactUuid',
|
|
@@ -48,76 +48,6 @@ const VALID_UUID = '550e8400-e29b-41d4-a716-446655440000';
|
|
|
48
48
|
].sort());
|
|
49
49
|
});
|
|
50
50
|
});
|
|
51
|
-
(0, vitest_1.describe)('validateArguments', () => {
|
|
52
|
-
(0, vitest_1.it)('validates uuid arguments', () => {
|
|
53
|
-
const descriptors = [
|
|
54
|
-
{ name: 'projectUuid', source: 'option', semanticType: 'uuid', required: true },
|
|
55
|
-
];
|
|
56
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ projectUuid: VALID_UUID }, descriptors)).not.toThrow();
|
|
57
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ projectUuid: 'bad-uuid' }, descriptors)).toThrow('Invalid UUID format');
|
|
58
|
-
});
|
|
59
|
-
(0, vitest_1.it)('validates slug arguments', () => {
|
|
60
|
-
const descriptors = [
|
|
61
|
-
{ name: 'slug', source: 'option', semanticType: 'slug' },
|
|
62
|
-
];
|
|
63
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ slug: 'my-chart' }, descriptors)).not.toThrow();
|
|
64
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ slug: 'bad slug' }, descriptors)).toThrow('Slug must contain only alphanumeric characters');
|
|
65
|
-
});
|
|
66
|
-
(0, vitest_1.it)('validates fingerprint arguments', () => {
|
|
67
|
-
const descriptors = [
|
|
68
|
-
{ name: 'fingerprint', source: 'body', semanticType: 'fingerprint' },
|
|
69
|
-
];
|
|
70
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ fingerprint: 'abc123' }, descriptors)).not.toThrow();
|
|
71
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ fingerprint: '' }, descriptors)).toThrow('Fingerprint must be between 1 and 512 characters');
|
|
72
|
-
});
|
|
73
|
-
(0, vitest_1.it)('throws when required argument is missing', () => {
|
|
74
|
-
const descriptors = [
|
|
75
|
-
{ name: 'projectUuid', source: 'option', semanticType: 'uuid', required: true },
|
|
76
|
-
];
|
|
77
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({}, descriptors)).toThrow("Required argument 'projectUuid' is missing");
|
|
78
|
-
});
|
|
79
|
-
(0, vitest_1.it)('skips optional missing arguments', () => {
|
|
80
|
-
const descriptors = [
|
|
81
|
-
{ name: 'slug', source: 'option', semanticType: 'slug' },
|
|
82
|
-
];
|
|
83
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({}, descriptors)).not.toThrow();
|
|
84
|
-
});
|
|
85
|
-
(0, vitest_1.it)('validates uuid arrays', () => {
|
|
86
|
-
const descriptors = [
|
|
87
|
-
{ name: 'projectUuid', source: 'body', semanticType: 'uuid' },
|
|
88
|
-
];
|
|
89
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ projectUuid: [VALID_UUID, VALID_UUID] }, descriptors)).not.toThrow();
|
|
90
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ projectUuid: [VALID_UUID, 'not-uuid'] }, descriptors)).toThrow('Invalid UUID format');
|
|
91
|
-
});
|
|
92
|
-
(0, vitest_1.it)('validates free-text with control char rejection only', () => {
|
|
93
|
-
const descriptors = [
|
|
94
|
-
{ name: 'query', source: 'positional', semanticType: 'free-text' },
|
|
95
|
-
];
|
|
96
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ query: 'what? growth%' }, descriptors)).not.toThrow();
|
|
97
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ query: 'bad\u0000' }, descriptors)).toThrow('Input contains invalid control characters');
|
|
98
|
-
});
|
|
99
|
-
(0, vitest_1.it)('validates json string arguments', () => {
|
|
100
|
-
const descriptors = [
|
|
101
|
-
{ name: 'payload', source: 'body', semanticType: 'json' },
|
|
102
|
-
];
|
|
103
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ payload: '{"a":1}' }, descriptors)).not.toThrow();
|
|
104
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ payload: 'not-json' }, descriptors)).toThrow("Argument 'payload' must be valid JSON");
|
|
105
|
-
});
|
|
106
|
-
(0, vitest_1.it)('validates boolean arguments', () => {
|
|
107
|
-
const descriptors = [
|
|
108
|
-
{ name: 'enabled', source: 'option', semanticType: 'boolean' },
|
|
109
|
-
];
|
|
110
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ enabled: true }, descriptors)).not.toThrow();
|
|
111
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ enabled: 'true' }, descriptors)).toThrow("Argument 'enabled' must be a boolean");
|
|
112
|
-
});
|
|
113
|
-
(0, vitest_1.it)('validates number arguments', () => {
|
|
114
|
-
const descriptors = [
|
|
115
|
-
{ name: 'limit', source: 'option', semanticType: 'number' },
|
|
116
|
-
];
|
|
117
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ limit: 10 }, descriptors)).not.toThrow();
|
|
118
|
-
(0, vitest_1.expect)(() => (0, argument_validation_1.validateArguments)({ limit: Number.NaN }, descriptors)).toThrow("Argument 'limit' must be a number");
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
51
|
(0, vitest_1.describe)('validateResourceIdsInObject', () => {
|
|
122
52
|
(0, vitest_1.it)('validates top-level resource id keys', () => {
|
|
123
53
|
(0, vitest_1.expect)(() => (0, argument_validation_1.validateResourceIdsInObject)({
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export declare function greet(name: string): string;
|
|
2
1
|
export * from './env';
|
|
3
2
|
export * from './safety';
|
|
4
3
|
export * from './input-validation';
|
|
@@ -8,6 +7,6 @@ export * from './agent-safe';
|
|
|
8
7
|
export * from './agentops/types';
|
|
9
8
|
export * from './agentops/snapshots';
|
|
10
9
|
export * from './agentops/formatters';
|
|
11
|
-
export { extractProjectUuidsFromToolArgs
|
|
10
|
+
export { extractProjectUuidsFromToolArgs } from './agentops/extract-yaml-project';
|
|
12
11
|
export * from './audit';
|
|
13
12
|
export * from './types';
|
package/dist/index.js
CHANGED
|
@@ -14,18 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
18
|
-
exports.greet = greet;
|
|
19
|
-
function greet(name) {
|
|
20
|
-
return `Hello, ${name}!`;
|
|
21
|
-
}
|
|
17
|
+
exports.extractProjectUuidsFromToolArgs = void 0;
|
|
22
18
|
// Export env var constants
|
|
23
19
|
__exportStar(require("./env"), exports);
|
|
24
20
|
// Export safety logic
|
|
25
21
|
__exportStar(require("./safety"), exports);
|
|
26
22
|
// Export input validation
|
|
27
23
|
__exportStar(require("./input-validation"), exports);
|
|
28
|
-
// Export argument /
|
|
24
|
+
// Export argument / resource-id validation
|
|
29
25
|
__exportStar(require("./argument-validation"), exports);
|
|
30
26
|
// MCP serving profile ids (shared with audit typing)
|
|
31
27
|
__exportStar(require("./profile-ids"), exports);
|
|
@@ -37,7 +33,6 @@ __exportStar(require("./agentops/snapshots"), exports);
|
|
|
37
33
|
__exportStar(require("./agentops/formatters"), exports);
|
|
38
34
|
var extract_yaml_project_1 = require("./agentops/extract-yaml-project");
|
|
39
35
|
Object.defineProperty(exports, "extractProjectUuidsFromToolArgs", { enumerable: true, get: function () { return extract_yaml_project_1.extractProjectUuidsFromToolArgs; } });
|
|
40
|
-
Object.defineProperty(exports, "hasYamlProjectDocumentArgs", { enumerable: true, get: function () { return extract_yaml_project_1.hasYamlProjectDocumentArgs; } });
|
|
41
36
|
// Export audit logger (shared by MCP and CLI)
|
|
42
37
|
__exportStar(require("./audit"), exports);
|
|
43
38
|
// Curated Lightdash API types + generated OpenAPI shapes (single public path)
|
package/dist/safety.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Semantic impact classification for operations (RFC Phase 0).
|
|
3
|
-
* Used by {@link OperationPolicy} and {@link isOperationAllowed}.
|
|
4
|
-
*/
|
|
5
|
-
export type SafetyImpact = 'credential-sensitive' | 'external-side-effect' | 'read' | 'write-destructive' | 'write-nondestructive';
|
|
6
1
|
/**
|
|
7
2
|
* Hierarchical safety modes for Lightdash AI tools and CLI.
|
|
8
3
|
*/
|
|
@@ -24,10 +19,6 @@ export type ToolAnnotations = {
|
|
|
24
19
|
idempotentHint?: boolean;
|
|
25
20
|
openWorldHint?: boolean;
|
|
26
21
|
};
|
|
27
|
-
/** Policy describing the semantic impact of an operation. */
|
|
28
|
-
export type OperationPolicy = {
|
|
29
|
-
impact: SafetyImpact;
|
|
30
|
-
};
|
|
31
22
|
/** Preset: read-only, non-destructive, idempotent, closed-world. Use for list/get/compile tools. */
|
|
32
23
|
export declare const READ_ONLY_DEFAULT: ToolAnnotations;
|
|
33
24
|
/**
|
|
@@ -51,11 +42,6 @@ export declare const WRITE_DESTRUCTIVE: ToolAnnotations;
|
|
|
51
42
|
* Unknown modes default to permissive for backward compatibility.
|
|
52
43
|
*/
|
|
53
44
|
export declare function isAllowed(mode: SafetyMode | string, annotations: ToolAnnotations): boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Validates if an operation is allowed in the current safety mode using semantic impact policy.
|
|
56
|
-
* Unknown modes fail closed.
|
|
57
|
-
*/
|
|
58
|
-
export declare function isOperationAllowed(mode: SafetyMode | string, policy: OperationPolicy): boolean;
|
|
59
45
|
/**
|
|
60
46
|
* Resolves safety mode from environment variable.
|
|
61
47
|
* Accepts `write-idempotent` as a deprecated alias for `write-nondestructive`.
|
|
@@ -70,11 +56,6 @@ export declare function getSafetyModeFromEnv(): SafetyMode;
|
|
|
70
56
|
* Note: CLI `--projects` takes priority over this env var.
|
|
71
57
|
*/
|
|
72
58
|
export declare function getAllowedProjectUuidsFromEnv(): string[];
|
|
73
|
-
/**
|
|
74
|
-
* Returns true if a single projectUuid is permitted by the allowlist.
|
|
75
|
-
* An empty allowlist means all projects are allowed.
|
|
76
|
-
*/
|
|
77
|
-
export declare function isProjectAllowed(allowedUuids: readonly string[], projectUuid: string): boolean;
|
|
78
59
|
/**
|
|
79
60
|
* Returns true when every UUID in projectUuids is permitted by the allowlist.
|
|
80
61
|
* An empty allowlist means all projects are allowed.
|
package/dist/safety.js
CHANGED
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WRITE_DESTRUCTIVE = exports.WRITE_OPEN_WORLD = exports.WRITE_NONDESTRUCTIVE = exports.WRITE_IDEMPOTENT = exports.READ_ONLY_TRANSIENT = exports.READ_ONLY_DEFAULT = exports.SafetyMode = void 0;
|
|
4
4
|
exports.isAllowed = isAllowed;
|
|
5
|
-
exports.isOperationAllowed = isOperationAllowed;
|
|
6
5
|
exports.getSafetyModeFromEnv = getSafetyModeFromEnv;
|
|
7
6
|
exports.getAllowedProjectUuidsFromEnv = getAllowedProjectUuidsFromEnv;
|
|
8
|
-
exports.isProjectAllowed = isProjectAllowed;
|
|
9
7
|
exports.areAllProjectsAllowed = areAllProjectsAllowed;
|
|
10
8
|
exports.extractProjectUuids = extractProjectUuids;
|
|
11
9
|
const extract_yaml_project_1 = require("./agentops/extract-yaml-project");
|
|
@@ -88,25 +86,6 @@ function isAllowed(mode, annotations) {
|
|
|
88
86
|
return true;
|
|
89
87
|
}
|
|
90
88
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Validates if an operation is allowed in the current safety mode using semantic impact policy.
|
|
93
|
-
* Unknown modes fail closed.
|
|
94
|
-
*/
|
|
95
|
-
function isOperationAllowed(mode, policy) {
|
|
96
|
-
switch (mode) {
|
|
97
|
-
case SafetyMode.READ_ONLY:
|
|
98
|
-
return policy.impact === 'read';
|
|
99
|
-
case SafetyMode.WRITE_NONDESTRUCTIVE:
|
|
100
|
-
case SafetyMode.WRITE_IDEMPOTENT:
|
|
101
|
-
return (policy.impact === 'read' ||
|
|
102
|
-
policy.impact === 'write-nondestructive' ||
|
|
103
|
-
policy.impact === 'external-side-effect');
|
|
104
|
-
case SafetyMode.WRITE_DESTRUCTIVE:
|
|
105
|
-
return true;
|
|
106
|
-
default:
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
89
|
/**
|
|
111
90
|
* Resolves safety mode from environment variable.
|
|
112
91
|
* Accepts `write-idempotent` as a deprecated alias for `write-nondestructive`.
|
|
@@ -142,15 +121,6 @@ function getAllowedProjectUuidsFromEnv() {
|
|
|
142
121
|
.map((s) => s.trim())
|
|
143
122
|
.filter(Boolean);
|
|
144
123
|
}
|
|
145
|
-
/**
|
|
146
|
-
* Returns true if a single projectUuid is permitted by the allowlist.
|
|
147
|
-
* An empty allowlist means all projects are allowed.
|
|
148
|
-
*/
|
|
149
|
-
function isProjectAllowed(allowedUuids, projectUuid) {
|
|
150
|
-
if (allowedUuids.length === 0)
|
|
151
|
-
return true;
|
|
152
|
-
return allowedUuids.includes(projectUuid);
|
|
153
|
-
}
|
|
154
124
|
/**
|
|
155
125
|
* Returns true when every UUID in projectUuids is permitted by the allowlist.
|
|
156
126
|
* An empty allowlist means all projects are allowed.
|
package/dist/safety.test.js
CHANGED
|
@@ -5,16 +5,6 @@ const safety_1 = require("./safety");
|
|
|
5
5
|
const UUID_A = '11111111-1111-1111-1111-111111111111';
|
|
6
6
|
const UUID_B = '22222222-2222-2222-2222-222222222222';
|
|
7
7
|
const UUID_C = '33333333-3333-3333-3333-333333333333';
|
|
8
|
-
const ALL_IMPACTS = [
|
|
9
|
-
'read',
|
|
10
|
-
'write-nondestructive',
|
|
11
|
-
'write-destructive',
|
|
12
|
-
'credential-sensitive',
|
|
13
|
-
'external-side-effect',
|
|
14
|
-
];
|
|
15
|
-
function policy(impact) {
|
|
16
|
-
return { impact };
|
|
17
|
-
}
|
|
18
8
|
(0, vitest_1.describe)('Safety Logic', () => {
|
|
19
9
|
(0, vitest_1.describe)('annotation presets', () => {
|
|
20
10
|
(0, vitest_1.it)('should define READ_ONLY_DEFAULT as read-only closed-world', () => {
|
|
@@ -101,37 +91,6 @@ function policy(impact) {
|
|
|
101
91
|
(0, vitest_1.expect)((0, safety_1.isAllowed)('custom-mode', safety_1.WRITE_DESTRUCTIVE)).toBe(true);
|
|
102
92
|
});
|
|
103
93
|
});
|
|
104
|
-
(0, vitest_1.describe)('isOperationAllowed', () => {
|
|
105
|
-
(0, vitest_1.it)('should allow only read impact in read-only mode', () => {
|
|
106
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('read'))).toBe(true);
|
|
107
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('write-nondestructive'))).toBe(false);
|
|
108
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('write-destructive'))).toBe(false);
|
|
109
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('credential-sensitive'))).toBe(false);
|
|
110
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('external-side-effect'))).toBe(false);
|
|
111
|
-
});
|
|
112
|
-
(0, vitest_1.it)('should allow read, write-nondestructive, and external-side-effect in write-nondestructive mode', () => {
|
|
113
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('read'))).toBe(true);
|
|
114
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-nondestructive'))).toBe(true);
|
|
115
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('external-side-effect'))).toBe(true);
|
|
116
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-destructive'))).toBe(false);
|
|
117
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('credential-sensitive'))).toBe(false);
|
|
118
|
-
});
|
|
119
|
-
(0, vitest_1.it)('should treat write-idempotent mode the same as write-nondestructive for policy checks', () => {
|
|
120
|
-
for (const impact of ALL_IMPACTS) {
|
|
121
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, policy(impact))).toBe((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy(impact)));
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
(0, vitest_1.it)('should allow all impacts in write-destructive mode', () => {
|
|
125
|
-
for (const impact of ALL_IMPACTS) {
|
|
126
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, policy(impact))).toBe(true);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
(0, vitest_1.it)('should fail closed for unknown mode strings', () => {
|
|
130
|
-
for (const impact of ALL_IMPACTS) {
|
|
131
|
-
(0, vitest_1.expect)((0, safety_1.isOperationAllowed)('custom-mode', policy(impact))).toBe(false);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
94
|
(0, vitest_1.describe)('getSafetyModeFromEnv', () => {
|
|
136
95
|
const originalEnv = process.env.LIGHTDASH_TOOLS_SAFETY_MODE;
|
|
137
96
|
(0, vitest_1.afterEach)(() => {
|
|
@@ -207,17 +166,6 @@ function policy(impact) {
|
|
|
207
166
|
(0, vitest_1.expect)(() => (0, safety_1.getAllowedProjectUuidsFromEnv)()).toThrow(/LIGHTDASH_TOOLS_ALLOWED_PROJECT_UUIDS/);
|
|
208
167
|
});
|
|
209
168
|
});
|
|
210
|
-
(0, vitest_1.describe)('isProjectAllowed', () => {
|
|
211
|
-
(0, vitest_1.it)('should allow all projects when allowlist is empty', () => {
|
|
212
|
-
(0, vitest_1.expect)((0, safety_1.isProjectAllowed)([], 'any-uuid')).toBe(true);
|
|
213
|
-
});
|
|
214
|
-
(0, vitest_1.it)('should allow a project that is in the allowlist', () => {
|
|
215
|
-
(0, vitest_1.expect)((0, safety_1.isProjectAllowed)(['uuid-a', 'uuid-b'], 'uuid-a')).toBe(true);
|
|
216
|
-
});
|
|
217
|
-
(0, vitest_1.it)('should deny a project that is not in the allowlist', () => {
|
|
218
|
-
(0, vitest_1.expect)((0, safety_1.isProjectAllowed)(['uuid-a', 'uuid-b'], 'uuid-c')).toBe(false);
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
169
|
(0, vitest_1.describe)('areAllProjectsAllowed', () => {
|
|
222
170
|
(0, vitest_1.it)('should allow everything when allowlist is empty', () => {
|
|
223
171
|
(0, vitest_1.expect)((0, safety_1.areAllProjectsAllowed)([], ['uuid-a', 'uuid-b'])).toBe(true);
|
package/package.json
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
import { parseLightdashAiAgentBundle, parseLightdashAiEvaluationGate } from './types';
|
|
2
2
|
|
|
3
|
-
/** True when MCP tool args carry AgentOps YAML documents that imply a project scope. */
|
|
4
|
-
export function hasYamlProjectDocumentArgs(args: unknown): boolean {
|
|
5
|
-
if (typeof args !== 'object' || args === null || Array.isArray(args)) {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
|
-
const record = args as Record<string, unknown>;
|
|
9
|
-
return typeof record.bundleYaml === 'string' || typeof record.gateYaml === 'string';
|
|
10
|
-
}
|
|
11
|
-
|
|
12
3
|
/**
|
|
13
4
|
* Extracts project UUIDs from MCP tool args, including YAML document fields
|
|
14
5
|
* (`bundleYaml`, `gateYaml`) used by AgentOps composite tools.
|
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
RESOURCE_ID_KEYS,
|
|
5
|
-
validateArguments,
|
|
6
|
-
validateResourceIdsInObject,
|
|
7
|
-
} from './argument-validation';
|
|
8
|
-
|
|
9
|
-
import type { ArgumentDescriptor } from './argument-validation';
|
|
3
|
+
import { RESOURCE_ID_KEYS, validateResourceIdsInObject } from './argument-validation';
|
|
10
4
|
|
|
11
5
|
const VALID_UUID = '550e8400-e29b-41d4-a716-446655440000';
|
|
12
6
|
|
|
13
7
|
describe('argument-validation', () => {
|
|
14
8
|
describe('RESOURCE_ID_KEYS', () => {
|
|
15
|
-
it('contains
|
|
9
|
+
it('contains known resource identifier keys', () => {
|
|
16
10
|
expect([...RESOURCE_ID_KEYS].sort()).toEqual(
|
|
17
11
|
[
|
|
18
12
|
'agentUuid',
|
|
@@ -58,106 +52,6 @@ describe('argument-validation', () => {
|
|
|
58
52
|
});
|
|
59
53
|
});
|
|
60
54
|
|
|
61
|
-
describe('validateArguments', () => {
|
|
62
|
-
it('validates uuid arguments', () => {
|
|
63
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
64
|
-
{ name: 'projectUuid', source: 'option', semanticType: 'uuid', required: true },
|
|
65
|
-
];
|
|
66
|
-
expect(() => validateArguments({ projectUuid: VALID_UUID }, descriptors)).not.toThrow();
|
|
67
|
-
expect(() => validateArguments({ projectUuid: 'bad-uuid' }, descriptors)).toThrow(
|
|
68
|
-
'Invalid UUID format',
|
|
69
|
-
);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('validates slug arguments', () => {
|
|
73
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
74
|
-
{ name: 'slug', source: 'option', semanticType: 'slug' },
|
|
75
|
-
];
|
|
76
|
-
expect(() => validateArguments({ slug: 'my-chart' }, descriptors)).not.toThrow();
|
|
77
|
-
expect(() => validateArguments({ slug: 'bad slug' }, descriptors)).toThrow(
|
|
78
|
-
'Slug must contain only alphanumeric characters',
|
|
79
|
-
);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('validates fingerprint arguments', () => {
|
|
83
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
84
|
-
{ name: 'fingerprint', source: 'body', semanticType: 'fingerprint' },
|
|
85
|
-
];
|
|
86
|
-
expect(() => validateArguments({ fingerprint: 'abc123' }, descriptors)).not.toThrow();
|
|
87
|
-
expect(() => validateArguments({ fingerprint: '' }, descriptors)).toThrow(
|
|
88
|
-
'Fingerprint must be between 1 and 512 characters',
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('throws when required argument is missing', () => {
|
|
93
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
94
|
-
{ name: 'projectUuid', source: 'option', semanticType: 'uuid', required: true },
|
|
95
|
-
];
|
|
96
|
-
expect(() => validateArguments({}, descriptors)).toThrow(
|
|
97
|
-
"Required argument 'projectUuid' is missing",
|
|
98
|
-
);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('skips optional missing arguments', () => {
|
|
102
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
103
|
-
{ name: 'slug', source: 'option', semanticType: 'slug' },
|
|
104
|
-
];
|
|
105
|
-
expect(() => validateArguments({}, descriptors)).not.toThrow();
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('validates uuid arrays', () => {
|
|
109
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
110
|
-
{ name: 'projectUuid', source: 'body', semanticType: 'uuid' },
|
|
111
|
-
];
|
|
112
|
-
expect(() =>
|
|
113
|
-
validateArguments({ projectUuid: [VALID_UUID, VALID_UUID] }, descriptors),
|
|
114
|
-
).not.toThrow();
|
|
115
|
-
expect(() =>
|
|
116
|
-
validateArguments({ projectUuid: [VALID_UUID, 'not-uuid'] }, descriptors),
|
|
117
|
-
).toThrow('Invalid UUID format');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('validates free-text with control char rejection only', () => {
|
|
121
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
122
|
-
{ name: 'query', source: 'positional', semanticType: 'free-text' },
|
|
123
|
-
];
|
|
124
|
-
expect(() => validateArguments({ query: 'what? growth%' }, descriptors)).not.toThrow();
|
|
125
|
-
expect(() => validateArguments({ query: 'bad\u0000' }, descriptors)).toThrow(
|
|
126
|
-
'Input contains invalid control characters',
|
|
127
|
-
);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('validates json string arguments', () => {
|
|
131
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
132
|
-
{ name: 'payload', source: 'body', semanticType: 'json' },
|
|
133
|
-
];
|
|
134
|
-
expect(() => validateArguments({ payload: '{"a":1}' }, descriptors)).not.toThrow();
|
|
135
|
-
expect(() => validateArguments({ payload: 'not-json' }, descriptors)).toThrow(
|
|
136
|
-
"Argument 'payload' must be valid JSON",
|
|
137
|
-
);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('validates boolean arguments', () => {
|
|
141
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
142
|
-
{ name: 'enabled', source: 'option', semanticType: 'boolean' },
|
|
143
|
-
];
|
|
144
|
-
expect(() => validateArguments({ enabled: true }, descriptors)).not.toThrow();
|
|
145
|
-
expect(() => validateArguments({ enabled: 'true' }, descriptors)).toThrow(
|
|
146
|
-
"Argument 'enabled' must be a boolean",
|
|
147
|
-
);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('validates number arguments', () => {
|
|
151
|
-
const descriptors: ArgumentDescriptor[] = [
|
|
152
|
-
{ name: 'limit', source: 'option', semanticType: 'number' },
|
|
153
|
-
];
|
|
154
|
-
expect(() => validateArguments({ limit: 10 }, descriptors)).not.toThrow();
|
|
155
|
-
expect(() => validateArguments({ limit: Number.NaN }, descriptors)).toThrow(
|
|
156
|
-
"Argument 'limit' must be a number",
|
|
157
|
-
);
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
55
|
describe('validateResourceIdsInObject', () => {
|
|
162
56
|
it('validates top-level resource id keys', () => {
|
|
163
57
|
expect(() =>
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Validates known resource identifier keys
|
|
2
|
+
* Resource-identifier validation for CLI and MCP tools.
|
|
3
|
+
* Validates known resource identifier keys when walking tool/command argument objects.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
|
-
rejectControlChars,
|
|
8
7
|
validateFingerprint,
|
|
9
8
|
validateSlug,
|
|
10
9
|
validateUuid,
|
|
@@ -63,113 +62,6 @@ const UUID_OR_SLUG_KEYS = new Set([
|
|
|
63
62
|
'contentUuidOrSlug',
|
|
64
63
|
]);
|
|
65
64
|
|
|
66
|
-
export type ArgumentSource = 'body' | 'option' | 'positional';
|
|
67
|
-
|
|
68
|
-
export type ArgumentSemanticType =
|
|
69
|
-
'boolean' | 'fingerprint' | 'free-text' | 'json' | 'number' | 'slug' | 'uuid-or-slug' | 'uuid';
|
|
70
|
-
|
|
71
|
-
/** Per-argument validation metadata supplied by CLI/MCP command definitions. */
|
|
72
|
-
export interface ArgumentDescriptor {
|
|
73
|
-
name: string;
|
|
74
|
-
source: ArgumentSource;
|
|
75
|
-
semanticType: ArgumentSemanticType;
|
|
76
|
-
required?: boolean;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function isMissing(value: unknown): boolean {
|
|
80
|
-
return value === undefined || value === null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function requireString(value: unknown, name: string, label: string): string {
|
|
84
|
-
if (typeof value !== 'string') {
|
|
85
|
-
throw new Error(`Argument '${name}' must be a string ${label}`);
|
|
86
|
-
}
|
|
87
|
-
return value;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function validateUuidArgument(value: unknown, name: string): void {
|
|
91
|
-
validateUuid(requireString(value, name, 'UUID'));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function validateSlugArgument(value: unknown, name: string): void {
|
|
95
|
-
validateSlug(requireString(value, name, 'slug'));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function validateFingerprintArgument(value: unknown, name: string): void {
|
|
99
|
-
validateFingerprint(requireString(value, name, 'fingerprint'));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function validateFreeTextArgument(value: unknown, _name: string): void {
|
|
103
|
-
if (typeof value === 'string') {
|
|
104
|
-
rejectControlChars(value);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function validateJsonArgument(value: unknown, name: string): void {
|
|
109
|
-
if (typeof value !== 'string') {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
rejectControlChars(value);
|
|
113
|
-
try {
|
|
114
|
-
JSON.parse(value);
|
|
115
|
-
} catch {
|
|
116
|
-
throw new Error(`Argument '${name}' must be valid JSON`);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function validateBooleanArgument(value: unknown, name: string): void {
|
|
121
|
-
if (typeof value !== 'boolean') {
|
|
122
|
-
throw new Error(`Argument '${name}' must be a boolean`);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function validateNumberArgument(value: unknown, name: string): void {
|
|
127
|
-
if (typeof value !== 'number' || Number.isNaN(value)) {
|
|
128
|
-
throw new Error(`Argument '${name}' must be a number`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function validateUuidOrSlugArgument(value: unknown, name: string): void {
|
|
133
|
-
validateUuidOrSlug(requireString(value, name, 'UUID or slug'));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function validateBySemanticType(
|
|
137
|
-
value: unknown,
|
|
138
|
-
semanticType: ArgumentSemanticType,
|
|
139
|
-
name: string,
|
|
140
|
-
): void {
|
|
141
|
-
switch (semanticType) {
|
|
142
|
-
case 'boolean':
|
|
143
|
-
validateBooleanArgument(value, name);
|
|
144
|
-
break;
|
|
145
|
-
case 'fingerprint':
|
|
146
|
-
validateFingerprintArgument(value, name);
|
|
147
|
-
break;
|
|
148
|
-
case 'free-text':
|
|
149
|
-
validateFreeTextArgument(value, name);
|
|
150
|
-
break;
|
|
151
|
-
case 'json':
|
|
152
|
-
validateJsonArgument(value, name);
|
|
153
|
-
break;
|
|
154
|
-
case 'number':
|
|
155
|
-
validateNumberArgument(value, name);
|
|
156
|
-
break;
|
|
157
|
-
case 'slug':
|
|
158
|
-
validateSlugArgument(value, name);
|
|
159
|
-
break;
|
|
160
|
-
case 'uuid':
|
|
161
|
-
validateUuidArgument(value, name);
|
|
162
|
-
break;
|
|
163
|
-
case 'uuid-or-slug':
|
|
164
|
-
validateUuidOrSlugArgument(value, name);
|
|
165
|
-
break;
|
|
166
|
-
default: {
|
|
167
|
-
const exhaustive: never = semanticType;
|
|
168
|
-
throw new Error(`Unknown semantic type: ${exhaustive}`);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
65
|
function validateResourceIdValue(key: string, value: string): void {
|
|
174
66
|
if (key === 'slug' || key === 'newSlug') {
|
|
175
67
|
validateSlug(value);
|
|
@@ -201,33 +93,6 @@ function validateResourceIdValues(key: string, value: unknown): void {
|
|
|
201
93
|
}
|
|
202
94
|
}
|
|
203
95
|
|
|
204
|
-
/**
|
|
205
|
-
* Validates tool arguments against explicit descriptors.
|
|
206
|
-
* Identifier semantic types (`uuid`, `slug`, `fingerprint`) receive strict validation.
|
|
207
|
-
*/
|
|
208
|
-
export function validateArguments(
|
|
209
|
-
args: Record<string, unknown>,
|
|
210
|
-
descriptors: ArgumentDescriptor[],
|
|
211
|
-
): void {
|
|
212
|
-
for (const descriptor of descriptors) {
|
|
213
|
-
const value = args[descriptor.name];
|
|
214
|
-
if (isMissing(value)) {
|
|
215
|
-
if (descriptor.required) {
|
|
216
|
-
throw new Error(`Required argument '${descriptor.name}' is missing`);
|
|
217
|
-
}
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (Array.isArray(value)) {
|
|
222
|
-
for (const item of value) {
|
|
223
|
-
validateBySemanticType(item, descriptor.semanticType, descriptor.name);
|
|
224
|
-
}
|
|
225
|
-
} else {
|
|
226
|
-
validateBySemanticType(value, descriptor.semanticType, descriptor.name);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
96
|
/**
|
|
232
97
|
* Recursively walks an object (and nested arrays/objects) and validates any
|
|
233
98
|
* string value under a key in {@link RESOURCE_ID_KEYS}.
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
export function greet(name: string): string {
|
|
2
|
-
return `Hello, ${name}!`;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
1
|
// Export env var constants
|
|
6
2
|
export * from './env';
|
|
7
3
|
|
|
@@ -11,7 +7,7 @@ export * from './safety';
|
|
|
11
7
|
// Export input validation
|
|
12
8
|
export * from './input-validation';
|
|
13
9
|
|
|
14
|
-
// Export argument /
|
|
10
|
+
// Export argument / resource-id validation
|
|
15
11
|
export * from './argument-validation';
|
|
16
12
|
|
|
17
13
|
// MCP serving profile ids (shared with audit typing)
|
|
@@ -24,10 +20,7 @@ export * from './agent-safe';
|
|
|
24
20
|
export * from './agentops/types';
|
|
25
21
|
export * from './agentops/snapshots';
|
|
26
22
|
export * from './agentops/formatters';
|
|
27
|
-
export {
|
|
28
|
-
extractProjectUuidsFromToolArgs,
|
|
29
|
-
hasYamlProjectDocumentArgs,
|
|
30
|
-
} from './agentops/extract-yaml-project';
|
|
23
|
+
export { extractProjectUuidsFromToolArgs } from './agentops/extract-yaml-project';
|
|
31
24
|
|
|
32
25
|
// Export audit logger (shared by MCP and CLI)
|
|
33
26
|
export * from './audit';
|
package/src/safety.test.ts
CHANGED
|
@@ -3,10 +3,8 @@ import { describe, it, expect, afterEach } from 'vitest';
|
|
|
3
3
|
import {
|
|
4
4
|
SafetyMode,
|
|
5
5
|
isAllowed,
|
|
6
|
-
isOperationAllowed,
|
|
7
6
|
getSafetyModeFromEnv,
|
|
8
7
|
getAllowedProjectUuidsFromEnv,
|
|
9
|
-
isProjectAllowed,
|
|
10
8
|
areAllProjectsAllowed,
|
|
11
9
|
extractProjectUuids,
|
|
12
10
|
READ_ONLY_DEFAULT,
|
|
@@ -14,26 +12,12 @@ import {
|
|
|
14
12
|
WRITE_NONDESTRUCTIVE,
|
|
15
13
|
WRITE_OPEN_WORLD,
|
|
16
14
|
WRITE_DESTRUCTIVE,
|
|
17
|
-
type OperationPolicy,
|
|
18
|
-
type SafetyImpact,
|
|
19
15
|
} from './safety';
|
|
20
16
|
|
|
21
17
|
const UUID_A = '11111111-1111-1111-1111-111111111111';
|
|
22
18
|
const UUID_B = '22222222-2222-2222-2222-222222222222';
|
|
23
19
|
const UUID_C = '33333333-3333-3333-3333-333333333333';
|
|
24
20
|
|
|
25
|
-
const ALL_IMPACTS: SafetyImpact[] = [
|
|
26
|
-
'read',
|
|
27
|
-
'write-nondestructive',
|
|
28
|
-
'write-destructive',
|
|
29
|
-
'credential-sensitive',
|
|
30
|
-
'external-side-effect',
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
function policy(impact: SafetyImpact): OperationPolicy {
|
|
34
|
-
return { impact };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
21
|
describe('Safety Logic', () => {
|
|
38
22
|
describe('annotation presets', () => {
|
|
39
23
|
it('should define READ_ONLY_DEFAULT as read-only closed-world', () => {
|
|
@@ -133,52 +117,6 @@ describe('Safety Logic', () => {
|
|
|
133
117
|
});
|
|
134
118
|
});
|
|
135
119
|
|
|
136
|
-
describe('isOperationAllowed', () => {
|
|
137
|
-
it('should allow only read impact in read-only mode', () => {
|
|
138
|
-
expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('read'))).toBe(true);
|
|
139
|
-
expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('write-nondestructive'))).toBe(false);
|
|
140
|
-
expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('write-destructive'))).toBe(false);
|
|
141
|
-
expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('credential-sensitive'))).toBe(false);
|
|
142
|
-
expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('external-side-effect'))).toBe(false);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('should allow read, write-nondestructive, and external-side-effect in write-nondestructive mode', () => {
|
|
146
|
-
expect(isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('read'))).toBe(true);
|
|
147
|
-
expect(
|
|
148
|
-
isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-nondestructive')),
|
|
149
|
-
).toBe(true);
|
|
150
|
-
expect(
|
|
151
|
-
isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('external-side-effect')),
|
|
152
|
-
).toBe(true);
|
|
153
|
-
expect(isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-destructive'))).toBe(
|
|
154
|
-
false,
|
|
155
|
-
);
|
|
156
|
-
expect(
|
|
157
|
-
isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('credential-sensitive')),
|
|
158
|
-
).toBe(false);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should treat write-idempotent mode the same as write-nondestructive for policy checks', () => {
|
|
162
|
-
for (const impact of ALL_IMPACTS) {
|
|
163
|
-
expect(isOperationAllowed(SafetyMode.WRITE_IDEMPOTENT, policy(impact))).toBe(
|
|
164
|
-
isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy(impact)),
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it('should allow all impacts in write-destructive mode', () => {
|
|
170
|
-
for (const impact of ALL_IMPACTS) {
|
|
171
|
-
expect(isOperationAllowed(SafetyMode.WRITE_DESTRUCTIVE, policy(impact))).toBe(true);
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('should fail closed for unknown mode strings', () => {
|
|
176
|
-
for (const impact of ALL_IMPACTS) {
|
|
177
|
-
expect(isOperationAllowed('custom-mode', policy(impact))).toBe(false);
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
|
|
182
120
|
describe('getSafetyModeFromEnv', () => {
|
|
183
121
|
const originalEnv = process.env.LIGHTDASH_TOOLS_SAFETY_MODE;
|
|
184
122
|
|
|
@@ -271,20 +209,6 @@ describe('Safety Logic', () => {
|
|
|
271
209
|
});
|
|
272
210
|
});
|
|
273
211
|
|
|
274
|
-
describe('isProjectAllowed', () => {
|
|
275
|
-
it('should allow all projects when allowlist is empty', () => {
|
|
276
|
-
expect(isProjectAllowed([], 'any-uuid')).toBe(true);
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it('should allow a project that is in the allowlist', () => {
|
|
280
|
-
expect(isProjectAllowed(['uuid-a', 'uuid-b'], 'uuid-a')).toBe(true);
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
it('should deny a project that is not in the allowlist', () => {
|
|
284
|
-
expect(isProjectAllowed(['uuid-a', 'uuid-b'], 'uuid-c')).toBe(false);
|
|
285
|
-
});
|
|
286
|
-
});
|
|
287
|
-
|
|
288
212
|
describe('areAllProjectsAllowed', () => {
|
|
289
213
|
it('should allow everything when allowlist is empty', () => {
|
|
290
214
|
expect(areAllProjectsAllowed([], ['uuid-a', 'uuid-b'])).toBe(true);
|
package/src/safety.ts
CHANGED
|
@@ -4,17 +4,6 @@ import { ENV_LIGHTDASH_TOOLS_ALLOWED_PROJECT_UUIDS, ENV_LIGHTDASH_TOOLS_SAFETY_M
|
|
|
4
4
|
/** Removed allowlist env — fail closed if still set. */
|
|
5
5
|
const ENV_ALLOWED_PROJECTS_REMOVED = 'LIGHTDASH_TOOLS_ALLOWED_PROJECTS';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Semantic impact classification for operations (RFC Phase 0).
|
|
9
|
-
* Used by {@link OperationPolicy} and {@link isOperationAllowed}.
|
|
10
|
-
*/
|
|
11
|
-
export type SafetyImpact =
|
|
12
|
-
| 'credential-sensitive'
|
|
13
|
-
| 'external-side-effect'
|
|
14
|
-
| 'read'
|
|
15
|
-
| 'write-destructive'
|
|
16
|
-
| 'write-nondestructive';
|
|
17
|
-
|
|
18
7
|
/**
|
|
19
8
|
* Hierarchical safety modes for Lightdash AI tools and CLI.
|
|
20
9
|
*/
|
|
@@ -38,11 +27,6 @@ export type ToolAnnotations = {
|
|
|
38
27
|
openWorldHint?: boolean;
|
|
39
28
|
};
|
|
40
29
|
|
|
41
|
-
/** Policy describing the semantic impact of an operation. */
|
|
42
|
-
export type OperationPolicy = {
|
|
43
|
-
impact: SafetyImpact;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
30
|
/** Preset: read-only, non-destructive, idempotent, closed-world. Use for list/get/compile tools. */
|
|
47
31
|
export const READ_ONLY_DEFAULT: ToolAnnotations = {
|
|
48
32
|
readOnlyHint: true,
|
|
@@ -115,28 +99,6 @@ export function isAllowed(mode: SafetyMode | string, annotations: ToolAnnotation
|
|
|
115
99
|
}
|
|
116
100
|
}
|
|
117
101
|
|
|
118
|
-
/**
|
|
119
|
-
* Validates if an operation is allowed in the current safety mode using semantic impact policy.
|
|
120
|
-
* Unknown modes fail closed.
|
|
121
|
-
*/
|
|
122
|
-
export function isOperationAllowed(mode: SafetyMode | string, policy: OperationPolicy): boolean {
|
|
123
|
-
switch (mode) {
|
|
124
|
-
case SafetyMode.READ_ONLY:
|
|
125
|
-
return policy.impact === 'read';
|
|
126
|
-
case SafetyMode.WRITE_NONDESTRUCTIVE:
|
|
127
|
-
case SafetyMode.WRITE_IDEMPOTENT:
|
|
128
|
-
return (
|
|
129
|
-
policy.impact === 'read' ||
|
|
130
|
-
policy.impact === 'write-nondestructive' ||
|
|
131
|
-
policy.impact === 'external-side-effect'
|
|
132
|
-
);
|
|
133
|
-
case SafetyMode.WRITE_DESTRUCTIVE:
|
|
134
|
-
return true;
|
|
135
|
-
default:
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
102
|
/**
|
|
141
103
|
* Resolves safety mode from environment variable.
|
|
142
104
|
* Accepts `write-idempotent` as a deprecated alias for `write-nondestructive`.
|
|
@@ -175,15 +137,6 @@ export function getAllowedProjectUuidsFromEnv(): string[] {
|
|
|
175
137
|
.filter(Boolean);
|
|
176
138
|
}
|
|
177
139
|
|
|
178
|
-
/**
|
|
179
|
-
* Returns true if a single projectUuid is permitted by the allowlist.
|
|
180
|
-
* An empty allowlist means all projects are allowed.
|
|
181
|
-
*/
|
|
182
|
-
export function isProjectAllowed(allowedUuids: readonly string[], projectUuid: string): boolean {
|
|
183
|
-
if (allowedUuids.length === 0) return true;
|
|
184
|
-
return allowedUuids.includes(projectUuid);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
140
|
/**
|
|
188
141
|
* Returns true when every UUID in projectUuids is permitted by the allowlist.
|
|
189
142
|
* An empty allowlist means all projects are allowed.
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const vitest_1 = require("vitest");
|
|
4
|
-
const index_1 = require("./index");
|
|
5
|
-
(0, vitest_1.describe)('greet', () => {
|
|
6
|
-
(0, vitest_1.it)('should return a greeting message', () => {
|
|
7
|
-
(0, vitest_1.expect)((0, index_1.greet)('World')).toBe('Hello, World!');
|
|
8
|
-
});
|
|
9
|
-
});
|