159 lines
6.6 KiB
TypeScript
159 lines
6.6 KiB
TypeScript
import type { Analysis, Campaign, EventItem, Incident, ParseIssue, ParseStats, SourceInfo } from "../types";
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
function isTimestamp(value: unknown): value is string | null {
|
|
return (
|
|
value === null ||
|
|
(typeof value === "string" && /(?:Z|[+-]\d{2}:\d{2})$/.test(value) && Number.isFinite(Date.parse(value)))
|
|
);
|
|
}
|
|
|
|
function isFiniteNumber(value: unknown): value is number {
|
|
return typeof value === "number" && Number.isFinite(value);
|
|
}
|
|
|
|
function isNonnegativeInteger(value: unknown): value is number {
|
|
return isFiniteNumber(value) && Number.isInteger(value) && value >= 0;
|
|
}
|
|
|
|
function isStringArray(value: unknown): value is string[] {
|
|
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
}
|
|
|
|
function isEventItem(value: unknown): value is EventItem {
|
|
if (!isRecord(value)) return false;
|
|
if (typeof value.id !== "string" || !isTimestamp(value.timestamp)) return false;
|
|
if (
|
|
typeof value.provider !== "string" ||
|
|
!(value.event_id === null || (isNonnegativeInteger(value.event_id) && value.event_id <= 4_294_967_295))
|
|
)
|
|
return false;
|
|
if (typeof value.level !== "string" || typeof value.category !== "string" || typeof value.summary !== "string")
|
|
return false;
|
|
if (value.channel !== undefined && typeof value.channel !== "string") return false;
|
|
if (value.computer !== undefined && typeof value.computer !== "string") return false;
|
|
if (
|
|
value.outcome !== undefined &&
|
|
value.outcome !== null &&
|
|
value.outcome !== "success" &&
|
|
value.outcome !== "failure"
|
|
)
|
|
return false;
|
|
if (value.fields !== undefined) {
|
|
if (!isRecord(value.fields) || !Object.values(value.fields).every((item) => typeof item === "string")) return false;
|
|
}
|
|
if (
|
|
typeof value.timestamp_valid !== "boolean" ||
|
|
!["event", "record", "missing", "invalid"].includes(String(value.timestamp_source))
|
|
)
|
|
return false;
|
|
if (value.timestamp === null && value.timestamp_valid) return false;
|
|
if (["event", "record"].includes(String(value.timestamp_source)) !== value.timestamp_valid) return false;
|
|
return true;
|
|
}
|
|
|
|
function isIncident(value: unknown): value is Incident {
|
|
if (!isRecord(value)) return false;
|
|
return (
|
|
typeof value.id === "string" &&
|
|
typeof value.rule_id === "string" &&
|
|
isTimestamp(value.start) &&
|
|
isTimestamp(value.end) &&
|
|
typeof value.title === "string" &&
|
|
typeof value.category === "string" &&
|
|
typeof value.severity === "string" &&
|
|
isStringArray(value.reasons) &&
|
|
isStringArray(value.evidence_event_ids) &&
|
|
isNonnegativeInteger(value.original_count) &&
|
|
value.original_count > 0 &&
|
|
isNonnegativeInteger(value.compressed_count) &&
|
|
value.compressed_count > 0 &&
|
|
(value.campaign_id === undefined || value.campaign_id === null || typeof value.campaign_id === "string")
|
|
);
|
|
}
|
|
|
|
function isCampaign(value: unknown): value is Campaign {
|
|
if (!isRecord(value)) return false;
|
|
return (
|
|
typeof value.id === "string" &&
|
|
typeof value.rule_id === "string" &&
|
|
typeof value.title === "string" &&
|
|
typeof value.category === "string" &&
|
|
typeof value.severity === "string" &&
|
|
typeof value.start === "string" &&
|
|
isTimestamp(value.start) &&
|
|
typeof value.end === "string" &&
|
|
isTimestamp(value.end) &&
|
|
isStringArray(value.incident_ids) &&
|
|
value.incident_ids.length >= 2 &&
|
|
isNonnegativeInteger(value.event_count) &&
|
|
isStringArray(value.reasons)
|
|
);
|
|
}
|
|
|
|
function isSourceInfo(value: unknown): value is SourceInfo {
|
|
return (
|
|
isRecord(value) &&
|
|
typeof value.sha256 === "string" &&
|
|
/^[0-9a-f]{64}$/.test(value.sha256) &&
|
|
isNonnegativeInteger(value.size_bytes)
|
|
);
|
|
}
|
|
|
|
function isIssueCounts(value: unknown): value is Record<string, number> {
|
|
return isRecord(value) && Object.values(value).every(isNonnegativeInteger);
|
|
}
|
|
|
|
function isParseIssue(value: unknown): value is ParseIssue {
|
|
if (!isRecord(value) || typeof value.code !== "string" || typeof value.message !== "string") return false;
|
|
if (value.record_id !== undefined && value.record_id !== null && !isNonnegativeInteger(value.record_id)) return false;
|
|
if (value.skipped_record !== undefined && typeof value.skipped_record !== "boolean") return false;
|
|
return true;
|
|
}
|
|
|
|
function isParseStats(value: unknown): value is ParseStats {
|
|
if (!isRecord(value)) return false;
|
|
if (typeof value.parser !== "string" || !["complete", "partial"].includes(String(value.status))) return false;
|
|
for (const key of ["parsed_events", "records_seen", "skipped_records", "issue_count", "duration_ms"] as const) {
|
|
if (!isNonnegativeInteger(value[key])) return false;
|
|
}
|
|
if (typeof value.truncated !== "boolean") return false;
|
|
if (
|
|
value.truncation_reason !== undefined &&
|
|
value.truncation_reason !== null &&
|
|
typeof value.truncation_reason !== "string"
|
|
)
|
|
return false;
|
|
if (value.issues_truncated !== undefined && typeof value.issues_truncated !== "boolean") return false;
|
|
if (value.issue_counts !== undefined && !isIssueCounts(value.issue_counts)) return false;
|
|
if (!Array.isArray(value.issues) || !value.issues.every(isParseIssue)) return false;
|
|
// The API caps the reported issue list; issue_count keeps the true total.
|
|
const issueCount = value.issue_count as number;
|
|
return value.issues_truncated === true ? issueCount >= value.issues.length : issueCount === value.issues.length;
|
|
}
|
|
|
|
export function isAnalysis(value: unknown): value is Analysis {
|
|
if (!isRecord(value) || typeof value.source !== "string") return false;
|
|
if (!Array.isArray(value.events) || !value.events.every(isEventItem)) return false;
|
|
if (!Array.isArray(value.incidents) || !value.incidents.every(isIncident)) return false;
|
|
const eventIds = new Set(value.events.map((event: EventItem) => event.id));
|
|
if (eventIds.size !== value.events.length) return false;
|
|
if (!value.incidents.every((incident: Incident) => incident.evidence_event_ids.every((id) => eventIds.has(id))))
|
|
return false;
|
|
if (value.campaigns !== undefined) {
|
|
if (!Array.isArray(value.campaigns) || !value.campaigns.every(isCampaign)) return false;
|
|
const incidentIds = new Set(value.incidents.map((incident: Incident) => incident.id));
|
|
if (!value.campaigns.every((campaign: Campaign) => campaign.incident_ids.every((id) => incidentIds.has(id))))
|
|
return false;
|
|
}
|
|
if (value.source_info !== undefined && value.source_info !== null && !isSourceInfo(value.source_info)) return false;
|
|
return (
|
|
value.parse === undefined ||
|
|
value.parse === null ||
|
|
(isParseStats(value.parse) && value.parse.parsed_events === value.events.length)
|
|
);
|
|
}
|