gunk-dev/the-valley

knots-e2e.sh

source4c55e5bb434cmain's published tip history
19.8 KiB
1# shellcheck shell=bash
2# The plan graph through the real host hook: bare repositories wired with
3# the pre-receive hook the protected host's valley-init renders, written by
4# the real valley knot as each principal, and pushed crafted commits that
5# break each rule the hook holds a push to refs/the-valley/knots to. Every
6# refused push must leave the ref where it was.
7# Paths and fixtures come from the derivation environment.
8# shellcheck disable=SC2154
9set -euo pipefail
10
11w="$TMPDIR/knots-e2e"
12mkdir -p "$w"
13export HOME="$w"
14export XDG_CACHE_HOME="$w/cache"
15export GIT_CONFIG_NOSYSTEM=1
16git config --global user.name knots-e2e
17git config --global user.email knots-e2e@localhost
18git config --global init.defaultBranch main
19git config --global advice.detachedHead false
20
21fail() {
22 printf 'knots-e2e: %s\n' "$*" >&2
23 if [ -s "$w/err" ]; then cat "$w/err" >&2; fi
24 exit 1
25}
26
27# valley is bin/valley, run by bash, with the valley-go helper on PATH.
28mkdir -p "$w/bin"
29printf '#!%s\nexec %q %q "$@"\n' "$BASH" "$BASH" "$cliScript" > "$w/bin/valley"
30chmod +x "$w/bin/valley"
31export PATH="$w/bin:$PATH"
32
33# A bare repo wired as valley-init wires it, with the hook followed from
34# the rendered init script, as protect-e2e wires it. No verifier keys are
35# named: no attestation is accepted here. git runs the hook through a
36# wrapper that adds a line to <name>.runs for each run.
37serve() {
38 local hook
39 hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)"
40 test -x "$hook"
41 git init --quiet --bare "$w/$1.git"
42 sed "s|/var/lib/valley-instance/known_signers|$w/no_such_keys|" "$hook" > "$w/$1.git/hooks/valley-protect"
43 chmod +x "$w/$1.git/hooks/valley-protect"
44 printf '#!%s\necho run >> %q\nexec %q\n' "$BASH" "$w/$1.runs" "$w/$1.git/hooks/valley-protect" \
45 > "$w/$1.git/hooks/pre-receive"
46 chmod +x "$w/$1.git/hooks/pre-receive"
47}
48serve guarded
49serve open
50
51# Pushing as a principal is pushing with the tag on, as protect-e2e does.
52as() {
53 local who="$1"
54 shift
55 if [ "$who" = anonymous ]; then
56 env -u VALLEY_PRINCIPAL "$@"
57 else
58 VALLEY_PRINCIPAL="$who" "$@"
59 fi
60}
61tip() { git -C "$w/$1.git" rev-parse --verify --quiet refs/the-valley/knots || true; }
62
63git init --quiet "$w/work"
64cd "$w/work"
65git remote add origin "$w/guarded.git"
66git remote add open "$w/open.git"
67
68# ----------------------------------------------------------------------
69# The real CLI, as each principal: patrick creates the graph, klaus-env
70# adds to it. The hook admits every write.
71git config valley.principal patrick
72as patrick valley knot new outcome "Slice one lands" > "$w/out" 2> "$w/err" || fail "patrick could not create the graph"
73milestone="oc-$(printf '%s' slice-one-lands | sha256sum | cut -c1-7)"
74created="$(tip guarded)"
75[ -n "$created" ] || fail "the create did not land"
76as patrick valley knot new bug "The hook reads too much" --parent "$milestone" > "$w/out" 2> "$w/err" \
77 || fail "patrick could not update the graph"
78bug="bd-$(printf '%s' the-hook-reads-too-much | sha256sum | cut -c1-7)"
79as patrick valley knot comment "$bug" -m "Seen on a large graph." > "$w/out" 2> "$w/err" || fail "patrick could not comment"
80git config valley.principal klaus-env
81as klaus-env valley knot new outcome "Write the hook case" --parent "$milestone" --blocked-by "$bug" \
82 > "$w/out" 2> "$w/err" || fail "klaus-env could not add a knot"
83work="oc-$(printf '%s' write-the-hook-case | sha256sum | cut -c1-7)"
84as klaus-env valley knot claim "$work" --run 20261015-0058-feb0a926 > "$w/out" 2> "$w/err" || fail "klaus-env could not claim"
85as klaus-env valley knot set "$bug" status=closed > "$w/out" 2> "$w/err" || fail "klaus-env could not change patrick's knot"
86as klaus-env valley knot comment "$work" -m "Claimed." > "$w/out" 2> "$w/err" || fail "klaus-env could not comment"
87[ "$(git -C "$w/guarded.git" log --format=%cn refs/the-valley/knots | sort -u | tr '\n' ' ')" = "klaus-env patrick " ] \
88 || fail "the graph's commits are not both principals'"
89
90# The CLI, written as a principal its key does not push as, is refused by
91# the hook, and says what the hook said. A refusal is the hook's answer,
92# not a race, so the CLI pushes once and does not try again.
93git config valley.principal patrick
94: > "$w/guarded.runs"
95as klaus-env valley knot comment "$bug" -m "Forged." > "$w/out" 2> "$w/err" && fail "a write by a principal other than the pusher landed"
96grep -q 'origin refused the write' "$w/err" || fail "the CLI did not report the hook's refusal"
97grep -q 'patrick as its committer, and klaus-env is pushing it' "$w/err" || fail "the refusal does not name the committer"
98[ "$(wc -l < "$w/guarded.runs")" -eq 1 ] || fail "a refused write ran the hook $(wc -l < "$w/guarded.runs") times, not once"
99
100# ----------------------------------------------------------------------
101# Crafted commits. Each is made on a scratch index from the current tip,
102# with the edits given, by the committer given, and pushed as a principal.
103git fetch --quiet origin refs/the-valley/knots
104good="$(tip guarded)"
105# blob writes a file's content, given as command substitution leaves it:
106# without the one newline every canonical file ends with.
107blob() { printf '%s\n' "$1" | git hash-object -w --stdin; }
108# craft <parent|-> <committer> <message|-> <edit…>, where each edit is
109# +PATH=CONTENT, -PATH, or MODE:PATH=OID.
110craft() {
111 local parent="$1" committer="$2" message="$3" edit tree
112 shift 3
113 export GIT_INDEX_FILE="$w/scratch.index"
114 rm -f "$GIT_INDEX_FILE"
115 if [ "$parent" = - ]; then git read-tree --empty; else git read-tree "$parent"; fi
116 for edit in "$@"; do
117 case "$edit" in
118 +*) git update-index --add --cacheinfo "100644,$(blob "${edit#*=}"),$(p="${edit#+}" && echo "${p%%=*}")" ;;
119 -*) git update-index --force-remove -- "${edit#-}" ;;
120 *:*) git update-index --add --cacheinfo "${edit%%:*},${edit#*=},$(p="${edit#*:}" && echo "${p%%=*}")" ;;
121 esac
122 done
123 tree="$(git write-tree)"
124 unset GIT_INDEX_FILE
125 if [ "$message" = - ]; then message="$(printf 'crafted\n\nValley-Op: %016x' "$RANDOM$RANDOM")"; fi
126 local parents=()
127 if [ "$parent" != - ]; then parents=(-p "$parent"); fi
128 GIT_COMMITTER_NAME="$committer" git commit-tree "$tree" "${parents[@]}" -m "$message"
129}
130# refused <what> <ERE> <who> <git push args…>: the push fails, saying
131# something matching the pattern, and the ref stays where it was.
132refused() {
133 local what="$1" says="$2" who="$3" before
134 shift 3
135 before="$(tip guarded)"
136 if as "$who" git push --quiet "$@" 2> "$w/err"; then fail "$what was admitted"; fi
137 grep -qE -- "$says" "$w/err" || fail "$what was refused without saying $says"
138 [ "$(tip guarded)" = "$before" ] || fail "$what moved the ref"
139}
140push_graph() { echo "$1:refs/the-valley/knots"; }
141
142knot_path() { git ls-tree -r --name-only "$good" | grep -m1 -- "/$1-"; }
143bug_path="$(knot_path "$bug")"
144work_path="$(knot_path "$work")"
145comment_path="$(git ls-tree -r --name-only "$good" -- "comments/$bug/" | head -n1)"
146bug_text="$(git cat-file blob "$good:$bug_path")"
147work_text="$(git cat-file blob "$good:$work_path")"
148stamp="20261015T120000Z"
149comment() { printf -- '---\nauthor: "%s"\n---\n\n%s\n' "$1" "$2"; }
150idea() { # idea <title> <slug> <creator> [body]
151 local id
152 id="ida-$(printf '%s' "$2" | sha256sum | cut -c1-7)"
153 printf -- '---\ntype: idea\nid: %s\nstatus: raw\ntitle: "%s"\ncreated: 2026-10-15\ncreated_by: "%s"\n---\n' "$id" "$1" "$3"
154 if [ -n "${4:-}" ]; then printf '\n%s\n' "$4"; fi
155}
156idea_path() { echo "ideas/ida-$(printf '%s' "$1" | sha256sum | cut -c1-7)-$1.md"; }
157
158# A well-formed next commit is admitted, so every refusal below is the
159# hook's answer to the one thing each breaks.
160next="$(craft "$good" patrick - "+$(idea_path a-crafted-idea)=$(idea "A crafted idea" a-crafted-idea patrick)")"
161cp /dev/null "$w/err"
162
163# Who: an untagged key is no principal.
164refused "an untagged key's write" 'untagged key.*named principal' anonymous origin "$(push_graph "$next")"
165
166# Shape: one commit whose only parent is the old tip.
167older="$(git rev-parse "$good~1")"
168rewind="$(craft "$older" patrick - "+$(idea_path rewound)=$(idea Rewound rewound patrick)")"
169refused "a non-fast-forward update" 'only parent is the tip it replaces' patrick --force origin "$(push_graph "$rewind")"
170side="$(craft "$older" patrick - "+$(idea_path side)=$(idea Side side patrick)")"
171merge="$(GIT_COMMITTER_NAME=patrick git commit-tree "$next^{tree}" -p "$good" -p "$side" -m "$(printf 'merge\n\nValley-Op: 0123456789abcdef')")"
172refused "a merge" 'only parent is the tip it replaces' patrick origin "$(push_graph "$merge")"
173two="$(craft "$next" patrick - "+$(idea_path second)=$(idea Second second patrick)")"
174refused "two commits at once" 'only parent is the tip it replaces' patrick origin "$(push_graph "$two")"
175git commit --quiet --allow-empty -m unrelated
176stranger="$(craft "$(git rev-parse HEAD)" patrick - "+$(idea_path stranger)=$(idea Stranger stranger patrick)")"
177refused "a parent that is not the old tip" 'only parent is the tip it replaces' patrick --force origin "$(push_graph "$stranger")"
178tagged="$(printf 'object %s\ntype commit\ntag t\ntagger t <t> 0 +0000\n\nt\n' "$next" | git mktag)"
179refused "a tag object as the tip" 'points at a tag' patrick origin "$tagged:refs/the-valley/knots"
180
181# Every commit: committed by the pusher, and carrying its operation.
182forged="$(craft "$good" klaus-env - "+$(idea_path forged)=$(idea Forged forged klaus-env)")"
183refused "a commit by another committer" 'names klaus-env as its committer, and patrick is pushing it' patrick origin "$(push_graph "$forged")"
184untrailed="$(craft "$good" patrick "no trailer" "+$(idea_path untrailed)=$(idea Untrailed untrailed patrick)")"
185refused "a commit without its trailer" 'Valley-Op' patrick origin "$(push_graph "$untrailed")"
186
187# Transitions: comments stay, knots stay, and what never changes does not.
188refused "a deleted comment" 'a comment is never deleted' patrick origin "$(push_graph "$(craft "$good" patrick - "-$comment_path")")"
189refused "a changed comment" 'stays exactly as it was written' patrick origin \
190 "$(push_graph "$(craft "$good" patrick - "+$comment_path=$(comment patrick 'Rewritten.')")")"
191refused "a deleted knot" 'a knot is never deleted' patrick origin "$(push_graph "$(craft "$good" patrick - "-$bug_path")")"
192refused "a changed id" 'filename says id' patrick origin \
193 "$(push_graph "$(craft "$good" patrick - "+$bug_path=${bug_text/id: $bug/id: bd-0000000}")")"
194refused "a changed type" "id is \"$bug\", and an outcome's id is oc-" patrick origin \
195 "$(push_graph "$(craft "$good" patrick - "+$bug_path=${bug_text/type: bug/type: outcome}")")"
196refused "a changed created_by" 'changes created_by from "patrick" to "klaus-env"' klaus-env origin \
197 "$(push_graph "$(craft "$good" klaus-env - "+$bug_path=${bug_text/created_by: \"patrick\"/created_by: \"klaus-env\"}")")"
198
199# Provenance: a new comment's author and a new knot's creator are the
200# pusher.
201refused "a comment with a forged author" 'names klaus-env as its author, and patrick is pushing it' patrick origin \
202 "$(push_graph "$(craft "$good" patrick - "+comments/$bug/$stamp-0123456789abcdef.md=$(comment klaus-env 'Not mine.')")")"
203refused "a knot with a forged creator" 'names klaus-env in created_by, and patrick is pushing it' patrick origin \
204 "$(push_graph "$(craft "$good" patrick - "+$(idea_path forged)=$(idea Forged forged klaus-env)")")"
205
206# Layout: plain files, in the layout, within the bounds.
207knot_blob="$(blob "$(idea Linked linked patrick)")"
208refused "a symlink" 'mode 120000' patrick origin "$(push_graph "$(craft "$good" patrick - "120000:$(idea_path linked)=$knot_blob")")"
209refused "an executable" 'mode 100755' patrick origin "$(push_graph "$(craft "$good" patrick - "100755:$(idea_path linked)=$knot_blob")")"
210refused "a file outside the layout" 'outside the graph.s layout' patrick origin "$(push_graph "$(craft "$good" patrick - "+README.md=hello")")"
211refused "a directory outside the layout" 'no directory a graph holds' patrick origin \
212 "$(push_graph "$(craft "$good" patrick - "+tasks/$(basename "$(idea_path tasked)")=$(idea Tasked tasked patrick)")")"
213refused "a non-canonical knot" 'not in canonical form' patrick origin \
214 "$(push_graph "$(craft "$good" patrick - "+$(idea_path loose)=$(idea Loose loose patrick | sed 's/^title: "Loose"/title: Loose/')")")"
215big="$(head -c 70000 /dev/zero | tr '\0' x)"
216refused "an oversized knot" 'more than the 65536' patrick origin \
217 "$(push_graph "$(craft "$good" patrick - "+$(idea_path big)=$(idea Big big patrick "$big")")")"
218
219# A tree that names one subtree many times over is refused at a bound,
220# before it is walked.
221leaf_lines=""
222for i in $(seq 1 64); do
223 leaf_lines+="$(printf '100644 blob %s\t%s-%016x.md' "$(blob "$(comment patrick x)")" "$stamp" "$i")"$'\n'
224done
225leaf="$(printf '%s' "$leaf_lines" | git mktree)"
226comments_tree="$(for i in $(seq 1 1000); do printf '040000 tree %s\toc-%07x\n' "$leaf" "$i"; done | git mktree)"
227root="$(git ls-tree "$good" | grep -v $'\tcomments$' | { cat; printf '040000 tree %s\tcomments\n' "$comments_tree"; } | git mktree)"
228bomb="$(GIT_COMMITTER_NAME=patrick git commit-tree "$root" -p "$good" -m "$(printf 'bomb\n\nValley-Op: 0123456789abcdef')")"
229start="$(date +%s)"
230refused "a shared-subtree bomb" 'entries in all' patrick origin "$(push_graph "$bomb")"
231[ $(($(date +%s) - start)) -lt 10 ] || fail "the bomb took more than 10 seconds to refuse"
232
233# Trees written by hand. A tree object can hold any bytes, and the host
234# runs no fsck on what a push brings, so the hook refuses every entry git
235# does not write. A name with a slash in it would be joined into a path
236# that is no directory's, and a directory in the mode 040000, which git
237# reads as a tree, would be a subtree the hook never walked. mktree and
238# update-index refuse to write such trees, so raw_tree writes the entries
239# it is given, each a mode, a name and an object, in the order given.
240raw_tree() {
241 local f="$w/raw-tree"
242 : > "$f"
243 while [ "$#" -gt 0 ]; do
244 printf '%s %s\0' "$1" "$2" >> "$f"
245 # shellcheck disable=SC2059 # the format is the object id as \x escapes
246 printf "$(printf '%s' "$3" | sed 's/../\\x&/g')" >> "$f"
247 shift 3
248 done
249 git hash-object -t tree --literally -w "$f"
250}
251# entries_of <tree> [-r]: the tree's entries, or with -r every file below
252# it at its full path, as raw_tree takes them.
253entries_of() {
254 git ls-tree "${@:2}" "$1" | while IFS=$' \t' read -r mode _ oid name; do
255 printf '%s\n%s\n%s\n' "${mode#0}" "$name" "$oid"
256 done
257}
258on_good() { GIT_COMMITTER_NAME=patrick git commit-tree "$1" -p "$good" -m "$(printf 'raw\n\nValley-Op: %016x' "$RANDOM$RANDOM")"; }
259mapfile -t root_entries < <(entries_of "$good")
260mapfile -t flat_entries < <(entries_of "$good" -r)
261named_idea="$(blob "$(idea Named named patrick)")"
262refused "a root file named ideas/<id>-<slug>.md" 'named "ideas/ida-[0-9a-f]{7}-named\.md", and git writes no name with a slash' \
263 patrick origin "$(push_graph "$(on_good "$(raw_tree "${root_entries[@]}" 100644 "$(idea_path named)" "$named_idea")")")"
264refused "every knot and comment flattened into names with slashes" 'git writes no name with a slash' \
265 patrick origin "$(push_graph "$(on_good "$(raw_tree "${flat_entries[@]}")")")"
266# comments/, in the mode 040000, naming a subtree with a forged comment.
267hidden="$(raw_tree 40000 "$bug" "$(raw_tree 100644 "$stamp-0123456789abcdef.md" "$(blob "$(comment klaus-env 'Never read.')")")")"
268hiding=()
269for ((i = 0; i < ${#root_entries[@]}; i += 3)); do
270 if [ "${root_entries[i + 1]}" = comments ]; then
271 hiding+=(040000 comments "$hidden")
272 else
273 hiding+=("${root_entries[@]:i:3}")
274 fi
275done
276refused "a directory in the mode 040000" 'holds "comments" with mode 040000, and git writes only the modes' \
277 patrick origin "$(push_graph "$(on_good "$(raw_tree "${hiding[@]}")")")"
278for name in . ..; do
279 refused "an entry named $name" "holds an entry named \"${name//./\\.}\", which git does not write" \
280 patrick origin "$(push_graph "$(on_good "$(raw_tree "${root_entries[@]}" 100644 "$name" "$named_idea")")")"
281done
282# git push walks every tree it sends, and will not send one that holds an
283# entry with an empty name. So the update is sent to receive-pack as any
284# client could send it: the command, then a pack of exactly the objects
285# named. git refuses the update itself, since its own walk cannot read the
286# tree, and the hook, which git still runs, refuses it too.
287nameless_tree="$(raw_tree "${root_entries[@]}" 100644 "" "$named_idea")"
288nameless="$(on_good "$nameless_tree")"
289update_line="$good $nameless refs/the-valley/knots"
290capabilities=report-status
291{
292 printf '%04x%s\0%s\n' $((4 + ${#update_line} + 1 + ${#capabilities} + 1)) "$update_line" "$capabilities"
293 printf 0000
294 printf '%s\n' "$nameless" "$nameless_tree" "$named_idea" | git pack-objects --quiet --stdout
295} > "$w/raw.request"
296as patrick git receive-pack "$w/guarded.git" < "$w/raw.request" > "$w/raw.report" 2> "$w/err" || true
297grep -q 'ng refs/the-valley/knots' "$w/raw.report" || fail "an entry with an empty name was admitted"
298grep -q 'its tree: it holds an entry with an empty name' "$w/err" || fail "the hook did not refuse an entry with an empty name"
299[ "$(tip guarded)" = "$good" ] || fail "an entry with an empty name moved the ref"
300
301# Edges: every edge resolves, a parent is an outcome, and no edge forms a
302# cycle. A cycle is crafted, since the CLI refuses to write one.
303refused "a blocker that is no knot" 'is no knot in the graph' patrick origin \
304 "$(push_graph "$(craft "$good" patrick - "+$work_path=${work_text/blocked_by: \[$bug\]/blocked_by: [$bug, oc-0000000]}")")"
305loop_bug="${bug_text/blocked_by: \[\]/blocked_by: [$work]}"
306[ "$loop_bug" != "$bug_text" ] || fail "the bug's blocked_by could not be edited"
307refused "a cycle" 'blocked_by edges form a cycle' patrick origin "$(push_graph "$(craft "$good" patrick - "+$bug_path=$loop_bug")")"
308
309# The ref: never deleted, and matched exactly.
310refused "a deletion of the graph" 'never deleted' patrick origin --delete refs/the-valley/knots
311refused "a ref that only begins with the graph's name" 'written only by the machinery' patrick origin "$next:refs/the-valley/knots-x"
312refused "a ref below the graph's name" 'written only by the machinery' patrick origin "$next:refs/the-valley/knots/x"
313
314# Create: one commit with no parent, on a project that has no graph yet.
315first="$(craft - patrick - "+$(idea_path first)=$(idea First first patrick)")"
316child="$(craft "$first" patrick - "+$(idea_path second)=$(idea Second second patrick)")"
317if as patrick git push --quiet open "$(push_graph "$child")" 2> "$w/err"; then fail "a create with a parent was admitted"; fi
318grep -q 'creates the graph has no parent' "$w/err" || fail "a create with a parent was refused for another reason"
319[ -z "$(tip open)" ] || fail "a refused create made the ref"
320as patrick git push --quiet open "$(push_graph "$first")" 2> "$w/err" || fail "a parentless create was refused"
321as patrick git push --quiet open "$(push_graph "$child")" 2> "$w/err" || fail "the update after it was refused"
322[ "$(tip open)" = "$child" ] || fail "the open project's graph is not at the update"
323
324# A push that carries a valid graph update with an invalid write to
325# another namespace is refused whole, atomic or not: a change record that
326# is no record, an attestation no key here accepts, and an integration
327# request from a principal without the request grant.
328record="refs/the-valley/changes/main/$(printf 'k%.0s' $(seq 1 32))"
329attestation="refs/the-valley/attestations/$(printf '%064d' 0)/00000000"
330request="refs/the-valley/integration-requests/main/topic"
331for other in "$next:$record" "$next:$attestation" "HEAD:$request"; do
332 for mode in --atomic --no-atomic; do
333 refused "a graph update beside $other ($mode)" 'refs/the-valley/(changes|attestations|integration-requests)/' \
334 patrick "$mode" origin "$(push_graph "$next")" "$other"
335 if git -C "$w/guarded.git" rev-parse --verify --quiet "${other#*:}" > /dev/null; then fail "$other landed"; fi
336 done
337done
338
339# The well-formed commit itself is admitted.
340as patrick git push --quiet origin "$(push_graph "$next")" 2> "$w/err" || fail "a well-formed update was refused"
341[ "$(tip guarded)" = "$next" ] || fail "the well-formed update did not land"
342touch "$out"