gunk-dev/the-valley

knots-e2e.sh

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