-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvalidate-objects-task.js
More file actions
195 lines (167 loc) · 6.46 KB
/
Copy pathvalidate-objects-task.js
File metadata and controls
195 lines (167 loc) · 6.46 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
'use strict';
const schedule = require('node-schedule');
const logger = require('../lib/logger');
const config = require('../config/config');
const { getSchema } = require('../lib/validation-schemas');
/**
* Recursively convert Date instances to ISO strings.
* MongoDB/.lean() returns BSON Date objects, but ADM Zod schemas
* expect RFC3339 strings (z.iso.datetime).
*/
function serializeDates(obj) {
if (obj instanceof Date) return obj.toISOString();
if (Array.isArray(obj)) return obj.map(serializeDates);
if (obj !== null && typeof obj === 'object') {
const out = {};
for (const [key, val] of Object.entries(obj)) {
out[key] = serializeDates(val);
}
return out;
}
return obj;
}
// Repositories for all collections that hold STIX objects
const attackObjectModel = require('../models/attack-object-model');
const RelationshipModel = require('../models/relationship-model');
const validationBypassesRepository = require('../repository/validation-bypasses-repository');
/**
* Re-validate all STIX documents and refresh `workspace.validation`.
*
* This combats "concept drift": when the ADM library or ATTACK_SPEC_VERSION
* changes, previously recorded validation results may become stale. Running
* this on a schedule keeps every document's validation metadata current.
*
* Logic mirrors BaseService._createFromImport and the backfill migration:
* - Revoked/deprecated objects skip validation (stale validation is cleared).
* - Errors that match a bypass rule are filtered out.
* - Documents that pass have workspace.validation removed.
* - Documents that fail get workspace.validation set/updated.
*
* @returns {Promise<object>} Summary of validation results
*/
async function validateObjects() {
logger.info('[validate-objects] Starting scheduled validation of all STIX objects');
const { ATTACK_SPEC_VERSION } = require('@mitre-attack/attack-data-model');
const admPkg = require('@mitre-attack/attack-data-model/package.json');
// Load bypass rules once for the entire run
let bypassRules = [];
try {
bypassRules = await validationBypassesRepository.findAll();
} catch (err) {
logger.warn(`[validate-objects] Could not load bypass rules: ${err.message}`);
}
const results = {
timestamp: new Date().toISOString(),
totalValidated: 0,
totalErrored: 0,
totalCleared: 0,
admVersion: admPkg.version,
attackSpecVersion: ATTACK_SPEC_VERSION,
};
// Process both collections: attackObjects (SDOs) and relationships (SROs)
const models = [
{ model: attackObjectModel, name: 'attackObjects' },
{ model: RelationshipModel, name: 'relationships' },
];
for (const { model, name } of models) {
logger.debug(`[validate-objects] Processing ${name} collection`);
// Target ALL objects (including non-latest, revoked, and deprecated)
const cursor = model.find({}).lean().cursor();
for await (const doc of cursor) {
results.totalValidated++;
const stixType = doc.stix?.type;
if (!stixType) continue;
const status = doc.workspace?.workflow?.state || 'reviewed';
const schema = getSchema(stixType, status);
if (!schema) continue;
const parseResult = schema.safeParse(serializeDates(doc.stix));
if (parseResult.success) {
// Valid — clear any stale validation errors
if (doc.workspace?.validation) {
await model.updateOne({ _id: doc._id }, { $unset: { 'workspace.validation': '' } });
results.totalCleared++;
}
continue;
}
// Convert Zod issues to error objects
let errors = parseResult.error.issues.map((issue) => ({
message: `${issue.path.join('.')} is ${issue.message}`,
path: issue.path,
code: issue.code,
}));
// Apply bypass rules (mirrors ValidationBypassesService.checkBypassRule)
if (bypassRules.length > 0) {
errors = errors.filter((error) => {
const errorPathStr = JSON.stringify(error.path.map(String));
return !bypassRules.some((rule) => {
if (!rule.suppressError && !rule.warningMessage) return false;
if (rule.stixType !== 'all' && rule.stixType !== stixType) return false;
if (rule.errorCode !== error.code) return false;
const rulePathStr = JSON.stringify(rule.fieldPath.map(String));
return rulePathStr === errorPathStr;
});
});
}
if (errors.length === 0) {
if (doc.workspace?.validation) {
await model.updateOne({ _id: doc._id }, { $unset: { 'workspace.validation': '' } });
results.totalCleared++;
}
continue;
}
// Determine if validation data has actually changed to avoid unnecessary writes
const existingValidation = doc.workspace?.validation;
const errorsChanged =
!existingValidation ||
existingValidation.adm_version !== admPkg.version ||
existingValidation.attack_spec_version !== ATTACK_SPEC_VERSION ||
existingValidation.errors?.length !== errors.length;
if (errorsChanged) {
await model.updateOne(
{ _id: doc._id },
{
$set: {
'workspace.validation': {
errors: errors.map((e) => ({ message: e.message, path: e.path, code: e.code })),
attack_spec_version: ATTACK_SPEC_VERSION,
adm_version: admPkg.version,
validated_at: new Date(),
},
},
},
);
}
results.totalErrored++;
}
}
logger.info(
`[validate-objects] Validation complete: ${results.totalValidated} documents validated, ` +
`${results.totalErrored} with errors, ${results.totalCleared} stale validations cleared ` +
`(ADM v${results.admVersion}, spec v${results.attackSpecVersion})`,
);
return results;
}
/**
* Initialize and schedule this task
*/
function initializeTask() {
const cronPattern = config.scheduler.validateObjectsCron;
logger.info(`[validate-objects] Scheduling task with cron pattern: ${cronPattern}`);
schedule.scheduleJob(cronPattern, async () => {
try {
await validateObjects();
} catch (err) {
logger.error(`[validate-objects] Task execution failed: ${err.message}`);
logger.error(err.stack);
}
});
logger.info('[validate-objects] Task scheduled successfully');
}
// Initialize the task when this module is loaded
if (config.scheduler.enableScheduler) {
initializeTask();
}
// Export for testing
module.exports = {
validateObjects,
};