~bigbes/sr-ht-spec

ref: a4d8cc52e917cf1db07c876e8b36654dcf28741b sr-ht-spec/hooks/e2e_test.go -rw-r--r-- 13.7 KiB
a4d8cc52 — Eugene Blikh authn: remove the local agent-token plane 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package hooks

import (
	"context"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"testing/fstest"

	"github.com/go-git/go-git/v5/plumbing"

	"sourcecraft.dev/bigbes/sr-ht-core/config"
	"sourcecraft.dev/bigbes/sr-ht-core/crypto"

	"sourcecraft.dev/bigbes/sr-ht-spec/core"
	"sourcecraft.dev/bigbes/sr-ht-spec/gitx"
	"sourcecraft.dev/bigbes/sr-ht-spec/service"
)

// TestMain makes this test binary double as the specsrht binary.
//
// Install writes each hook as a symlink to whatever binary it is given and the
// hook dispatches on the name git invoked it as, so pointing those symlinks at
// the test binary is enough to make a real `git push` run this package's code
// through git's real receive path. That is the production dispatch mechanism,
// unchanged and unmocked — not a stand-in for it.
//
// It also initialises crypto, because the agent credential this package
// validates is a signed working token and the signing key lives in core-go's
// globals; the keys are the ones core-go's own tests use. A hook process skips
// that: a hook reads no config and validates nothing, which is the property
// that lets it run with no state at all.
func TestMain(m *testing.M) {
	if _, _, isHook := ModeFromArgs(os.Args); isHook {
		os.Exit(Run(Runtime{Args: os.Args}))
	}
	config.FS = fstest.MapFS{
		"config.ini": &fstest.MapFile{Data: []byte(`
[webhooks]
private-key=ebzsjPaN6E13ln/FeNWly1C92q6bVMVdOnDo1HPl5fc=

[sr.ht]
network-key=tbuG-7Vh44vrDq1L_HKWkHnWrDOtJhEkPKPiauaLeuk=
`)},
	}
	crypto.InitCrypto(config.LoadConfig())
	os.Exit(m.Run())
}

// git runs the real git binary. Shelling out is confined to tests: nothing in
// this package's non-test code executes a subprocess.
func gitMust(t *testing.T, dir string, args ...string) string {
	t.Helper()
	out, err := runGit(t, dir, nil, args...)
	if err != nil {
		t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
	}
	return out
}

func runGit(t *testing.T, dir string, env map[string]string, args ...string) (string, error) {
	t.Helper()
	cmd := exec.Command("git", args...)
	cmd.Dir = dir
	cmd.Env = append(os.Environ(),
		"GIT_CONFIG_GLOBAL=/dev/null",
		"GIT_CONFIG_SYSTEM=/dev/null",
		"GIT_AUTHOR_NAME=bigbes", "GIT_AUTHOR_EMAIL=bigbes@example.invalid",
		"GIT_COMMITTER_NAME=bigbes", "GIT_COMMITTER_EMAIL=bigbes@example.invalid",
	)
	for k, v := range env {
		cmd.Env = append(cmd.Env, k+"="+v)
	}
	out, err := cmd.CombinedOutput()
	return string(out), err
}

// pushEnv is what the forced-command wrapper exports for a push by the owner.
// The local transport spawns receive-pack as a child of the client, so the
// client's environment is what the hooks see.
func pushEnv() map[string]string { return map[string]string{EnvPrincipal: string(PrincipalOwner)} }

