-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdetection-strategy.test.ts
More file actions
292 lines (258 loc) · 10.8 KB
/
detection-strategy.test.ts
File metadata and controls
292 lines (258 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { v4 as uuidv4 } from 'uuid';
import { describe, expect, it } from 'vitest';
import { createSyntheticStixObject } from '../../src/generator';
import { type ExternalReferences } from '../../src/schemas/common/index';
import {
type DetectionStrategy,
detectionStrategySchema,
} from '../../src/schemas/sdo/detection-strategy.schema';
describe('detectionStrategySchema', () => {
const minimalDetectionStrategy = createSyntheticStixObject('x-mitre-detection-strategy');
describe('Valid Inputs', () => {
it('should accept minimal valid object (only required fields)', () => {
expect(() => detectionStrategySchema.parse(minimalDetectionStrategy)).not.toThrow();
});
it('should accept fully populated valid object (required + optional ATT&CK fields)', () => {
const fullDetectionStrategy: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_deprecated: false,
x_mitre_contributors: ['John Doe', 'Jane Smith'],
x_mitre_analytic_refs: [
`x-mitre-analytic--${uuidv4()}`,
`x-mitre-analytic--${uuidv4()}`,
`x-mitre-analytic--${uuidv4()}`,
],
};
expect(() => detectionStrategySchema.parse(fullDetectionStrategy)).not.toThrow();
});
it('should accept multiple analytics', () => {
const multiAnalyticsDetectionStrategy: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: [
`x-mitre-analytic--${uuidv4()}`,
`x-mitre-analytic--${uuidv4()}`,
`x-mitre-analytic--${uuidv4()}`,
`x-mitre-analytic--${uuidv4()}`,
`x-mitre-analytic--${uuidv4()}`,
],
};
expect(() => detectionStrategySchema.parse(multiAnalyticsDetectionStrategy)).not.toThrow();
});
});
describe('Field-Specific Tests', () => {
const testField = (
fieldName: keyof DetectionStrategy,
invalidValue: any,
isRequired = true,
) => {
it(`should reject invalid values for ${fieldName}`, () => {
const invalidObject = { ...minimalDetectionStrategy, [fieldName]: invalidValue };
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
if (isRequired) {
it(`should reject omission of ${fieldName}`, () => {
const { [fieldName]: omitted, ...objectWithoutField } = minimalDetectionStrategy;
expect(() => detectionStrategySchema.parse(objectWithoutField)).toThrow();
});
}
};
describe('id', () => {
testField('id', 'invalid-id');
testField('id', `x-mitre-analytic--${uuidv4()}`); // Wrong prefix
});
describe('type', () => {
testField('type', 'invalid-type');
testField('type', 'x-mitre-analytic'); // Wrong type
});
describe('created_by_ref', () => {
testField('created_by_ref', 'invalid-created-by-ref');
});
describe('external_references', () => {
testField('external_references', 'not-an-array' as unknown as ExternalReferences);
testField('external_references', []); // Empty array should fail
});
describe('object_marking_refs', () => {
testField('object_marking_refs', ['invalid-object-marking-refs']);
});
describe('x_mitre_domains', () => {
testField('x_mitre_domains', ['invalid-mitre-domains']);
});
describe('x_mitre_modified_by_ref', () => {
testField('x_mitre_modified_by_ref', 'invalid-modified-by-ref');
});
describe('x_mitre_analytic_refs', () => {
it('should reject empty array', () => {
const invalidObject = { ...minimalDetectionStrategy, x_mitre_analytic_refs: [] };
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
it('should reject analytics with invalid STIX ID format', () => {
const invalidObject = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: ['invalid-analytic-id'],
};
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
it('should reject analytics with wrong STIX type prefix', () => {
const invalidObject = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: [`x-mitre-detection-strategy--${uuidv4()}`],
};
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
it('should reject non-array value', () => {
const invalidObject = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: 'not-an-array',
};
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
it('should reject analytics with malformed UUID', () => {
const invalidObject = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: ['x-mitre-analytic--invalid-uuid'],
};
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
});
// Optional Fields Testing
describe('x_mitre_deprecated', () => {
testField('x_mitre_deprecated', 'not-a-boolean', false);
});
describe('x_mitre_contributors', () => {
it('should reject non-array value', () => {
const invalidObject = {
...minimalDetectionStrategy,
x_mitre_contributors: 'not-an-array',
};
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
it('should reject empty strings in contributors array', () => {
const invalidObject = {
...minimalDetectionStrategy,
x_mitre_contributors: ['John Doe', '', 'Jane Smith'],
};
expect(() => detectionStrategySchema.parse(invalidObject)).toThrow();
});
it('should accept valid contributors array', () => {
const validObject = {
...minimalDetectionStrategy,
x_mitre_contributors: ['John Doe', 'Jane Smith'],
};
expect(() => detectionStrategySchema.parse(validObject)).not.toThrow();
});
it('should reject empty array', () => {
const validObject = {
...minimalDetectionStrategy,
x_mitre_contributors: [],
};
expect(() => detectionStrategySchema.parse(validObject)).toThrow();
});
});
});
describe('Schema Refinements', () => {
describe('External References Validation', () => {
it('should reject when ATT&CK ID is missing', () => {
const invalidDetectionStrategy = {
...minimalDetectionStrategy,
external_references: [{ source_name: 'mitre-attack' }],
};
expect(() => detectionStrategySchema.parse(invalidDetectionStrategy)).toThrow(
/ATT&CK ID must be defined/,
);
});
it('should reject invalid ATT&CK ID format', () => {
const invalidDetectionStrategy = {
...minimalDetectionStrategy,
external_references: [{ source_name: 'mitre-attack', external_id: 'DS123' }],
};
expect(() => detectionStrategySchema.parse(invalidDetectionStrategy)).toThrow(
`The first external_reference must match the ATT&CK ID format DET####.`,
);
});
it('should reject ATT&CK ID with wrong prefix', () => {
const invalidDetectionStrategy = {
...minimalDetectionStrategy,
external_references: [{ source_name: 'mitre-attack', external_id: 'LS0001' }],
};
expect(() => detectionStrategySchema.parse(invalidDetectionStrategy)).toThrow(
`The first external_reference must match the ATT&CK ID format DET####.`,
);
});
});
});
describe('Schema-Level Tests', () => {
it('should reject unknown properties', () => {
const invalidDetectionStrategy = {
...minimalDetectionStrategy,
unknown_property: true,
} as DetectionStrategy;
expect(() => detectionStrategySchema.parse(invalidDetectionStrategy)).toThrow();
});
it('should enforce strict mode', () => {
const invalidDetectionStrategy = {
...minimalDetectionStrategy,
foo: 'bar', // Not a valid field
};
expect(() => detectionStrategySchema.parse(invalidDetectionStrategy)).toThrow();
});
});
describe('Edge Cases and Special Scenarios', () => {
it('should handle duplicate analytic IDs', () => {
const analyticId = `x-mitre-analytic--${uuidv4()}`;
const detectionStrategyWithDuplicates: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: [analyticId, analyticId, analyticId],
};
// Schema prevents duplicates, so this should throw
expect(() => detectionStrategySchema.parse(detectionStrategyWithDuplicates)).toThrow();
});
it('should handle large number of analytics', () => {
const manyAnalytics = Array.from({ length: 100 }, () => `x-mitre-analytic--${uuidv4()}`);
const detectionStrategyWithManyAnalytics: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: manyAnalytics,
};
expect(() => detectionStrategySchema.parse(detectionStrategyWithManyAnalytics)).not.toThrow();
});
it('should handle very long contributor names', () => {
const longName = 'A'.repeat(1000);
const detectionStrategyWithLongNames: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_contributors: [longName, 'Normal Name'],
};
expect(() => detectionStrategySchema.parse(detectionStrategyWithLongNames)).not.toThrow();
});
it('should handle special characters in contributor names', () => {
const detectionStrategyWithSpecialChars: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_contributors: [
'John Doe-Smith',
"Jane O'Connor",
'José García',
'李小明',
'user@domain.com',
],
};
expect(() => detectionStrategySchema.parse(detectionStrategyWithSpecialChars)).not.toThrow();
});
it('should handle mixed case and special characters in names', () => {
const detectionStrategyWithMixedCase: DetectionStrategy = {
...minimalDetectionStrategy,
name: 'Advanced PowerShell Command-Line Detection (v2.1)',
x_mitre_contributors: ['Dr. John Smith, PhD', 'Jane Doe (Security Analyst)'],
};
expect(() => detectionStrategySchema.parse(detectionStrategyWithMixedCase)).not.toThrow();
});
it('should handle analytics from different UUIDs formats', () => {
const detectionStrategyWithVariousUUIDs: DetectionStrategy = {
...minimalDetectionStrategy,
x_mitre_analytic_refs: [
'x-mitre-analytic--550e8400-e29b-41d4-a716-446655440000', // Version 1 UUID format
'x-mitre-analytic--6ba7b810-9dad-11d1-80b4-00c04fd430c8', // Version 1 UUID format
'x-mitre-analytic--6ba7b811-9dad-11d1-80b4-00c04fd430c8', // Version 1 UUID format
],
};
expect(() => detectionStrategySchema.parse(detectionStrategyWithVariousUUIDs)).not.toThrow();
});
});
});