@lightdash-tools/common 0.5.0 → 0.7.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 (94) hide show
  1. package/dist/agentops/extract-yaml-project.d.ts +7 -0
  2. package/dist/agentops/extract-yaml-project.js +42 -0
  3. package/dist/agentops/formatters.d.ts +5 -0
  4. package/dist/agentops/formatters.js +47 -0
  5. package/dist/agentops/snapshots.d.ts +32 -0
  6. package/dist/agentops/snapshots.js +34 -0
  7. package/dist/agentops/types.d.ts +224 -0
  8. package/dist/agentops/types.js +430 -0
  9. package/dist/argument-validation.d.ts +25 -0
  10. package/dist/argument-validation.js +199 -0
  11. package/dist/argument-validation.test.d.ts +1 -0
  12. package/dist/argument-validation.test.js +183 -0
  13. package/dist/audit.d.ts +9 -2
  14. package/dist/audit.js +17 -2
  15. package/dist/audit.test.d.ts +1 -0
  16. package/dist/audit.test.js +73 -0
  17. package/dist/env.d.ts +8 -0
  18. package/dist/env.js +9 -1
  19. package/dist/index.d.ts +7 -1
  20. package/dist/index.js +14 -0
  21. package/dist/input-validation.d.ts +35 -0
  22. package/dist/input-validation.js +87 -0
  23. package/dist/input-validation.test.js +93 -0
  24. package/dist/operations/agent-safe.d.ts +7 -0
  25. package/dist/operations/agent-safe.js +16 -0
  26. package/dist/operations/ai-agents.d.ts +6 -0
  27. package/dist/operations/ai-agents.js +411 -0
  28. package/dist/operations/index.d.ts +5 -0
  29. package/dist/operations/index.js +16 -0
  30. package/dist/operations/parity.test.d.ts +1 -0
  31. package/dist/operations/parity.test.js +28 -0
  32. package/dist/operations/registry.d.ts +10 -0
  33. package/dist/operations/registry.js +35 -0
  34. package/dist/operations/registry.test.d.ts +1 -0
  35. package/dist/operations/registry.test.js +196 -0
  36. package/dist/operations/types.d.ts +69 -0
  37. package/dist/operations/types.js +70 -0
  38. package/dist/operations/types.test.d.ts +1 -0
  39. package/dist/operations/types.test.js +104 -0
  40. package/dist/operations/users.d.ts +6 -0
  41. package/dist/operations/users.js +60 -0
  42. package/dist/safety.d.ts +27 -2
  43. package/dist/safety.js +52 -5
  44. package/dist/safety.test.js +217 -4
  45. package/dist/types/generated/openapi-types.d.ts +35895 -10622
  46. package/dist/types/generated/openapi-types.js +0 -1
  47. package/dist/types/lightdash-api.d.ts +1 -1
  48. package/dist/types/v1/ai-agents.d.ts +112 -3
  49. package/dist/types/v1/charts.d.ts +5 -3
  50. package/dist/types/v1/dashboards.d.ts +13 -0
  51. package/dist/types/v1/index.d.ts +45 -8
  52. package/dist/types/v1/index.js +1 -1
  53. package/dist/types/v1/lightdash-api.d.ts +107 -16
  54. package/dist/types/v1/users.d.ts +4 -0
  55. package/dist/types/v2/content.d.ts +2 -0
  56. package/dist/types/v2/content.js +8 -0
  57. package/dist/types/v2/index.d.ts +11 -9
  58. package/dist/types/v2/index.js +1 -1
  59. package/package.json +16 -3
  60. package/src/agentops/extract-yaml-project.ts +42 -0
  61. package/src/agentops/formatters.ts +52 -0
  62. package/src/agentops/snapshots.ts +56 -0
  63. package/src/agentops/types.ts +599 -0
  64. package/src/argument-validation.test.ts +270 -0
  65. package/src/argument-validation.ts +240 -0
  66. package/src/audit.test.ts +87 -0
  67. package/src/audit.ts +21 -3
  68. package/src/env.ts +9 -0
  69. package/src/index.test.ts +1 -0
  70. package/src/index.ts +16 -1
  71. package/src/input-validation.test.ts +157 -1
  72. package/src/input-validation.ts +95 -0
  73. package/src/operations/agent-safe.ts +18 -0
  74. package/src/operations/ai-agents.ts +451 -0
  75. package/src/operations/index.ts +19 -0
  76. package/src/operations/parity.test.ts +35 -0
  77. package/src/operations/registry.test.ts +219 -0
  78. package/src/operations/registry.ts +44 -0
  79. package/src/operations/types.test.ts +148 -0
  80. package/src/operations/types.ts +168 -0
  81. package/src/operations/users.ts +67 -0
  82. package/src/safety.test.ts +276 -4
  83. package/src/safety.ts +72 -4
  84. package/src/types/generated/openapi-types.ts +37017 -11241
  85. package/src/types/lightdash-api.ts +39 -0
  86. package/src/types/v1/ai-agents.ts +133 -3
  87. package/src/types/v1/charts.ts +5 -3
  88. package/src/types/v1/dashboards.ts +19 -0
  89. package/src/types/v1/index.ts +51 -8
  90. package/src/types/v1/lightdash-api.ts +129 -20
  91. package/src/types/v1/users.ts +4 -0
  92. package/src/types/v2/content.ts +8 -0
  93. package/src/types/v2/index.ts +11 -9
  94. package/tsconfig.json +1 -0