// realish is a Backend that answers with the actual rules where they need no
// database: gitx.CheckRefUpdate for the refs rule, and core.ParseDocument plus
// the space's schema for frontmatter.
//
// The document-id registry needs Postgres and is therefore not covered here;
// service/'s own tests cover it. What this proves is the receive path itself —
// that git runs our hooks, that they reach the daemon, that a refusal comes
// back as a readable message and a non-zero exit, and that skip-validation
// reaches the right half of the decision.
func realish(t *testing.T, root string) func(context.Context, service.PushRequest) error {
	t.Helper()
	return func(ctx context.Context, req service.PushRequest) error {
		repo, err := gitx.Open(root, req.Space)
		if err != nil {
			return fmt.Errorf("open %s: %w", req.Space, err)
		}

		old, new := plumbing.NewHash(req.Old), plumbing.NewHash(req.New)
		fastForward := old.IsZero()
		if !old.IsZero() && !new.IsZero() {
			ff, err := repo.IsAncestor(ctx, old, new)
			if err != nil {
				return fmt.Errorf("ancestry of %s..%s: %w", req.Old, req.New, err)
			}
			fastForward = ff
		}
		kind := gitx.PrincipalHuman
		if req.Principal.IsAgent() {
			kind = gitx.PrincipalAgent
		}
		if err := gitx.CheckRefUpdate(kind, repo.ApprovedBranch(), gitx.RefUpdate{
			Ref: req.Ref, Old: old, New: new, FastForward: fastForward,
		}); err != nil {
			return &service.PushRejection{
				Space: req.Space, Ref: req.Ref, Skippable: false,
				Problems: []service.PushProblem{{Kind: service.ProblemRefsRule, Detail: err.Error()}},
			}
		}

		if req.SkipValidation || new.IsZero() {
			return nil
		}

		docs, err := repo.ListDocuments(ctx, req.New)
		if err != nil {
			return fmt.Errorf("list documents at %s: %w", req.New, err)
		}
		schema := core.DefaultSchema()
		var problems []service.PushProblem
		for _, d := range docs {
			fm, _, err := core.ParseDocument(d.Data)
			if err == nil {
				err = schema.ValidateFrontmatter(fm)
			}
			if err != nil {
				problems = append(problems, service.PushProblem{
					Kind: service.ProblemFrontmatter, Path: d.Path, Detail: err.Error(),
				})
			}
		}
		if len(problems) > 0 {
			return &service.PushRejection{
				Space: req.Space, Ref: req.Ref, Problems: problems, Skippable: true,
			}
		}
		return nil
	}
}

// e2e is a repos root with one hooked space and a working clone.
type e2e struct {
	root   string
	repo   string
	work   string
	server *Server
	back   *fakeBackend
	landed *[]core.SpaceRef
}

func newE2E(t *testing.T) *e2e {
	t.Helper()
	if _, err := exec.LookPath("git"); err != nil {
		t.Skipf("git is not on PATH: %v", err)
	}
	binary, err := os.Executable()
	if err != nil {
		t.Fatalf("os.Executable: %v", err)
	}

	root := shortTempDir(t)
	repo := bareRepo(t, root, testSpace)
	if err := InstallSpace(root, testSpace, InstallOptions{Binary: binary}); err != nil {
		t.Fatalf("InstallSpace: %v", err)
	}

	back := newFakeBackend(t, root)
	back.validate = realish(t, root)
	srv, landed := startServer(t, back)

	work := filepath.Join(shortTempDir(t), "work")
	gitMust(t, "", "init", "--quiet", "--initial-branch=main", work)

	return &e2e{root: root, repo: repo, work: work, server: srv, back: back, landed: landed}
}

// write stages a file in the working clone.
func (e *e2e) write(t *testing.T, path, body string) {
	t.Helper()
	full := filepath.Join(e.work, path)
	if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
		t.Fatalf("MkdirAll: %v", err)
	}
	if err := os.WriteFile(full, []byte(body), 0o644); err != nil {
		t.Fatalf("WriteFile: %v", err)
	}
	gitMust(t, e.work, "add", path)
}

func (e *e2e) commit(t *testing.T, message string) {
	t.Helper()
	gitMust(t, e.work, "commit", "--quiet", "-m", message)
}

// push runs a real `git push` and returns its combined output plus whether it
// succeeded.
func (e *e2e) push(t *testing.T, args ...string) (string, bool) {
	t.Helper()
	out, err := runGit(t, e.work, pushEnv(), append([]string{"push", e.repo}, args...)...)
	return out, err == nil
}

