@celilo/e2e 0.7.15 → 0.7.17
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/AGENTS.md +117 -0
- package/COVERAGE.md +60 -0
- package/bin/e2e-build +20 -0
- package/bin/e2e-cache-images +13 -6
- package/config/dns/iamtheinternet.org.zone +16 -0
- package/config/proxy/startup.sh +14 -0
- package/config/routing/fw-ext-routes.sh +13 -0
- package/package.json +6 -3
- package/registry-server/src/auth.test.ts +56 -30
- package/registry-server/src/auth.ts +88 -19
- package/registry-server/src/scoped-token-store.test.ts +93 -0
- package/registry-server/src/scoped-token-store.ts +110 -0
- package/registry-server/src/server.test.ts +92 -10
- package/registry-server/src/server.ts +104 -31
- package/scripts/pack-celilo-packages.ts +114 -22
- package/scripts/stage-apt-repo.ts +14 -2
- package/src/cli/build.ts +29 -0
- package/src/cli/command-registry.ts +3 -1
- package/src/cli/completion.ts +2 -0
- package/src/cli/index.ts +16 -3
- package/src/docker-compose-generator.ts +11 -0
- package/src/runner.ts +63 -6
- package/src/shared-infra.ts +52 -5
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
import { hashToken } from './auth';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Persisted store of minted, package-scoped publish tokens (build-bus Phase 3,
|
|
8
|
+
* ISS-0140). The `registry_publish` capability mints one token per registered
|
|
9
|
+
* repo, scoped to the single package that repo publishes — so a compromised
|
|
10
|
+
* runner can ship only its own module, never another.
|
|
11
|
+
*
|
|
12
|
+
* Only the token HASH is persisted (never cleartext) — the raw token is returned
|
|
13
|
+
* exactly once at mint time, for the caller to set as the repo's Forgejo Actions
|
|
14
|
+
* secret. At startup the server loads this store into its {@link TokenAuth} via
|
|
15
|
+
* `addHashed`; mint/revoke update both the in-memory auth and this file, so a
|
|
16
|
+
* minted token works without a server restart.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export interface ScopedTokenEntry {
|
|
20
|
+
/** SHA-256 of the raw token. */
|
|
21
|
+
hash: string;
|
|
22
|
+
/** Package this token may publish. */
|
|
23
|
+
scope: string;
|
|
24
|
+
/** Repo path on the source forge this token was minted for (the reconcile key). */
|
|
25
|
+
repo: string;
|
|
26
|
+
/** ISO timestamp of mint. */
|
|
27
|
+
mintedAt: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface MintResult {
|
|
31
|
+
/** The raw token — returned ONCE; never persisted in cleartext. */
|
|
32
|
+
token: string;
|
|
33
|
+
entry: ScopedTokenEntry;
|
|
34
|
+
/** Hashes of tokens this mint superseded for the same repo (revoke them in auth). */
|
|
35
|
+
revokedHashes: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** I/O seam (Rule 2.3) so the store is unit-testable without the filesystem. */
|
|
39
|
+
export interface ScopedTokenPersistence {
|
|
40
|
+
load(): ScopedTokenEntry[];
|
|
41
|
+
save(entries: ScopedTokenEntry[]): void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** File-backed persistence: a JSON array at `filePath`, tolerant of a missing file. */
|
|
45
|
+
export function fileScopedTokenPersistence(filePath: string): ScopedTokenPersistence {
|
|
46
|
+
return {
|
|
47
|
+
load() {
|
|
48
|
+
if (!existsSync(filePath)) return [];
|
|
49
|
+
try {
|
|
50
|
+
const parsed = JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
51
|
+
return Array.isArray(parsed) ? (parsed as ScopedTokenEntry[]) : [];
|
|
52
|
+
} catch {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
save(entries) {
|
|
57
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
58
|
+
writeFileSync(filePath, JSON.stringify(entries, null, 2));
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function defaultGenToken(): string {
|
|
64
|
+
return `cpt_${randomBytes(32).toString('base64url')}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class ScopedTokenStore {
|
|
68
|
+
private entries: ScopedTokenEntry[];
|
|
69
|
+
|
|
70
|
+
constructor(
|
|
71
|
+
private readonly persistence: ScopedTokenPersistence,
|
|
72
|
+
private readonly genToken: () => string = defaultGenToken,
|
|
73
|
+
private readonly now: () => string = () => new Date().toISOString(),
|
|
74
|
+
) {
|
|
75
|
+
this.entries = persistence.load();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
list(): ScopedTokenEntry[] {
|
|
79
|
+
return [...this.entries];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Mint a fresh scoped token for `repo`, reconciling: any existing token for the
|
|
84
|
+
* same repo is superseded (its hash returned in `revokedHashes`). Idempotent at
|
|
85
|
+
* the repo level — re-running rotates the token cleanly.
|
|
86
|
+
*/
|
|
87
|
+
mint(repo: string, scope: string): MintResult {
|
|
88
|
+
const revokedHashes = this.entries.filter((e) => e.repo === repo).map((e) => e.hash);
|
|
89
|
+
const token = this.genToken();
|
|
90
|
+
const entry: ScopedTokenEntry = {
|
|
91
|
+
hash: hashToken(token),
|
|
92
|
+
scope,
|
|
93
|
+
repo,
|
|
94
|
+
mintedAt: this.now(),
|
|
95
|
+
};
|
|
96
|
+
this.entries = [...this.entries.filter((e) => e.repo !== repo), entry];
|
|
97
|
+
this.persistence.save(this.entries);
|
|
98
|
+
return { token, entry, revokedHashes };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Revoke all tokens for `repo`. Returns the removed hashes. */
|
|
102
|
+
revoke(repo: string): string[] {
|
|
103
|
+
const removed = this.entries.filter((e) => e.repo === repo).map((e) => e.hash);
|
|
104
|
+
if (removed.length > 0) {
|
|
105
|
+
this.entries = this.entries.filter((e) => e.repo !== repo);
|
|
106
|
+
this.persistence.save(this.entries);
|
|
107
|
+
}
|
|
108
|
+
return removed;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
|
2
2
|
import { mkdtempSync, rmSync } from 'node:fs';
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { TokenAuth } from './auth';
|
|
5
|
+
import { ADMIN_SCOPE, TokenAuth } from './auth';
|
|
6
6
|
import { createRateLimiter } from './rate-limit';
|
|
7
7
|
import { startServer } from './server';
|
|
8
8
|
|
|
@@ -17,7 +17,7 @@ beforeEach(() => {
|
|
|
17
17
|
port: 0, // let Bun pick an ephemeral port
|
|
18
18
|
pathPrefix: '',
|
|
19
19
|
publicUrl: 'https://example.test',
|
|
20
|
-
auth: new TokenAuth(['valid-token']),
|
|
20
|
+
auth: new TokenAuth([{ token: 'valid-token', scope: ADMIN_SCOPE }]),
|
|
21
21
|
});
|
|
22
22
|
baseUrl = `http://localhost:${server.port}`;
|
|
23
23
|
});
|
|
@@ -107,9 +107,10 @@ describe('PUT /api/v1/modules/new', () => {
|
|
|
107
107
|
const res = await publish('homebridge', '1.0.0+1', 'valid-token');
|
|
108
108
|
expect(res.status).toBe(200);
|
|
109
109
|
|
|
110
|
-
const meta = (await fetch(`${baseUrl}/api/v1/modules/homebridge`).then((r) =>
|
|
111
|
-
|
|
112
|
-
|
|
110
|
+
const meta = (await fetch(`${baseUrl}/api/v1/modules/homebridge`).then((r) => r.json())) as {
|
|
111
|
+
name: string;
|
|
112
|
+
versions: { num: string }[];
|
|
113
|
+
};
|
|
113
114
|
expect(meta.name).toBe('homebridge');
|
|
114
115
|
expect(meta.versions).toHaveLength(1);
|
|
115
116
|
expect(meta.versions[0].num).toBe('1.0.0+1');
|
|
@@ -258,9 +259,10 @@ describe('GET /api/v1/modules/{name} (metadata) — total_downloads', () => {
|
|
|
258
259
|
await fetch(`${baseUrl}/api/v1/modules/homebridge/1.0.0+1/download`);
|
|
259
260
|
await fetch(`${baseUrl}/api/v1/modules/homebridge/1.0.0+1/download`);
|
|
260
261
|
|
|
261
|
-
const meta = (await fetch(`${baseUrl}/api/v1/modules/homebridge`).then((r) =>
|
|
262
|
-
|
|
263
|
-
|
|
262
|
+
const meta = (await fetch(`${baseUrl}/api/v1/modules/homebridge`).then((r) => r.json())) as {
|
|
263
|
+
name: string;
|
|
264
|
+
total_downloads: number;
|
|
265
|
+
};
|
|
264
266
|
expect(meta.total_downloads).toBe(2);
|
|
265
267
|
});
|
|
266
268
|
});
|
|
@@ -344,7 +346,7 @@ describe('write endpoints are rate limited', () => {
|
|
|
344
346
|
port: 0,
|
|
345
347
|
pathPrefix: '',
|
|
346
348
|
publicUrl: 'https://example.test',
|
|
347
|
-
auth: new TokenAuth(['tok']),
|
|
349
|
+
auth: new TokenAuth([{ token: 'tok', scope: ADMIN_SCOPE }]),
|
|
348
350
|
rateLimiter: createRateLimiter({ max: 2, windowMs: 60_000 }),
|
|
349
351
|
});
|
|
350
352
|
try {
|
|
@@ -386,7 +388,7 @@ describe('path prefix routing', () => {
|
|
|
386
388
|
port: 0,
|
|
387
389
|
pathPrefix: '/registry',
|
|
388
390
|
publicUrl: 'https://example.test/registry',
|
|
389
|
-
auth: new TokenAuth(['valid-token']),
|
|
391
|
+
auth: new TokenAuth([{ token: 'valid-token', scope: ADMIN_SCOPE }]),
|
|
390
392
|
});
|
|
391
393
|
try {
|
|
392
394
|
const base = `http://localhost:${prefixed.port}`;
|
|
@@ -400,6 +402,86 @@ describe('path prefix routing', () => {
|
|
|
400
402
|
});
|
|
401
403
|
});
|
|
402
404
|
|
|
405
|
+
// ── scoped publish tokens (ISS-0140) ──────────────────────────────────────────
|
|
406
|
+
|
|
407
|
+
describe('POST /api/v1/modules/tokens/mint + scoped publish', () => {
|
|
408
|
+
async function mint(repo: string, scope: string, token: string): Promise<Response> {
|
|
409
|
+
return fetch(`${baseUrl}/api/v1/modules/tokens/mint`, {
|
|
410
|
+
method: 'POST',
|
|
411
|
+
headers: token ? { Authorization: token, 'Content-Type': 'application/json' } : {},
|
|
412
|
+
body: JSON.stringify({ repo, scope }),
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
test('admin token mints a scoped token; non-admin / no token is 401', async () => {
|
|
417
|
+
const noTok = await mint('celilo/lunacycle', 'lunacycle', '');
|
|
418
|
+
expect(noTok.status).toBe(401);
|
|
419
|
+
|
|
420
|
+
const ok = await mint('celilo/lunacycle', 'lunacycle', 'valid-token');
|
|
421
|
+
expect(ok.status).toBe(200);
|
|
422
|
+
const body = (await ok.json()) as { ok: boolean; token: string; scope: string };
|
|
423
|
+
expect(body.ok).toBe(true);
|
|
424
|
+
expect(body.scope).toBe('lunacycle');
|
|
425
|
+
expect(body.token.length).toBeGreaterThan(10);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test('a minted scoped token publishes ONLY its package', async () => {
|
|
429
|
+
const minted = (await (await mint('celilo/lunacycle', 'lunacycle', 'valid-token')).json()) as {
|
|
430
|
+
token: string;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
// Can publish its own package…
|
|
434
|
+
const ok = await publish('lunacycle', '1.0.0+1', minted.token);
|
|
435
|
+
expect(ok.status).toBe(200);
|
|
436
|
+
|
|
437
|
+
// …but NOT another module.
|
|
438
|
+
const denied = await publish('caddy', '1.0.0+1', minted.token);
|
|
439
|
+
expect(denied.status).toBe(401);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test('a scoped token cannot mint (not admin)', async () => {
|
|
443
|
+
const minted = (await (await mint('celilo/lunacycle', 'lunacycle', 'valid-token')).json()) as {
|
|
444
|
+
token: string;
|
|
445
|
+
};
|
|
446
|
+
const reMint = await mint('celilo/caddy', 'caddy', minted.token);
|
|
447
|
+
expect(reMint.status).toBe(401);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test('re-minting rotates: the old token stops working, the new one publishes', async () => {
|
|
451
|
+
const first = (await (await mint('celilo/lunacycle', 'lunacycle', 'valid-token')).json()) as {
|
|
452
|
+
token: string;
|
|
453
|
+
};
|
|
454
|
+
const second = (await (await mint('celilo/lunacycle', 'lunacycle', 'valid-token')).json()) as {
|
|
455
|
+
token: string;
|
|
456
|
+
};
|
|
457
|
+
expect(second.token).not.toBe(first.token);
|
|
458
|
+
|
|
459
|
+
const oldDenied = await publish('lunacycle', '2.0.0+1', first.token);
|
|
460
|
+
expect(oldDenied.status).toBe(401);
|
|
461
|
+
const newOk = await publish('lunacycle', '2.0.0+1', second.token);
|
|
462
|
+
expect(newOk.status).toBe(200);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
test('revoke disables the token', async () => {
|
|
466
|
+
const minted = (await (await mint('celilo/lunacycle', 'lunacycle', 'valid-token')).json()) as {
|
|
467
|
+
token: string;
|
|
468
|
+
};
|
|
469
|
+
const revoke = await fetch(`${baseUrl}/api/v1/modules/tokens/revoke`, {
|
|
470
|
+
method: 'POST',
|
|
471
|
+
headers: { Authorization: 'valid-token', 'Content-Type': 'application/json' },
|
|
472
|
+
body: JSON.stringify({ repo: 'celilo/lunacycle' }),
|
|
473
|
+
});
|
|
474
|
+
expect(revoke.status).toBe(200);
|
|
475
|
+
const denied = await publish('lunacycle', '1.0.0+1', minted.token);
|
|
476
|
+
expect(denied.status).toBe(401);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
test('mint rejects an invalid scope (not a valid package name)', async () => {
|
|
480
|
+
const res = await mint('celilo/lunacycle', 'Bad_Scope', 'valid-token');
|
|
481
|
+
expect(res.status).toBe(400);
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
403
485
|
// ─── helpers ──────────────────────────────────────────────────────────────────
|
|
404
486
|
|
|
405
487
|
async function publish(
|
|
@@ -19,19 +19,19 @@
|
|
|
19
19
|
* can publish-then-import against a live registry.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import { readFileSync } from 'node:fs';
|
|
23
22
|
import { join } from 'node:path';
|
|
24
23
|
import { TokenAuth } from './auth';
|
|
25
|
-
import { landingResponse } from './landing';
|
|
26
24
|
import {
|
|
25
|
+
type BootstrapEntry,
|
|
27
26
|
bootstrapIndexEntry,
|
|
28
27
|
packageBootstrapModule,
|
|
29
28
|
scanBootstrapDir,
|
|
30
29
|
scanUploadsDir,
|
|
31
|
-
type BootstrapEntry,
|
|
32
30
|
} from './bootstrap';
|
|
33
|
-
import {
|
|
34
|
-
import {
|
|
31
|
+
import { landingResponse } from './landing';
|
|
32
|
+
import { type RateLimiter, clientIp, createRateLimiter } from './rate-limit';
|
|
33
|
+
import { ScopedTokenStore, fileScopedTokenPersistence } from './scoped-token-store';
|
|
34
|
+
import { type IndexEntry, RegistryStorage } from './storage';
|
|
35
35
|
import { isValidName, isValidVersion, validateNameAndVersion } from './validation';
|
|
36
36
|
|
|
37
37
|
/** Max total publish body size. Real modules are a few MB; 100MB is generous. */
|
|
@@ -70,14 +70,26 @@ interface ServerOptions {
|
|
|
70
70
|
* to slow a bad actor to a crawl.
|
|
71
71
|
*/
|
|
72
72
|
rateLimiter?: RateLimiter;
|
|
73
|
+
/**
|
|
74
|
+
* Store of minted, per-repo package-scoped tokens (build-bus Phase 3,
|
|
75
|
+
* ISS-0140). Defaults to a JSON file in `dataDir`. Its persisted hashes are
|
|
76
|
+
* loaded into `auth` at startup so minted tokens survive a restart.
|
|
77
|
+
*/
|
|
78
|
+
scopedTokenStore?: ScopedTokenStore;
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve> {
|
|
76
82
|
const { dataDir, port, pathPrefix, publicUrl, auth } = options;
|
|
77
83
|
const storage = new RegistryStorage(dataDir);
|
|
84
|
+
const scopedTokenStore =
|
|
85
|
+
options.scopedTokenStore ??
|
|
86
|
+
new ScopedTokenStore(fileScopedTokenPersistence(join(dataDir, 'scoped-tokens.json')));
|
|
87
|
+
// Load persisted minted tokens into the in-memory authorizer.
|
|
88
|
+
for (const entry of scopedTokenStore.list()) {
|
|
89
|
+
auth.addHashed(entry.hash, entry.scope);
|
|
90
|
+
}
|
|
78
91
|
const bootstrapCacheDir = options.bootstrapCacheDir ?? join(dataDir, '.bootstrap-cache');
|
|
79
|
-
const rateLimiter =
|
|
80
|
-
options.rateLimiter ?? createRateLimiter({ max: 30, windowMs: 60_000 });
|
|
92
|
+
const rateLimiter = options.rateLimiter ?? createRateLimiter({ max: 30, windowMs: 60_000 });
|
|
81
93
|
|
|
82
94
|
/**
|
|
83
95
|
* Rescan every request so .netapps dropped into bootstrapUploadsDir at
|
|
@@ -122,15 +134,24 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
122
134
|
return err('Unauthorized — provide a valid publish token in the Authorization header', 401);
|
|
123
135
|
}
|
|
124
136
|
|
|
125
|
-
|
|
126
|
-
|
|
137
|
+
/** True when the request's token may publish/yank `pkg` (admin or exact scope). */
|
|
138
|
+
function authorizePackage(req: Request, pkg: string): boolean {
|
|
139
|
+
return auth.authorize(req.headers.get('Authorization') ?? '', pkg);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** True when the request carries an admin (`*`-scoped) token — required to mint. */
|
|
143
|
+
function authorizeAdmin(req: Request): boolean {
|
|
144
|
+
return auth.isAdmin(req.headers.get('Authorization') ?? '');
|
|
127
145
|
}
|
|
128
146
|
|
|
129
147
|
/**
|
|
130
148
|
* Rate-limit check for write endpoints. Returns a 429 response when
|
|
131
149
|
* exceeded (with Retry-After), null when allowed.
|
|
132
150
|
*/
|
|
133
|
-
function rateLimitOrNull(
|
|
151
|
+
function rateLimitOrNull(
|
|
152
|
+
req: Request,
|
|
153
|
+
srv: { requestIP(r: Request): { address: string } | null },
|
|
154
|
+
): Response | null {
|
|
134
155
|
const ip = clientIp(req, srv);
|
|
135
156
|
const result = rateLimiter.take(ip);
|
|
136
157
|
if (result.ok) return null;
|
|
@@ -214,12 +235,15 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
214
235
|
// celilo CLI reads manifest.yml#description and includes it). When
|
|
215
236
|
// not provided we leave it undefined; the search endpoint falls
|
|
216
237
|
// back to bootstrap data or empty.
|
|
217
|
-
const description =
|
|
218
|
-
typeof meta.description === 'string' ? meta.description : undefined;
|
|
238
|
+
const description = typeof meta.description === 'string' ? meta.description : undefined;
|
|
219
239
|
|
|
220
240
|
const validation = validateNameAndVersion(name, vers);
|
|
221
241
|
if (!validation.ok) return err(validation.message);
|
|
222
242
|
|
|
243
|
+
// Scope check AFTER the name is known: a package-scoped token may publish
|
|
244
|
+
// only its own package; an admin token publishes anything (ISS-0140).
|
|
245
|
+
if (!authorizePackage(req, name)) return unauthorized();
|
|
246
|
+
|
|
223
247
|
const fileOffset = 4 + metaLen;
|
|
224
248
|
const fileLen = buf.readUInt32LE(fileOffset);
|
|
225
249
|
if (buf.length < fileOffset + 4 + fileLen) return err('Request body truncated (file)');
|
|
@@ -253,6 +277,51 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
253
277
|
return Response.json({ ok: true });
|
|
254
278
|
}
|
|
255
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Mint a per-repo, package-scoped publish token (ISS-0140). Admin-only. The
|
|
282
|
+
* `registry_publish` capability calls this on behalf of `source_forge.
|
|
283
|
+
* registerRepo`. Reconciled: re-minting for the same repo rotates the token.
|
|
284
|
+
* The raw token is returned ONCE — the caller sets it as the repo's Actions
|
|
285
|
+
* secret; the server keeps only its hash.
|
|
286
|
+
*/
|
|
287
|
+
async function handleMintToken(req: Request): Promise<Response> {
|
|
288
|
+
if (!authorizeAdmin(req)) return unauthorized();
|
|
289
|
+
let body: { repo?: unknown; scope?: unknown };
|
|
290
|
+
try {
|
|
291
|
+
body = (await req.json()) as { repo?: unknown; scope?: unknown };
|
|
292
|
+
} catch {
|
|
293
|
+
return err('Invalid JSON body');
|
|
294
|
+
}
|
|
295
|
+
const repo = typeof body.repo === 'string' ? body.repo.trim() : '';
|
|
296
|
+
const scope = typeof body.scope === 'string' ? body.scope.trim() : '';
|
|
297
|
+
if (!repo) return err('repo is required');
|
|
298
|
+
if (!scope || !isValidName(scope)) return err('scope must be a valid package name');
|
|
299
|
+
|
|
300
|
+
const { token, entry, revokedHashes } = scopedTokenStore.mint(repo, scope);
|
|
301
|
+
for (const h of revokedHashes) auth.removeHashed(h);
|
|
302
|
+
auth.addHashed(entry.hash, entry.scope);
|
|
303
|
+
console.log(`[registry] minted scoped token for ${repo} → ${scope}`);
|
|
304
|
+
return Response.json({ ok: true, token, scope, repo });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Revoke all scoped tokens for a repo (ISS-0140). Admin-only. */
|
|
308
|
+
async function handleRevokeToken(req: Request): Promise<Response> {
|
|
309
|
+
if (!authorizeAdmin(req)) return unauthorized();
|
|
310
|
+
let body: { repo?: unknown };
|
|
311
|
+
try {
|
|
312
|
+
body = (await req.json()) as { repo?: unknown };
|
|
313
|
+
} catch {
|
|
314
|
+
return err('Invalid JSON body');
|
|
315
|
+
}
|
|
316
|
+
const repo = typeof body.repo === 'string' ? body.repo.trim() : '';
|
|
317
|
+
if (!repo) return err('repo is required');
|
|
318
|
+
|
|
319
|
+
const removed = scopedTokenStore.revoke(repo);
|
|
320
|
+
for (const h of removed) auth.removeHashed(h);
|
|
321
|
+
console.log(`[registry] revoked ${removed.length} scoped token(s) for ${repo}`);
|
|
322
|
+
return Response.json({ ok: true, repo, revoked: removed.length });
|
|
323
|
+
}
|
|
324
|
+
|
|
256
325
|
const server = Bun.serve({
|
|
257
326
|
port,
|
|
258
327
|
async fetch(req, srv) {
|
|
@@ -273,10 +342,7 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
273
342
|
// - suffix='/' → request was prefix + '/', or just '/'
|
|
274
343
|
// - suffix='/index.html'
|
|
275
344
|
// See apps/celilo/designs/REGISTRY_BROWSE_UI.md (decision D2).
|
|
276
|
-
if (
|
|
277
|
-
method === 'GET' &&
|
|
278
|
-
(suffix === '' || suffix === '/' || suffix === '/index.html')
|
|
279
|
-
) {
|
|
345
|
+
if (method === 'GET' && (suffix === '' || suffix === '/' || suffix === '/index.html')) {
|
|
280
346
|
return landingResponse(publicUrl);
|
|
281
347
|
}
|
|
282
348
|
|
|
@@ -329,10 +395,7 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
329
395
|
// 3. Empty string (older publishes that pre-date the
|
|
330
396
|
// description capture and haven't been republished).
|
|
331
397
|
const bootstrapEntry = bootstrap.get(name);
|
|
332
|
-
const description =
|
|
333
|
-
latest?.description ??
|
|
334
|
-
bootstrapEntry?.description ??
|
|
335
|
-
'';
|
|
398
|
+
const description = latest?.description ?? bootstrapEntry?.description ?? '';
|
|
336
399
|
return {
|
|
337
400
|
name,
|
|
338
401
|
max_version: latest?.vers ?? '0.0.0',
|
|
@@ -359,14 +422,26 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
359
422
|
}
|
|
360
423
|
|
|
361
424
|
// Publish — rate-limit before even checking auth so a bad-token flood
|
|
362
|
-
// can't crowd out legitimate traffic on the shared buckets.
|
|
425
|
+
// can't crowd out legitimate traffic on the shared buckets. The scope
|
|
426
|
+
// check happens inside handlePublish, once the package name is known.
|
|
363
427
|
if (method === 'PUT' && suffix === `${apiBase}/new`) {
|
|
364
428
|
const rl = rateLimitOrNull(req, srv);
|
|
365
429
|
if (rl) return rl;
|
|
366
|
-
if (!requireAuth(req)) return unauthorized();
|
|
367
430
|
return handlePublish(req);
|
|
368
431
|
}
|
|
369
432
|
|
|
433
|
+
// Mint / revoke per-repo scoped publish tokens (admin-only — ISS-0140).
|
|
434
|
+
if (method === 'POST' && suffix === `${apiBase}/tokens/mint`) {
|
|
435
|
+
const rl = rateLimitOrNull(req, srv);
|
|
436
|
+
if (rl) return rl;
|
|
437
|
+
return handleMintToken(req);
|
|
438
|
+
}
|
|
439
|
+
if (method === 'POST' && suffix === `${apiBase}/tokens/revoke`) {
|
|
440
|
+
const rl = rateLimitOrNull(req, srv);
|
|
441
|
+
if (rl) return rl;
|
|
442
|
+
return handleRevokeToken(req);
|
|
443
|
+
}
|
|
444
|
+
|
|
370
445
|
if (suffix.startsWith(`${apiBase}/`)) {
|
|
371
446
|
const modulePath = suffix.slice(apiBase.length + 1);
|
|
372
447
|
// URL-decode path segments — module versions use a literal '+' to
|
|
@@ -387,10 +462,7 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
387
462
|
// endpoint — see notes there.
|
|
388
463
|
const latest = entries.filter((v) => !v.yanked).at(-1) ?? entries.at(-1);
|
|
389
464
|
const bootstrapEntry = resolveBootstrap().get(name);
|
|
390
|
-
const description =
|
|
391
|
-
latest?.description ??
|
|
392
|
-
bootstrapEntry?.description ??
|
|
393
|
-
'';
|
|
465
|
+
const description = latest?.description ?? bootstrapEntry?.description ?? '';
|
|
394
466
|
return Response.json({
|
|
395
467
|
name,
|
|
396
468
|
description,
|
|
@@ -434,9 +506,9 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
434
506
|
if (method === 'DELETE' && parts.length === 3 && parts[2] === 'yank') {
|
|
435
507
|
const rl = rateLimitOrNull(req, srv);
|
|
436
508
|
if (rl) return rl;
|
|
437
|
-
if (!requireAuth(req)) return unauthorized();
|
|
438
509
|
const [name, version] = parts;
|
|
439
510
|
if (!isValidName(name) || !isValidVersion(version)) return notFound();
|
|
511
|
+
if (!authorizePackage(req, name)) return unauthorized();
|
|
440
512
|
return setYanked(name, version, true);
|
|
441
513
|
}
|
|
442
514
|
|
|
@@ -444,9 +516,9 @@ export function startServer(options: ServerOptions): ReturnType<typeof Bun.serve
|
|
|
444
516
|
if (method === 'PUT' && parts.length === 3 && parts[2] === 'unyank') {
|
|
445
517
|
const rl = rateLimitOrNull(req, srv);
|
|
446
518
|
if (rl) return rl;
|
|
447
|
-
if (!requireAuth(req)) return unauthorized();
|
|
448
519
|
const [name, version] = parts;
|
|
449
520
|
if (!isValidName(name) || !isValidVersion(version)) return notFound();
|
|
521
|
+
if (!authorizePackage(req, name)) return unauthorized();
|
|
450
522
|
return setYanked(name, version, false);
|
|
451
523
|
}
|
|
452
524
|
}
|
|
@@ -464,8 +536,7 @@ export function startFromEnv(): void {
|
|
|
464
536
|
const port = Number(process.env.PORT ?? 3000);
|
|
465
537
|
const pathPrefix = (process.env.PATH_PREFIX ?? '').replace(/\/$/, '');
|
|
466
538
|
const publicUrl =
|
|
467
|
-
process.env.PUBLIC_URL ??
|
|
468
|
-
`https://${process.env.DOMAIN ?? 'celilo.computer'}${pathPrefix}`;
|
|
539
|
+
process.env.PUBLIC_URL ?? `https://${process.env.DOMAIN ?? 'celilo.computer'}${pathPrefix}`;
|
|
469
540
|
const bootstrapModulesDir = process.env.BOOTSTRAP_MODULES_DIR || undefined;
|
|
470
541
|
const bootstrapUploadsDir = process.env.BOOTSTRAP_UPLOADS_DIR || undefined;
|
|
471
542
|
const bootstrapCacheDir = process.env.BOOTSTRAP_CACHE_DIR || undefined;
|
|
@@ -485,7 +556,9 @@ export function startFromEnv(): void {
|
|
|
485
556
|
const bootstrapBits = [
|
|
486
557
|
bootstrapModulesDir ? `modulesDir=${bootstrapModulesDir}` : '',
|
|
487
558
|
bootstrapUploadsDir ? `uploadsDir=${bootstrapUploadsDir}` : '',
|
|
488
|
-
]
|
|
559
|
+
]
|
|
560
|
+
.filter(Boolean)
|
|
561
|
+
.join(', ');
|
|
489
562
|
console.log(
|
|
490
563
|
`[registry] listening on :${server.port} (prefix=${pathPrefix || '(none)'}, publicUrl=${publicUrl}${
|
|
491
564
|
bootstrapBits ? `, bootstrap=[${bootstrapBits}]` : ''
|