| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | git config --global advice.detachedHead false |
| 10 | cd "$TMPDIR" || exit 1 |
| 11 | |
| 12 | # The keys this host accepts evidence from, as a controller would: the |
| 13 | # signer's, and nobody else's. |
| 14 | ssh-keygen -q -t ed25519 -N "" -C signer -f "$TMPDIR/signer" |
| 15 | ssh-keygen -q -t ed25519 -N "" -C other -f "$TMPDIR/other" |
| 16 | attest key --key "$TMPDIR/signer" --name signer.valley.invalid/attestations > "$TMPDIR/known_signers" |
| 17 | |
| 18 | # A bare repo wired as valley-init wires it, with the hook followed from |
| 19 | # the rendered init script. Every project the host serves gets the hook, |
| 20 | # protected or not. The one edit is where the verifier keys are, which a |
| 21 | # host keeps under /var/lib and this sandbox cannot; open's points at no |
| 22 | # file at all, as a host that names no keys would. |
| 23 | serve() { |
| 24 | local hook keys="$2" |
| 25 | hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)" |
| 26 | test -x "$hook" |
| 27 | grep -qF -- '--known-signers /var/lib/valley-instance/known_signers' "$hook" |
| 28 | git init --quiet --bare "$1.git" |
| 29 | sed "s|/var/lib/valley-instance/known_signers|$keys|" "$hook" > "$1.git/hooks/pre-receive" |
| 30 | chmod +x "$1.git/hooks/pre-receive" |
| 31 | } |
| 32 | serve sealed "$TMPDIR/known_signers" |
| 33 | serve guarded "$TMPDIR/known_signers" |
| 34 | serve released "$TMPDIR/known_signers" |
| 35 | serve open "$TMPDIR/no_such_keys" |
| 36 | |
| 37 | # Pushing as a principal is pushing with the tag on; pushing |
| 38 | # with an untagged key is pushing with it off. That sshd sets |
| 39 | # the tag from a key's entry, and nothing else does, is what |
| 40 | # ssh-e2e checks against a real sshd. |
| 41 | as() { |
| 42 | local who="$1" |
| 43 | shift |
| 44 | if [ "$who" = anonymous ]; then |
| 45 | env -u VALLEY_PRINCIPAL "$@" |
| 46 | else |
| 47 | VALLEY_PRINCIPAL="$who" "$@" |
| 48 | fi |
| 49 | } |
| 50 | refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; } |
| 51 | has_ref() { |
| 52 | if ! refs "$1" | grep -qx "$2"; then |
| 53 | echo "protect-e2e: $1 has no $2" >&2 |
| 54 | refs "$1" >&2 |
| 55 | exit 1 |
| 56 | fi |
| 57 | } |
| 58 | lacks_ref() { |
| 59 | if refs "$1" | grep -qx "$2"; then |
| 60 | echo "protect-e2e: $1 kept $2 through a rejected push" >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | } |
| 64 | # refused <file> <what> <command…> — the push must fail. |
| 65 | refused() { |
| 66 | local err="$1" what="$2" |
| 67 | shift 2 |
| 68 | if "$@" 2> "$err"; then |
| 69 | echo "protect-e2e: $what" >&2 |
| 70 | exit 1 |
| 71 | fi |
| 72 | } |
| 73 | |
| 74 | git init --quiet work |
| 75 | cd work || exit 1 |
| 76 | git commit --quiet --allow-empty -m one |
| 77 | for name in sealed guarded released open; do |
| 78 | git remote add "$name" "$TMPDIR/$name.git" |
| 79 | done |
| 80 | |
| 81 | # ---------------------------------------------------------------------- |
| 82 | # Protected refs. |
| 83 | |
| 84 | # A non-writer cannot create a protected ref, and the |
| 85 | # rejection says who was pushing, what they were writing, |
| 86 | # and who may. |
| 87 | refused denied.err "a non-writer wrote a protected ref" \ |
| 88 | as contributor git push --quiet guarded main |
| 89 | grep -q 'contributor' denied.err |
| 90 | grep -q 'refs/heads/main' denied.err |
| 91 | grep -q 'writers: integrator' denied.err |
| 92 | lacks_ref guarded refs/heads/main |
| 93 | |
| 94 | # An untagged key is nobody, and nobody is not a writer. |
| 95 | refused untagged.err "an untagged key wrote a protected ref" \ |
| 96 | as anonymous git push --quiet guarded main |
| 97 | grep -q '<untagged key>' untagged.err |
| 98 | lacks_ref guarded refs/heads/main |
| 99 | |
| 100 | # Topic branches are open to the same non-writer, including a |
| 101 | # branch some other project's declaration protects. |
| 102 | git branch idea/one |
| 103 | git tag v1 |
| 104 | git branch release/1.0 |
| 105 | as contributor git push --quiet guarded idea/one release/1.0 |
| 106 | has_ref guarded refs/heads/idea/one |
| 107 | has_ref guarded refs/heads/release/1.0 |
| 108 | |
| 109 | # The same ref is protected where a declaration says so: the |
| 110 | # glob in released's set matches it. |
| 111 | refused glob.err "a glob in the protected set did not match" \ |
| 112 | as contributor git push --quiet released release/1.0 |
| 113 | grep -q 'refs/heads/release/1.0' glob.err |
| 114 | lacks_ref released refs/heads/release/1.0 |
| 115 | |
| 116 | # The writer creates it, and updates it. |
| 117 | as integrator git push --quiet guarded main |
| 118 | has_ref guarded refs/heads/main |
| 119 | git commit --quiet --allow-empty -m two |
| 120 | as integrator git push --quiet guarded main |
| 121 | second="$(git rev-parse HEAD)" |
| 122 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 123 | |
| 124 | # A delete is a write. |
| 125 | refused deleted.err "a non-writer deleted a protected ref" \ |
| 126 | as contributor git push --quiet --delete guarded main |
| 127 | grep -q 'refs/heads/main' deleted.err |
| 128 | has_ref guarded refs/heads/main |
| 129 | |
| 130 | # pre-receive answers for the whole push: an open ref |
| 131 | # travelling with a protected one lands only if the |
| 132 | # protected one does, atomic push or not. |
| 133 | git commit --quiet --allow-empty -m three |
| 134 | git branch idea/two |
| 135 | for mode in --atomic --no-atomic; do |
| 136 | refused mixed.err "a protected ref rode in on a $mode push" \ |
| 137 | as contributor git push --quiet "$mode" guarded idea/two main |
| 138 | grep -q 'refs/heads/main' mixed.err |
| 139 | lacks_ref guarded refs/heads/idea/two |
| 140 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 141 | done |
| 142 | |
| 143 | # The norm, on a project that declares no writer at all: the |
| 144 | # protected ref takes no push from anyone. Even the principal |
| 145 | # that writes guarded's main is refused here, and told how a |
| 146 | # change lands instead of being told who may. |
| 147 | refused sealed.err "a protected ref with no declared writer took a push" \ |
| 148 | as integrator git push --quiet sealed main |
| 149 | grep -q 'integrator' sealed.err |
| 150 | grep -q 'no writer is declared' sealed.err |
| 151 | grep -q 'integration request' sealed.err |
| 152 | lacks_ref sealed refs/heads/main |
| 153 | |
| 154 | # A project with no protection block protects nothing: its |
| 155 | # main is a branch like any other. Everything else the policy |
| 156 | # says still holds there, as the sections below show. |
| 157 | as contributor git push --quiet open main |
| 158 | has_ref open refs/heads/main |
| 159 | |
| 160 | # ---------------------------------------------------------------------- |
| 161 | # A symbolic ref takes no push. One cannot be pushed, but one |
| 162 | # can exist on the server, and git would write the ref it |
| 163 | # points at: here, a topic-branch name standing for the |
| 164 | # protected main. Its writer is refused too. |
| 165 | git -C "$TMPDIR/guarded.git" symbolic-ref refs/heads/alias refs/heads/main |
| 166 | for who in contributor integrator; do |
| 167 | refused symref.err "$who wrote through a symbolic ref" \ |
| 168 | as "$who" git push --quiet --force guarded HEAD:refs/heads/alias |
| 169 | grep -q 'symbolic ref' symref.err |
| 170 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 171 | done |
| 172 | git -C "$TMPDIR/guarded.git" symbolic-ref --delete refs/heads/alias |
| 173 | |
| 174 | # ---------------------------------------------------------------------- |
| 175 | # Namespaces no push may write, on a protected project and an |
| 176 | # unprotected one, by the protected ref's own writer. |
| 177 | |
| 178 | # Tags open only through a named grant, and guarded has none. |
| 179 | refused tag.err "a tag was pushed that no grant opens" \ |
| 180 | as integrator git push --quiet guarded v1 |
| 181 | grep -q 'refs/tags/ opens only through a named grant' tag.err |
| 182 | grep -q 'no grant of guarded' tag.err |
| 183 | lacks_ref guarded refs/tags/v1 |
| 184 | |
| 185 | # Notes, replacement refs and the valley's own namespace are |
| 186 | # closed outright, wherever they are pushed and by whom. |
| 187 | git notes add -m "a note" HEAD |
| 188 | git commit --quiet --allow-empty -m decoy |
| 189 | replaced="refs/replace/$(git rev-parse HEAD~1)" |
| 190 | git update-ref "$replaced" HEAD |
| 191 | outcome=refs/the-valley/integration-outcomes/main/idea |
| 192 | for project in guarded open; do |
| 193 | for who in contributor integrator; do |
| 194 | refused notes.err "$who pushed a notes ref to $project" \ |
| 195 | as "$who" git push --quiet "$project" refs/notes/commits |
| 196 | grep -q 'refs/notes/ is closed to every push' notes.err |
| 197 | refused replace.err "$who pushed a replacement ref to $project" \ |
| 198 | as "$who" git push --quiet "$project" "$replaced:$replaced" |
| 199 | grep -q 'refs/replace/ is closed to every push' replace.err |
| 200 | refused outcome.err "$who pushed an integration outcome to $project" \ |
| 201 | as "$who" git push --quiet "$project" "HEAD:$outcome" |
| 202 | grep -q 'refs/the-valley/integration-outcomes/ is written only by the machinery' outcome.err |
| 203 | done |
| 204 | lacks_ref "$project" refs/notes/commits |
| 205 | lacks_ref "$project" "$replaced" |
| 206 | lacks_ref "$project" "$outcome" |
| 207 | done |
| 208 | git update-ref -d "$replaced" |
| 209 | git reset --quiet --hard HEAD~1 |
| 210 | |
| 211 | # A grant opens what it names to whom it names, and nothing |
| 212 | # more. released grants release tags to the integrator |
| 213 | # principal: that principal pushes one, nobody else does, and |
| 214 | # a tag outside the pattern stays closed to it too. |
| 215 | git tag release/1.0 |
| 216 | tag=refs/tags/release/1.0 |
| 217 | refused opened.err "a grant opened a ref to a principal it does not name" \ |
| 218 | as contributor git push --quiet released "$tag:$tag" |
| 219 | grep -q 'the grant release-tags of released opens this ref to integrator only' opened.err |
| 220 | lacks_ref released "$tag" |
| 221 | as integrator git push --quiet released "$tag:$tag" |
| 222 | has_ref released "$tag" |
| 223 | refused unopened.err "a grant opened a ref outside its pattern" \ |
| 224 | as integrator git push --quiet released v1 |
| 225 | grep -q 'no grant of released' unopened.err |
| 226 | lacks_ref released refs/tags/v1 |
| 227 | |
| 228 | # ---------------------------------------------------------------------- |
| 229 | # Integration requests take writes only from a holder of the |
| 230 | # request grant. Pushing topic branches does not make a |
| 231 | # principal one, and neither does being a declared writer. |
| 232 | request=refs/the-valley/integration-requests/main/idea |
| 233 | for who in contributor integrator anonymous; do |
| 234 | refused request.err "$who filed a request without the request grant" \ |
| 235 | as "$who" git push --quiet guarded "idea/one:$request" |
| 236 | grep -q 'refs/the-valley/integration-requests/' request.err |
| 237 | grep -q 'request grant' request.err |
| 238 | lacks_ref guarded "$request" |
| 239 | done |
| 240 | |
| 241 | # The holder files one, replaces it with a head that does not |
| 242 | # descend from the first, and withdraws it. In between, a |
| 243 | # principal without the grant can neither replace nor |
| 244 | # withdraw what the holder filed. |
| 245 | git commit --quiet --allow-empty -m "first ask" |
| 246 | as requester git push --quiet guarded "HEAD:$request" |
| 247 | filed="$(git rev-parse HEAD)" |
| 248 | git reset --quiet --hard HEAD~1 |
| 249 | git commit --quiet --allow-empty -m "second ask" |
| 250 | refused replaced.err "a principal without the grant replaced a request" \ |
| 251 | as contributor git push --quiet --force guarded "HEAD:$request" |
| 252 | grep -q 'request grant' replaced.err |
| 253 | refused withdrawn.err "a principal without the grant withdrew a request" \ |
| 254 | as contributor git push --quiet --delete guarded "$request" |
| 255 | grep -q 'request grant' withdrawn.err |
| 256 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$filed" ] |
| 257 | as requester git push --quiet --force guarded "HEAD:$request" |
| 258 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ] |
| 259 | as requester git push --quiet --delete guarded "$request" |
| 260 | lacks_ref guarded "$request" |
| 261 | git reset --quiet --hard HEAD~1 |
| 262 | |
| 263 | # Where protection covers the request namespace too, each rule |
| 264 | # still applies and neither stands in for the other: released's |
| 265 | # writer lacks the grant, and the holder of the grant is not |
| 266 | # released's writer. |
| 267 | refused overlap-writer.err "a writer filed a request without the grant" \ |
| 268 | as integrator git push --quiet released "idea/one:$request" |
| 269 | grep -q 'request grant' overlap-writer.err |
| 270 | refused overlap-holder.err "a request holder wrote a protected request ref" \ |
| 271 | as requester git push --quiet released "idea/one:$request" |
| 272 | grep -q 'a protected ref of released' overlap-holder.err |
| 273 | lacks_ref released "$request" |
| 274 | |
| 275 | # The grant is the same on a project that declares no |
| 276 | # protection at all. |
| 277 | refused open-request.err "a request was filed on an unprotected project without the grant" \ |
| 278 | as contributor git push --quiet open "idea/one:$request" |
| 279 | as requester git push --quiet open "idea/one:$request" |
| 280 | has_ref open "$request" |
| 281 | |
| 282 | # ---------------------------------------------------------------------- |
| 283 | # Attestation refs: anyone may create one, if it is what its |
| 284 | # name says, and it opens under the keys this host accepts. |
| 285 | attest run --key "$TMPDIR/signer" --name signer.valley.invalid/attestations \ |
| 286 | --command ok=true > run.out |
| 287 | att="$(sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' run.out)" |
| 288 | digest="$(echo "$att" | cut -d/ -f4)" |
| 289 | keyhash="$(echo "$att" | cut -d/ -f5)" |
| 290 | as contributor git push --quiet guarded "$att:$att" |
| 291 | has_ref guarded "$att" |
| 292 | landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" |
| 293 | |
| 294 | # … and nobody may rewrite or drop one, writer or not. |
| 295 | git update-ref "$att" "$(git rev-parse HEAD^{tree})" |
| 296 | refused rewritten.err "an attestation ref was rewritten" \ |
| 297 | as contributor git push --quiet --force guarded "$att:$att" |
| 298 | grep -q 'create-only' rewritten.err |
| 299 | grep -q "$att" rewritten.err |
| 300 | refused rewritten-by-writer.err "a writer rewrote an attestation ref" \ |
| 301 | as integrator git push --quiet --force guarded "$att:$att" |
| 302 | grep -q 'create-only' rewritten-by-writer.err |
| 303 | refused dropped.err "an attestation ref was deleted" \ |
| 304 | as integrator git push --quiet --delete guarded "$att" |
| 305 | grep -q 'create-only' dropped.err |
| 306 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ] |
| 307 | git update-ref "$att" "$landed" |
| 308 | |
| 309 | # Occupation: a name in a create-only namespace is held for |
| 310 | # good by whoever creates it first, so a name may only hold |
| 311 | # what it says. Each of these would keep the real signer's |
| 312 | # evidence out, and each is refused on a project that has |
| 313 | # none yet. |
| 314 | other_hash="$(attest key --key "$TMPDIR/other" --name other.valley.invalid/attestations | cut -d+ -f2)" |
| 315 | occupy() { |
| 316 | local name="$1" target="$2" err="$3" why="$4" |
| 317 | refused "$err" "an attestation ref took a name it does not hold: $why" \ |
| 318 | as contributor git push --quiet sealed "$target:refs/the-valley/attestations/$name" |
| 319 | lacks_ref sealed "refs/the-valley/attestations/$name" |
| 320 | } |
| 321 | # A ref named for the digest alone would block every ref |
| 322 | # beneath it, and a name that is not hex is no signer's. |
| 323 | occupy "$digest" "$landed" shape.err "the digest alone" |
| 324 | grep -q '<digest>/<key hash>' shape.err |
| 325 | occupy "$digest/$keyhash/extra" "$landed" deep.err "a segment too many" |
| 326 | occupy "$digest/NOTAHASH" "$landed" nothex.err "a key hash that is not hex" |
| 327 | # The real signer's note, under another signer's key hash. |
| 328 | occupy "$digest/$other_hash" "$landed" squat.err "another signer's slot" |
| 329 | grep -q "no signature under the key hash $other_hash" squat.err |
| 330 | # The real signer's note, under another tree's digest. |
| 331 | elsewhere="$(printf '%064d' 7)" |
| 332 | occupy "$elsewhere/$keyhash" "$landed" subject.err "another tree's slot" |
| 333 | grep -q 'is about the tree' subject.err |
| 334 | # Something that is not a tree of notes at all. |
| 335 | occupy "$elsewhere/$keyhash" HEAD commit.err "a commit" |
| 336 | grep -q 'points at a commit' commit.err |
| 337 | # The real signer's note with a line appended that no verifier |
| 338 | # would read: the signature in it is still genuine, and the |
| 339 | # note is still unusable, so it may not take the name either. |
| 340 | git cat-file blob "$landed:ok/statement.note" > genuine.note |
| 341 | { cat genuine.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note |
| 342 | blob="$(git hash-object -w poisoned.note)" |
| 343 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git mktree)" |
| 344 | poisoned="$(printf '040000 tree %s\tok\n' "$sub" | git mktree)" |
| 345 | occupy "$digest/$keyhash" "$poisoned" poisoned.err "a poisoned note" |
| 346 | grep -q 'does not open' poisoned.err |
| 347 | # A tree naming one subtree over and over, which describes more |
| 348 | # paths than any walk could visit, is refused at a bound. |
| 349 | wide="$sub" |
| 350 | for _ in 1 2; do |
| 351 | wide="$(for i in $(seq -w 1 64); do printf '040000 tree %s\tt%s\n' "$wide" "$i"; done | git mktree)" |
| 352 | done |
| 353 | occupy "$digest/$keyhash" "$wide" wide.err "a tree of shared subtrees" |
| 354 | grep -q 'entries in all' wide.err |
| 355 | # The ref the real evidence wants is still free, and takes it. |
| 356 | as anonymous git push --quiet sealed "$att:$att" |
| 357 | has_ref sealed "$att" |
| 358 | |
| 359 | # A host that names no keys accepts no attestation at all. |
| 360 | refused nokeys.err "an attestation was accepted where no key is named" \ |
| 361 | as anonymous git push --quiet open "$att:$att" |
| 362 | grep -q 'names no keys it accepts evidence from' nokeys.err |
| 363 | lacks_ref open "$att" |
| 364 | |
| 365 | # ---------------------------------------------------------------------- |
| 366 | # A hook a project composes after the policy runs once the |
| 367 | # policy accepts, and can only refuse more: released's refuses |
| 368 | # branches under frozen/. |
| 369 | git branch frozen/one |
| 370 | refused frozen.err "the composed hook did not run" \ |
| 371 | as contributor git push --quiet released frozen/one |
| 372 | grep -q 'released: refs/heads/frozen/one is frozen' frozen.err |
| 373 | lacks_ref released refs/heads/frozen/one |
| 374 | as contributor git push --quiet released idea/one |
| 375 | has_ref released refs/heads/idea/one |
| 376 | |
| 377 | # ---------------------------------------------------------------------- |
| 378 | # A protected project's first main. No push may create it — sealed's main |
| 379 | # has refused every one above — and the integrator lands only onto a main |
| 380 | # that exists. So it is seeded on the host, by the git user, from a source |
| 381 | # whose commit the operator has checked: fetched, compared with the commit |
| 382 | # expected, and created only if no main exists yet. A local write is not a |
| 383 | # push, so no hook is asked. |
| 384 | expected="$(git rev-parse HEAD)" |
| 385 | git -C "$TMPDIR/sealed.git" fetch --quiet --no-tags "$TMPDIR/work" HEAD |
| 386 | [ "$(git -C "$TMPDIR/sealed.git" rev-parse FETCH_HEAD)" = "$expected" ] |
| 387 | git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected" "" |
| 388 | has_ref sealed refs/heads/main |
| 389 | # The empty old value makes the seed a creation: over a main that exists, |
| 390 | # it refuses. |
| 391 | if git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected~1" "" 2> /dev/null; then |
| 392 | echo "protect-e2e: a seed overwrote a main that existed" >&2 |
| 393 | exit 1 |
| 394 | fi |
| 395 | [ "$(git -C "$TMPDIR/sealed.git" rev-parse main)" = "$expected" ] |
| 396 | |
| 397 | touch "$out" |