@astram/core 1.4.0 → 1.6.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/dist/audit/index.d.ts +1 -1
- package/dist/audit/index.d.ts.map +1 -1
- package/dist/audit/index.js.map +1 -1
- package/dist/authentication/credential-resolver.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/session/ports.d.ts +34 -0
- package/dist/session/ports.d.ts.map +1 -1
- package/dist/session/session-authority.d.ts +21 -1
- package/dist/session/session-authority.d.ts.map +1 -1
- package/dist/session/session-authority.js +152 -6
- package/dist/session/session-authority.js.map +1 -1
- package/dist/storage/file-ingest.d.ts +90 -0
- package/dist/storage/file-ingest.d.ts.map +1 -0
- package/dist/storage/file-ingest.js +508 -0
- package/dist/storage/file-ingest.js.map +1 -0
- package/dist/storage/index.d.ts +13 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +11 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/ports.d.ts +66 -0
- package/dist/storage/ports.d.ts.map +1 -0
- package/dist/storage/ports.js +30 -0
- package/dist/storage/ports.js.map +1 -0
- package/dist-cjs/index.js +1 -0
- package/dist-cjs/session/session-authority.js +153 -7
- package/dist-cjs/storage/file-ingest.js +512 -0
- package/dist-cjs/storage/index.js +19 -0
- package/dist-cjs/storage/ports.js +34 -0
- package/package.json +1 -1
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The pure file-ingest check (SPEC docs/features/file-storage/SPEC.md §5 D, §10, §13): decides
|
|
4
|
+
* whether one uploaded file is acceptable for a named {@link FileProfile} and, if so, returns the
|
|
5
|
+
* server-built metadata (`storageKey`, `sha256`, canonical `contentType`, `sizeBytes`, sanitised
|
|
6
|
+
* `originalName`) the storage layer writes. R-8 factory: `createFileIngest(deps)` over injected
|
|
7
|
+
* ports (`ByteDigest`, `newId`) and the profile table.
|
|
8
|
+
*
|
|
9
|
+
* ⛔ CLIENT INPUT IS NEVER TRUSTED: the declared MIME type only has to be IN the allowlist; the
|
|
10
|
+
* returned `contentType` is the profile's own spelling. The bytes must pass the profile's
|
|
11
|
+
* `magic` check. No part of the storage key comes from the request: it is
|
|
12
|
+
* `t/<tenantId>/<newId()>`, and `tenantId` (from the session) is re-checked as a canonical uuid.
|
|
13
|
+
* ⛔ NO LEAKAGE: every rejection is a {@link FileRejectedError} with a FIXED reason code; messages
|
|
14
|
+
* never echo the file name, the declared type or the bytes.
|
|
15
|
+
* ⛔ CHEAP CHECKS FIRST: size is checked before any parsing or hashing; an id is drawn only for an
|
|
16
|
+
* accepted file.
|
|
17
|
+
* ⛔ CEILING AT BOOT: a profile above {@link FILE_MAX_BYTES_CEILING} throws at construction.
|
|
18
|
+
*
|
|
19
|
+
* Pure: no IO, no clock, no randomness. ADR-0003: framework-agnostic — no framework / node imports.
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.XLSX_PROFILE = exports.FileRejectedError = exports.FILE_MAX_BYTES_CEILING = void 0;
|
|
23
|
+
exports.createFileIngest = createFileIngest;
|
|
24
|
+
/** Hard upper bound for any profile's `maxBytes` (50 MiB). No profile, route or env value can exceed it. */
|
|
25
|
+
exports.FILE_MAX_BYTES_CEILING = 50 * 1024 * 1024;
|
|
26
|
+
/** Default per-file cap of the shipped xlsx profile (10 MiB). */
|
|
27
|
+
const DEFAULT_MAX_BYTES = 10 * 1024 * 1024;
|
|
28
|
+
/** A file was refused. The message is fixed per reason: no name, type or bytes are ever included. */
|
|
29
|
+
class FileRejectedError extends Error {
|
|
30
|
+
reason;
|
|
31
|
+
constructor(reason) {
|
|
32
|
+
super(`file rejected: ${reason}`);
|
|
33
|
+
this.name = 'FileRejectedError';
|
|
34
|
+
this.reason = reason;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.FileRejectedError = FileRejectedError;
|
|
38
|
+
// ---------------------------------------------------------------------------------------------
|
|
39
|
+
// Name sanitisation
|
|
40
|
+
// ---------------------------------------------------------------------------------------------
|
|
41
|
+
const MAX_NAME_CODE_POINTS = 255;
|
|
42
|
+
// C0, DEL, C1, soft hyphen, zero-width and line/paragraph separators, BOM, the bidi marks /
|
|
43
|
+
// embedding / override / isolate characters, and the Unicode tag characters U+E0000-U+E007F
|
|
44
|
+
// (invisible or display-spoofing names).
|
|
45
|
+
const NAME_CONTROL_RE =
|
|
46
|
+
// eslint-disable-next-line no-control-regex
|
|
47
|
+
/[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff\u{e0000}-\u{e007f}]/gu;
|
|
48
|
+
// A UTF-16 surrogate without its partner (not well-formed text; would break NFC and storage).
|
|
49
|
+
const LONE_SURROGATE_RE = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g;
|
|
50
|
+
/**
|
|
51
|
+
* Basename only: lone surrogates and control characters removed, NFC, directory parts and `..`
|
|
52
|
+
* dropped, leading/trailing whitespace trimmed (so `'a.xlsx '` keeps its extension). May be ''.
|
|
53
|
+
*/
|
|
54
|
+
function sanitizeName(raw) {
|
|
55
|
+
if (typeof raw !== 'string')
|
|
56
|
+
return '';
|
|
57
|
+
const cleaned = raw.replace(LONE_SURROGATE_RE, '').replace(NAME_CONTROL_RE, '').normalize('NFC');
|
|
58
|
+
const parts = cleaned.split(/[\\/]/);
|
|
59
|
+
const base = (parts[parts.length - 1] ?? '').replace(/\.{2,}/g, '.').trim();
|
|
60
|
+
return base === '.' ? '' : base;
|
|
61
|
+
}
|
|
62
|
+
/** The lower-cased extension (with the dot) of `name`, or '' when it has none or no stem. */
|
|
63
|
+
function extensionOf(name) {
|
|
64
|
+
const dot = name.lastIndexOf('.');
|
|
65
|
+
return dot > 0 ? name.slice(dot).toLowerCase() : '';
|
|
66
|
+
}
|
|
67
|
+
/** Cap at 255 code points, keeping the extension. */
|
|
68
|
+
function capName(name) {
|
|
69
|
+
const cps = Array.from(name);
|
|
70
|
+
if (cps.length <= MAX_NAME_CODE_POINTS)
|
|
71
|
+
return name;
|
|
72
|
+
const dot = name.lastIndexOf('.');
|
|
73
|
+
const ext = dot > 0 ? Array.from(name.slice(dot)) : [];
|
|
74
|
+
const stem = Array.from(dot > 0 ? name.slice(0, dot) : name);
|
|
75
|
+
return stem.slice(0, MAX_NAME_CODE_POINTS - ext.length).join('') + ext.join('');
|
|
76
|
+
}
|
|
77
|
+
// ---------------------------------------------------------------------------------------------
|
|
78
|
+
// Profile validation and the factory
|
|
79
|
+
// ---------------------------------------------------------------------------------------------
|
|
80
|
+
// Canonical (lower-case, 8-4-4-4-12 ASCII hex) uuid only: no trimming, braces or case folding.
|
|
81
|
+
const CANONICAL_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
82
|
+
const SHA256_HEX_RE = /^[0-9a-f]{64}$/;
|
|
83
|
+
// The id segment of the key: a path-safe token (no '/', '.', whitespace).
|
|
84
|
+
const ID_SEGMENT_RE = /^[A-Za-z0-9_-]{1,128}$/;
|
|
85
|
+
function resolveProfile(name, p) {
|
|
86
|
+
const bad = (what) => new Error(`file profile '${name}': ${what}`);
|
|
87
|
+
if (typeof p !== 'object' || p === null)
|
|
88
|
+
throw bad('not an object');
|
|
89
|
+
if (!Number.isInteger(p.maxBytes) || p.maxBytes <= 0) {
|
|
90
|
+
throw bad('maxBytes must be a positive integer');
|
|
91
|
+
}
|
|
92
|
+
if (p.maxBytes > exports.FILE_MAX_BYTES_CEILING) {
|
|
93
|
+
throw bad(`maxBytes exceeds the ${exports.FILE_MAX_BYTES_CEILING}-byte ceiling`);
|
|
94
|
+
}
|
|
95
|
+
if (!Array.isArray(p.contentTypes) || p.contentTypes.length === 0) {
|
|
96
|
+
throw bad('contentTypes must be a non-empty list');
|
|
97
|
+
}
|
|
98
|
+
if (!Array.isArray(p.extensions) || p.extensions.length === 0) {
|
|
99
|
+
throw bad('extensions must be a non-empty list');
|
|
100
|
+
}
|
|
101
|
+
for (const t of p.contentTypes) {
|
|
102
|
+
if (typeof t !== 'string' || t.length === 0)
|
|
103
|
+
throw bad('invalid content type');
|
|
104
|
+
}
|
|
105
|
+
for (const e of p.extensions) {
|
|
106
|
+
if (typeof e !== 'string' || !/^\.[A-Za-z0-9]+$/.test(e))
|
|
107
|
+
throw bad('invalid extension');
|
|
108
|
+
}
|
|
109
|
+
if (typeof p.magic !== 'function')
|
|
110
|
+
throw bad('magic must be a function');
|
|
111
|
+
return Object.freeze({
|
|
112
|
+
maxBytes: p.maxBytes,
|
|
113
|
+
contentTypes: Object.freeze([...p.contentTypes]),
|
|
114
|
+
contentTypesLower: Object.freeze(p.contentTypes.map((t) => t.toLowerCase())),
|
|
115
|
+
extensionsLower: Object.freeze(p.extensions.map((e) => e.toLowerCase())),
|
|
116
|
+
fallbackName: `upload${p.extensions[0].toLowerCase()}`,
|
|
117
|
+
magic: p.magic,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Build the ingest check. Validates every profile now (boot): a cap that is not a positive
|
|
122
|
+
* integer or exceeds {@link FILE_MAX_BYTES_CEILING} throws here, never at request time.
|
|
123
|
+
*/
|
|
124
|
+
function createFileIngest(deps) {
|
|
125
|
+
const { digest, newId } = deps;
|
|
126
|
+
if (typeof digest?.sha256 !== 'function')
|
|
127
|
+
throw new Error('createFileIngest: digest port required');
|
|
128
|
+
if (typeof newId !== 'function')
|
|
129
|
+
throw new Error('createFileIngest: newId port required');
|
|
130
|
+
const profiles = new Map();
|
|
131
|
+
for (const [name, p] of Object.entries(deps.profiles ?? {})) {
|
|
132
|
+
profiles.set(name, resolveProfile(name, p));
|
|
133
|
+
}
|
|
134
|
+
async function ingest(input) {
|
|
135
|
+
// Server-side configuration and session facts first: fail closed, not a file rejection.
|
|
136
|
+
const profile = typeof input.profile === 'string' ? profiles.get(input.profile) : undefined;
|
|
137
|
+
if (!profile)
|
|
138
|
+
throw new Error('file ingest: unknown file profile');
|
|
139
|
+
if (typeof input.tenantId !== 'string' || !CANONICAL_UUID_RE.test(input.tenantId)) {
|
|
140
|
+
throw new Error('file ingest: tenant id is not a canonical uuid');
|
|
141
|
+
}
|
|
142
|
+
const tenantId = input.tenantId;
|
|
143
|
+
const bytes = input.bytes;
|
|
144
|
+
if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
|
|
145
|
+
throw new FileRejectedError('empty');
|
|
146
|
+
}
|
|
147
|
+
if (bytes.length > profile.maxBytes)
|
|
148
|
+
throw new FileRejectedError('too_large');
|
|
149
|
+
const sanitized = sanitizeName(input.originalName);
|
|
150
|
+
const name = sanitized === '' ? profile.fallbackName : sanitized;
|
|
151
|
+
if (!profile.extensionsLower.includes(extensionOf(name))) {
|
|
152
|
+
throw new FileRejectedError('type_not_allowed');
|
|
153
|
+
}
|
|
154
|
+
const declared = typeof input.declaredContentType === 'string' ? input.declaredContentType.toLowerCase() : '';
|
|
155
|
+
const typeIndex = profile.contentTypesLower.indexOf(declared);
|
|
156
|
+
if (typeIndex < 0)
|
|
157
|
+
throw new FileRejectedError('type_not_allowed');
|
|
158
|
+
const contentType = profile.contentTypes[typeIndex];
|
|
159
|
+
let magicOk = false;
|
|
160
|
+
try {
|
|
161
|
+
magicOk = profile.magic(bytes) === true;
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
magicOk = false;
|
|
165
|
+
}
|
|
166
|
+
if (!magicOk)
|
|
167
|
+
throw new FileRejectedError('content_mismatch');
|
|
168
|
+
const sha256 = await digest.sha256(bytes);
|
|
169
|
+
if (typeof sha256 !== 'string' || !SHA256_HEX_RE.test(sha256)) {
|
|
170
|
+
throw new Error('file ingest: digest port returned an invalid sha256');
|
|
171
|
+
}
|
|
172
|
+
const id = newId();
|
|
173
|
+
if (typeof id !== 'string' || !ID_SEGMENT_RE.test(id)) {
|
|
174
|
+
throw new Error('file ingest: newId returned an unsafe key segment');
|
|
175
|
+
}
|
|
176
|
+
return Object.freeze({
|
|
177
|
+
storageKey: `t/${tenantId}/${id}`,
|
|
178
|
+
sha256,
|
|
179
|
+
contentType,
|
|
180
|
+
sizeBytes: bytes.length,
|
|
181
|
+
originalName: capName(name),
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function maxBytesFor(profile) {
|
|
185
|
+
return typeof profile === 'string' ? profiles.get(profile)?.maxBytes : undefined;
|
|
186
|
+
}
|
|
187
|
+
return Object.freeze({ ingest, maxBytesFor });
|
|
188
|
+
}
|
|
189
|
+
// ---------------------------------------------------------------------------------------------
|
|
190
|
+
// XLSX content check: ZIP + OOXML [Content_Types].xml naming a spreadsheetml main part.
|
|
191
|
+
// Dependency-free: central-directory walk plus a bounded raw-DEFLATE decoder (RFC 1951).
|
|
192
|
+
// ---------------------------------------------------------------------------------------------
|
|
193
|
+
const XLSX_CONTENT_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
194
|
+
const XLSX_MAIN_PART_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml';
|
|
195
|
+
const CONTENT_TYPES_ENTRY = '[content_types].xml';
|
|
196
|
+
/** `[Content_Types].xml` is small; a larger (decompressed) one is refused (zip-bomb bound). */
|
|
197
|
+
const MAX_CONTENT_TYPES_BYTES = 1024 * 1024;
|
|
198
|
+
const SIG_LOCAL = 0x04034b50;
|
|
199
|
+
const SIG_CENTRAL = 0x02014b50;
|
|
200
|
+
const SIG_EOCD = 0x06054b50;
|
|
201
|
+
function u16(b, o) {
|
|
202
|
+
if (o < 0 || o + 2 > b.length)
|
|
203
|
+
throw new RangeError('zip: out of bounds');
|
|
204
|
+
return b[o] | (b[o + 1] << 8);
|
|
205
|
+
}
|
|
206
|
+
function u32(b, o) {
|
|
207
|
+
if (o < 0 || o + 4 > b.length)
|
|
208
|
+
throw new RangeError('zip: out of bounds');
|
|
209
|
+
return (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
|
|
210
|
+
}
|
|
211
|
+
/** ASCII-lower-case of a byte range (non-ASCII bytes become '?'). */
|
|
212
|
+
function asciiLower(b, start, end) {
|
|
213
|
+
let s = '';
|
|
214
|
+
for (let i = start; i < end; i++) {
|
|
215
|
+
const c = b[i];
|
|
216
|
+
s += c < 0x80 ? String.fromCharCode(c >= 0x41 && c <= 0x5a ? c + 32 : c) : '?';
|
|
217
|
+
}
|
|
218
|
+
return s;
|
|
219
|
+
}
|
|
220
|
+
/** The decompressed `[Content_Types].xml`, or `null` when the bytes are not a well-formed ZIP holding exactly one. */
|
|
221
|
+
function readContentTypesEntry(b) {
|
|
222
|
+
if (b.length < 30 || u32(b, 0) !== SIG_LOCAL)
|
|
223
|
+
return null;
|
|
224
|
+
// End of central directory: the last 22 bytes plus at most a 65535-byte comment.
|
|
225
|
+
let eocd = -1;
|
|
226
|
+
for (let o = b.length - 22; o >= Math.max(0, b.length - 22 - 0xffff); o--) {
|
|
227
|
+
if (u32(b, o) === SIG_EOCD) {
|
|
228
|
+
eocd = o;
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (eocd < 0)
|
|
233
|
+
return null;
|
|
234
|
+
const count = u16(b, eocd + 10);
|
|
235
|
+
const cdSize = u32(b, eocd + 12);
|
|
236
|
+
const cdOffset = u32(b, eocd + 16);
|
|
237
|
+
if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff)
|
|
238
|
+
return null; // zip64
|
|
239
|
+
if (cdOffset + cdSize > eocd)
|
|
240
|
+
return null;
|
|
241
|
+
let found = null;
|
|
242
|
+
let o = cdOffset;
|
|
243
|
+
for (let i = 0; i < count; i++) {
|
|
244
|
+
if (u32(b, o) !== SIG_CENTRAL)
|
|
245
|
+
return null;
|
|
246
|
+
const flags = u16(b, o + 8);
|
|
247
|
+
const method = u16(b, o + 10);
|
|
248
|
+
const compSize = u32(b, o + 20);
|
|
249
|
+
const uncompSize = u32(b, o + 24);
|
|
250
|
+
const nameLen = u16(b, o + 28);
|
|
251
|
+
const extraLen = u16(b, o + 30);
|
|
252
|
+
const commentLen = u16(b, o + 32);
|
|
253
|
+
const local = u32(b, o + 42);
|
|
254
|
+
if (o + 46 + nameLen > b.length)
|
|
255
|
+
return null;
|
|
256
|
+
if (nameLen === CONTENT_TYPES_ENTRY.length) {
|
|
257
|
+
if (asciiLower(b, o + 46, o + 46 + nameLen) === CONTENT_TYPES_ENTRY) {
|
|
258
|
+
if (found)
|
|
259
|
+
return null; // duplicate entry: ambiguous, refuse
|
|
260
|
+
if (flags & 0x1)
|
|
261
|
+
return null; // encrypted
|
|
262
|
+
found = { method, compSize, uncompSize, local };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
o += 46 + nameLen + extraLen + commentLen;
|
|
266
|
+
}
|
|
267
|
+
if (!found)
|
|
268
|
+
return null;
|
|
269
|
+
if (u32(b, found.local) !== SIG_LOCAL)
|
|
270
|
+
return null;
|
|
271
|
+
const dataStart = found.local + 30 + u16(b, found.local + 26) + u16(b, found.local + 28);
|
|
272
|
+
const dataEnd = dataStart + found.compSize;
|
|
273
|
+
if (dataEnd > b.length)
|
|
274
|
+
return null;
|
|
275
|
+
if (found.method === 0) {
|
|
276
|
+
if (found.compSize > MAX_CONTENT_TYPES_BYTES)
|
|
277
|
+
return null;
|
|
278
|
+
return b.subarray(dataStart, dataEnd);
|
|
279
|
+
}
|
|
280
|
+
if (found.method === 8)
|
|
281
|
+
return inflateRaw(b, dataStart, dataEnd, MAX_CONTENT_TYPES_BYTES);
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
/** True when `[Content_Types].xml` has an `<Override>` whose ContentType is the spreadsheetml main part. */
|
|
285
|
+
function namesSpreadsheetMainPart(xml) {
|
|
286
|
+
const start = xml.length >= 3 && xml[0] === 0xef && xml[1] === 0xbb && xml[2] === 0xbf ? 3 : 0;
|
|
287
|
+
const text = asciiLower(xml, start, xml.length).replace(/<!--[\s\S]*?-->/g, '');
|
|
288
|
+
const overrides = text.match(/<override\b[^>]*>/g) ?? [];
|
|
289
|
+
for (const el of overrides) {
|
|
290
|
+
const m = /\scontenttype\s*=\s*(?:"([^"]*)"|'([^']*)')/.exec(el);
|
|
291
|
+
const value = (m?.[1] ?? m?.[2] ?? '').trim();
|
|
292
|
+
if (value === XLSX_MAIN_PART_TYPE)
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
/** The shipped xlsx content check: a ZIP whose `[Content_Types].xml` names a spreadsheetml main part. */
|
|
298
|
+
function isXlsx(bytes) {
|
|
299
|
+
try {
|
|
300
|
+
const ct = readContentTypesEntry(bytes);
|
|
301
|
+
return ct !== null && namesSpreadsheetMainPart(ct);
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** The `.xlsx` profile: 10 MiB default cap, the spreadsheetml content type, OOXML content check. */
|
|
308
|
+
exports.XLSX_PROFILE = Object.freeze({
|
|
309
|
+
maxBytes: DEFAULT_MAX_BYTES,
|
|
310
|
+
contentTypes: Object.freeze([XLSX_CONTENT_TYPE]),
|
|
311
|
+
extensions: Object.freeze(['.xlsx']),
|
|
312
|
+
magic: isXlsx,
|
|
313
|
+
});
|
|
314
|
+
// ---------------------------------------------------------------------------------------------
|
|
315
|
+
// Bounded raw DEFLATE decoder (RFC 1951). Returns null on any malformed input or when the output
|
|
316
|
+
// would exceed `maxOut`. Only used to read `[Content_Types].xml`.
|
|
317
|
+
// ---------------------------------------------------------------------------------------------
|
|
318
|
+
const MAXBITS = 15;
|
|
319
|
+
const LEN_BASE = [
|
|
320
|
+
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
|
|
321
|
+
163, 195, 227, 258,
|
|
322
|
+
];
|
|
323
|
+
const LEN_EXTRA = [
|
|
324
|
+
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0,
|
|
325
|
+
];
|
|
326
|
+
const DIST_BASE = [
|
|
327
|
+
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049,
|
|
328
|
+
3073, 4097, 6145, 8193, 12289, 16385, 24577,
|
|
329
|
+
];
|
|
330
|
+
const DIST_EXTRA = [
|
|
331
|
+
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
|
|
332
|
+
];
|
|
333
|
+
const CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
334
|
+
class InflateError extends Error {
|
|
335
|
+
}
|
|
336
|
+
function buildHuffman(lengths, n) {
|
|
337
|
+
const count = new Uint16Array(MAXBITS + 1);
|
|
338
|
+
const symbol = new Uint16Array(n);
|
|
339
|
+
for (let s = 0; s < n; s++)
|
|
340
|
+
count[lengths[s]]++;
|
|
341
|
+
if (count[0] === n)
|
|
342
|
+
return { count, symbol };
|
|
343
|
+
let left = 1;
|
|
344
|
+
for (let len = 1; len <= MAXBITS; len++) {
|
|
345
|
+
left <<= 1;
|
|
346
|
+
left -= count[len];
|
|
347
|
+
if (left < 0)
|
|
348
|
+
throw new InflateError('over-subscribed');
|
|
349
|
+
}
|
|
350
|
+
const offs = new Uint16Array(MAXBITS + 1);
|
|
351
|
+
for (let len = 1; len < MAXBITS; len++)
|
|
352
|
+
offs[len + 1] = offs[len] + count[len];
|
|
353
|
+
for (let s = 0; s < n; s++) {
|
|
354
|
+
const l = lengths[s];
|
|
355
|
+
if (l !== 0)
|
|
356
|
+
symbol[offs[l]++] = s;
|
|
357
|
+
}
|
|
358
|
+
return { count, symbol };
|
|
359
|
+
}
|
|
360
|
+
function inflateRaw(src, start, end, maxOut) {
|
|
361
|
+
let pos = start;
|
|
362
|
+
let bitBuf = 0;
|
|
363
|
+
let bitCnt = 0;
|
|
364
|
+
let out = new Uint8Array(Math.min(maxOut, 4096));
|
|
365
|
+
let outLen = 0;
|
|
366
|
+
const bits = (need) => {
|
|
367
|
+
let val = bitBuf;
|
|
368
|
+
while (bitCnt < need) {
|
|
369
|
+
if (pos >= end)
|
|
370
|
+
throw new InflateError('eof');
|
|
371
|
+
val |= src[pos++] << bitCnt;
|
|
372
|
+
bitCnt += 8;
|
|
373
|
+
}
|
|
374
|
+
bitBuf = val >>> need;
|
|
375
|
+
bitCnt -= need;
|
|
376
|
+
return val & ((1 << need) - 1);
|
|
377
|
+
};
|
|
378
|
+
const emit = (byte) => {
|
|
379
|
+
if (outLen >= maxOut)
|
|
380
|
+
throw new InflateError('output cap');
|
|
381
|
+
if (outLen === out.length) {
|
|
382
|
+
const grown = new Uint8Array(Math.min(maxOut, out.length * 2));
|
|
383
|
+
grown.set(out);
|
|
384
|
+
out = grown;
|
|
385
|
+
}
|
|
386
|
+
out[outLen++] = byte;
|
|
387
|
+
};
|
|
388
|
+
const decode = (h) => {
|
|
389
|
+
let code = 0;
|
|
390
|
+
let first = 0;
|
|
391
|
+
let index = 0;
|
|
392
|
+
for (let len = 1; len <= MAXBITS; len++) {
|
|
393
|
+
code |= bits(1);
|
|
394
|
+
const c = h.count[len];
|
|
395
|
+
if (code - c < first)
|
|
396
|
+
return h.symbol[index + (code - first)];
|
|
397
|
+
index += c;
|
|
398
|
+
first += c;
|
|
399
|
+
first <<= 1;
|
|
400
|
+
code <<= 1;
|
|
401
|
+
}
|
|
402
|
+
throw new InflateError('bad code');
|
|
403
|
+
};
|
|
404
|
+
const codes = (lencode, distcode) => {
|
|
405
|
+
for (;;) {
|
|
406
|
+
let sym = decode(lencode);
|
|
407
|
+
if (sym < 256) {
|
|
408
|
+
emit(sym);
|
|
409
|
+
}
|
|
410
|
+
else if (sym === 256) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
sym -= 257;
|
|
415
|
+
if (sym >= 29)
|
|
416
|
+
throw new InflateError('bad length');
|
|
417
|
+
const len = LEN_BASE[sym] + bits(LEN_EXTRA[sym]);
|
|
418
|
+
const dsym = decode(distcode);
|
|
419
|
+
if (dsym >= 30)
|
|
420
|
+
throw new InflateError('bad distance');
|
|
421
|
+
const dist = DIST_BASE[dsym] + bits(DIST_EXTRA[dsym]);
|
|
422
|
+
if (dist > outLen)
|
|
423
|
+
throw new InflateError('distance too far');
|
|
424
|
+
for (let i = 0; i < len; i++)
|
|
425
|
+
emit(out[outLen - dist]);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
let fixed = null;
|
|
430
|
+
try {
|
|
431
|
+
let last = 0;
|
|
432
|
+
do {
|
|
433
|
+
last = bits(1);
|
|
434
|
+
const type = bits(2);
|
|
435
|
+
if (type === 0) {
|
|
436
|
+
bitBuf = 0;
|
|
437
|
+
bitCnt = 0;
|
|
438
|
+
if (pos + 4 > end)
|
|
439
|
+
throw new InflateError('eof');
|
|
440
|
+
const len = src[pos] | (src[pos + 1] << 8);
|
|
441
|
+
const nlen = src[pos + 2] | (src[pos + 3] << 8);
|
|
442
|
+
pos += 4;
|
|
443
|
+
if (len !== (~nlen & 0xffff))
|
|
444
|
+
throw new InflateError('bad stored length');
|
|
445
|
+
if (pos + len > end)
|
|
446
|
+
throw new InflateError('eof');
|
|
447
|
+
for (let i = 0; i < len; i++)
|
|
448
|
+
emit(src[pos + i]);
|
|
449
|
+
pos += len;
|
|
450
|
+
}
|
|
451
|
+
else if (type === 1) {
|
|
452
|
+
if (!fixed) {
|
|
453
|
+
const l = new Uint8Array(288);
|
|
454
|
+
l.fill(8, 0, 144);
|
|
455
|
+
l.fill(9, 144, 256);
|
|
456
|
+
l.fill(7, 256, 280);
|
|
457
|
+
l.fill(8, 280, 288);
|
|
458
|
+
fixed = { len: buildHuffman(l, 288), dist: buildHuffman(new Uint8Array(30).fill(5), 30) };
|
|
459
|
+
}
|
|
460
|
+
codes(fixed.len, fixed.dist);
|
|
461
|
+
}
|
|
462
|
+
else if (type === 2) {
|
|
463
|
+
const nlen = bits(5) + 257;
|
|
464
|
+
const ndist = bits(5) + 1;
|
|
465
|
+
const ncode = bits(4) + 4;
|
|
466
|
+
if (nlen > 286 || ndist > 30)
|
|
467
|
+
throw new InflateError('bad counts');
|
|
468
|
+
const cl = new Uint8Array(19);
|
|
469
|
+
for (let i = 0; i < ncode; i++)
|
|
470
|
+
cl[CL_ORDER[i]] = bits(3);
|
|
471
|
+
const clcode = buildHuffman(cl, 19);
|
|
472
|
+
const lengths = new Uint8Array(nlen + ndist);
|
|
473
|
+
let i = 0;
|
|
474
|
+
while (i < nlen + ndist) {
|
|
475
|
+
const sym = decode(clcode);
|
|
476
|
+
if (sym < 16) {
|
|
477
|
+
lengths[i++] = sym;
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
let value = 0;
|
|
481
|
+
let rep;
|
|
482
|
+
if (sym === 16) {
|
|
483
|
+
if (i === 0)
|
|
484
|
+
throw new InflateError('repeat with no previous');
|
|
485
|
+
value = lengths[i - 1];
|
|
486
|
+
rep = 3 + bits(2);
|
|
487
|
+
}
|
|
488
|
+
else if (sym === 17) {
|
|
489
|
+
rep = 3 + bits(3);
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
rep = 11 + bits(7);
|
|
493
|
+
}
|
|
494
|
+
if (i + rep > nlen + ndist)
|
|
495
|
+
throw new InflateError('too many lengths');
|
|
496
|
+
while (rep--)
|
|
497
|
+
lengths[i++] = value;
|
|
498
|
+
}
|
|
499
|
+
if (lengths[256] === 0)
|
|
500
|
+
throw new InflateError('no end-of-block code');
|
|
501
|
+
codes(buildHuffman(lengths.subarray(0, nlen), nlen), buildHuffman(lengths.subarray(nlen), ndist));
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
throw new InflateError('bad block type');
|
|
505
|
+
}
|
|
506
|
+
} while (!last);
|
|
507
|
+
}
|
|
508
|
+
catch {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
return out.subarray(0, outLen);
|
|
512
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Engine seam: **Storage** — file bytes live outside the database in a private, server-side
|
|
4
|
+
* encrypted object store, behind a pure ingest check (SPEC docs/features/file-storage/SPEC.md §5).
|
|
5
|
+
* Core defines the `ObjectStore` / `ByteDigest` ports and the pure `createFileIngest`; the S3
|
|
6
|
+
* adapter and `node:crypto` digest ship in `@astram/guard` and are injected (ADR-0003).
|
|
7
|
+
*
|
|
8
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / fs imports.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.createFileIngest = exports.XLSX_PROFILE = exports.FileRejectedError = exports.FILE_MAX_BYTES_CEILING = exports.ObjectNotFoundError = exports.ObjectStoreUnavailableError = void 0;
|
|
12
|
+
var ports_js_1 = require("./ports.js");
|
|
13
|
+
Object.defineProperty(exports, "ObjectStoreUnavailableError", { enumerable: true, get: function () { return ports_js_1.ObjectStoreUnavailableError; } });
|
|
14
|
+
Object.defineProperty(exports, "ObjectNotFoundError", { enumerable: true, get: function () { return ports_js_1.ObjectNotFoundError; } });
|
|
15
|
+
var file_ingest_js_1 = require("./file-ingest.js");
|
|
16
|
+
Object.defineProperty(exports, "FILE_MAX_BYTES_CEILING", { enumerable: true, get: function () { return file_ingest_js_1.FILE_MAX_BYTES_CEILING; } });
|
|
17
|
+
Object.defineProperty(exports, "FileRejectedError", { enumerable: true, get: function () { return file_ingest_js_1.FileRejectedError; } });
|
|
18
|
+
Object.defineProperty(exports, "XLSX_PROFILE", { enumerable: true, get: function () { return file_ingest_js_1.XLSX_PROFILE; } });
|
|
19
|
+
Object.defineProperty(exports, "createFileIngest", { enumerable: true, get: function () { return file_ingest_js_1.createFileIngest; } });
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Object storage PORTS (SPEC docs/features/file-storage/SPEC.md §5 A-C) — the framework-agnostic
|
|
4
|
+
* contract for keeping file bytes OUTSIDE the database (a private, server-side-encrypted bucket),
|
|
5
|
+
* plus the byte-digest capability the pure ingest check needs.
|
|
6
|
+
*
|
|
7
|
+
* ADR-0003 (zero-dep core): the backends are real dependencies (an S3 SDK, `node:crypto`), so
|
|
8
|
+
* core defines only these interfaces; `@astram/guard` ships the concrete adapters
|
|
9
|
+
* (`S3ObjectStore`, `NodeSha256Digest`) and INJECTS them.
|
|
10
|
+
*
|
|
11
|
+
* SECURITY: keys are server-built (`t/<tenantId>/<id>`), never client input. Errors carry FIXED
|
|
12
|
+
* messages: no key, credential, endpoint userinfo, file name or bytes ever reaches a message.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ObjectNotFoundError = exports.ObjectStoreUnavailableError = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* The object store cannot be reached or refused the operation. Fixed message, and deliberately no
|
|
18
|
+
* `cause`: backend (SDK) errors can carry the endpoint, request ids or userinfo, so adapters drop them.
|
|
19
|
+
*/
|
|
20
|
+
class ObjectStoreUnavailableError extends Error {
|
|
21
|
+
constructor() {
|
|
22
|
+
super('object store unavailable');
|
|
23
|
+
this.name = 'ObjectStoreUnavailableError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.ObjectStoreUnavailableError = ObjectStoreUnavailableError;
|
|
27
|
+
/** The requested object does not exist. Fixed message (the key is never echoed). */
|
|
28
|
+
class ObjectNotFoundError extends Error {
|
|
29
|
+
constructor() {
|
|
30
|
+
super('object not found');
|
|
31
|
+
this.name = 'ObjectNotFoundError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.ObjectNotFoundError = ObjectNotFoundError;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astram/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Astram framework — sealed security core (Identity, Authentication, Session, Risk, Authorization, Audit). ZERO framework deps — the portable decision brain (ADR-0003). The frozen public contract consumers build against; installed, not edited (ADR-0023).",
|
|
6
6
|
"type": "module",
|