@zabaca/zbc 0.24.0 → 0.26.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/CHANGELOG.md +45 -0
- package/package.json +3 -2
- package/src/commands/add.ts +78 -5
- package/src/commands/init.ts +105 -10
- package/src/commands/list.ts +6 -3
- package/src/commands/update.ts +75 -6
- package/src/engine/destroy.ts +23 -2
- package/src/engine/resolve.ts +5 -0
- package/src/utils/detect-mode.ts +1 -1
- package/src/utils/host-workspaces.ts +57 -0
- package/src/utils/subtree.ts +78 -9
- package/src/utils/vendor-audit.ts +12 -8
- package/src/utils/vendor-stamp.ts +102 -1
- package/src/utils/vendored-app.ts +356 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/walgit/CONTEXT.md +45 -4
- package/templates/apps/walgit/README.md +394 -11
- package/templates/apps/walgit/shared/browse.ts +61 -0
- package/templates/apps/walgit/shared/capabilities.ts +26 -0
- package/templates/apps/walgit/shared/container-env.ts +12 -0
- package/templates/apps/walgit/shared/container-lifecycle.ts +1 -1
- package/templates/apps/walgit/shared/credentials.ts +4 -0
- package/templates/apps/walgit/shared/events.ts +35 -1
- package/templates/apps/walgit/shared/instructions.ts +4 -1
- package/templates/apps/walgit/shared/landing.ts +2 -1
- package/templates/apps/walgit/shared/llms.ts +73 -5
- package/templates/apps/walgit/shared/mcp.ts +19 -3
- package/templates/apps/walgit/shared/protocol.ts +40 -3
- package/templates/apps/walgit/shared/repo-list.ts +46 -6
- package/templates/apps/walgit/shared/scoped-token.ts +500 -0
- package/templates/apps/walgit/shared/token-revocation.ts +134 -0
- package/templates/apps/walgit/shared/wal-index.ts +19 -0
- package/templates/apps/walgit/shared/webhook.ts +281 -0
- package/templates/apps/walgit/src/admin.ts +510 -0
- package/templates/apps/walgit/src/announce-main.ts +17 -9
- package/templates/apps/walgit/src/announce.ts +86 -0
- package/templates/apps/walgit/src/browse.ts +255 -2
- package/templates/apps/walgit/src/cli.ts +140 -0
- package/templates/apps/walgit/src/expire.ts +5 -0
- package/templates/apps/walgit/src/git.ts +103 -0
- package/templates/apps/walgit/src/hook-main.ts +6 -3
- package/templates/apps/walgit/src/http.ts +563 -3
- package/templates/apps/walgit/src/limits.ts +3 -0
- package/templates/apps/walgit/src/merge.ts +525 -0
- package/templates/apps/walgit/src/rate-limit.ts +51 -14
- package/templates/apps/walgit/src/server.ts +65 -1
- package/templates/apps/walgit/worker/events-do.ts +218 -3
- package/templates/apps/walgit/worker/index.ts +134 -9
- package/templates/apps/walgit/worker/mcp.ts +18 -4
- package/templates/apps/warehouse/worker/router.ts +1 -2
- package/templates/apps/warehouse/worker/test-setup.ts +1 -2
- package/templates/infra/modules/cloudflare-access/index.ts +245 -28
- package/templates/infra/modules/cloudflare-access/registry.json +1 -1
- package/templates/infra/modules/d1/index.ts +133 -4
- package/templates/infra/modules/d1/registry.json +4 -1
- package/templates/infra/modules/queue/index.ts +313 -0
- package/templates/infra/modules/queue/registry.json +13 -0
- package/templates/infra/modules/r2/registry.json +1 -1
- package/templates/infra/src/define-module.ts +19 -2
- package/templates/infra/src/types.ts +24 -0
- package/templates/apps/inbox/package.json +0 -26
- package/templates/apps/inbox/public/index.html +0 -379
- package/templates/apps/inbox/registry.json +0 -9
- package/templates/apps/inbox/tsconfig.json +0 -19
- package/templates/apps/inbox/worker/api.ts +0 -197
- package/templates/apps/inbox/worker/commands.ts +0 -299
- package/templates/apps/inbox/worker/inbox-do.ts +0 -577
- package/templates/apps/inbox/worker/index.ts +0 -195
- package/templates/apps/inbox/worker/mcp.ts +0 -228
- package/templates/apps/inbox/worker/shared.ts +0 -117
- package/templates/apps/inbox/wrangler.jsonc +0 -48
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
|
|
13
13
|
import {
|
|
14
14
|
BLOB_MAX_BYTES,
|
|
15
|
+
COMPARE_MAX_DIFF_BYTES,
|
|
16
|
+
COMPARE_MAX_FILES,
|
|
15
17
|
cleanPath,
|
|
16
18
|
defaultBranch,
|
|
17
19
|
OID,
|
|
@@ -19,13 +21,22 @@ import {
|
|
|
19
21
|
resolveRef,
|
|
20
22
|
splitRefPath,
|
|
21
23
|
type Commit,
|
|
24
|
+
type Comparison,
|
|
22
25
|
type TreeEntry,
|
|
23
26
|
} from '../shared/browse'
|
|
24
27
|
import { capabilitiesFrom, type Capabilities } from '../shared/capabilities'
|
|
25
28
|
import { authorizedBy, presentedSignature } from '../shared/credentials'
|
|
26
29
|
import {
|
|
30
|
+
scopeRefusal,
|
|
31
|
+
scopeTargetOf,
|
|
32
|
+
scopedAccessOf,
|
|
33
|
+
type ScopedClaims,
|
|
34
|
+
} from '../shared/scoped-token'
|
|
35
|
+
import {
|
|
36
|
+
ADMIN_REPOS_HTTP,
|
|
27
37
|
BASIC_CHALLENGE,
|
|
28
38
|
BROWSE_PATH,
|
|
39
|
+
MERGE_PATH,
|
|
29
40
|
CHALLENGE_PATH,
|
|
30
41
|
EXPIRE_PATH,
|
|
31
42
|
HEALTH_PATH,
|
|
@@ -41,11 +52,20 @@ import {
|
|
|
41
52
|
SMART_HTTP,
|
|
42
53
|
type ContainerRejectKind,
|
|
43
54
|
} from '../shared/protocol'
|
|
55
|
+
import { MAX_MERGE_BODY_BYTES, parseMergeRequest, type MergeOps, type MergeResult } from './merge'
|
|
56
|
+
import { MAX_SEED_BYTES, parseRevoke, parseSeed, type AdminOps } from './admin'
|
|
57
|
+
import { revokedMessage, tokenRevoked } from '../shared/token-revocation'
|
|
44
58
|
import { emptyReadResponse } from './empty-read'
|
|
45
59
|
import { renderInstructions } from '../shared/instructions'
|
|
46
60
|
import { acceptedNonces, readAllowed, readChallengeNonce, renderReadChallenge } from './private'
|
|
47
61
|
import type { ProposalListing } from './proposals'
|
|
48
|
-
import {
|
|
62
|
+
import {
|
|
63
|
+
countsNewRepos,
|
|
64
|
+
createSourceLimiter,
|
|
65
|
+
rateLimitsEnforced,
|
|
66
|
+
rateLimitsOf,
|
|
67
|
+
type RateSource,
|
|
68
|
+
} from './rate-limit'
|
|
49
69
|
import type { ResolvedRepo } from './repo'
|
|
50
70
|
import { resolveRepo } from './repo'
|
|
51
71
|
import type { Claim, Provenance } from '../shared/wal-index'
|
|
@@ -189,6 +209,34 @@ export type HttpHandlerDeps = {
|
|
|
189
209
|
* stand still inside one or step over its edge (`src/rate-limit.ts`).
|
|
190
210
|
*/
|
|
191
211
|
now?: () => number
|
|
212
|
+
/**
|
|
213
|
+
* `WALGIT_TOKEN_PUBLIC_KEYS`, verbatim (`shared/scoped-token.ts`,
|
|
214
|
+
* docs/adr/0021). Absent or blank means scoped tokens are off and every
|
|
215
|
+
* credential is judged exactly as before.
|
|
216
|
+
*/
|
|
217
|
+
scopedTokenKeys?: string
|
|
218
|
+
/** The clock scoped tokens are checked against; injectable for tests. */
|
|
219
|
+
tokenNow?: () => number
|
|
220
|
+
/**
|
|
221
|
+
* A repository's tokens-valid-since instant from its Index, or `null`
|
|
222
|
+
* (`shared/token-revocation.ts`). A scoped token issued before it is refused
|
|
223
|
+
* 401. Absent without a store, where there is no Index to revoke in.
|
|
224
|
+
*/
|
|
225
|
+
tokensValidSince?: (repoId: string) => Promise<string | null>
|
|
226
|
+
/**
|
|
227
|
+
* Create, delete and measure one repository over HTTP (`src/admin.ts`),
|
|
228
|
+
* answered only to the admin credential. Absent without a store — there is
|
|
229
|
+
* no Index to claim a name in or to measure — and then the routes do not
|
|
230
|
+
* exist, as they do not on a deployment with no `tokens` at all.
|
|
231
|
+
*/
|
|
232
|
+
admin?: AdminOps
|
|
233
|
+
/**
|
|
234
|
+
* Merge one branch into another on request (`src/merge.ts`, docs/adr/0022).
|
|
235
|
+
* The route exists only where the capabilities say `merge` AND this is
|
|
236
|
+
* wired — absent without a store, where there is no Index to compare-and-swap
|
|
237
|
+
* on — so with `WALGIT_MERGE` unset it is a 404.
|
|
238
|
+
*/
|
|
239
|
+
merge?: MergeOps
|
|
192
240
|
}
|
|
193
241
|
|
|
194
242
|
/** Everything the Private gate needs, present or absent as a whole. */
|
|
@@ -284,6 +332,24 @@ export type BrowseReads = {
|
|
|
284
332
|
before: string | null,
|
|
285
333
|
limit: number,
|
|
286
334
|
) => Promise<Commit[] | null>
|
|
335
|
+
/**
|
|
336
|
+
* The head against its merge base with the base (three-dot), within the
|
|
337
|
+
* bounds given, with both sides peeled to the commits it was read at; `null`
|
|
338
|
+
* when the Cache cannot resolve one of the two, `'unrelated'` when they share
|
|
339
|
+
* no history, and `'too-large'` when it cannot be computed inside the bounds.
|
|
340
|
+
*
|
|
341
|
+
* Optional, unlike the four readers above it: a deployment wired without one
|
|
342
|
+
* answers `op=compare` with the 404 every absent read gets, rather than every
|
|
343
|
+
* wiring of a browse having to grow one at once.
|
|
344
|
+
*/
|
|
345
|
+
compare?: (
|
|
346
|
+
repo: ResolvedRepo,
|
|
347
|
+
base: string,
|
|
348
|
+
head: string,
|
|
349
|
+
limits: { maxFiles: number; maxDiffBytes: number; timeoutMs: number },
|
|
350
|
+
) => Promise<
|
|
351
|
+
(Comparison & { peeled: { base: string; head: string } }) | 'unrelated' | 'too-large' | null
|
|
352
|
+
>
|
|
287
353
|
}
|
|
288
354
|
|
|
289
355
|
/** What a browse reads out of one Index. */
|
|
@@ -360,6 +426,38 @@ const json = (body: unknown): Response =>
|
|
|
360
426
|
*/
|
|
361
427
|
const LOG_PAGE = 50
|
|
362
428
|
|
|
429
|
+
/**
|
|
430
|
+
* Does this compare want the unified diff alone, as text, rather than JSON?
|
|
431
|
+
*
|
|
432
|
+
* `?format=diff`, or an `Accept` that prefers `text/x-diff` or `text/plain` to
|
|
433
|
+
* `application/json` by q-value — so `*\/*`, which is what curl and a default
|
|
434
|
+
* fetch send, keeps the JSON answer, and a client listing both at the same
|
|
435
|
+
* weight (axios's `application/json, text/plain, *\/*`) is a JSON client.
|
|
436
|
+
*/
|
|
437
|
+
function wantsDiffText(request: Request, url: URL): boolean {
|
|
438
|
+
if (url.searchParams.get('format') === 'diff') return true
|
|
439
|
+
let textQ = 0
|
|
440
|
+
let jsonQ = 0
|
|
441
|
+
for (const range of (request.headers.get('accept') ?? '').split(',')) {
|
|
442
|
+
const [type = '', ...params] = range.split(';').map((part) => part.trim().toLowerCase())
|
|
443
|
+
const qParam = params.find((param) => param.startsWith('q='))
|
|
444
|
+
const q = qParam === undefined ? 1 : Number(qParam.slice(2))
|
|
445
|
+
if (!Number.isFinite(q)) continue
|
|
446
|
+
if (type === 'text/x-diff' || type === 'text/plain') textQ = Math.max(textQ, q)
|
|
447
|
+
if (type === 'application/json') jsonQ = Math.max(jsonQ, q)
|
|
448
|
+
}
|
|
449
|
+
return textQ > 0 && textQ > jsonQ
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* How long one compare may run git for, in all: ten seconds.
|
|
454
|
+
*
|
|
455
|
+
* Every git call here is synchronous in the one process that also serves
|
|
456
|
+
* pushes, so this is the longest a compare can hold that process. A comparison
|
|
457
|
+
* that needs longer is refused (503), and is one to run against a clone.
|
|
458
|
+
*/
|
|
459
|
+
const COMPARE_TIMEOUT_MS = 10_000
|
|
460
|
+
|
|
363
461
|
/**
|
|
364
462
|
* One blob, as bytes, with the type its CONTENT earns and never its name.
|
|
365
463
|
*
|
|
@@ -592,6 +690,7 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
592
690
|
|
|
593
691
|
const op = url.searchParams.get('op')
|
|
594
692
|
if (op === 'refs') return json(common)
|
|
693
|
+
if (op === 'compare') return compare(reads, request, url, resolved, index.refs, common)
|
|
595
694
|
if (op !== 'tree' && op !== 'blob' && op !== 'raw' && op !== 'log') return NOT_FOUND()
|
|
596
695
|
|
|
597
696
|
// The history cursor, checked before anything else this request would do.
|
|
@@ -734,6 +833,236 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
734
833
|
return json({ ...common, ref: at.ref, path, entries, ...(readme ? { readme } : {}) })
|
|
735
834
|
}
|
|
736
835
|
|
|
836
|
+
/**
|
|
837
|
+
* `op=compare`: the head against its merge base with the base, three-dot.
|
|
838
|
+
*
|
|
839
|
+
* Reached only from `browse`, after the Private gate and the Index — so a
|
|
840
|
+
* compare is read under exactly the rules every other browse read is, and a
|
|
841
|
+
* name nobody has pushed to is still the 404 that creates nothing. Both refs
|
|
842
|
+
* resolve the way a browse ref does: an Index key (prefix optional) or a full
|
|
843
|
+
* oid, and nothing else reaches git.
|
|
844
|
+
*/
|
|
845
|
+
async function compare(
|
|
846
|
+
reads: BrowseReads,
|
|
847
|
+
request: Request,
|
|
848
|
+
url: URL,
|
|
849
|
+
resolved: ResolvedRepo,
|
|
850
|
+
refs: Record<string, string>,
|
|
851
|
+
common: Record<string, unknown>,
|
|
852
|
+
): Promise<Response> {
|
|
853
|
+
if (!reads.compare) return NOT_FOUND()
|
|
854
|
+
const baseName = url.searchParams.get('base') ?? ''
|
|
855
|
+
const headName = url.searchParams.get('head') ?? ''
|
|
856
|
+
// 400, not 404: a compare that names no side is a malformed request rather
|
|
857
|
+
// than one about a ref that might have existed.
|
|
858
|
+
if (baseName === '' || headName === '') {
|
|
859
|
+
return reject(400, 'invalid', 'walgit: a compare names both a base and a head\n')
|
|
860
|
+
}
|
|
861
|
+
const base = resolveRef(refs, baseName)
|
|
862
|
+
const head = resolveRef(refs, headName)
|
|
863
|
+
if (base === null || head === null) return NOT_FOUND()
|
|
864
|
+
const baseOid = refs[base] ?? base
|
|
865
|
+
const headOid = refs[head] ?? head
|
|
866
|
+
|
|
867
|
+
const repo = deps.ensureRepo(resolved)
|
|
868
|
+
if (deps.syncRepo) {
|
|
869
|
+
try {
|
|
870
|
+
await deps.syncRepo(repo)
|
|
871
|
+
} catch (err) {
|
|
872
|
+
return reject(503, 'unavailable', `walgit: ${(err as Error).message}\n`)
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
const result = await reads.compare(repo, baseOid, headOid, {
|
|
877
|
+
maxFiles: COMPARE_MAX_FILES,
|
|
878
|
+
maxDiffBytes: COMPARE_MAX_DIFF_BYTES,
|
|
879
|
+
timeoutMs: COMPARE_TIMEOUT_MS,
|
|
880
|
+
})
|
|
881
|
+
// An oid the Cache does not hold: absent, as an unknown ref is.
|
|
882
|
+
if (result === null) return NOT_FOUND()
|
|
883
|
+
// Refused rather than answered partially: past the deadline there is no
|
|
884
|
+
// honest partial answer — the totals are the first thing computed after
|
|
885
|
+
// the merge base, and a list with no totals is a list nobody can trust.
|
|
886
|
+
if (result === 'too-large') {
|
|
887
|
+
return reject(
|
|
888
|
+
503,
|
|
889
|
+
'unavailable',
|
|
890
|
+
'walgit: this comparison is too large to compute on the request path — clone the repository to compare it locally\n',
|
|
891
|
+
)
|
|
892
|
+
}
|
|
893
|
+
if (result === 'unrelated') {
|
|
894
|
+
return reject(
|
|
895
|
+
400,
|
|
896
|
+
'invalid',
|
|
897
|
+
`walgit: ${baseName} and ${headName} share no history, so there is no merge base to compare against\n`,
|
|
898
|
+
)
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
const { peeled, ...comparison } = result
|
|
902
|
+
if (wantsDiffText(request, url)) {
|
|
903
|
+
return new Response(comparison.diff, {
|
|
904
|
+
headers: {
|
|
905
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
906
|
+
'x-content-type-options': 'nosniff',
|
|
907
|
+
'cache-control': 'no-store',
|
|
908
|
+
'x-robots-tag': 'noindex',
|
|
909
|
+
// The one fact the JSON carries that the text cannot.
|
|
910
|
+
'x-walgit-truncated': comparison.truncated ? 'true' : 'false',
|
|
911
|
+
'x-walgit-merge-base': comparison.mergeBase,
|
|
912
|
+
},
|
|
913
|
+
})
|
|
914
|
+
}
|
|
915
|
+
return json({
|
|
916
|
+
...common,
|
|
917
|
+
// The COMMITS compared, peeled: an annotated tag's Index entry is the
|
|
918
|
+
// tag object, which is not what a reviewer diffed.
|
|
919
|
+
base: { ref: base, oid: peeled.base },
|
|
920
|
+
head: { ref: head, oid: peeled.head },
|
|
921
|
+
...comparison,
|
|
922
|
+
})
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* One admin request, already authenticated. The verbs are the routes:
|
|
927
|
+
* POST creates, DELETE deletes, GET of `/usage` measures, and anything else
|
|
928
|
+
* on the prefix is a 404 rather than a 405 — the same answer every path
|
|
929
|
+
* walgit does not serve gets.
|
|
930
|
+
*/
|
|
931
|
+
async function administer(
|
|
932
|
+
admin: AdminOps,
|
|
933
|
+
request: Request,
|
|
934
|
+
name: string,
|
|
935
|
+
suffix: string | undefined,
|
|
936
|
+
): Promise<Response> {
|
|
937
|
+
let resolved: ResolvedRepo
|
|
938
|
+
try {
|
|
939
|
+
resolved = resolveRepo(deps.reposDir, name)
|
|
940
|
+
} catch {
|
|
941
|
+
return reject(
|
|
942
|
+
400,
|
|
943
|
+
'invalid',
|
|
944
|
+
`walgit: ${JSON.stringify(name)} is not a repository name walgit serves\n`,
|
|
945
|
+
)
|
|
946
|
+
}
|
|
947
|
+
try {
|
|
948
|
+
if (suffix === '/usage') {
|
|
949
|
+
if (request.method !== 'GET') return NOT_FOUND()
|
|
950
|
+
const row = await admin.usage(resolved.repoId)
|
|
951
|
+
return row ? json(row) : NOT_FOUND()
|
|
952
|
+
}
|
|
953
|
+
if (suffix === '/revoke-tokens') {
|
|
954
|
+
if (request.method !== 'POST') return NOT_FOUND()
|
|
955
|
+
const declared = Number(request.headers.get('content-length') ?? '0')
|
|
956
|
+
if (Number.isFinite(declared) && declared > MAX_REVOKE_BYTES) return revokeTooLarge()
|
|
957
|
+
const text = await readBounded(request, MAX_REVOKE_BYTES)
|
|
958
|
+
if (text === null) return revokeTooLarge()
|
|
959
|
+
const parsed = parseRevoke(text)
|
|
960
|
+
if (!parsed.ok) return reject(400, 'invalid', `walgit: ${parsed.message}\n`)
|
|
961
|
+
const revoked = await admin.revokeTokens(resolved.repoId, parsed.since)
|
|
962
|
+
if (revoked === 'later-than-now') {
|
|
963
|
+
return reject(400, 'invalid', 'walgit: "since" must not be later than now\n')
|
|
964
|
+
}
|
|
965
|
+
return revoked ? json(revoked) : NOT_FOUND()
|
|
966
|
+
}
|
|
967
|
+
if (request.method === 'DELETE') {
|
|
968
|
+
const result = await admin.delete(resolved)
|
|
969
|
+
return json({
|
|
970
|
+
repo: result.repoId,
|
|
971
|
+
status: result.status,
|
|
972
|
+
...(result.collectAfter ? { collectAfter: result.collectAfter } : {}),
|
|
973
|
+
deleted: result.deleted.length,
|
|
974
|
+
retained: result.retained.length,
|
|
975
|
+
})
|
|
976
|
+
}
|
|
977
|
+
if (request.method !== 'POST') return NOT_FOUND()
|
|
978
|
+
|
|
979
|
+
// Bounded before it is read whole: a declared length over the cap is
|
|
980
|
+
// refused unread, and a chunked body is cut off at the cap.
|
|
981
|
+
const declared = Number(request.headers.get('content-length') ?? '0')
|
|
982
|
+
if (Number.isFinite(declared) && declared > MAX_SEED_BYTES) return seedTooLarge()
|
|
983
|
+
const text = await readBounded(request, MAX_SEED_BYTES)
|
|
984
|
+
if (text === null) return seedTooLarge()
|
|
985
|
+
const parsed = parseSeed(text)
|
|
986
|
+
if (!parsed.ok) return reject(400, 'invalid', `walgit: ${parsed.message}\n`)
|
|
987
|
+
|
|
988
|
+
const created = await admin.create(resolved, parsed.seed)
|
|
989
|
+
if (created.kind === 'exists') {
|
|
990
|
+
const when = created.collectAfter
|
|
991
|
+
? ` and is being deleted — the name is free once it is collected after ${created.collectAfter}`
|
|
992
|
+
: ''
|
|
993
|
+
return reject(
|
|
994
|
+
409,
|
|
995
|
+
'collision',
|
|
996
|
+
`walgit: the repository "${created.repo}" already exists${when}\n`,
|
|
997
|
+
)
|
|
998
|
+
}
|
|
999
|
+
if (created.kind === 'refused') {
|
|
1000
|
+
return reject(422, 'invalid', `walgit: ${created.message}\n`)
|
|
1001
|
+
}
|
|
1002
|
+
const { kind: _kind, ...body } = created
|
|
1003
|
+
return new Response(`${JSON.stringify(body)}\n`, {
|
|
1004
|
+
status: 201,
|
|
1005
|
+
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
|
|
1006
|
+
})
|
|
1007
|
+
} catch (err) {
|
|
1008
|
+
// The store, most often. Refused rather than thrown past the router, so
|
|
1009
|
+
// the answer carries the `served` stamp and a reason.
|
|
1010
|
+
return reject(503, 'unavailable', `walgit: ${(err as Error).message}\n`)
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/** One merge request, already authenticated. */
|
|
1015
|
+
async function merge(
|
|
1016
|
+
ops: MergeOps,
|
|
1017
|
+
request: Request,
|
|
1018
|
+
url: URL,
|
|
1019
|
+
claims: ScopedClaims | null,
|
|
1020
|
+
): Promise<Response> {
|
|
1021
|
+
let resolved: ResolvedRepo
|
|
1022
|
+
try {
|
|
1023
|
+
resolved = resolveRepo(deps.reposDir, url.searchParams.get('repo') ?? '')
|
|
1024
|
+
} catch {
|
|
1025
|
+
return mergeError(404, 'not-found', 'walgit: not found')
|
|
1026
|
+
}
|
|
1027
|
+
const declared = Number(request.headers.get('content-length') ?? '0')
|
|
1028
|
+
const tooLarge = () =>
|
|
1029
|
+
mergeError(413, 'invalid', `walgit: a merge request is at most ${MAX_MERGE_BODY_BYTES} bytes`)
|
|
1030
|
+
if (Number.isFinite(declared) && declared > MAX_MERGE_BODY_BYTES) return tooLarge()
|
|
1031
|
+
const text = await readBounded(request, MAX_MERGE_BODY_BYTES)
|
|
1032
|
+
if (text === null) return tooLarge()
|
|
1033
|
+
const parsed = parseMergeRequest(text)
|
|
1034
|
+
if (!parsed.ok) return mergeError(400, 'invalid', `walgit: ${parsed.message}`)
|
|
1035
|
+
|
|
1036
|
+
// A merge is a push the host makes, so it is counted as one against the
|
|
1037
|
+
// per-source window (`src/rate-limit.ts`) — the tenant a scoped token
|
|
1038
|
+
// names, else the address. Unlike a client's push the verdict CAN be a
|
|
1039
|
+
// status here: this route is not git's, and nothing retries a 429 as a
|
|
1040
|
+
// transport fault. Charged before any work, so a refused merge costs the
|
|
1041
|
+
// container nothing; it uploads no bytes of its own.
|
|
1042
|
+
if (limiter) {
|
|
1043
|
+
const source = rateSourceOf(request, claims)
|
|
1044
|
+
if (source) {
|
|
1045
|
+
const verdict = limiter.admit({
|
|
1046
|
+
source,
|
|
1047
|
+
repoId: resolved.repoId,
|
|
1048
|
+
creating: false,
|
|
1049
|
+
bytes: 0,
|
|
1050
|
+
})
|
|
1051
|
+
if (!verdict.ok) return mergeError(429, 'rate-limited', verdict.message.trim())
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
let result: MergeResult
|
|
1056
|
+
try {
|
|
1057
|
+
result = await ops.merge(resolved, parsed.request)
|
|
1058
|
+
} catch (err) {
|
|
1059
|
+
// The store or the Cache. Nothing moved: every way a merge moves a ref
|
|
1060
|
+
// is a push that succeeded, and a push that succeeded returns.
|
|
1061
|
+
return mergeError(503, 'unavailable', `walgit: ${(err as Error).message}`)
|
|
1062
|
+
}
|
|
1063
|
+
return mergeAnswer(result)
|
|
1064
|
+
}
|
|
1065
|
+
|
|
737
1066
|
return async (request) => {
|
|
738
1067
|
const url = new URL(request.url)
|
|
739
1068
|
|
|
@@ -845,7 +1174,75 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
845
1174
|
)
|
|
846
1175
|
}
|
|
847
1176
|
|
|
848
|
-
|
|
1177
|
+
// A scoped token (docs/adr/0021), judged by the same function the edge
|
|
1178
|
+
// judged it with. The edge has normally refused a bad one already; this is
|
|
1179
|
+
// the same rule held over whatever reaches the container another way.
|
|
1180
|
+
//
|
|
1181
|
+
// A valid one stands in for the deployment credential below, for its own
|
|
1182
|
+
// repository only — which is also what lets a `write` token create the
|
|
1183
|
+
// repository it names on first push, exactly as an admin token does. Every
|
|
1184
|
+
// gate after this one (the Read Challenge, Signer Lists, the rate limits)
|
|
1185
|
+
// still applies on top.
|
|
1186
|
+
const scoped = await scopedAccessOf(request.headers.get('authorization'), {
|
|
1187
|
+
keys: deps.scopedTokenKeys,
|
|
1188
|
+
known: [...deps.tokens, ...(priv?.announceSecret ? [priv.announceSecret] : [])],
|
|
1189
|
+
now: deps.tokenNow,
|
|
1190
|
+
})
|
|
1191
|
+
if (scoped.kind === 'invalid') {
|
|
1192
|
+
return reject(401, 'unauthorized', `walgit: ${scoped.reason}\n`, {
|
|
1193
|
+
'www-authenticate': BASIC_CHALLENGE,
|
|
1194
|
+
})
|
|
1195
|
+
}
|
|
1196
|
+
if (scoped.kind === 'misconfigured') {
|
|
1197
|
+
return reject(503, 'unavailable', `${scoped.reason}\n`)
|
|
1198
|
+
}
|
|
1199
|
+
// The admin routes (`src/admin.ts`). Here, below the scoped-token gate and
|
|
1200
|
+
// above the deployment one, because their credential is neither: a scoped
|
|
1201
|
+
// token has already been refused 403 above (the path is none of its
|
|
1202
|
+
// repository's surfaces), and the admin credential is demanded even on a
|
|
1203
|
+
// public deployment, whose gate below admits everyone. With no admin
|
|
1204
|
+
// credential configured the routes do not exist — a public instance has
|
|
1205
|
+
// nobody who could call them — and a 404 says exactly that.
|
|
1206
|
+
const adminRoute = ADMIN_REPOS_HTTP.exec(url.pathname)
|
|
1207
|
+
if (adminRoute && scoped.kind !== 'scoped') {
|
|
1208
|
+
if (!deps.admin || deps.tokens.length === 0) return NOT_FOUND()
|
|
1209
|
+
if (!authorizedBy(request.headers.get('authorization'), deps.tokens)) return UNAUTHORIZED()
|
|
1210
|
+
return administer(deps.admin, request, adminRoute[1]!, adminRoute[2])
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
if (scoped.kind === 'scoped') {
|
|
1214
|
+
const target = scopeTargetOf(request.method, url.pathname, url.search)
|
|
1215
|
+
// `per-message` is a promise that each named repository is judged, and
|
|
1216
|
+
// only the edge keeps it (the MCP tools, the event socket). Nothing here
|
|
1217
|
+
// does, so the container holds those paths as `elsewhere` — a route
|
|
1218
|
+
// added under them later is closed to a scoped token until it judges.
|
|
1219
|
+
const refused = scopeRefusal(
|
|
1220
|
+
scoped.claims,
|
|
1221
|
+
target.kind === 'per-message' ? { kind: 'elsewhere' } : target,
|
|
1222
|
+
)
|
|
1223
|
+
if (refused) return reject(refused.status, 'unauthorized', refused.message)
|
|
1224
|
+
// Revoked (`shared/token-revocation.ts`): the claimed repository's
|
|
1225
|
+
// Index says tokens issued before some instant are no longer good, read
|
|
1226
|
+
// on every request so a revocation holds from the next one. A read that
|
|
1227
|
+
// fails refuses rather than lets the token through.
|
|
1228
|
+
if (deps.tokensValidSince) {
|
|
1229
|
+
let since: string | null
|
|
1230
|
+
try {
|
|
1231
|
+
since = await deps.tokensValidSince(scoped.claims.repo)
|
|
1232
|
+
} catch (err) {
|
|
1233
|
+
return reject(
|
|
1234
|
+
503,
|
|
1235
|
+
'unavailable',
|
|
1236
|
+
`walgit: could not check token revocation: ${(err as Error).message}\n`,
|
|
1237
|
+
)
|
|
1238
|
+
}
|
|
1239
|
+
if (since !== null && tokenRevoked(scoped.claims.iat, since)) {
|
|
1240
|
+
return reject(401, 'unauthorized', revokedMessage(scoped.claims.repo, since), {
|
|
1241
|
+
'www-authenticate': BASIC_CHALLENGE,
|
|
1242
|
+
})
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
} else if (!deps.public && !authorizedBy(request.headers.get('authorization'), deps.tokens)) {
|
|
849
1246
|
return UNAUTHORIZED()
|
|
850
1247
|
}
|
|
851
1248
|
|
|
@@ -905,6 +1302,19 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
905
1302
|
return browse(deps.browse, request, url)
|
|
906
1303
|
}
|
|
907
1304
|
|
|
1305
|
+
// A merge the host performs (docs/adr/0022). Below the credential gate, so
|
|
1306
|
+
// it takes exactly the credential a push takes: a `write` scoped token for
|
|
1307
|
+
// this repository (a `read` one and another repository's were refused 403
|
|
1308
|
+
// above, by `scopeTargetOf` naming this path a write), or the deployment
|
|
1309
|
+
// credential. Every rule a push meets after that it meets too, because the
|
|
1310
|
+
// merge IS a push into the Cache (`src/merge.ts`).
|
|
1311
|
+
if (url.pathname === MERGE_PATH) {
|
|
1312
|
+
const offered = (deps.capabilities ?? ADVERTISES_NOTHING).merge
|
|
1313
|
+
if (!offered || !deps.merge) return NOT_FOUND()
|
|
1314
|
+
if (request.method !== 'POST') return NOT_FOUND()
|
|
1315
|
+
return merge(deps.merge, request, url, scoped.kind === 'scoped' ? scoped.claims : null)
|
|
1316
|
+
}
|
|
1317
|
+
|
|
908
1318
|
// What Proposals a repository holds (docs/adr/0018). Beside the provenance
|
|
909
1319
|
// read and gated identically — the deployment credential above, then the
|
|
910
1320
|
// Read Challenge below — because it answers the same kind of question about
|
|
@@ -1053,7 +1463,10 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
1053
1463
|
// before a single object is stored.
|
|
1054
1464
|
let refusal: string | undefined
|
|
1055
1465
|
if (limiter && route[2] === 'git-receive-pack' && request.method === 'POST') {
|
|
1056
|
-
|
|
1466
|
+
// The tenant a VERIFIED scoped token names, when it names one — `scoped`
|
|
1467
|
+
// is only `scoped` once the signature, expiry and grant have all passed
|
|
1468
|
+
// above — and otherwise the address, exactly as before.
|
|
1469
|
+
const source = rateSourceOf(request, scoped.kind === 'scoped' ? scoped.claims : null)
|
|
1057
1470
|
// Unattributable: a request that reached the container without passing
|
|
1058
1471
|
// the edge, or a local deployment. Pooling everything walgit cannot name
|
|
1059
1472
|
// into one bucket would let the first such request throttle every other.
|
|
@@ -1106,6 +1519,118 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
1106
1519
|
}
|
|
1107
1520
|
}
|
|
1108
1521
|
|
|
1522
|
+
/**
|
|
1523
|
+
* The kind the edge counts a merge refusal by, as `reject` names one: a lost
|
|
1524
|
+
* race or a conflict is a `collision`, and the rest name themselves.
|
|
1525
|
+
*/
|
|
1526
|
+
function mergeRejectKind(status: number): ContainerRejectKind {
|
|
1527
|
+
if (status === 409) return 'collision'
|
|
1528
|
+
if (status === 429) return 'size-cap'
|
|
1529
|
+
if (status === 503) return 'unavailable'
|
|
1530
|
+
if (status === 404) return 'not-found'
|
|
1531
|
+
if (status === 403) return 'unauthorized'
|
|
1532
|
+
return 'invalid'
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
/**
|
|
1536
|
+
* A merge's result as HTTP. Every refusal is JSON with an `error` that says
|
|
1537
|
+
* which one it is, because a client choosing between "read the target again"
|
|
1538
|
+
* (`stale`, `contended`), "resolve it in a clone" (`conflict`) and "this will
|
|
1539
|
+
* never work here" (`signed`) must not have to parse a sentence to know which.
|
|
1540
|
+
*/
|
|
1541
|
+
function mergeJson(status: number, body: Record<string, unknown>): Response {
|
|
1542
|
+
return new Response(`${JSON.stringify(body)}\n`, {
|
|
1543
|
+
status,
|
|
1544
|
+
headers: {
|
|
1545
|
+
'content-type': 'application/json; charset=utf-8',
|
|
1546
|
+
'cache-control': 'no-store',
|
|
1547
|
+
...(status >= 400 ? { [REJECT_HEADER]: mergeRejectKind(status) } : {}),
|
|
1548
|
+
},
|
|
1549
|
+
})
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
/** A merge refusal that is not one of `MergeResult`'s: the request, the window, the store. */
|
|
1553
|
+
function mergeError(status: number, error: string, message: string): Response {
|
|
1554
|
+
return mergeJson(status, { error, message })
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
function mergeAnswer(result: MergeResult): Response {
|
|
1558
|
+
switch (result.kind) {
|
|
1559
|
+
case 'merged': {
|
|
1560
|
+
const { kind: _kind, summary, ...rest } = result
|
|
1561
|
+
return mergeJson(200, {
|
|
1562
|
+
...rest,
|
|
1563
|
+
...(summary ?? { files: null, totals: null, truncated: true }),
|
|
1564
|
+
})
|
|
1565
|
+
}
|
|
1566
|
+
case 'not-found':
|
|
1567
|
+
return mergeJson(404, { error: 'not-found', message: `walgit: ${result.message}` })
|
|
1568
|
+
case 'invalid':
|
|
1569
|
+
return mergeJson(400, { error: 'invalid', message: `walgit: ${result.message}` })
|
|
1570
|
+
case 'unrelated':
|
|
1571
|
+
return mergeJson(400, {
|
|
1572
|
+
error: 'unrelated',
|
|
1573
|
+
message: 'walgit: the source and the target share no history, so there is nothing to merge',
|
|
1574
|
+
})
|
|
1575
|
+
case 'stale':
|
|
1576
|
+
return mergeJson(409, {
|
|
1577
|
+
error: 'stale',
|
|
1578
|
+
ref: result.ref,
|
|
1579
|
+
expected: result.expected,
|
|
1580
|
+
actual: result.actual,
|
|
1581
|
+
message: `walgit: ${result.ref} is at ${result.actual}, not ${result.expected} — read it again and decide whether to merge onto it`,
|
|
1582
|
+
})
|
|
1583
|
+
case 'contended':
|
|
1584
|
+
return mergeJson(409, {
|
|
1585
|
+
error: 'contended',
|
|
1586
|
+
ref: result.ref,
|
|
1587
|
+
message: `walgit: ${result.message}`,
|
|
1588
|
+
})
|
|
1589
|
+
case 'not-fast-forward':
|
|
1590
|
+
return mergeJson(409, {
|
|
1591
|
+
error: 'not-fast-forward',
|
|
1592
|
+
ref: result.ref,
|
|
1593
|
+
message: `walgit: ${result.ref} is not an ancestor of the source, so this is not a fast-forward — squash it, or merge in a clone`,
|
|
1594
|
+
})
|
|
1595
|
+
case 'up-to-date':
|
|
1596
|
+
return mergeJson(409, {
|
|
1597
|
+
error: 'up-to-date',
|
|
1598
|
+
ref: result.ref,
|
|
1599
|
+
message: `walgit: ${result.ref} already holds everything the source would bring`,
|
|
1600
|
+
})
|
|
1601
|
+
case 'conflict':
|
|
1602
|
+
return mergeJson(409, {
|
|
1603
|
+
error: 'conflict',
|
|
1604
|
+
conflicts: result.paths,
|
|
1605
|
+
message: `walgit: the squash conflicts in ${result.paths.length} path${result.paths.length === 1 ? '' : 's'} — resolve it in a clone and push`,
|
|
1606
|
+
})
|
|
1607
|
+
case 'signed':
|
|
1608
|
+
return mergeJson(403, { error: 'signed', message: `walgit: ${result.message}` })
|
|
1609
|
+
case 'refused':
|
|
1610
|
+
return mergeJson(422, { error: 'refused', message: result.message })
|
|
1611
|
+
case 'too-large':
|
|
1612
|
+
return mergeJson(503, {
|
|
1613
|
+
error: 'too-large',
|
|
1614
|
+
message:
|
|
1615
|
+
'walgit: this merge is too large to build on the request path — merge it in a clone and push',
|
|
1616
|
+
})
|
|
1617
|
+
case 'unavailable':
|
|
1618
|
+
return mergeJson(503, { error: 'unavailable', message: `walgit: ${result.message}` })
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
/**
|
|
1623
|
+
* Who a push is counted against (`src/rate-limit.ts`): the tenant, when a
|
|
1624
|
+
* verified scoped token carries `sub`, else the address `sourceOf` attributes.
|
|
1625
|
+
* A tenant is counted even when no address is visible — the token, not the
|
|
1626
|
+
* network, is what names it.
|
|
1627
|
+
*/
|
|
1628
|
+
function rateSourceOf(request: Request, claims: ScopedClaims | null): RateSource | null {
|
|
1629
|
+
if (claims?.sub !== undefined && claims.sub !== '') return { per: 'tenant', sub: claims.sub }
|
|
1630
|
+
const address = sourceOf(request)
|
|
1631
|
+
return address === null ? null : { per: 'address', address }
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1109
1634
|
/**
|
|
1110
1635
|
* The client this request is attributed to, or `null` when walgit cannot
|
|
1111
1636
|
* attribute it.
|
|
@@ -1138,6 +1663,41 @@ function declaredBytes(request: Request): number {
|
|
|
1138
1663
|
return Number.isFinite(declared) && declared > 0 ? declared : 0
|
|
1139
1664
|
}
|
|
1140
1665
|
|
|
1666
|
+
/** A revoke body is `{"since": "<ISO instant>"}` at most. */
|
|
1667
|
+
const MAX_REVOKE_BYTES = 1024
|
|
1668
|
+
|
|
1669
|
+
const revokeTooLarge = () =>
|
|
1670
|
+
reject(413, 'size-cap', `walgit: a revoke body is at most ${MAX_REVOKE_BYTES} bytes\n`)
|
|
1671
|
+
|
|
1672
|
+
const seedTooLarge = () =>
|
|
1673
|
+
reject(413, 'size-cap', `walgit: a seed body is at most ${MAX_SEED_BYTES} bytes\n`)
|
|
1674
|
+
|
|
1675
|
+
/** A request body as text, or `null` once it passes `limit` bytes. */
|
|
1676
|
+
async function readBounded(request: Request, limit: number): Promise<string | null> {
|
|
1677
|
+
if (!request.body) return ''
|
|
1678
|
+
const reader = request.body.getReader()
|
|
1679
|
+
const chunks: Uint8Array[] = []
|
|
1680
|
+
let total = 0
|
|
1681
|
+
for (;;) {
|
|
1682
|
+
const { done, value } = await reader.read()
|
|
1683
|
+
if (done) break
|
|
1684
|
+
total += value.byteLength
|
|
1685
|
+
if (total > limit) {
|
|
1686
|
+
// Stop the upload rather than leave the rest of it in flight.
|
|
1687
|
+
await reader.cancel().catch(() => {})
|
|
1688
|
+
return null
|
|
1689
|
+
}
|
|
1690
|
+
chunks.push(value)
|
|
1691
|
+
}
|
|
1692
|
+
const joined = new Uint8Array(total)
|
|
1693
|
+
let at = 0
|
|
1694
|
+
for (const chunk of chunks) {
|
|
1695
|
+
joined.set(chunk, at)
|
|
1696
|
+
at += chunk.byteLength
|
|
1697
|
+
}
|
|
1698
|
+
return new TextDecoder().decode(joined)
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1141
1701
|
/**
|
|
1142
1702
|
* The fingerprint this request PROVED, or `null`.
|
|
1143
1703
|
*
|
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
* refused only AFTER all 104,879,625 bytes have gone up — 37 s of upload to
|
|
10
10
|
* learn the answer, reported as `RPC failed; HTTP 413` and `unexpected
|
|
11
11
|
* disconnect`. That reads like a dropped network, so an agent retries it.
|
|
12
|
+
* Behind Cloudflare the same cutoff is the plan's request-body limit, 100 MB
|
|
13
|
+
* (decimal) on Free and Pro, so a cap set from the Fly number would be one the
|
|
14
|
+
* edge enforces first; the public instance's is 90 MiB for that reason.
|
|
12
15
|
*
|
|
13
16
|
* So walgit enforces its own caps, and it does so in `pre-receive`: the pack is
|
|
14
17
|
* sitting in the quarantine, its size is known exactly, and nothing has been
|