@mitre-attack/attack-data-model
Version:
A TypeScript API for the MITRE ATT&CK data model
66 lines (63 loc) • 2.62 kB
JavaScript
import {
attackIdPatterns,
getAttackIdExample,
stixTypeToAttackIdMapping
} from "./chunk-RWOQWV2O.js";
import {
createStixIdValidator,
stixIdentifierSchema
} from "./chunk-OM2DJ5DL.js";
// src/schemas/common/misc.ts
import { z } from "zod/v4";
var externalReferenceSchema = z.object({
source_name: z.string({
error: (issue) => issue.input === void 0 ? "Source name is required" : "Source name must be a string"
}),
description: z.string({
error: "Description must be a string"
}).optional(),
url: z.url({
error: (issue) => issue.input === null ? "URL cannot be null" : "Invalid URL format. Please provide a valid URL"
}).optional(),
external_id: z.string({
error: "External ID must be a string"
}).optional()
});
var externalReferencesSchema = z.array(externalReferenceSchema).min(1, "At least one external reference is required when 'external_references' is defined").meta({
description: "A list of external references which refers to non-STIX information"
});
var createAttackExternalReferencesSchema = (stixType) => {
return z.array(externalReferenceSchema).min(1, "At least one external reference is required").refine((refs) => !!refs[0]?.external_id, {
message: "ATT&CK ID must be defined in the first external_references entry.",
path: [0, "external_id"]
}).refine(
(refs) => {
if (!refs[0]?.external_id) return true;
const attackIdType = stixTypeToAttackIdMapping[stixType];
if (attackIdType === "technique") {
return attackIdPatterns["technique"].test(refs[0].external_id) || attackIdPatterns["subtechnique"].test(refs[0].external_id);
}
return attackIdPatterns[attackIdType].test(refs[0].external_id);
},
{
message: `The first external_reference must match the ATT&CK ID format ${getAttackIdExample(stixType)}.`,
path: [0, "external_id"]
}
).meta({
description: "A list of external references with the first containing a valid ATT&CK ID"
});
};
var stixCreatedByRefSchema = createStixIdValidator("identity").meta({
description: "The created_by_ref property specifies the id property of the identity object that describes the entity that created this object. If this attribute is omitted, the source of this information is undefined. This may be used by object creators who wish to remain anonymous."
});
var granularMarkingSchema = z.object({
marking_ref: stixIdentifierSchema,
selectors: z.array(z.string())
});
export {
externalReferenceSchema,
externalReferencesSchema,
createAttackExternalReferencesSchema,
stixCreatedByRefSchema,
granularMarkingSchema
};