@sentropic/artifacts 0.2.13 → 0.2.15
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/CHANGELOG.md +37 -0
- package/dist/adapters/local-blob.d.ts +3 -0
- package/dist/adapters/local-blob.js +93 -0
- package/dist/adapters/postgres-uploads.d.ts +14 -0
- package/dist/adapters/postgres-uploads.js +174 -0
- package/dist/adapters/s3.js +23 -0
- package/dist/adapters/sqlite-activation.d.ts +20 -0
- package/dist/adapters/sqlite-activation.js +205 -0
- package/dist/adapters/sqlite-commit.d.ts +15 -0
- package/dist/adapters/sqlite-commit.js +89 -0
- package/dist/adapters/sqlite-management.d.ts +10 -0
- package/dist/adapters/sqlite-management.js +60 -0
- package/dist/adapters/sqlite-sql.d.ts +32 -0
- package/dist/adapters/sqlite-sql.js +36 -0
- package/dist/adapters/sqlite-store.d.ts +12 -0
- package/dist/adapters/sqlite-store.js +410 -0
- package/dist/adapters/sqlite.d.ts +33 -0
- package/dist/adapters/sqlite.js +95 -0
- package/dist/content/handler.d.ts +5 -0
- package/dist/content/handler.js +80 -9
- package/dist/http.d.ts +16 -0
- package/dist/http.js +435 -97
- package/dist/mcp-core.d.ts +3 -3
- package/dist/mcp-core.js +43 -4
- package/dist/profile/contracts.d.ts +1 -1
- package/dist/profile/contracts.js +4 -0
- package/dist/profile/index.js +12 -2
- package/dist/profile/schemas.js +60 -0
- package/dist/profile/validation.js +31 -4
- package/dist/server/command-identity.d.ts +1 -1
- package/dist/server/command-identity.js +4 -3
- package/dist/server/gc.d.ts +28 -3
- package/dist/server/gc.js +81 -5
- package/dist/server/management.js +10 -5
- package/dist/server/metrics.d.ts +21 -0
- package/dist/server/metrics.js +6 -0
- package/dist/server/ports.d.ts +7 -0
- package/dist/server/ports.js +7 -0
- package/dist/server/publication-limits.d.ts +8 -0
- package/dist/server/publication-limits.js +8 -0
- package/dist/server/publication.d.ts +5 -0
- package/dist/server/publication.js +6 -0
- package/dist/server/source-read.d.ts +12 -9
- package/dist/server/source-read.js +17 -5
- package/dist/server/upload-page.d.ts +157 -0
- package/dist/server/upload-page.js +438 -0
- package/dist/server/uploads.d.ts +75 -1
- package/dist/server/uploads.js +187 -27
- package/migrations/0009_upload_pages.sql +25 -0
- package/package.json +13 -1
- package/sql/sqlite/0001_artifacts.sql +156 -0
- package/sql/sqlite/0002_activation.sql +73 -0
package/dist/server/ports.js
CHANGED
|
@@ -21,6 +21,13 @@ export function createCryptoUuid() {
|
|
|
21
21
|
},
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* R1 typed unknown principal (review 1002): every model-visible projection of
|
|
26
|
+
* an internal principal whose directory entry is missing emits this opaque
|
|
27
|
+
* value instead of the raw internal id (`sub`). It never resolves back to a
|
|
28
|
+
* principal, so re-injecting it fails closed with `person_not_shareable`.
|
|
29
|
+
*/
|
|
30
|
+
export const UNKNOWN_USER_REF = 'unknown';
|
|
24
31
|
/**
|
|
25
32
|
* Mask an email for model payloads: first local-part character kept, rest of
|
|
26
33
|
* the local part masked, domain visible — lifts homonyms without disclosing
|
|
@@ -15,6 +15,14 @@ export declare const SERVER_MAX_FILE_BYTES: number;
|
|
|
15
15
|
export declare const SERVER_MAX_TOTAL_BYTES: number;
|
|
16
16
|
/** Concurrent open upload sessions per principal (leaked-URL DoS bound). */
|
|
17
17
|
export declare const SERVER_MAX_UPLOAD_SESSIONS_PER_PRINCIPAL = 8;
|
|
18
|
+
/**
|
|
19
|
+
* Generic `get_source` server maxima (files parity LOT 2 §B.3): 32 files, 1 MiB
|
|
20
|
+
* per file, 2 MiB total. A profile may only narrow them; the frozen definition
|
|
21
|
+
* caps `paths` at 32, so larger listings belong to `artifacts_list_assets`.
|
|
22
|
+
*/
|
|
23
|
+
export declare const SERVER_MAX_SOURCE_FILES = 32;
|
|
24
|
+
export declare const SERVER_MAX_SOURCE_INLINE_BYTES: number;
|
|
25
|
+
export declare const SERVER_MAX_SOURCE_TOTAL_BYTES: number;
|
|
18
26
|
/** Generic defaults: 255 files, 16 MiB per file, 64 MiB total (files parity LOT 1). */
|
|
19
27
|
export declare const DEFAULT_PUBLICATION_LIMITS: Readonly<PublicationLimits>;
|
|
20
28
|
export declare function publicationLimits(overrides?: Partial<PublicationLimits>): Readonly<PublicationLimits>;
|
|
@@ -9,6 +9,14 @@ export const SERVER_MAX_FILE_BYTES = 16 * 1024 * 1024;
|
|
|
9
9
|
export const SERVER_MAX_TOTAL_BYTES = 64 * 1024 * 1024;
|
|
10
10
|
/** Concurrent open upload sessions per principal (leaked-URL DoS bound). */
|
|
11
11
|
export const SERVER_MAX_UPLOAD_SESSIONS_PER_PRINCIPAL = 8;
|
|
12
|
+
/**
|
|
13
|
+
* Generic `get_source` server maxima (files parity LOT 2 §B.3): 32 files, 1 MiB
|
|
14
|
+
* per file, 2 MiB total. A profile may only narrow them; the frozen definition
|
|
15
|
+
* caps `paths` at 32, so larger listings belong to `artifacts_list_assets`.
|
|
16
|
+
*/
|
|
17
|
+
export const SERVER_MAX_SOURCE_FILES = 32;
|
|
18
|
+
export const SERVER_MAX_SOURCE_INLINE_BYTES = 1024 * 1024;
|
|
19
|
+
export const SERVER_MAX_SOURCE_TOTAL_BYTES = 2 * 1024 * 1024;
|
|
12
20
|
/** Generic defaults: 255 files, 16 MiB per file, 64 MiB total (files parity LOT 1). */
|
|
13
21
|
export const DEFAULT_PUBLICATION_LIMITS = Object.freeze({
|
|
14
22
|
maxFiles: SERVER_MAX_FILES_PER_RELEASE,
|
|
@@ -31,6 +31,11 @@ export interface PublicationStateStore {
|
|
|
31
31
|
export interface PublicationBlobStore {
|
|
32
32
|
putBlob(key: string, bytes: Uint8Array): Promise<void>;
|
|
33
33
|
getBlob(key: string): Promise<Uint8Array | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Files parity LOT 2 §B.4: byte-range read (inclusive bounds). Optional:
|
|
36
|
+
* the content handler falls back to getBlob + in-memory slicing.
|
|
37
|
+
*/
|
|
38
|
+
getBlobRange?(key: string, start: number, endInclusive: number): Promise<Uint8Array | undefined>;
|
|
34
39
|
/** List keys under a prefix (orphan enumeration for crash recovery/GC). */
|
|
35
40
|
listKeys(prefix: string): Promise<string[]>;
|
|
36
41
|
/** Delete a key (strict: unknown keys fail so callers notice). */
|
|
@@ -67,6 +67,12 @@ export function createMemoryBlobStore() {
|
|
|
67
67
|
getBlob(key) {
|
|
68
68
|
return Promise.resolve(blobs.get(key));
|
|
69
69
|
},
|
|
70
|
+
getBlobRange(key, start, endInclusive) {
|
|
71
|
+
const bytes = blobs.get(key);
|
|
72
|
+
if (bytes === undefined || start < 0 || endInclusive < start)
|
|
73
|
+
return Promise.resolve(undefined);
|
|
74
|
+
return Promise.resolve(bytes.slice(start, Math.min(endInclusive + 1, bytes.length)));
|
|
75
|
+
},
|
|
70
76
|
async listKeys(prefix) {
|
|
71
77
|
assertBlobPrefix(prefix);
|
|
72
78
|
return [...blobs.keys()].filter((key) => key.startsWith(prefix)).sort();
|
|
@@ -16,7 +16,11 @@ export interface SourceReadPorts {
|
|
|
16
16
|
tool: string;
|
|
17
17
|
version: string;
|
|
18
18
|
};
|
|
19
|
+
/** Data-file slots per call (`sourceFilesMax`, server ceiling 32). */
|
|
19
20
|
maxFiles: number;
|
|
21
|
+
/** Per-file raw-byte budget (`sourceInlineFileBytesMax`, server ceiling 1 MiB). */
|
|
22
|
+
maxInlineBytes: number;
|
|
23
|
+
/** Raw-byte budget for files with data (`sourceTotalBytesMax`, server ceiling 2 MiB). */
|
|
20
24
|
maxTotalBytes: number;
|
|
21
25
|
}
|
|
22
26
|
export interface SourceReadInput {
|
|
@@ -33,20 +37,19 @@ export declare function readArtifactSource(ports: SourceReadPorts, input: Source
|
|
|
33
37
|
digest: string;
|
|
34
38
|
manifest: string;
|
|
35
39
|
files: ({
|
|
36
|
-
text: string;
|
|
37
|
-
encoding: "utf8";
|
|
38
40
|
path: string;
|
|
39
41
|
mimeType: string;
|
|
40
|
-
digest: string;
|
|
41
42
|
size: number;
|
|
43
|
+
digest: string;
|
|
44
|
+
} & ({
|
|
45
|
+
text: string;
|
|
46
|
+
encoding: 'utf8';
|
|
42
47
|
} | {
|
|
43
48
|
base64: string;
|
|
44
|
-
encoding:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
size: number;
|
|
49
|
-
})[];
|
|
49
|
+
encoding: 'base64';
|
|
50
|
+
} | {
|
|
51
|
+
omitted: 'too_large' | 'over_total_budget';
|
|
52
|
+
}))[];
|
|
50
53
|
sourceRef: SourceRef | null;
|
|
51
54
|
guideRef: {
|
|
52
55
|
readonly tool: "artifacts_get_api_guide";
|
|
@@ -29,21 +29,17 @@ export async function readArtifactSource(ports, input) {
|
|
|
29
29
|
throw new ArtifactOperationError('verification_failed');
|
|
30
30
|
if (release.state === 'purged')
|
|
31
31
|
throw new ArtifactOperationError('release_unavailable', { publicDetails: { reason: 'purged' } });
|
|
32
|
-
if (!manifest.digest || !Array.isArray(manifest.files)
|
|
32
|
+
if (!manifest.digest || !Array.isArray(manifest.files)) {
|
|
33
33
|
throw new ArtifactOperationError('verification_failed');
|
|
34
34
|
}
|
|
35
35
|
const entries = [];
|
|
36
36
|
const paths = new Set();
|
|
37
|
-
let total = 0;
|
|
38
37
|
for (const value of manifest.files) {
|
|
39
38
|
const file = value;
|
|
40
39
|
if (!file || typeof file.path !== 'string' || typeof file.mimeType !== 'string' ||
|
|
41
40
|
!/^sha256:[0-9a-f]{64}$/.test(file.digest) || !Number.isSafeInteger(file.size) || file.size < 0 ||
|
|
42
41
|
paths.has(file.path))
|
|
43
42
|
throw new ArtifactOperationError('verification_failed');
|
|
44
|
-
total += file.size;
|
|
45
|
-
if (total > ports.maxTotalBytes)
|
|
46
|
-
throw new ArtifactOperationError('limit_exceeded');
|
|
47
43
|
paths.add(file.path);
|
|
48
44
|
entries.push(file);
|
|
49
45
|
}
|
|
@@ -52,8 +48,22 @@ export async function readArtifactSource(ports, input) {
|
|
|
52
48
|
throw new ArtifactOperationError('invalid_request');
|
|
53
49
|
}
|
|
54
50
|
const selected = input.paths === undefined ? entries : entries.filter((file) => input.paths.includes(file.path));
|
|
51
|
+
// LOT 2 §B.3: failure turns into data. Files are evaluated in manifest
|
|
52
|
+
// order against raw-byte budgets: above the per-file cap they are
|
|
53
|
+
// `too_large`; past the slot or total budget they are `over_total_budget`.
|
|
54
|
+
// Previously successful reads (within every budget) are byte-identical.
|
|
55
55
|
const files = [];
|
|
56
|
+
let dataFiles = 0;
|
|
57
|
+
let dataTotal = 0;
|
|
56
58
|
for (const file of selected) {
|
|
59
|
+
if (file.size > ports.maxInlineBytes) {
|
|
60
|
+
files.push({ ...file, omitted: 'too_large' });
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (dataFiles >= ports.maxFiles || dataTotal + file.size > ports.maxTotalBytes) {
|
|
64
|
+
files.push({ ...file, omitted: 'over_total_budget' });
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
57
67
|
const bytes = await ports.getBlob(`artifacts/${manifest.storageScopeId}/blobs/sha256/${file.digest.slice(7)}`);
|
|
58
68
|
if (!bytes)
|
|
59
69
|
throw new ArtifactOperationError('release_unavailable', { publicDetails: { reason: 'not_retained' } });
|
|
@@ -74,6 +84,8 @@ export async function readArtifactSource(ports, input) {
|
|
|
74
84
|
files.push({ ...file, ...(text !== undefined
|
|
75
85
|
? { text, encoding: 'utf8' }
|
|
76
86
|
: { base64: Buffer.from(bytes).toString('base64'), encoding: 'base64' }) });
|
|
87
|
+
dataFiles += 1;
|
|
88
|
+
dataTotal += file.size;
|
|
77
89
|
}
|
|
78
90
|
const sourceRef = await ports.getSource(input.artifactId) ?? null;
|
|
79
91
|
await ports.authorize();
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { StagedFileRecord, StagingStore, UploadSessionStore, UploadSignerPort } from './uploads.js';
|
|
2
|
+
import type { PublicationBlobStore } from './publication.js';
|
|
3
|
+
import type { RefcountStore } from './gc.js';
|
|
4
|
+
import type { IdempotencyStore } from './idempotency.js';
|
|
5
|
+
import type { ArtifactsMetrics } from './metrics.js';
|
|
6
|
+
export declare const UPLOAD_PAGE_TTL_MS: number;
|
|
7
|
+
export declare const UPLOAD_PAGE_COOKIE = "__Host-artifacts_upload";
|
|
8
|
+
export declare const SERVER_MAX_OPEN_UPLOAD_PAGES = 4;
|
|
9
|
+
export type UploadPageState = 'open' | 'exchanged' | 'closed' | 'expired';
|
|
10
|
+
export interface UploadPageRow {
|
|
11
|
+
readonly pageSessionId: string;
|
|
12
|
+
readonly artifactId: string;
|
|
13
|
+
readonly storageScopeId: string;
|
|
14
|
+
readonly ownerId: string;
|
|
15
|
+
readonly tokenHash: string;
|
|
16
|
+
readonly state: UploadPageState;
|
|
17
|
+
readonly maxFiles: number;
|
|
18
|
+
readonly createdAt: string;
|
|
19
|
+
readonly expiresAt: string;
|
|
20
|
+
readonly exchangedAt?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface UploadPageStore {
|
|
23
|
+
create(row: UploadPageRow): Promise<void>;
|
|
24
|
+
get(pageSessionId: string): Promise<UploadPageRow | undefined>;
|
|
25
|
+
countActiveByOwner(ownerId: string): Promise<number>;
|
|
26
|
+
listExpiredPages(now: Date): Promise<UploadPageRow[]>;
|
|
27
|
+
markExchanged(pageSessionId: string, at: string): Promise<void>;
|
|
28
|
+
markExpired(pageSessionId: string): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
export declare function createMemoryUploadPageStore(): UploadPageStore;
|
|
31
|
+
export interface UploadPageCookieSigner {
|
|
32
|
+
sign(pageSessionId: string): string;
|
|
33
|
+
verify(value: string): string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* HMAC cookie signer for one upload page (mirrors `content/grants.ts`).
|
|
37
|
+
* The cookie carries `{sid, exp}`; the page row carries state and expiry, so
|
|
38
|
+
* a stolen cookie is bounded by the row TTL and the owner binding.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createHmacUploadPageCookieSigner(options: {
|
|
41
|
+
secret: string | Uint8Array;
|
|
42
|
+
clock?: () => number;
|
|
43
|
+
ttlSeconds?: number;
|
|
44
|
+
}): UploadPageCookieSigner;
|
|
45
|
+
/**
|
|
46
|
+
* Host-only page cookie value (review finding 1: the `__Host-` prefix
|
|
47
|
+
* requires `Path=/`, exactly like the shipped grant cookie).
|
|
48
|
+
*/
|
|
49
|
+
export declare function buildUploadPageCookie(value: string, maxAgeSeconds: number): string;
|
|
50
|
+
export declare function parseUploadPageCookie(header: string | undefined): string | undefined;
|
|
51
|
+
/** Page CSP (§A.4): no external assets; S3 origins are host-supplied. */
|
|
52
|
+
export declare function uploadPageCsp(s3Origins: readonly string[]): string;
|
|
53
|
+
/**
|
|
54
|
+
* Self-contained static upload page (no external assets). Bytes travel
|
|
55
|
+
* browser → S3 via raw PUT (FormData is banned: multipart wrapping changes
|
|
56
|
+
* length and type); one session per file with `size = File.size`; at most 4
|
|
57
|
+
* PUTs in flight; commit each file before opening further sessions.
|
|
58
|
+
*/
|
|
59
|
+
export declare function renderUploadPage(options: {
|
|
60
|
+
s3Origins: readonly string[];
|
|
61
|
+
}): string;
|
|
62
|
+
export interface UploadPageProfileCaps {
|
|
63
|
+
readonly maxFiles: number;
|
|
64
|
+
readonly maxFileBytes: number;
|
|
65
|
+
readonly maxTotalBytes: number;
|
|
66
|
+
readonly mimeTypes: readonly string[];
|
|
67
|
+
/** Manifest path byte budget (profile `pathMaxBytes`); absent ⇒ 1024. */
|
|
68
|
+
readonly pathMaxBytes?: number;
|
|
69
|
+
}
|
|
70
|
+
export interface UploadPageServiceDeps {
|
|
71
|
+
readonly pages: UploadPageStore;
|
|
72
|
+
readonly sessions: UploadSessionStore;
|
|
73
|
+
readonly staging: StagingStore;
|
|
74
|
+
readonly signer: UploadSignerPort;
|
|
75
|
+
readonly blobs: PublicationBlobStore;
|
|
76
|
+
readonly refs: RefcountStore;
|
|
77
|
+
readonly idempotency?: IdempotencyStore;
|
|
78
|
+
readonly cookieSigner: UploadPageCookieSigner;
|
|
79
|
+
readonly controlOrigin: string;
|
|
80
|
+
readonly pageBaseUrl: string;
|
|
81
|
+
readonly s3Origins: readonly string[];
|
|
82
|
+
readonly profile: UploadPageProfileCaps;
|
|
83
|
+
/** Per-artifact staged caps, enforced at commit for the browser channel. */
|
|
84
|
+
readonly budget: {
|
|
85
|
+
filesMax: number;
|
|
86
|
+
totalBytesMax: number;
|
|
87
|
+
};
|
|
88
|
+
/** Per-scope staging aggregate shared with the agent channel. */
|
|
89
|
+
readonly stagingQuotaBytesPerScope?: number;
|
|
90
|
+
readonly metrics?: ArtifactsMetrics;
|
|
91
|
+
}
|
|
92
|
+
export interface MintUploadPageInput {
|
|
93
|
+
readonly artifactId: string;
|
|
94
|
+
readonly storageScopeId: string;
|
|
95
|
+
readonly ownerId: string;
|
|
96
|
+
readonly maxFiles?: number;
|
|
97
|
+
}
|
|
98
|
+
export interface UploadPageMint {
|
|
99
|
+
readonly pageSessionId: string;
|
|
100
|
+
readonly token: string;
|
|
101
|
+
readonly uploadPageUrl: string;
|
|
102
|
+
readonly expiresAt: string;
|
|
103
|
+
readonly maxFiles: number;
|
|
104
|
+
readonly maxFileBytes: number;
|
|
105
|
+
readonly maxTotalBytes: number;
|
|
106
|
+
readonly allowedMimeTypes: readonly string[];
|
|
107
|
+
}
|
|
108
|
+
export interface UploadPageExchange {
|
|
109
|
+
readonly cookie: string;
|
|
110
|
+
readonly maxAgeSeconds: number;
|
|
111
|
+
}
|
|
112
|
+
export interface PageFileInput {
|
|
113
|
+
readonly cookie: string;
|
|
114
|
+
readonly origin: string;
|
|
115
|
+
readonly customHeader: boolean;
|
|
116
|
+
readonly artifactId: string;
|
|
117
|
+
readonly path: string;
|
|
118
|
+
readonly mimeType: string;
|
|
119
|
+
readonly size: number;
|
|
120
|
+
}
|
|
121
|
+
export interface PageCommitInput extends PageFileInput {
|
|
122
|
+
readonly uploadId: string;
|
|
123
|
+
readonly digest: string;
|
|
124
|
+
readonly idempotencyKey: string;
|
|
125
|
+
}
|
|
126
|
+
export interface UploadPageService {
|
|
127
|
+
mint(input: MintUploadPageInput): Promise<UploadPageMint>;
|
|
128
|
+
exchange(input: {
|
|
129
|
+
pageSessionId: string;
|
|
130
|
+
token: string;
|
|
131
|
+
origin: string;
|
|
132
|
+
}): Promise<UploadPageExchange>;
|
|
133
|
+
createFile(input: PageFileInput): Promise<{
|
|
134
|
+
uploadId: string;
|
|
135
|
+
uploadUrl: string;
|
|
136
|
+
method: 'PUT';
|
|
137
|
+
expiresAt: string;
|
|
138
|
+
maxSize: number;
|
|
139
|
+
requiredHeaders: Record<string, string>;
|
|
140
|
+
checksumHeader: 'x-amz-checksum-sha256';
|
|
141
|
+
stagingKey: string;
|
|
142
|
+
}>;
|
|
143
|
+
commitFile(input: PageCommitInput): Promise<StagedFileRecord>;
|
|
144
|
+
listStaged(input: {
|
|
145
|
+
cookie: string;
|
|
146
|
+
origin: string;
|
|
147
|
+
customHeader: boolean;
|
|
148
|
+
artifactId: string;
|
|
149
|
+
}): Promise<StagedFileRecord[]>;
|
|
150
|
+
}
|
|
151
|
+
export declare function createUploadPageService(deps: UploadPageServiceDeps, options?: {
|
|
152
|
+
now?: () => Date;
|
|
153
|
+
ids?: () => string;
|
|
154
|
+
randomToken?: () => string;
|
|
155
|
+
pageTtlMs?: number;
|
|
156
|
+
maxOpenPages?: number;
|
|
157
|
+
}): UploadPageService;
|