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