@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,135 @@
|
|
|
1
|
+
import https from 'node:https';
|
|
2
|
+
import { lookup } from 'node:dns/promises';
|
|
3
|
+
import { BlockList, isIP } from 'node:net';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { insist } from './errors.js';
|
|
6
|
+
|
|
7
|
+
const privateIPv4 = new BlockList();
|
|
8
|
+
for (const [network, bits] of [
|
|
9
|
+
['0.0.0.0', 8],
|
|
10
|
+
['10.0.0.0', 8],
|
|
11
|
+
['100.64.0.0', 10],
|
|
12
|
+
['127.0.0.0', 8],
|
|
13
|
+
['169.254.0.0', 16],
|
|
14
|
+
['172.16.0.0', 12],
|
|
15
|
+
['192.0.0.0', 24],
|
|
16
|
+
['192.0.2.0', 24],
|
|
17
|
+
['192.88.99.0', 24],
|
|
18
|
+
['192.168.0.0', 16],
|
|
19
|
+
['198.18.0.0', 15],
|
|
20
|
+
['198.51.100.0', 24],
|
|
21
|
+
['203.0.113.0', 24],
|
|
22
|
+
['224.0.0.0', 4],
|
|
23
|
+
['240.0.0.0', 4],
|
|
24
|
+
])
|
|
25
|
+
privateIPv4.addSubnet(network, bits);
|
|
26
|
+
const globalIPv6 = new BlockList();
|
|
27
|
+
globalIPv6.addSubnet('2000::', 3, 'ipv6');
|
|
28
|
+
const reservedIPv6 = new BlockList();
|
|
29
|
+
for (const [network, bits] of [
|
|
30
|
+
['2001::', 23],
|
|
31
|
+
['2001:db8::', 32],
|
|
32
|
+
['2002::', 16],
|
|
33
|
+
['3fff::', 20],
|
|
34
|
+
])
|
|
35
|
+
reservedIPv6.addSubnet(network, bits, 'ipv6');
|
|
36
|
+
|
|
37
|
+
export function publicAddress(address) {
|
|
38
|
+
return isIP(address) === 4
|
|
39
|
+
? !privateIPv4.check(address)
|
|
40
|
+
: isIP(address) === 6 &&
|
|
41
|
+
globalIPv6.check(address, 'ipv6') &&
|
|
42
|
+
!reservedIPv6.check(address, 'ipv6');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function documentURL(value) {
|
|
46
|
+
const url = new URL(value);
|
|
47
|
+
const host = url.hostname.replace(/^\[|\]$/g, '');
|
|
48
|
+
insist(
|
|
49
|
+
url.protocol === 'https:' && !url.username && !url.password && !url.port,
|
|
50
|
+
'Use a public HTTPS document URL without credentials or a custom port.',
|
|
51
|
+
);
|
|
52
|
+
insist(!isIP(host) || publicAddress(host), 'Document URLs must use a public internet address.');
|
|
53
|
+
url.hash = '';
|
|
54
|
+
return url;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Resolve only public addresses and pass those exact addresses to the TLS socket.
|
|
58
|
+
// Every redirect uses this same lookup, so it cannot redirect to a local service.
|
|
59
|
+
function publicLookup(hostname, options, callback) {
|
|
60
|
+
lookup(hostname, { all: true, family: options.family || 0 })
|
|
61
|
+
.then((addresses) => {
|
|
62
|
+
insist(
|
|
63
|
+
addresses.length && addresses.every(({ address }) => publicAddress(address)),
|
|
64
|
+
'Document URLs must resolve to public internet addresses.',
|
|
65
|
+
);
|
|
66
|
+
if (options.all) callback(null, addresses);
|
|
67
|
+
else callback(null, addresses[0].address, addresses[0].family);
|
|
68
|
+
})
|
|
69
|
+
.catch(callback);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function downloadDocument(
|
|
73
|
+
value,
|
|
74
|
+
{ signal, maxBytes = 8 * 1024 * 1024, request = https.get } = {},
|
|
75
|
+
) {
|
|
76
|
+
const deadline = AbortSignal.any([AbortSignal.timeout(30000), ...(signal ? [signal] : [])]);
|
|
77
|
+
let url = documentURL(value);
|
|
78
|
+
for (let redirects = 0; redirects <= 3; redirects++) {
|
|
79
|
+
deadline.throwIfAborted();
|
|
80
|
+
const response = await new Promise((resolve, reject) => {
|
|
81
|
+
const req = request(
|
|
82
|
+
url,
|
|
83
|
+
{
|
|
84
|
+
lookup: publicLookup,
|
|
85
|
+
signal: deadline,
|
|
86
|
+
headers: { 'user-agent': 'AiTM/2.0', 'accept-encoding': 'identity' },
|
|
87
|
+
},
|
|
88
|
+
resolve,
|
|
89
|
+
);
|
|
90
|
+
req.on('error', reject);
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
if ([301, 302, 303, 307, 308].includes(response.statusCode)) {
|
|
94
|
+
insist(redirects < 3 && response.headers.location, 'Document URL has too many redirects.');
|
|
95
|
+
url = documentURL(new URL(response.headers.location, url));
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
insist(
|
|
99
|
+
response.statusCode === 200,
|
|
100
|
+
`Document download returned HTTP ${response.statusCode}.`,
|
|
101
|
+
);
|
|
102
|
+
insist(
|
|
103
|
+
!response.headers['content-encoding'] ||
|
|
104
|
+
response.headers['content-encoding'] === 'identity',
|
|
105
|
+
'Use an uncompressed document URL.',
|
|
106
|
+
);
|
|
107
|
+
insist(
|
|
108
|
+
Number(response.headers['content-length'] || 0) <= maxBytes,
|
|
109
|
+
'Document exceeds the 8 MiB limit.',
|
|
110
|
+
);
|
|
111
|
+
const type = (response.headers['content-type'] || '').split(';')[0].trim();
|
|
112
|
+
let name = path.posix.basename(decodeURIComponent(url.pathname)) || 'document.txt';
|
|
113
|
+
const extension = {
|
|
114
|
+
'application/pdf': '.pdf',
|
|
115
|
+
'text/html': '.html',
|
|
116
|
+
'application/xhtml+xml': '.html',
|
|
117
|
+
'application/zip': '.zip',
|
|
118
|
+
'application/x-zip-compressed': '.zip',
|
|
119
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx',
|
|
120
|
+
}[type];
|
|
121
|
+
if (extension && !name.toLowerCase().endsWith(extension)) name += extension;
|
|
122
|
+
const chunks = [];
|
|
123
|
+
let size = 0;
|
|
124
|
+
for await (const chunk of response) {
|
|
125
|
+
deadline.throwIfAborted();
|
|
126
|
+
size += chunk.length;
|
|
127
|
+
insist(size <= maxBytes, 'Document exceeds the 8 MiB limit.');
|
|
128
|
+
chunks.push(chunk);
|
|
129
|
+
}
|
|
130
|
+
return { name, buffer: Buffer.concat(chunks), url: url.toString() };
|
|
131
|
+
} finally {
|
|
132
|
+
response.destroy();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|