@@ -1,5 +1,14 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import { rejectControlChars, validateResourceId, validateSafeOutputDir } from './input-validation';
2
+
3
+ import {
4
+ rejectControlChars,
5
+ validateFingerprint,
6
+ validateResourceId,
7
+ validateSafeOutputDir,
8
+ validateSlug,
9
+ validateUuid,
10
+ validateUuidOrSlug,
11
+ } from './input-validation';
3
12
 
4
13
  describe('input-validation', () => {
5
14
  describe('rejectControlChars', () => {
@@ -75,6 +84,153 @@ describe('input-validation', () => {
75
84
  });
76
85
  });
77
86
 
87
+ describe('validateUuid', () => {
88
+ const validUuid = '550e8400-e29b-41d4-a716-446655440000';
89
+
90
+ it('accepts lowercase UUID', () => {
91
+ expect(validateUuid(validUuid)).toBe(validUuid);
92
+ });
93
+
94
+ it('accepts uppercase UUID', () => {
95
+ expect(validateUuid(validUuid.toUpperCase())).toBe(validUuid.toUpperCase());
96
+ });
97
+
98
+ it('throws on invalid format', () => {
99
+ expect(() => validateUuid('not-a-uuid')).toThrow('Invalid UUID format');
100
+ expect(() => validateUuid('550e8400-e29b-41d4-a716')).toThrow('Invalid UUID format');
101
+ expect(() => validateUuid('550e8400-e29b-41d4-a716-446655440000-extra')).toThrow(
102
+ 'Invalid UUID format',
103
+ );
104
+ });
105
+
106
+ it('throws on control characters', () => {
107
+ expect(() => validateUuid('550e8400-e29b-41d4-a716\u000044655440000')).toThrow(
108
+ 'Input contains invalid control characters',
109
+ );
110
+ });
111
+
112
+ it('throws when id is not a string', () => {
113
+ expect(() => validateUuid(123 as unknown as string)).toThrow('UUID must be a string');
114
+ });
115
+ });
116
+
117
+ describe('validateSlug', () => {
118
+ it('accepts valid slugs', () => {
119
+ expect(validateSlug('my-chart')).toBe('my-chart');
120
+ expect(validateSlug('chart_123')).toBe('chart_123');
121
+ expect(validateSlug('a.b-c_d')).toBe('a.b-c_d');
122
+ });
123
+
124
+ it('throws on empty slug', () => {
125
+ expect(() => validateSlug('')).toThrow('Slug must be between 1 and 256 characters');
126
+ });
127
+
128
+ it('throws when slug exceeds max length', () => {
129
+ expect(() => validateSlug('a'.repeat(257))).toThrow(
130
+ 'Slug must be between 1 and 256 characters',
131
+ );
132
+ });
133
+
134
+ it('accepts slug at max length', () => {
135
+ const slug = 'a'.repeat(256);
136
+ expect(validateSlug(slug)).toBe(slug);
137
+ });
138
+
139
+ it('throws on invalid characters', () => {
140
+ expect(() => validateSlug('has space')).toThrow(
141
+ 'Slug must contain only alphanumeric characters, dots, underscores, and hyphens',
142
+ );
143
+ expect(() => validateSlug('slug?query')).toThrow(
144
+ 'Slug must contain only alphanumeric characters, dots, underscores, and hyphens',
145
+ );
146
+ });
147
+
148
+ it('rejects path traversal segments', () => {
149
+ expect(() => validateSlug('.')).toThrow('Slug must not contain path traversal segments');
150
+ expect(() => validateSlug('..')).toThrow('Slug must not contain path traversal segments');
151
+ expect(() => validateSlug('foo..bar')).toThrow(
152
+ 'Slug must not contain path traversal segments',
153
+ );
154
+ });
155
+
156
+ it('throws on control characters', () => {
157
+ expect(() => validateSlug('slug\u0001')).toThrow('Input contains invalid control characters');
158
+ });
159
+
160
+ it('throws when id is not a string', () => {
161
+ expect(() => validateSlug(123 as unknown as string)).toThrow('Slug must be a string');
162
+ });
163
+ });
164
+
165
+ describe('validateUuidOrSlug', () => {
166
+ const validUuid = '550e8400-e29b-41d4-a716-446655440000';
167
+
168
+ it('accepts UUIDs', () => {
169
+ expect(validateUuidOrSlug(validUuid)).toBe(validUuid);
170
+ });
171
+
172
+ it('accepts slugs such as dashboard names', () => {
173
+ expect(validateUuidOrSlug('sales-overview')).toBe('sales-overview');
174
+ });
175
+
176
+ it('rejects invalid slug characters', () => {
177
+ expect(() => validateUuidOrSlug('bad?')).toThrow(
178
+ 'Slug must contain only alphanumeric characters',
179
+ );
180
+ });
181
+
182
+ it('rejects path traversal slugs', () => {
183
+ expect(() => validateUuidOrSlug('.')).toThrow(
184
+ 'Slug must not contain path traversal segments',
185
+ );
186
+ expect(() => validateUuidOrSlug('..')).toThrow(
187
+ 'Slug must not contain path traversal segments',
188
+ );
189
+ });
190
+
191
+ it('throws when id is not a string', () => {
192
+ expect(() => validateUuidOrSlug(123 as unknown as string)).toThrow(
193
+ 'Resource ID must be a string',
194
+ );
195
+ });
196
+ });
197
+
198
+ describe('validateFingerprint', () => {
199
+ it('accepts non-empty fingerprints', () => {
200
+ expect(validateFingerprint('abc123')).toBe('abc123');
201
+ expect(validateFingerprint('sha256:deadbeef')).toBe('sha256:deadbeef');
202
+ });
203
+
204
+ it('throws on empty fingerprint', () => {
205
+ expect(() => validateFingerprint('')).toThrow(
206
+ 'Fingerprint must be between 1 and 512 characters',
207
+ );
208
+ });
209
+
210
+ it('throws when fingerprint exceeds max length', () => {
211
+ expect(() => validateFingerprint('x'.repeat(513))).toThrow(
212
+ 'Fingerprint must be between 1 and 512 characters',
213
+ );
214
+ });
215
+
216
+ it('accepts fingerprint at max length', () => {
217
+ const fp = 'x'.repeat(512);
218
+ expect(validateFingerprint(fp)).toBe(fp);
219
+ });
220
+
221
+ it('throws on control characters', () => {
222
+ expect(() => validateFingerprint('fp\u0000')).toThrow(
223
+ 'Input contains invalid control characters',
224
+ );
225
+ });
226
+
227
+ it('throws when id is not a string', () => {
228
+ expect(() => validateFingerprint(123 as unknown as string)).toThrow(
229
+ 'Fingerprint must be a string',
230
+ );
231
+ });
232
+ });
233
+
78
234
  describe('validateSafeOutputDir', () => {
79
235
  it('accepts path within CWD', () => {
80
236
  const result = validateSafeOutputDir('output');
@@ -9,6 +9,17 @@ import * as path from 'node:path';
9
9
 
10
10
  const FORBIDDEN_IN_RESOURCE_ID = /[?#%]|\.\./;
11
11
 
12
+ /** RFC 4122 UUID (any version), case-insensitive hex. */
13
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
14
+
15
+ const SLUG_REGEX = /^[A-Za-z0-9._-]+$/;
16
+
17
+ const SLUG_MIN_LENGTH = 1;
18
+ const SLUG_MAX_LENGTH = 256;
19
+
20
+ const FINGERPRINT_MIN_LENGTH = 1;
21
+ const FINGERPRINT_MAX_LENGTH = 512;
22
+
12
23
  function hasControlChars(str: string): boolean {
13
24
  for (let i = 0; i < str.length; i++) {
14
25
  if (str.charCodeAt(i) < 0x20) return true;
@@ -52,6 +63,90 @@ export function validateResourceId(id: string): string {
52
63
  return id;
53
64
  }
54
65
 
66
+ /**
67
+ * Validates a UUID string (RFC 4122 format).
68
+ *
69
+ * @param id - UUID to validate
70
+ * @returns The input if valid
71
+ * @throws Error if format is invalid or control characters are present
72
+ */
73
+ export function validateUuid(id: string): string {
74
+ if (typeof id !== 'string') {
75
+ throw new Error('UUID must be a string');
76
+ }
77
+ rejectControlChars(id);
78
+ if (!UUID_REGEX.test(id)) {
79
+ throw new Error('Invalid UUID format');
80
+ }
81
+ return id;
82
+ }
83
+
84
+ /**
85
+ * Validates a slug: 1–256 chars, alphanumeric plus `.`, `_`, `-`.
86
+ * Rejects `.` / `..` (and embedded `..`) so path clients cannot normalize
87
+ * the identifier into a parent URL segment.
88
+ *
89
+ * @param id - Slug to validate
90
+ * @returns The input if valid
91
+ * @throws Error if length or character set is invalid
92
+ */
93
+ export function validateSlug(id: string): string {
94
+ if (typeof id !== 'string') {
95
+ throw new Error('Slug must be a string');
96
+ }
97
+ rejectControlChars(id);
98
+ if (id.length < SLUG_MIN_LENGTH || id.length > SLUG_MAX_LENGTH) {
99
+ throw new Error(`Slug must be between ${SLUG_MIN_LENGTH} and ${SLUG_MAX_LENGTH} characters`);
100
+ }
101
+ if (id === '.' || id === '..' || id.includes('..')) {
102
+ throw new Error('Slug must not contain path traversal segments (. or ..)');
103
+ }
104
+ if (!SLUG_REGEX.test(id)) {
105
+ throw new Error(
106
+ 'Slug must contain only alphanumeric characters, dots, underscores, and hyphens',
107
+ );
108
+ }
109
+ return id;
110
+ }
111
+
112
+ /**
113
+ * Validates an OpenAPI UuidOrSlug identifier: UUID when the string matches UUID form,
114
+ * otherwise a safe slug.
115
+ *
116
+ * @param id - UUID or slug to validate
117
+ * @returns The input if valid
118
+ * @throws Error if neither UUID nor slug rules pass
119
+ */
120
+ export function validateUuidOrSlug(id: string): string {
121
+ if (typeof id !== 'string') {
122
+ throw new Error('Resource ID must be a string');
123
+ }
124
+ if (UUID_REGEX.test(id)) {
125
+ return validateUuid(id);
126
+ }
127
+ return validateSlug(id);
128
+ }
129
+
130
+ /**
131
+ * Validates a fingerprint identifier: 1–512 chars, no control characters.
132
+ *
133
+ * @param id - Fingerprint to validate
134
+ * @returns The input if valid
135
+ * @throws Error if length is invalid or control characters are present
136
+ */
137
+ export function validateFingerprint(id: string): string {
138
+ if (typeof id !== 'string') {
139
+ throw new Error('Fingerprint must be a string');
140
+ }
141
+ rejectControlChars(id);
142
+ if (id.length < FINGERPRINT_MIN_LENGTH || id.length > FINGERPRINT_MAX_LENGTH) {
143
+ throw new Error(
144
+ `Fingerprint must be between ${FINGERPRINT_MIN_LENGTH} and ${FINGERPRINT_MAX_LENGTH} characters`,
145
+ );
146
+ }
147
+ return id;
148
+ }
149
+
55
150
  /**
56
151
  * Validates that a file path is safe for output within the current working directory.
57
152
  * Rejects: .. traversal, absolute paths outside CWD.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Agent-safe surface policy helpers (ADR-0004).
3
+ */
4
+
5
+ import { listOperations } from './registry';
6
+
7
+ /** MCP tool names that must never be registered, derived from client-only registry entries. */
8
+ export function listBannedMcpToolNames(): readonly string[] {
9
+ return listOperations()
10
+ .filter(
11
+ (operation) =>
12
+ operation.agentExposure === 'client-only' && operation.mcp.toolName.trim().length > 0,
13
+ )
14
+ .map((operation) => operation.mcp.toolName);
15
+ }
16
+
17
+ /** Banned MCP tool names (without `lightdash_` prefix) for regression tests. */
18
+ export const IRRECOVERABLE_TOOL_DENYLIST = listBannedMcpToolNames();