@oaksecurity/aitm 0.0.0-stage → 2.3.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.
- package/.env.example +11 -0
- package/CHANGELOG.md +100 -0
- package/LICENSE +21 -0
- package/README.md +229 -2
- package/SECURITY.md +49 -0
- package/SPEC.md +304 -0
- package/docs/api.md +332 -0
- package/docs/inputs-and-limits.md +78 -0
- package/docs/usage.md +351 -0
- package/examples/custody-architecture.md +27 -0
- package/package.json +67 -4
- package/src/api/server.js +591 -0
- package/src/api/web.js +38 -0
- package/src/application.js +32 -0
- package/src/assessments.js +37 -0
- package/src/assets/BDOGrotesk-Bold.ttf +0 -0
- package/src/assets/BDOGrotesk-Regular.ttf +0 -0
- package/src/assets/OFL.txt +93 -0
- package/src/assets/README.md +10 -0
- package/src/cli.js +224 -0
- package/src/client.js +88 -0
- package/src/config.js +105 -0
- package/src/doctor.js +128 -0
- package/src/engine/credentials.js +31 -0
- package/src/engine/download.js +135 -0
- package/src/engine/engine.js +783 -0
- package/src/engine/errors.js +23 -0
- package/src/engine/generation.js +121 -0
- package/src/engine/ingest.js +444 -0
- package/src/engine/pdf-layout.js +204 -0
- package/src/engine/pdf-report.js +573 -0
- package/src/engine/prompts.js +82 -0
- package/src/engine/providers.js +476 -0
- package/src/engine/references.js +112 -0
- package/src/engine/report.js +200 -0
- package/src/engine/schema.js +267 -0
- package/src/engine/store.js +115 -0
- package/src/engine/validate.js +397 -0
- package/src/engine/zip.js +70 -0
- package/src/index.js +5 -0
- package/src/source-input.js +32 -0
- package/src/token.js +59 -0
- package/src/tui/app.js +694 -0
- package/src/tui/editor.js +34 -0
- package/src/tui/file-picker.js +111 -0
- package/src/tui/logo.js +8 -0
- package/src/tui/progress.js +90 -0
- package/src/tui/report-reader.js +105 -0
- package/src/tui/select.js +63 -0
- package/src/tui/workbench.js +377 -0
- package/src/version.js +5 -0
- package/src/web/app.js +729 -0
- package/src/web/assets/BDOGrotesk-Bold.woff2 +0 -0
- package/src/web/assets/BDOGrotesk-Medium.woff2 +0 -0
- package/src/web/assets/BDOGrotesk-Regular.woff2 +0 -0
- package/src/web/assets/README.md +5 -0
- package/src/web/assets/aitm-mark.svg +12 -0
- package/src/web/assets/oak-white.svg +166 -0
- package/src/web/html.js +32 -0
- package/src/web/index.html +26 -0
- package/src/web/markdown.js +48 -0
- package/src/web/styles.css +1450 -0
- package/src/web/views.js +254 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { enabledPhases } from './schema.js';
|
|
2
|
+
import { rankedThreats } from './validate.js';
|
|
3
|
+
import { insist } from './errors.js';
|
|
4
|
+
import { needsFullSourceRun } from './ingest.js';
|
|
5
|
+
|
|
6
|
+
const clean = (text) =>
|
|
7
|
+
String(text ?? '').replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, '');
|
|
8
|
+
const cell = (text) => clean(text).replace(/\|/g, '\\|').replace(/\n/g, '<br>');
|
|
9
|
+
const table = (head, rows) =>
|
|
10
|
+
[head, head.map(() => '---'), ...rows]
|
|
11
|
+
.map((row) => `| ${row.map(cell).join(' | ')} |`)
|
|
12
|
+
.join('\n');
|
|
13
|
+
const list = (values) =>
|
|
14
|
+
values.length ? values.map((v) => `- ${clean(v)}`).join('\n') : 'None recorded.';
|
|
15
|
+
const evidence = (item) =>
|
|
16
|
+
[
|
|
17
|
+
...item.evidence.map((e) => `${e.fileId}:${e.startLine}-${e.endLine} — “${e.quote}”`),
|
|
18
|
+
...(item.assumption ? [`Assumption: ${item.assumption}`] : []),
|
|
19
|
+
].join('; ');
|
|
20
|
+
|
|
21
|
+
export function markdownReport(project) {
|
|
22
|
+
insist(project.artifacts.report, 'Generate the report phase first.', 409, 'report_missing');
|
|
23
|
+
const enabled = new Set(enabledPhases(project).map((p) => p.id));
|
|
24
|
+
const reviewNotes = enabledPhases(project).flatMap((phase) =>
|
|
25
|
+
(project.artifacts[phase.id]?.reviewNotes || []).map(
|
|
26
|
+
(note) => `${phase.title} — ${note.claim}: ${note.note}`,
|
|
27
|
+
),
|
|
28
|
+
);
|
|
29
|
+
const model = project.artifacts.model.data,
|
|
30
|
+
report = project.artifacts.report.data;
|
|
31
|
+
const reviewed = enabledPhases(project)
|
|
32
|
+
.filter((p) => p.gate)
|
|
33
|
+
.every((p) => project.artifacts[p.id]?.status === 'approved');
|
|
34
|
+
const sections = [
|
|
35
|
+
`# ${clean(project.name)} — TRACE threat model`,
|
|
36
|
+
`**${reviewed ? 'Reviewed' : 'DRAFT — expert review required'}** · ${project.artifacts.report.generatedAt}`,
|
|
37
|
+
'**Generated by AiTM using AI. This report was not authored, reviewed or endorsed by Oak Security.**',
|
|
38
|
+
...(needsFullSourceRun(project)
|
|
39
|
+
? [
|
|
40
|
+
'**This saved analysis used incomplete source text. Run the full pipeline again to process the complete source and rebuild the assessment.**',
|
|
41
|
+
]
|
|
42
|
+
: []),
|
|
43
|
+
'## Executive summary',
|
|
44
|
+
report.executiveSummary,
|
|
45
|
+
'## Scope',
|
|
46
|
+
project.scope,
|
|
47
|
+
'## Source inventory',
|
|
48
|
+
table(
|
|
49
|
+
['Source', 'Kind', 'Revision / SHA-256', 'Files'],
|
|
50
|
+
project.sources.map((s) => [s.label, s.kind, s.commit || s.hash, s.files.length]),
|
|
51
|
+
),
|
|
52
|
+
'### Evidence files',
|
|
53
|
+
table(
|
|
54
|
+
['File ID', 'Path', 'SHA-256'],
|
|
55
|
+
project.sources.flatMap((s) => s.files.map((f) => [f.id, `${s.label}/${f.path}`, f.hash])),
|
|
56
|
+
),
|
|
57
|
+
...(reviewNotes.length ? ['### Evidence review required', list(reviewNotes)] : []),
|
|
58
|
+
'## System overview',
|
|
59
|
+
model.overview,
|
|
60
|
+
];
|
|
61
|
+
for (const [key, title] of [
|
|
62
|
+
['threatActors', 'Threat actors'],
|
|
63
|
+
['roles', 'Roles'],
|
|
64
|
+
['protectedAssets', 'Protected assets'],
|
|
65
|
+
['invariants', 'Critical invariants'],
|
|
66
|
+
['components', 'Components'],
|
|
67
|
+
]) {
|
|
68
|
+
sections.push(
|
|
69
|
+
`## ${title}`,
|
|
70
|
+
table(
|
|
71
|
+
['ID', 'Name', 'Description', 'Evidence / assumption'],
|
|
72
|
+
model[key].map((i) => [i.id, i.name, i.description, evidence(i)]),
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
sections.push(
|
|
77
|
+
'## Edges & trust boundaries',
|
|
78
|
+
table(
|
|
79
|
+
['ID', 'From → To', 'Kind / boundary', 'Description', 'Evidence / assumption'],
|
|
80
|
+
model.edges.map((e) => [
|
|
81
|
+
e.id,
|
|
82
|
+
`${e.from} → ${e.to}`,
|
|
83
|
+
`${e.kind}: ${e.boundary}`,
|
|
84
|
+
e.description,
|
|
85
|
+
evidence(e),
|
|
86
|
+
]),
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
sections.push(
|
|
90
|
+
'## Ranked STRIDE threats',
|
|
91
|
+
table(
|
|
92
|
+
['ID', 'Threat', 'Category', 'L × I = Risk', 'Targets / assets / invariants'],
|
|
93
|
+
rankedThreats(project).map((t) => [
|
|
94
|
+
t.id,
|
|
95
|
+
t.title,
|
|
96
|
+
t.category,
|
|
97
|
+
`${t.likelihood} × ${t.impact} = ${t.likelihood * t.impact}`,
|
|
98
|
+
[...t.targetIds, ...t.assetIds, ...t.invariantIds].join(', '),
|
|
99
|
+
]),
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
for (const t of rankedThreats(project))
|
|
103
|
+
sections.push(
|
|
104
|
+
`### ${t.id}: ${t.title}`,
|
|
105
|
+
t.scenario,
|
|
106
|
+
`**Ranking rationale:** ${t.rationale}\n\n**Feasibility:** ${t.feasibility}\n\n**Incentives:** ${t.incentives}\n\n**Blast radius:** ${t.blastRadius}\n\n**Time sensitivity:** ${t.timeSensitivity}\n\n**Confidence:** ${t.confidence}`,
|
|
107
|
+
`**Evidence / assumption:** ${evidence(t)}`,
|
|
108
|
+
'**Existing controls**',
|
|
109
|
+
list(t.existingControls),
|
|
110
|
+
'**Mitigations**',
|
|
111
|
+
list(t.mitigations),
|
|
112
|
+
);
|
|
113
|
+
sections.push(
|
|
114
|
+
'## Coverage',
|
|
115
|
+
table(
|
|
116
|
+
['Model object', 'Threats', 'Review / rationale'],
|
|
117
|
+
project.artifacts.stride.data.coverage.map((r) => [
|
|
118
|
+
r.objectId,
|
|
119
|
+
r.threatIds.join(', '),
|
|
120
|
+
r.explanation,
|
|
121
|
+
]),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
if (enabled.has('trees') && project.artifacts.trees) {
|
|
125
|
+
sections.push('## Attack trees');
|
|
126
|
+
for (const tree of project.artifacts.trees.data.trees) {
|
|
127
|
+
sections.push(`### ${tree.threatId}: ${tree.goal}`);
|
|
128
|
+
const lines = [];
|
|
129
|
+
function walk(parent, depth) {
|
|
130
|
+
for (const n of tree.nodes.filter((n) => n.parentId === parent)) {
|
|
131
|
+
lines.push(
|
|
132
|
+
`${' '.repeat(depth)}- **${n.id} [${n.operator}]** ${n.label}${n.speculative ? ' (speculative)' : ''}. Prerequisites: ${n.prerequisites.join('; ') || 'none recorded'}. Mitigations: ${n.mitigations.join('; ') || 'see leaves'}. ${evidence(n)}`,
|
|
133
|
+
);
|
|
134
|
+
walk(n.id, depth + 1);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
walk(null, 0);
|
|
138
|
+
sections.push(lines.join('\n'));
|
|
139
|
+
}
|
|
140
|
+
sections.push(
|
|
141
|
+
'### Documented tree omissions',
|
|
142
|
+
list(project.artifacts.trees.data.omitted.map((t) => `${t.threatId}: ${t.reason}`)),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
if (enabled.has('coordination') && project.artifacts.coordination) {
|
|
146
|
+
const c = project.artifacts.coordination.data;
|
|
147
|
+
sections.push('## Collusion & coordination');
|
|
148
|
+
for (const s of c.scenarios)
|
|
149
|
+
sections.push(
|
|
150
|
+
`### ${s.id}: ${s.title}`,
|
|
151
|
+
`Actors / roles: ${s.actorIds.join(', ')}. Assets: ${s.assetIds.join(', ')}. Threshold: ${s.threshold}.`,
|
|
152
|
+
`Incentive: ${s.incentive}\n\nShared dependencies: ${s.sharedDependencies.join('; ')}\n\nConsequence: ${s.consequence}\n\n${evidence(s)}`,
|
|
153
|
+
list(s.mitigations),
|
|
154
|
+
);
|
|
155
|
+
sections.push('### Coordination assumptions', list(c.assumptions));
|
|
156
|
+
}
|
|
157
|
+
sections.push(
|
|
158
|
+
'## Mitigation roadmap',
|
|
159
|
+
table(
|
|
160
|
+
['ID / Priority', 'Action', 'Owner', 'Links', 'Verification'],
|
|
161
|
+
report.roadmap.map((r) => [
|
|
162
|
+
`${r.id} / ${r.priority}`,
|
|
163
|
+
r.action,
|
|
164
|
+
r.owner,
|
|
165
|
+
r.linkedIds.join(', '),
|
|
166
|
+
r.verification,
|
|
167
|
+
]),
|
|
168
|
+
),
|
|
169
|
+
'## Residual risks',
|
|
170
|
+
list(report.residualRisks),
|
|
171
|
+
'## Open assumptions & questions',
|
|
172
|
+
list([
|
|
173
|
+
...model.openQuestions,
|
|
174
|
+
...project.artifacts.stride.data.openQuestions,
|
|
175
|
+
...(enabled.has('coordination')
|
|
176
|
+
? project.artifacts.coordination?.data.openQuestions || []
|
|
177
|
+
: []),
|
|
178
|
+
...report.openQuestions,
|
|
179
|
+
]),
|
|
180
|
+
'## Next steps',
|
|
181
|
+
list(report.nextSteps),
|
|
182
|
+
'## Review record',
|
|
183
|
+
table(
|
|
184
|
+
['Phase', 'Status', 'Reviewer / date'],
|
|
185
|
+
enabledPhases(project).map((p) => {
|
|
186
|
+
const a = project.artifacts[p.id];
|
|
187
|
+
return [
|
|
188
|
+
p.title,
|
|
189
|
+
a?.status || 'missing',
|
|
190
|
+
a?.review ? `${a.review.reviewer || 'Approved'} / ${a.review.at}` : 'Not approved',
|
|
191
|
+
];
|
|
192
|
+
}),
|
|
193
|
+
),
|
|
194
|
+
'## Notice',
|
|
195
|
+
'This AI-assisted design-level analysis depends on the supplied evidence and expert review. It is not a code audit, penetration test, certification or warranty of completeness or accuracy. It does not imply that Oak Security authored or endorsed the assessment.',
|
|
196
|
+
);
|
|
197
|
+
return sections.map(clean).join('\n\n') + '\n';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export { pdfReport } from './pdf-report.js';
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const text = z.string().max(12000);
|
|
4
|
+
const short = z.string().min(1).max(300);
|
|
5
|
+
const id = z
|
|
6
|
+
.string()
|
|
7
|
+
.min(1)
|
|
8
|
+
.max(100)
|
|
9
|
+
.regex(/^[\w.-]+$/);
|
|
10
|
+
const strings = z.array(text).max(200);
|
|
11
|
+
const ids = z.array(id).max(200);
|
|
12
|
+
const object = z.strictObject;
|
|
13
|
+
const evidence = object({
|
|
14
|
+
fileId: id,
|
|
15
|
+
startLine: z.int().min(1),
|
|
16
|
+
endLine: z.int().min(1),
|
|
17
|
+
quote: text,
|
|
18
|
+
});
|
|
19
|
+
const grounded = {
|
|
20
|
+
evidence: z.array(evidence).max(20),
|
|
21
|
+
assumption: text.describe('Explicit inference if evidence is absent; otherwise empty.'),
|
|
22
|
+
};
|
|
23
|
+
const item = object({ id, name: short, description: text, ...grounded });
|
|
24
|
+
|
|
25
|
+
export const PHASES = Object.freeze([
|
|
26
|
+
{ id: 'scope', title: 'Scope & source inventory', required: true, gate: false },
|
|
27
|
+
{ id: 'ingest', title: 'Ingest & classify', required: true, gate: false },
|
|
28
|
+
{ id: 'model', title: 'Construct TRACE model', required: true, gate: true },
|
|
29
|
+
{ id: 'stride', title: 'STRIDE & ranking', required: true, gate: true },
|
|
30
|
+
{ id: 'trees', title: 'Attack trees', required: false, gate: true },
|
|
31
|
+
{ id: 'coordination', title: 'Collusion & coordination', required: false, gate: true },
|
|
32
|
+
{ id: 'report', title: 'Roadmap & report', required: true, gate: true },
|
|
33
|
+
]);
|
|
34
|
+
export const PhaseId = z.enum(PHASES.map((p) => p.id));
|
|
35
|
+
export const ProviderId = z.enum(['openai', 'anthropic']);
|
|
36
|
+
// The fast slot reads every source batch; the deep slot reasons about STRIDE and
|
|
37
|
+
// coordination from the finished model. Verified against provider documentation
|
|
38
|
+
// on 2026-11-26; the account model list replaces the catalog when a key is set.
|
|
39
|
+
export const DEFAULT_MODELS = Object.freeze({
|
|
40
|
+
openai: { fastModel: 'gpt-6.2-sol', deepModel: 'gpt-6.2-astra' },
|
|
41
|
+
anthropic: { fastModel: 'claude-sonnet-5-5', deepModel: 'claude-opus-5-5' },
|
|
42
|
+
});
|
|
43
|
+
// Offered when the account model list is not available. Defaults come first.
|
|
44
|
+
export const MODEL_CATALOG = Object.freeze({
|
|
45
|
+
openai: ['gpt-6.2-sol', 'gpt-6.2-astra', 'gpt-6.1-terra', 'gpt-6-luna'],
|
|
46
|
+
anthropic: ['claude-sonnet-5-5', 'claude-opus-5-5', 'claude-fable-5-2', 'claude-haiku-5-5'],
|
|
47
|
+
});
|
|
48
|
+
export const Config = object({
|
|
49
|
+
provider: ProviderId.default('openai'),
|
|
50
|
+
fastModel: short.optional(),
|
|
51
|
+
deepModel: short.optional(),
|
|
52
|
+
effort: z.enum(['low', 'medium', 'high']).default('medium'),
|
|
53
|
+
trees: z.boolean().default(true),
|
|
54
|
+
coordination: z.enum(['auto', 'on', 'off']).default('auto'),
|
|
55
|
+
treeLimit: z.int().min(1).max(10).default(3),
|
|
56
|
+
});
|
|
57
|
+
export function resolveConfig(input) {
|
|
58
|
+
const config = Config.parse(input);
|
|
59
|
+
return {
|
|
60
|
+
...config,
|
|
61
|
+
fastModel: config.fastModel || DEFAULT_MODELS[config.provider].fastModel,
|
|
62
|
+
deepModel: config.deepModel || DEFAULT_MODELS[config.provider].deepModel,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export const DEFAULT_SCOPE =
|
|
66
|
+
'Assess all added sources using TRACE. Identify threats, trust boundaries, protected assets and critical invariants. Flag undocumented assumptions and scope gaps for review.';
|
|
67
|
+
export const CreateProject = object({
|
|
68
|
+
name: short,
|
|
69
|
+
scope: text.default(''),
|
|
70
|
+
config: Config.default(() => Config.parse({})),
|
|
71
|
+
});
|
|
72
|
+
export const UpdateProject = object({
|
|
73
|
+
revision: z.int().min(1),
|
|
74
|
+
name: short.optional(),
|
|
75
|
+
scope: text.optional(),
|
|
76
|
+
config: Config.optional(),
|
|
77
|
+
});
|
|
78
|
+
export const Review = object({
|
|
79
|
+
revision: z.int().min(1),
|
|
80
|
+
reviewer: z.string().trim().max(300).default(''),
|
|
81
|
+
notes: text.default(''),
|
|
82
|
+
});
|
|
83
|
+
export const RunInput = object({
|
|
84
|
+
mode: z.enum(['review', 'draft', 'full']).default('review'),
|
|
85
|
+
resume: z.boolean().default(true),
|
|
86
|
+
phase: PhaseId.exclude(['scope']).optional(),
|
|
87
|
+
}).refine((input) => input.mode !== 'full' || !input.phase, {
|
|
88
|
+
message: 'Full pipeline runs cannot select a single phase.',
|
|
89
|
+
});
|
|
90
|
+
export const SourceInput = z.discriminatedUnion('kind', [
|
|
91
|
+
object({ kind: z.literal('local'), path: short, label: short.optional() }),
|
|
92
|
+
object({ kind: z.literal('git'), url: z.string().url().max(2000), label: short.optional() }),
|
|
93
|
+
object({ kind: z.literal('url'), url: z.string().url().max(2000), label: short.optional() }),
|
|
94
|
+
object({ kind: z.literal('text'), label: short, text: z.string().min(1).max(500000) }),
|
|
95
|
+
object({
|
|
96
|
+
kind: z.literal('files'),
|
|
97
|
+
label: short,
|
|
98
|
+
files: z
|
|
99
|
+
.array(
|
|
100
|
+
object({
|
|
101
|
+
path: z.string().min(1).max(500),
|
|
102
|
+
content: z.string().max(12000000),
|
|
103
|
+
encoding: z.enum(['utf8', 'base64']).default('utf8'),
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
.min(1)
|
|
107
|
+
.max(1000),
|
|
108
|
+
}),
|
|
109
|
+
]);
|
|
110
|
+
export const Schemas = {
|
|
111
|
+
scope: object({
|
|
112
|
+
scope: text,
|
|
113
|
+
inventory: z
|
|
114
|
+
.array(object({ sourceId: id, label: short, hash: short, files: z.int().min(0) }))
|
|
115
|
+
.max(100),
|
|
116
|
+
}),
|
|
117
|
+
ingest: object({
|
|
118
|
+
summary: text,
|
|
119
|
+
profile: short,
|
|
120
|
+
pillars: z.array(z.enum(['protocols', 'systems', 'organisations'])).min(1),
|
|
121
|
+
coordinatingActors: z.boolean(),
|
|
122
|
+
rationale: text,
|
|
123
|
+
candidates: z.array(item).max(100),
|
|
124
|
+
missingEvidence: strings,
|
|
125
|
+
}),
|
|
126
|
+
model: object({
|
|
127
|
+
overview: text,
|
|
128
|
+
threatActors: z.array(item).max(100),
|
|
129
|
+
roles: z.array(item).max(100),
|
|
130
|
+
protectedAssets: z.array(item).min(1).max(100),
|
|
131
|
+
invariants: z.array(item).min(1).max(100),
|
|
132
|
+
components: z.array(item).min(1).max(100),
|
|
133
|
+
edges: z
|
|
134
|
+
.array(
|
|
135
|
+
object({
|
|
136
|
+
...item.shape,
|
|
137
|
+
from: id,
|
|
138
|
+
to: id,
|
|
139
|
+
kind: z.enum(['trust', 'value', 'data', 'authority']),
|
|
140
|
+
boundary: text,
|
|
141
|
+
}),
|
|
142
|
+
)
|
|
143
|
+
.max(200),
|
|
144
|
+
openQuestions: strings,
|
|
145
|
+
}),
|
|
146
|
+
stride: object({
|
|
147
|
+
threats: z
|
|
148
|
+
.array(
|
|
149
|
+
object({
|
|
150
|
+
id,
|
|
151
|
+
title: short,
|
|
152
|
+
category: z.enum([
|
|
153
|
+
'Spoofing',
|
|
154
|
+
'Tampering',
|
|
155
|
+
'Repudiation',
|
|
156
|
+
'Information disclosure',
|
|
157
|
+
'Denial of service',
|
|
158
|
+
'Elevation of privilege',
|
|
159
|
+
]),
|
|
160
|
+
scenario: text,
|
|
161
|
+
targetIds: ids.min(1),
|
|
162
|
+
actorIds: ids,
|
|
163
|
+
assetIds: ids,
|
|
164
|
+
invariantIds: ids,
|
|
165
|
+
likelihood: z.int().min(1).max(5),
|
|
166
|
+
impact: z.int().min(1).max(5),
|
|
167
|
+
rationale: text,
|
|
168
|
+
feasibility: text,
|
|
169
|
+
incentives: text,
|
|
170
|
+
blastRadius: text,
|
|
171
|
+
timeSensitivity: text,
|
|
172
|
+
confidence: z.enum(['low', 'medium', 'high']),
|
|
173
|
+
existingControls: strings,
|
|
174
|
+
mitigations: strings,
|
|
175
|
+
...grounded,
|
|
176
|
+
}),
|
|
177
|
+
)
|
|
178
|
+
.max(120),
|
|
179
|
+
coverage: z.array(object({ objectId: id, threatIds: ids, explanation: text })).max(400),
|
|
180
|
+
openQuestions: strings,
|
|
181
|
+
}),
|
|
182
|
+
trees: object({
|
|
183
|
+
trees: z
|
|
184
|
+
.array(
|
|
185
|
+
object({
|
|
186
|
+
threatId: id,
|
|
187
|
+
goal: short,
|
|
188
|
+
nodes: z
|
|
189
|
+
.array(
|
|
190
|
+
object({
|
|
191
|
+
id,
|
|
192
|
+
parentId: id.nullable(),
|
|
193
|
+
label: short,
|
|
194
|
+
operator: z.enum(['AND', 'OR', 'LEAF']),
|
|
195
|
+
prerequisites: strings,
|
|
196
|
+
speculative: z.boolean(),
|
|
197
|
+
mitigations: strings,
|
|
198
|
+
...grounded,
|
|
199
|
+
}),
|
|
200
|
+
)
|
|
201
|
+
.min(1)
|
|
202
|
+
.max(60),
|
|
203
|
+
}),
|
|
204
|
+
)
|
|
205
|
+
.max(10),
|
|
206
|
+
omitted: z.array(object({ threatId: id, reason: short })).max(120),
|
|
207
|
+
}),
|
|
208
|
+
coordination: object({
|
|
209
|
+
scenarios: z
|
|
210
|
+
.array(
|
|
211
|
+
object({
|
|
212
|
+
id,
|
|
213
|
+
title: short,
|
|
214
|
+
actorIds: ids.min(1),
|
|
215
|
+
assetIds: ids.min(1),
|
|
216
|
+
threshold: text,
|
|
217
|
+
incentive: text,
|
|
218
|
+
sharedDependencies: strings,
|
|
219
|
+
consequence: text,
|
|
220
|
+
mitigations: strings,
|
|
221
|
+
...grounded,
|
|
222
|
+
}),
|
|
223
|
+
)
|
|
224
|
+
.max(60),
|
|
225
|
+
assumptions: strings,
|
|
226
|
+
openQuestions: strings,
|
|
227
|
+
}),
|
|
228
|
+
report: object({
|
|
229
|
+
executiveSummary: text,
|
|
230
|
+
roadmap: z
|
|
231
|
+
.array(
|
|
232
|
+
object({
|
|
233
|
+
id,
|
|
234
|
+
action: text,
|
|
235
|
+
priority: z.enum(['immediate', '30 days', '60 days', '90 days']),
|
|
236
|
+
owner: short,
|
|
237
|
+
linkedIds: ids.min(1),
|
|
238
|
+
verification: text,
|
|
239
|
+
}),
|
|
240
|
+
)
|
|
241
|
+
.max(100),
|
|
242
|
+
residualRisks: strings,
|
|
243
|
+
openQuestions: strings,
|
|
244
|
+
nextSteps: strings,
|
|
245
|
+
}),
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export function enabledPhases(project) {
|
|
249
|
+
const model = project.artifacts.model?.data;
|
|
250
|
+
const multipleActors = (model?.threatActors.length || 0) + (model?.roles.length || 0) > 1;
|
|
251
|
+
const coordination =
|
|
252
|
+
project.config.coordination === 'on' ||
|
|
253
|
+
(project.config.coordination === 'auto' &&
|
|
254
|
+
(project.artifacts.ingest?.data.coordinatingActors || multipleActors));
|
|
255
|
+
return PHASES.filter(
|
|
256
|
+
(p) =>
|
|
257
|
+
p.required ||
|
|
258
|
+
(p.id === 'trees' && project.config.trees) ||
|
|
259
|
+
(p.id === 'coordination' && coordination),
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function jsonSchema(schema, options = {}) {
|
|
264
|
+
const result = z.toJSONSchema(schema, { target: 'draft-7', ...options });
|
|
265
|
+
delete result.$schema;
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mkdirSync,
|
|
3
|
+
readdirSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
writeFileSync,
|
|
6
|
+
renameSync,
|
|
7
|
+
unlinkSync,
|
|
8
|
+
chmodSync,
|
|
9
|
+
openSync,
|
|
10
|
+
fsyncSync,
|
|
11
|
+
closeSync,
|
|
12
|
+
} from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import lockfile from 'proper-lockfile';
|
|
15
|
+
import { insist, AppError } from './errors.js';
|
|
16
|
+
|
|
17
|
+
export const now = () => new Date().toISOString();
|
|
18
|
+
|
|
19
|
+
export class Store {
|
|
20
|
+
static async open(directory) {
|
|
21
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
22
|
+
chmodSync(directory, 0o700);
|
|
23
|
+
let release;
|
|
24
|
+
try {
|
|
25
|
+
release = await lockfile.lock(directory, { stale: 10000, retries: 0 });
|
|
26
|
+
} catch {
|
|
27
|
+
throw new AppError(
|
|
28
|
+
409,
|
|
29
|
+
'store_locked',
|
|
30
|
+
'AiTM is already open with this data directory. Close that instance first, or use --data-dir for a separate assessment store.',
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return new Store(directory, release);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
await release();
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
constructor(directory, release) {
|
|
41
|
+
this.directory = directory;
|
|
42
|
+
this.release = release;
|
|
43
|
+
this.projects = new Map();
|
|
44
|
+
for (const file of readdirSync(directory).filter((f) => /^[0-9a-f-]{36}\.json$/.test(f))) {
|
|
45
|
+
const project = JSON.parse(readFileSync(path.join(directory, file), 'utf8'));
|
|
46
|
+
insist(
|
|
47
|
+
project.schemaVersion === 2 &&
|
|
48
|
+
`${project.id}.json` === file &&
|
|
49
|
+
project.artifacts &&
|
|
50
|
+
Array.isArray(project.runs),
|
|
51
|
+
`Invalid project file ${file}; restore it from a backup.`,
|
|
52
|
+
500,
|
|
53
|
+
'corrupt_store',
|
|
54
|
+
);
|
|
55
|
+
this.projects.set(project.id, project);
|
|
56
|
+
let interrupted = false;
|
|
57
|
+
for (const run of project.runs)
|
|
58
|
+
if (['queued', 'running'].includes(run.status)) {
|
|
59
|
+
run.status = 'interrupted';
|
|
60
|
+
run.finishedAt = now();
|
|
61
|
+
run.error = 'AiTM stopped during this run. Resume to retry the unfinished phase.';
|
|
62
|
+
run.events.push({
|
|
63
|
+
id: ++run.lastEventId,
|
|
64
|
+
type: 'interrupted',
|
|
65
|
+
at: now(),
|
|
66
|
+
phase: run.phase,
|
|
67
|
+
message: run.error,
|
|
68
|
+
});
|
|
69
|
+
interrupted = true;
|
|
70
|
+
}
|
|
71
|
+
if (interrupted) this.save(project);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
get(id) {
|
|
75
|
+
const project = this.projects.get(id);
|
|
76
|
+
insist(project, 'Project not found.', 404, 'not_found');
|
|
77
|
+
return structuredClone(project);
|
|
78
|
+
}
|
|
79
|
+
list() {
|
|
80
|
+
return [...this.projects.values()]
|
|
81
|
+
.map((p) => ({
|
|
82
|
+
id: p.id,
|
|
83
|
+
name: p.name,
|
|
84
|
+
updatedAt: p.updatedAt,
|
|
85
|
+
revision: p.revision,
|
|
86
|
+
sources: p.sources.length,
|
|
87
|
+
lastRun: p.runs.at(-1)?.status || null,
|
|
88
|
+
}))
|
|
89
|
+
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
90
|
+
}
|
|
91
|
+
save(project) {
|
|
92
|
+
project.updatedAt = now();
|
|
93
|
+
project.revision++;
|
|
94
|
+
const target = path.join(this.directory, `${project.id}.json`),
|
|
95
|
+
temporary = `${target}.tmp`;
|
|
96
|
+
writeFileSync(temporary, JSON.stringify(project), { mode: 0o600, flag: 'w' });
|
|
97
|
+
const fd = openSync(temporary, 'r');
|
|
98
|
+
try {
|
|
99
|
+
fsyncSync(fd);
|
|
100
|
+
} finally {
|
|
101
|
+
closeSync(fd);
|
|
102
|
+
}
|
|
103
|
+
renameSync(temporary, target);
|
|
104
|
+
this.projects.set(project.id, structuredClone(project));
|
|
105
|
+
return structuredClone(project);
|
|
106
|
+
}
|
|
107
|
+
remove(id) {
|
|
108
|
+
this.get(id);
|
|
109
|
+
unlinkSync(path.join(this.directory, `${id}.json`));
|
|
110
|
+
this.projects.delete(id);
|
|
111
|
+
}
|
|
112
|
+
async close() {
|
|
113
|
+
await this.release();
|
|
114
|
+
}
|
|
115
|
+
}
|