// pushAsAgent is the same push with the environment the forced-command wrapper
// exports for an agent: a tokens.sr.ht working token and the provenance fields.
func (e *e2e) pushAsAgent(t *testing.T, token string, args ...string) (string, bool) {
	t.Helper()
	env := map[string]string{
		EnvPrincipal:    string(PrincipalAgent),
		EnvAgentToken:   token,
		EnvAgent:        "claude-code/spec-writer",
		EnvAgentSession: "8fb9c9a4-b078-4af1-89eb-d97c522f9921",
	}
	out, err := runGit(t, e.work, env, append([]string{"push", e.repo}, args...)...)
	return out, err == nil
}

const goodDoc = `---
id: SPEC-0001
title: Storage
status: draft
---

# Storage

One storage tier.
`

const badDoc = `---
title: No identity
status: draft
---

This document has no id.
`

// TestEndToEndPush drives the whole receive path with a real git push against
// a real bare repository with our hooks installed.
func TestEndToEndPush(t *testing.T) {
	e := newE2E(t)

	t.Run("a valid push is accepted", func(t *testing.T) {
		e.write(t, "specs/0001-storage.md", goodDoc)
		e.commit(t, "add the storage spec")
		out, ok := e.push(t, "main:refs/heads/main")
		if !ok {
			t.Fatalf("a valid push was rejected:\n%s", out)
		}
		if got := len(*e.landed); got == 0 {
			t.Errorf("post-receive did not notify the daemon (landed=%d)", got)
		}
		if head := strings.TrimSpace(gitMust(t, e.repo, "rev-parse", "refs/heads/main")); head == "" {
			t.Error("main was not updated")
		}
	})

	t.Run("bad frontmatter is rejected with a message naming the document", func(t *testing.T) {
		e.write(t, "specs/0002-broken.md", badDoc)
		e.commit(t, "add a document with no id")
		out, ok := e.push(t, "main:refs/heads/main")
		if ok {
			t.Fatalf("a push with malformed frontmatter was accepted:\n%s", out)
		}
		mentions(t, "the rejection", out,
			"spec.sr.ht rejected this push",
			"specs/0002-broken.md",
			"--push-option=skip-validation",
		)
		if head := strings.TrimSpace(gitMust(t, e.repo, "log", "--oneline", "-1", "refs/heads/main")); strings.Contains(head, "no id") {
			t.Error("the rejected commit landed anyway")
		}
	})

	t.Run("skip-validation lets the same push through", func(t *testing.T) {
		out, ok := e.push(t, "--push-option=skip-validation", "main:refs/heads/main")
		if !ok {
			t.Fatalf("skip-validation did not waive frontmatter validation:\n%s", out)
		}
	})

	t.Run("an unknown push option is refused rather than ignored", func(t *testing.T) {
		e.write(t, "specs/0003-note.md", goodDoc)
		e.commit(t, "another document")
		out, ok := e.push(t, "--push-option=skip-validaton", "main:refs/heads/main")
		if ok {
			t.Fatalf("a mistyped push option was ignored:\n%s", out)
		}
		mentions(t, "the rejection", out, "skip-validaton", "is not a push option")
	})

	t.Run("a force-push to the approved branch is refused", func(t *testing.T) {
		// Rewrite history so the update is not a fast-forward.
		gitMust(t, e.work, "reset", "--quiet", "--hard", "HEAD~2")
		e.write(t, "specs/0009-rewritten.md", goodDoc)
		e.commit(t, "rewrite history")

		out, ok := e.push(t, "--force", "main:refs/heads/main")
		if ok {
			t.Fatalf("a force-push to the approved branch was accepted:\n%s", out)
		}
		mentions(t, "the rejection", out,
			"spec.sr.ht rejected this push",
			"The refs rule cannot be bypassed",
		)
	})

	t.Run("the refs rule is not skippable", func(t *testing.T) {
		out, ok := e.push(t, "--push-option=skip-validation", "--force", "main:refs/heads/main")
		if ok {
			t.Fatalf("skip-validation waived the refs rule:\n%s", out)
		}
		mentions(t, "the rejection", out, "The refs rule cannot be bypassed")
	})

	t.Run("a proposal branch may be force-updated", func(t *testing.T) {
		out, ok := e.push(t, "--force", "main:refs/heads/proposals/1")
		if !ok {
			t.Fatalf("a proposal branch was refused:\n%s", out)
		}
	})
}

