@zabaca/zbc 0.12.0 → 0.13.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 +63 -0
- package/package.json +2 -1
- package/templates/apps/inbox/package.json +1 -1
- package/templates/apps/walgit/CONTEXT.md +135 -0
- package/templates/apps/walgit/Dockerfile +40 -18
- package/templates/apps/walgit/README.md +518 -55
- package/templates/apps/walgit/e2e/README.md +100 -6
- package/templates/apps/walgit/e2e/harness.ts +262 -1
- package/templates/apps/walgit/e2e/live.ts +588 -0
- package/templates/apps/walgit/e2e/scenarios.ts +367 -5
- package/templates/apps/walgit/e2e/suite.ts +5 -5
- package/templates/apps/walgit/examples/watch.ts +209 -0
- package/templates/apps/walgit/package.json +12 -3
- package/templates/apps/walgit/registry.json +6 -11
- package/templates/apps/walgit/shared/capabilities.ts +189 -0
- package/templates/apps/walgit/shared/container-env.ts +114 -0
- package/templates/apps/walgit/shared/credentials.ts +81 -0
- package/templates/apps/walgit/shared/events.ts +277 -0
- package/templates/apps/walgit/shared/landing.ts +1241 -0
- package/templates/apps/walgit/shared/llms.ts +463 -0
- package/templates/apps/walgit/shared/outbox.ts +266 -0
- package/templates/apps/walgit/shared/policy.ts +59 -0
- package/templates/apps/walgit/shared/protocol.ts +197 -0
- package/templates/apps/walgit/shared/provenance.ts +154 -0
- package/templates/apps/walgit/shared/ref-cache.ts +181 -0
- package/templates/apps/walgit/shared/telemetry.ts +219 -0
- package/templates/apps/walgit/src/announce-main.ts +37 -0
- package/templates/apps/walgit/src/announce.ts +98 -0
- package/templates/apps/walgit/src/append-only.ts +112 -0
- package/templates/apps/walgit/src/cache.ts +172 -0
- package/templates/apps/walgit/src/cli.ts +493 -0
- package/templates/apps/walgit/src/compact.ts +25 -23
- package/templates/apps/walgit/src/delete-repo.ts +237 -0
- package/templates/apps/walgit/src/expire.ts +248 -0
- package/templates/apps/walgit/src/gc.ts +8 -16
- package/templates/apps/walgit/src/git-backend.ts +29 -0
- package/templates/apps/walgit/src/git.ts +78 -0
- package/templates/apps/walgit/src/hook-main.ts +259 -17
- package/templates/apps/walgit/src/hooks.ts +4 -3
- package/templates/apps/walgit/src/http.ts +237 -45
- package/templates/apps/walgit/src/instructions.ts +240 -0
- package/templates/apps/walgit/src/keys.ts +126 -0
- package/templates/apps/walgit/src/limits.ts +158 -0
- package/templates/apps/walgit/src/materialize.bench.ts +10 -10
- package/templates/apps/walgit/src/materialize.ts +48 -40
- package/templates/apps/walgit/src/mkdir-lock.ts +66 -0
- package/templates/apps/walgit/src/orphans.ts +2 -5
- package/templates/apps/walgit/src/pending.ts +201 -0
- package/templates/apps/walgit/src/push-cert.ts +254 -0
- package/templates/apps/walgit/src/push.ts +233 -46
- package/templates/apps/walgit/src/reconcile.ts +5 -8
- package/templates/apps/walgit/src/repo.ts +22 -77
- package/templates/apps/walgit/src/server.ts +148 -26
- package/templates/apps/walgit/src/signers.ts +577 -0
- package/templates/apps/walgit/src/store-env.ts +5 -4
- package/templates/apps/walgit/src/store.ts +47 -24
- package/templates/apps/walgit/src/usage.ts +404 -0
- package/templates/apps/walgit/src/verify.ts +171 -0
- package/templates/apps/walgit/src/wal-index.ts +189 -11
- package/templates/apps/walgit/tsconfig.json +8 -2
- package/templates/apps/walgit/tsconfig.worker.json +24 -0
- package/templates/apps/walgit/worker/events-do.ts +259 -0
- package/templates/apps/walgit/worker/index.ts +572 -0
- package/templates/apps/walgit/wrangler.jsonc +95 -0
- package/templates/apps/warehouse/package.json +1 -1
- package/templates/infra/modules/cloudflare/index.ts +77 -6
- package/templates/infra/modules/fly/index.ts +150 -10
- package/templates/infra/modules/turso/index.ts +43 -3
- package/templates/infra/package.json +1 -1
- package/templates/apps/walgit/entrypoint.sh +0 -59
- package/templates/apps/walgit/fly.toml +0 -52
- package/templates/apps/walgit/src/ssh-shell.ts +0 -36
- package/templates/apps/walgit/sshd_config +0 -38
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hand-off between the two hook processes of one push.
|
|
3
|
+
*
|
|
4
|
+
* `pre-receive` and `reference-transaction` are separate processes with no
|
|
5
|
+
* channel between them, so what the first uploaded has to be written down for
|
|
6
|
+
* the second to find. That record is what lives here: a file in the repo
|
|
7
|
+
* directory, keyed by the `git-receive-pack` invocation that owns it.
|
|
8
|
+
*
|
|
9
|
+
* It is deliberately NOT a WAL object and knows nothing about object storage —
|
|
10
|
+
* it describes an upload nobody can see yet. Everything in this file is about
|
|
11
|
+
* pids, liveness and local scratch; everything in `push.ts` is about packfiles
|
|
12
|
+
* and compare-and-swap. They were one module and the split is why
|
|
13
|
+
* `cache.ts` can sweep stale records without depending on the write-ahead log.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import * as fs from 'node:fs'
|
|
17
|
+
import * as path from 'node:path'
|
|
18
|
+
|
|
19
|
+
import type { PushSigner } from './signers'
|
|
20
|
+
import type { Claim, Provenance, WalEntry } from './wal-index'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* What `pre-receive` hands to `reference-transaction`.
|
|
24
|
+
*/
|
|
25
|
+
export interface PendingPush {
|
|
26
|
+
/** Absent for a push that carried no objects, e.g. a branch deletion. */
|
|
27
|
+
entry: Omit<WalEntry, 'seq'> | null
|
|
28
|
+
/**
|
|
29
|
+
* Set once a `reference-transaction` has published (or reported as orphaned)
|
|
30
|
+
* the entry above. A push whose refs arrive in more than one transaction — git
|
|
31
|
+
* updates refs one transaction at a time unless the client asked for
|
|
32
|
+
* `--atomic` — publishes its pack with the first of them and the rest as
|
|
33
|
+
* ref-only changes.
|
|
34
|
+
*/
|
|
35
|
+
consumed?: boolean
|
|
36
|
+
/**
|
|
37
|
+
* Who signed this push, when one did and walgit could verify it
|
|
38
|
+
* (docs/adr/0011). Determined in `pre-receive`, because that is the only hook
|
|
39
|
+
* git hands the certificate to — the blob lives in the push's quarantine and
|
|
40
|
+
* is gone by the time the refs move — and carried here for the same reason
|
|
41
|
+
* the entry is: the transaction that publishes it is a different process.
|
|
42
|
+
* Absent for every unsigned push, which is most of them.
|
|
43
|
+
*/
|
|
44
|
+
provenance?: Provenance
|
|
45
|
+
/**
|
|
46
|
+
* What `pre-receive` established about who made this push — all three answers,
|
|
47
|
+
* where `provenance` above records only the one that names a key.
|
|
48
|
+
*
|
|
49
|
+
* Carried because the publish re-asks the ownership question against the
|
|
50
|
+
* Index it is about to commit onto, and a refusal reached there has to read
|
|
51
|
+
* like the one `pre-receive` makes: *sign your push* and *your signature did
|
|
52
|
+
* not verify* are different things to go and do, and the certificate that
|
|
53
|
+
* distinguishes them is in the quarantine, which is gone by then.
|
|
54
|
+
*
|
|
55
|
+
* Optional only so a caller with a record to hand-build — a test, a later
|
|
56
|
+
* transaction of a multi-transaction push — need not restate what it can be
|
|
57
|
+
* read back from; `pre-receive` always writes it. See `pendingSigner`.
|
|
58
|
+
*/
|
|
59
|
+
signer?: PushSigner
|
|
60
|
+
/**
|
|
61
|
+
* The Signer List this push writes, resolved in `pre-receive` from the commit
|
|
62
|
+
* sitting in the quarantine (docs/adr/0012), and carried here for the reason
|
|
63
|
+
* everything else is: the transaction that publishes it is a different
|
|
64
|
+
* process, and by then the quarantine is gone. Absent for every push that
|
|
65
|
+
* does not move `refs/walgit/signers`, which is nearly all of them.
|
|
66
|
+
*/
|
|
67
|
+
claim?: Claim
|
|
68
|
+
/**
|
|
69
|
+
* The `git-receive-pack` that owns this record; see `invocationId`. Optional
|
|
70
|
+
* only because `publishPush` takes a `PendingPush` as a plain argument and
|
|
71
|
+
* has no interest in who wrote it.
|
|
72
|
+
*/
|
|
73
|
+
pid?: number
|
|
74
|
+
/** Written for the sweep, which cannot ask a dead process how old it was. */
|
|
75
|
+
ts?: number
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const PENDING_DIR = 'walgit-pending'
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The hand-off is per `git-receive-pack` invocation, never per repository.
|
|
82
|
+
*
|
|
83
|
+
* git takes no repository-wide lock on a push — only per-ref locks inside the
|
|
84
|
+
* ref transaction — so two clients pushing one repo at once run two overlapping
|
|
85
|
+
* `pre-receive`/`reference-transaction` pairs. A single file per repository lets
|
|
86
|
+
* one of them read the other's upload, and lets one of them clear the other's
|
|
87
|
+
* state, which ends in a push that git acknowledged and the log never received.
|
|
88
|
+
*
|
|
89
|
+
* `process.ppid` is the discriminator: both hooks are `exec`ed by the same
|
|
90
|
+
* `git-receive-pack` (the hook script `exec`s bun, so no shell survives in
|
|
91
|
+
* between), so it is stable across the two hooks of one push and distinct
|
|
92
|
+
* between concurrent pushes. `GIT_QUARANTINE_PATH` would be the obvious
|
|
93
|
+
* alternative and is not one — git does not guarantee it for
|
|
94
|
+
* `reference-transaction`.
|
|
95
|
+
*/
|
|
96
|
+
export function invocationId(): number {
|
|
97
|
+
return process.ppid
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function pendingDir(gitDir: string): string {
|
|
101
|
+
return path.join(gitDir, PENDING_DIR)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function pendingPath(gitDir: string, invocation: number = invocationId()): string {
|
|
105
|
+
return path.join(pendingDir(gitDir), `${invocation}.json`)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function writePending(
|
|
109
|
+
gitDir: string,
|
|
110
|
+
pending: Omit<PendingPush, 'pid' | 'ts'>,
|
|
111
|
+
invocation: number = invocationId(),
|
|
112
|
+
): void {
|
|
113
|
+
fs.mkdirSync(pendingDir(gitDir), { recursive: true })
|
|
114
|
+
const record: PendingPush = { ...pending, pid: invocation, ts: Date.now() }
|
|
115
|
+
fs.writeFileSync(pendingPath(gitDir, invocation), JSON.stringify(record))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function readPending(
|
|
119
|
+
gitDir: string,
|
|
120
|
+
invocation: number = invocationId(),
|
|
121
|
+
): PendingPush | null {
|
|
122
|
+
const file = pendingPath(gitDir, invocation)
|
|
123
|
+
if (!fs.existsSync(file)) return null
|
|
124
|
+
const record = JSON.parse(fs.readFileSync(file, 'utf8')) as PendingPush
|
|
125
|
+
// A pid is reused eventually. A record left by a dead `git-receive-pack` that
|
|
126
|
+
// happened to hold this pid must not be mistaken for this push's own upload.
|
|
127
|
+
if (record.pid !== invocation) return null
|
|
128
|
+
return record
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Record that the entry has been published, without deleting the record.
|
|
133
|
+
*
|
|
134
|
+
* The record has to outlive the transaction that consumed it: the next thing
|
|
135
|
+
* `hook-main` must be able to tell apart is "no `pre-receive` ran here, this is
|
|
136
|
+
* an administrative ref edit" from "`pre-receive` ran and its record is gone".
|
|
137
|
+
* The first is a legitimate exit 0; the second is the silent acknowledgement
|
|
138
|
+
* this file exists to prevent. Deleting on `committed` erases that distinction,
|
|
139
|
+
* so cleanup happens in `post-receive` and in the sweep, never mid-push.
|
|
140
|
+
*/
|
|
141
|
+
export function markConsumed(gitDir: string, invocation: number = invocationId()): void {
|
|
142
|
+
const record = readPending(gitDir, invocation)
|
|
143
|
+
if (!record) return
|
|
144
|
+
fs.writeFileSync(pendingPath(gitDir, invocation), JSON.stringify({ ...record, consumed: true }))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function clearPending(gitDir: string, invocation: number = invocationId()): void {
|
|
148
|
+
fs.rmSync(pendingPath(gitDir, invocation), { force: true })
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** An hour is far longer than any push and far shorter than any operator cares. */
|
|
152
|
+
export const PENDING_MAX_AGE_MS = 60 * 60 * 1000
|
|
153
|
+
|
|
154
|
+
function isAlive(pid: number): boolean {
|
|
155
|
+
try {
|
|
156
|
+
process.kill(pid, 0)
|
|
157
|
+
return true
|
|
158
|
+
} catch (err) {
|
|
159
|
+
return (err as NodeJS.ErrnoException).code === 'EPERM'
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Drop records whose `git-receive-pack` is gone, so a killed push leaves no
|
|
165
|
+
* file a later one could read.
|
|
166
|
+
*
|
|
167
|
+
* Liveness AND age, not either alone: liveness is exact but only inside one pid
|
|
168
|
+
* namespace, so a record written before a container restart can name a pid that
|
|
169
|
+
* is alive again as something else; age alone would keep a killed push's record
|
|
170
|
+
* readable for an hour. A live pid younger than the cutoff is left strictly
|
|
171
|
+
* alone — that is a push in flight.
|
|
172
|
+
*/
|
|
173
|
+
export function sweepPending(gitDir: string, now = Date.now()): string[] {
|
|
174
|
+
const dir = pendingDir(gitDir)
|
|
175
|
+
let names: string[]
|
|
176
|
+
try {
|
|
177
|
+
names = fs.readdirSync(dir)
|
|
178
|
+
} catch {
|
|
179
|
+
return []
|
|
180
|
+
}
|
|
181
|
+
const swept: string[] = []
|
|
182
|
+
for (const name of names) {
|
|
183
|
+
const file = path.join(dir, name)
|
|
184
|
+
let record: PendingPush | null = null
|
|
185
|
+
try {
|
|
186
|
+
record = JSON.parse(fs.readFileSync(file, 'utf8')) as PendingPush
|
|
187
|
+
} catch {
|
|
188
|
+
// Unreadable or half-written by a process that died mid-write: it can
|
|
189
|
+
// never be claimed by anyone, so it is garbage by definition.
|
|
190
|
+
}
|
|
191
|
+
const stale =
|
|
192
|
+
!record ||
|
|
193
|
+
typeof record.pid !== 'number' ||
|
|
194
|
+
!isAlive(record.pid) ||
|
|
195
|
+
now - (record.ts ?? 0) > PENDING_MAX_AGE_MS
|
|
196
|
+
if (!stale) continue
|
|
197
|
+
fs.rmSync(file, { force: true })
|
|
198
|
+
swept.push(file)
|
|
199
|
+
}
|
|
200
|
+
return swept
|
|
201
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signed pushes: the one seed the capability hangs off.
|
|
3
|
+
*
|
|
4
|
+
* A push certificate is a claim about *who moved this ref*, signed by the
|
|
5
|
+
* pusher and carrying a nonce this server issued (docs/adr/0011). Everything
|
|
6
|
+
* about it is a `receive-pack` capability rather than a transport feature —
|
|
7
|
+
* which is why it works over smart-HTTP with no SSH anywhere (ADR-0008) — and
|
|
8
|
+
* `git-receive-pack` advertises that capability if, and only if, the receiving
|
|
9
|
+
* repository has `receive.certNonceSeed` set. With no seed a client asking for
|
|
10
|
+
* `--signed=yes` is refused by its OWN git, before a byte reaches the network:
|
|
11
|
+
* `fatal: the receiving end does not support --signed push`. That refusal is
|
|
12
|
+
* the correct answer for a deployment that has not turned provenance on, and it
|
|
13
|
+
* is why this needs no flag of its own — the seed IS the flag.
|
|
14
|
+
*
|
|
15
|
+
* Configuration, never generated. The nonce is an HMAC of the seed and a
|
|
16
|
+
* timestamp, and a client holds one across the round trip between advertisement
|
|
17
|
+
* and push. The container's disk is a cache that is wiped on every restart
|
|
18
|
+
* (ADR-0007), so a seed minted at boot would be a different seed on the other
|
|
19
|
+
* side of a restart and would reject every certificate in flight. It therefore
|
|
20
|
+
* arrives as an environment variable, and joins the forward list in
|
|
21
|
+
* `shared/container-env.ts` like every other value the container boots with.
|
|
22
|
+
*
|
|
23
|
+
* The seed turns the capability on; the second half of this file is what walgit
|
|
24
|
+
* then does with a certificate — read it, verify it itself, and name the key
|
|
25
|
+
* that signed it. It refuses nothing on the strength of that: a signed push
|
|
26
|
+
* lands exactly as an unsigned one does, and every way this can go wrong
|
|
27
|
+
* records no Signer rather than failing the push. See `shared/provenance.ts`
|
|
28
|
+
* for why git's own verdict on the signature is unusable.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { spawnSync } from 'node:child_process'
|
|
32
|
+
import * as fs from 'node:fs'
|
|
33
|
+
import * as os from 'node:os'
|
|
34
|
+
import * as path from 'node:path'
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
fingerprintIn,
|
|
38
|
+
PUSH_CERT_NAMESPACE,
|
|
39
|
+
pushCertSeed as seedFrom,
|
|
40
|
+
splitPushCertificate,
|
|
41
|
+
type SplitCertificate,
|
|
42
|
+
} from '../shared/provenance'
|
|
43
|
+
import { git } from './git'
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The seed this deployment configures, or `null` when it configures none.
|
|
47
|
+
*
|
|
48
|
+
* The reading itself lives in `shared/provenance.ts`: the Worker renders both
|
|
49
|
+
* agent-facing documents from the same answer and cannot import this module at
|
|
50
|
+
* all (it spawns a subprocess, and `shared/` has no runtime — ADR-0010). A
|
|
51
|
+
* document that advertised signed pushes from a second reading of the variable
|
|
52
|
+
* would be one spelling away from promising a capability the push path does
|
|
53
|
+
* not have, which is the exact drift `flagEnabled` was extracted to end. What
|
|
54
|
+
* stays here is only where the container finds it.
|
|
55
|
+
*/
|
|
56
|
+
export function pushCertSeed(env: Record<string, string | undefined> = process.env): string | null {
|
|
57
|
+
return seedFrom(env.WALGIT_PUSH_CERT_SEED)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Does this instance take signed pushes? The seed, read as a yes/no. */
|
|
61
|
+
export function signedPushEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
62
|
+
return pushCertSeed(env) !== null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* How old a nonce may be and still count as this server's, in seconds.
|
|
67
|
+
*
|
|
68
|
+
* Written to `receive.certNonceSlop` beside the seed (`src/cache.ts`), and the
|
|
69
|
+
* one number that makes signed pushes work over smart-HTTP at all.
|
|
70
|
+
*
|
|
71
|
+
* git mints the nonce as an HMAC over the repository path and *the unix second
|
|
72
|
+
* it was minted in*, and a push is two requests: the advertisement mints one,
|
|
73
|
+
* the push validates it in a second process that minted its own. The two agree
|
|
74
|
+
* as strings only when both landed inside the same second, which is a property
|
|
75
|
+
* of the network rather than of the pusher — so git falls through to its
|
|
76
|
+
* stateless-RPC path, recomputes the HMAC for the timestamp inside the client's
|
|
77
|
+
* nonce, and then asks how stale it is. `receive.certNonceSlop` is the answer
|
|
78
|
+
* it measures against, it defaults to 0, and a round trip that crosses a second
|
|
79
|
+
* boundary therefore reads as `SLOP`: intermittently anonymous on an unclaimed
|
|
80
|
+
* name, and intermittently *refused* on a claimed one.
|
|
81
|
+
*
|
|
82
|
+
* Five minutes, because the gap it has to cover is a client signing its
|
|
83
|
+
* certificate, a real network, and a container that may have been asleep when
|
|
84
|
+
* the advertisement arrived — and because the window is exactly the replay
|
|
85
|
+
* window for a captured certificate, so it is bounded rather than generous.
|
|
86
|
+
* Inside it, a replayed certificate is a push of the ref updates it names, made
|
|
87
|
+
* again: usually a no-op, because `publishPush` compare-and-swaps each ref
|
|
88
|
+
* against the old oid the certificate carries and the ref has since moved past
|
|
89
|
+
* it — but a ref that was moved and then moved back is at that old oid again,
|
|
90
|
+
* and the replay lands. Bounding it is what keeps that a five-minute race
|
|
91
|
+
* against an attacker who already captured the request rather than a standing
|
|
92
|
+
* one.
|
|
93
|
+
*/
|
|
94
|
+
export const PUSH_CERT_NONCE_SLOP_SECONDS = 300
|
|
95
|
+
|
|
96
|
+
// ── Reading the certificate, and verifying it ───────────────────────────────
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* What `git-receive-pack` leaves for the hook, as this module reads it.
|
|
100
|
+
*
|
|
101
|
+
* Only two of the seven `GIT_PUSH_CERT_*` variables are used, and the five
|
|
102
|
+
* omitted ones are omitted for one reason each: `_SIGNER`, `_KEY` and `_STATUS`
|
|
103
|
+
* are git's own GPG verdict, which is `N` for every SSH signature (see
|
|
104
|
+
* `shared/provenance.ts`); `_NONCE` is the value whose verdict `_NONCE_STATUS`
|
|
105
|
+
* already is; and `_NONCE_SLOP` says by how many seconds a stale nonce missed a
|
|
106
|
+
* window git has already judged it against (`PUSH_CERT_NONCE_SLOP_SECONDS`).
|
|
107
|
+
*/
|
|
108
|
+
export interface PushCertEnv {
|
|
109
|
+
/** Blob object name of the certificate, readable with `git cat-file`. */
|
|
110
|
+
GIT_PUSH_CERT?: string | undefined
|
|
111
|
+
/** `OK`, `SLOP`, `MISSING`, `BAD` or `UNSOLICITED`. Only `OK` counts. */
|
|
112
|
+
GIT_PUSH_CERT_NONCE_STATUS?: string | undefined
|
|
113
|
+
/** Open, so `process.env` — an index signature and nothing else — fits it. */
|
|
114
|
+
[name: string]: string | undefined
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Verify a split certificate and name the key that signed it, or `null`. */
|
|
118
|
+
export type CertVerifier = (cert: SplitCertificate) => string | null
|
|
119
|
+
|
|
120
|
+
/** Read a blob out of the repository the hook is running in. */
|
|
121
|
+
export type BlobReader = (oid: string) => string | null
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* `git cat-file blob <oid>` in the repository this hook belongs to.
|
|
125
|
+
*
|
|
126
|
+
* The certificate blob is written before `pre-receive` runs and lands in the
|
|
127
|
+
* push's quarantine, which is on the hook's object path — so this reads it with
|
|
128
|
+
* no special handling, and returns `null` rather than throwing once the
|
|
129
|
+
* quarantine is gone (which is what makes calling this from a later hook a
|
|
130
|
+
* no-op rather than a crash).
|
|
131
|
+
*/
|
|
132
|
+
function catBlob(oid: string): string | null {
|
|
133
|
+
const res = git(['cat-file', 'blob', oid])
|
|
134
|
+
return res.status === 0 ? res.stdout : null
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Did this push offer a certificate at all — whatever became of it?
|
|
139
|
+
*
|
|
140
|
+
* Provenance never needed this question: a certificate that cannot be turned
|
|
141
|
+
* into a key and a push that carried none are both `null`, and `null` is a push
|
|
142
|
+
* that lands. The gate in `src/signers.ts` does need it, because on a claimed
|
|
143
|
+
* repository the two are different refusals with different fixes — *sign your
|
|
144
|
+
* push* and *your signature did not verify, push again* — and an agent told the
|
|
145
|
+
* first when the second is true will keep re-sending a signature that keeps
|
|
146
|
+
* failing.
|
|
147
|
+
*
|
|
148
|
+
* git sets `GIT_PUSH_CERT` to the certificate's blob whenever one arrived, and
|
|
149
|
+
* reports what it thinks of the nonce separately, so this stays true for the
|
|
150
|
+
* stale-nonce case that `readPushCertificate` refuses just below.
|
|
151
|
+
*/
|
|
152
|
+
export function certificatePresented(env: PushCertEnv = process.env): boolean {
|
|
153
|
+
return (env.GIT_PUSH_CERT ?? '').trim() !== ''
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The certificate this push carries, split and ready to verify, or `null`.
|
|
158
|
+
*
|
|
159
|
+
* The nonce gate is here rather than after verification because it is a
|
|
160
|
+
* different question with a different answer: the signature says *who*, the
|
|
161
|
+
* nonce says *this server, now*. A certificate with a stale, missing or
|
|
162
|
+
* unsolicited nonce may carry a perfectly good signature over a push directed
|
|
163
|
+
* somewhere else, and recording its key as the Signer here would attribute one
|
|
164
|
+
* host's push to a ref on another. git already decided that question — its
|
|
165
|
+
* verdict is `GIT_PUSH_CERT_NONCE_STATUS`, and only `OK` is one.
|
|
166
|
+
*
|
|
167
|
+
* `SLOP` is deliberately not accepted, and that is only defensible because the
|
|
168
|
+
* window it is measured against is configured: it means the nonce is ours but
|
|
169
|
+
* older than `receive.certNonceSlop`, which `ensureBareRepo` writes beside the
|
|
170
|
+
* seed as `PUSH_CERT_NONCE_SLOP_SECONDS`. Accepting `SLOP` as it stands would
|
|
171
|
+
* accept a nonce of any age, since git reports the same word for one issued
|
|
172
|
+
* five minutes ago and one issued last week. git does hand the staleness over
|
|
173
|
+
* as `GIT_PUSH_CERT_NONCE_SLOP`, so a second bound could be enforced here — it
|
|
174
|
+
* is not, because two windows that have to agree are one more thing to get
|
|
175
|
+
* wrong than the one git already enforces before this code runs.
|
|
176
|
+
*
|
|
177
|
+
* Left unconfigured the window is zero, and this line then refuses every signed
|
|
178
|
+
* push whose round trip crossed a second boundary.
|
|
179
|
+
*/
|
|
180
|
+
export function readPushCertificate(
|
|
181
|
+
env: PushCertEnv = process.env,
|
|
182
|
+
readBlob: BlobReader = catBlob,
|
|
183
|
+
): SplitCertificate | null {
|
|
184
|
+
if (!certificatePresented(env)) return null
|
|
185
|
+
const oid = (env.GIT_PUSH_CERT ?? '').trim()
|
|
186
|
+
if ((env.GIT_PUSH_CERT_NONCE_STATUS ?? '').trim() !== 'OK') return null
|
|
187
|
+
const text = readBlob(oid)
|
|
188
|
+
if (text === null) return null
|
|
189
|
+
return splitPushCertificate(text)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The real verifier: `ssh-keygen -Y check-novalidate -n git`.
|
|
194
|
+
*
|
|
195
|
+
* `check-novalidate` is the whole point — it verifies the signature and reports
|
|
196
|
+
* the key, and does NOT ask whether that key is allowed. `-Y verify` would, and
|
|
197
|
+
* would need an allowed-signers file to ask against: a key registry, which is
|
|
198
|
+
* the one thing this design does not have. walgit is recording who pushed, not
|
|
199
|
+
* deciding whether they may.
|
|
200
|
+
*
|
|
201
|
+
* The signature must be a file (`-s` takes no `-`), so it is written to a
|
|
202
|
+
* private temporary directory and removed; the body goes on stdin, which keeps
|
|
203
|
+
* the bytes that were signed off the disk and out of the process table.
|
|
204
|
+
*
|
|
205
|
+
* Never throws, and returns `null` for every failure alike — a bad signature, a
|
|
206
|
+
* namespace mismatch, and an `ssh-keygen` that is not installed are all "no
|
|
207
|
+
* Signer", because none of them may cost the pusher their push.
|
|
208
|
+
*/
|
|
209
|
+
export function sshKeygenVerifier(cert: SplitCertificate): string | null {
|
|
210
|
+
let dir: string | null = null
|
|
211
|
+
try {
|
|
212
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'walgit-cert-'))
|
|
213
|
+
const sigFile = path.join(dir, 'push-cert.sig')
|
|
214
|
+
fs.writeFileSync(sigFile, cert.signature)
|
|
215
|
+
const res = spawnSync(
|
|
216
|
+
'ssh-keygen',
|
|
217
|
+
['-Y', 'check-novalidate', '-n', PUSH_CERT_NAMESPACE, '-s', sigFile],
|
|
218
|
+
{ encoding: 'utf8', input: cert.body },
|
|
219
|
+
)
|
|
220
|
+
// A missing binary leaves `status` null and `error` set, which is the same
|
|
221
|
+
// answer as a refusal: exit 0 is the only verdict that names a key.
|
|
222
|
+
if (res.status !== 0) return null
|
|
223
|
+
return fingerprintIn(`${res.stdout ?? ''}\n${res.stderr ?? ''}`)
|
|
224
|
+
} catch {
|
|
225
|
+
return null
|
|
226
|
+
} finally {
|
|
227
|
+
if (dir) fs.rmSync(dir, { recursive: true, force: true })
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Who signed this push, or `null` for "nobody, as far as walgit can tell".
|
|
233
|
+
*
|
|
234
|
+
* This is the fail-open seam, and the failing open is the feature. A push with
|
|
235
|
+
* no certificate, a malformed one, a bad nonce, an unverifiable signature,
|
|
236
|
+
* verifier output naming no key, an `ssh-keygen` that is absent, and a verifier
|
|
237
|
+
* that throws all return the same `null` — and a `null` Signer is a push that
|
|
238
|
+
* lands exactly as it always has. Provenance is metadata; it must never become
|
|
239
|
+
* a new way for a push to fail, which is why the catch here is unconditional
|
|
240
|
+
* rather than a list of anticipated errors.
|
|
241
|
+
*/
|
|
242
|
+
export function certSigner(
|
|
243
|
+
env: PushCertEnv = process.env,
|
|
244
|
+
verify: CertVerifier = sshKeygenVerifier,
|
|
245
|
+
readBlob: BlobReader = catBlob,
|
|
246
|
+
): string | null {
|
|
247
|
+
try {
|
|
248
|
+
const cert = readPushCertificate(env, readBlob)
|
|
249
|
+
if (!cert) return null
|
|
250
|
+
return verify(cert)
|
|
251
|
+
} catch {
|
|
252
|
+
return null
|
|
253
|
+
}
|
|
254
|
+
}
|