// TestEndToEndAgentPush is the credential change under a real `git push`: an
// agent holding a tokens.sr.ht working token can push, the refs rule still
// confines it to proposals/*, and a token without spec:propose gets nowhere.
//
// Before this, the SSH path checked agent_token directly and would have refused
// every one of these; it is now the same authn.Resolver the HTTP surfaces use.
func TestEndToEndAgentPush(t *testing.T) {
	e := newE2E(t)
	e.write(t, "specs/0001-storage.md", goodDoc)
	e.commit(t, "add the storage spec")

	t.Run("a token without spec:propose cannot push", func(t *testing.T) {
		out, ok := e.pushAsAgent(t, instanceToken("spec:read"), "main:refs/heads/proposals/1")
		if ok {
			t.Fatalf("a token with no propose grant was accepted:\n%s", out)
		}
		mentions(t, "the rejection", out, "spec.sr.ht rejected this push", "spec:propose")
		if out, err := runGit(t, e.repo, nil, "rev-parse", "--verify", "refs/heads/proposals/1"); err == nil {
			t.Errorf("the ref moved despite the refusal: %s", out)
		}
	})

	t.Run("the old opaque agent token cannot push", func(t *testing.T) {
		out, ok := e.pushAsAgent(t, "s3cret-from-agent-token", "main:refs/heads/proposals/1")
		if ok {
			t.Fatalf("a credential from the removed plane was accepted:\n%s", out)
		}
		mentionsNot(t, "the rejection", out, "s3cret-from-agent-token")
	})

	t.Run("an instance token with spec:propose pushes a proposal branch", func(t *testing.T) {
		out, ok := e.pushAsAgent(t, instanceToken("spec:propose"), "main:refs/heads/proposals/1")
		if !ok {
			t.Fatalf("an agent holding a tokens.sr.ht token was refused:\n%s", out)
		}
		if head := strings.TrimSpace(gitMust(t, e.repo, "rev-parse", "refs/heads/proposals/1")); head == "" {
			t.Error("the proposal branch was not created")
		}
	})

	t.Run("the refs rule still confines it to the proposal prefix", func(t *testing.T) {
		out, ok := e.pushAsAgent(t, instanceToken("*"), "main:refs/heads/main")
		if ok {
			t.Fatalf("an agent moved the approved branch:\n%s", out)
		}
		mentions(t, "the rejection", out, "spec.sr.ht rejected this push")
		if out, err := runGit(t, e.repo, nil, "rev-parse", "--verify", "refs/heads/main"); err == nil {
			t.Errorf("the approved branch moved despite the refusal: %s", out)
		}
	})
}

// TestEndToEndFailsClosed is the fail-closed rule under a real push: with the
// daemon gone, the push is refused rather than silently accepted unvalidated.
func TestEndToEndFailsClosed(t *testing.T) {
	e := newE2E(t)
	e.write(t, "specs/0001-storage.md", goodDoc)
	e.commit(t, "add the storage spec")

	// Take the daemon down exactly as a crash would: stop serving and unlink
	// the socket.
	if err := e.server.Close(); err != nil {
		t.Fatalf("Close: %v", err)
	}

	out, ok := e.push(t, "main:refs/heads/main")
	if ok {
		t.Fatalf("a push was accepted with no daemon to validate it:\n%s", out)
	}
	mentions(t, "the fail-closed message", out,
		"could not validate this push",
		SocketPath(e.root),
		"Start the spec.sr.ht daemon and push again",
	)
	// The escape hatch must not be advertised here: it waives frontmatter
	// checks, not the daemon that performs them.
	mentionsNot(t, "the fail-closed message", out, "Re-push with --push-option=skip-validation")

	if out, err := runGit(t, e.repo, nil, "rev-parse", "--verify", "refs/heads/main"); err == nil {
		t.Errorf("the ref moved despite the refusal: %s", out)
	}
}