| 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 | |
| 10 | # Throwaway keys in a scratch directory. The signer is a |
| 11 | # parameter: provisioning a host identity is deployment, and |
| 12 | # nothing here reads or writes anything under a real ~/.ssh. |
| 13 | # A verifier is given key names and hashes, so what it holds |
| 14 | # is what `attest key` prints and nothing more. |
| 15 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host" |
| 16 | ssh-keygen -q -t ed25519 -N "" -C witness -f "$TMPDIR/witness" |
| 17 | ssh-keygen -q -t ed25519 -N "" -C stranger -f "$TMPDIR/stranger" |
| 18 | host=host.valley.invalid/attestations |
| 19 | witness=witness.valley.invalid/attestations |
| 20 | stranger=stranger.elsewhere.invalid/attestations |
| 21 | attest key --key "$TMPDIR/host" --name "$host" > "$TMPDIR/known_keys" |
| 22 | attest key --key "$TMPDIR/witness" --name "$witness" >> "$TMPDIR/known_keys" |
| 23 | attest key --key "$TMPDIR/stranger" --name "$stranger" > "$TMPDIR/other_keys" |
| 24 | grep -q "^$host+[0-9a-f]\{8\}+A" "$TMPDIR/known_keys" |
| 25 | |
| 26 | # A key behind a passphrase is refused rather than prompted |
| 27 | # for: attest runs unattended. |
| 28 | ssh-keygen -q -t ed25519 -N "a passphrase" -C locked -f "$TMPDIR/locked" |
| 29 | if attest key --key "$TMPDIR/locked" --name "$host" 2> locked.err; then |
| 30 | echo "attest-e2e: an encrypted key must be refused" >&2 |
| 31 | exit 1 |
| 32 | fi |
| 33 | grep -q 'cannot ask for a passphrase' locked.err |
| 34 | |
| 35 | repo="$TMPDIR/tree" |
| 36 | git init --quiet "$repo" |
| 37 | cd "$repo" || exit 1 |
| 38 | echo hello > hello.txt |
| 39 | mkdir -p sub |
| 40 | echo nested > sub/nested.txt |
| 41 | ln -s hello.txt link |
| 42 | printf '#!/bin/sh\ntrue\n' > run.sh |
| 43 | chmod 755 run.sh |
| 44 | git add -A |
| 45 | git commit --quiet -m base |
| 46 | |
| 47 | digest="$(attest digest | sed 's/^valley-tree-v1://')" |
| 48 | case "$digest" in |
| 49 | [0-9a-f][0-9a-f]*) ;; |
| 50 | *) echo "attest-e2e: no tree digest: $digest" >&2; exit 1 ;; |
| 51 | esac |
| 52 | |
| 53 | refs() { git for-each-ref --format='%(refname)' refs/the-valley; } |
| 54 | nothing_published() { |
| 55 | if [ -n "$(refs)" ]; then |
| 56 | echo "attest-e2e: $1" >&2 |
| 57 | git for-each-ref refs/the-valley >&2 |
| 58 | exit 1 |
| 59 | fi |
| 60 | } |
| 61 | |
| 62 | # Without a key, nothing is emitted — an unsigned statement |
| 63 | # is not an attestation. |
| 64 | if attest run --command 'ok=true' 2> nokey.err; then |
| 65 | echo "attest-e2e: a run with no signing key must fail" >&2 |
| 66 | exit 1 |
| 67 | fi |
| 68 | grep -q 'no signing key' nokey.err |
| 69 | nothing_published "a run with no signing key stored a ref" |
| 70 | |
| 71 | # A failing check publishes nothing, and says which check. |
| 72 | if attest run --key "$TMPDIR/host" --name "$host" \ |
| 73 | --command 'ok=true' --command 'nope=false' > failing.out 2> failing.err; then |
| 74 | echo "attest-e2e: a failing check must fail the run" >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | grep -qE '^check nope +command failed$' failing.out |
| 78 | grep -q 'nothing published' failing.err |
| 79 | nothing_published "a failing check left a ref behind" |
| 80 | |
| 81 | # A statement the schema rejects is stopped before it is |
| 82 | # signed or stored. Provenance is the caller's, so a |
| 83 | # malformed one is how a malformed statement gets composed |
| 84 | # at all. |
| 85 | echo '{"harness":"valley-check","authority":"root"}' > bad-provenance.json |
| 86 | if attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \ |
| 87 | --provenance bad-provenance.json > malformed.out 2> malformed.err; then |
| 88 | echo "attest-e2e: a malformed statement must fail the run" >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | grep -q 'provenance.authority: field not allowed' malformed.err |
| 92 | grep -q 'nothing published' malformed.err |
| 93 | nothing_published "a malformed statement left a ref behind" |
| 94 | |
| 95 | # A statement the schema admits but no line can carry is |
| 96 | # stopped too, at the same point and for the same reason. |
| 97 | printf '{"harness":"valley\\ncheck"}\n' > unwritable-provenance.json |
| 98 | if attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \ |
| 99 | --provenance unwritable-provenance.json > unwritable.out 2> unwritable.err; then |
| 100 | echo "attest-e2e: a statement with no written form must fail the run" >&2 |
| 101 | exit 1 |
| 102 | fi |
| 103 | grep -q 'nothing published' unwritable.err |
| 104 | nothing_published "a statement with no written form left a ref behind" |
| 105 | |
| 106 | # The successful run. |
| 107 | echo '{"harness":"valley-check","model":"none","delegation":[{"principal":"human:integrator","grant":"land changes"}]}' > provenance.json |
| 108 | attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \ |
| 109 | --provenance provenance.json > run.out |
| 110 | grep -q "^subject valley-tree-v1:$digest\$" run.out |
| 111 | grep -q '^check ok *command passed$' run.out |
| 112 | grep -q "^signer $host+" run.out |
| 113 | |
| 114 | # Stored at a ref keyed by the subject digest, as one note |
| 115 | # rather than a statement and a signature beside it. |
| 116 | ref="$(refs)" |
| 117 | case "$ref" in |
| 118 | refs/the-valley/attestations/"$digest"/*) ;; |
| 119 | *) echo "attest-e2e: ref $ref is not keyed by the subject digest" >&2; exit 1 ;; |
| 120 | esac |
| 121 | git cat-file -p "$ref:ok/statement.note" > statement.note |
| 122 | head -n 1 statement.note | grep -qx 'the-valley/attestation/v1' |
| 123 | grep -qx "subject.digest.valley-tree-v1 $digest" statement.note |
| 124 | grep -qx 'provenance.harness valley-check' statement.note |
| 125 | grep -q "^— $host " statement.note |
| 126 | |
| 127 | # No git object is signed: the signature is over the |
| 128 | # statement's text and nothing else. |
| 129 | if git cat-file commit HEAD | grep -q '^gpgsig'; then |
| 130 | echo "attest-e2e: attest signed a git object" >&2 |
| 131 | exit 1 |
| 132 | fi |
| 133 | |
| 134 | verify() { |
| 135 | attest verify --note "$1" --known-keys "$2" --repo "$repo" |
| 136 | } |
| 137 | |
| 138 | verify statement.note "$TMPDIR/known_keys" > verify.out |
| 139 | grep -q "^signature ok $host+" verify.out |
| 140 | grep -q 'matches the recorded subject digest' verify.out |
| 141 | |
| 142 | # One atomic native-git push carries the branch and the |
| 143 | # attestation ref together. |
| 144 | git init --quiet --bare "$TMPDIR/host.git" |
| 145 | git remote add valley "$TMPDIR/host.git" |
| 146 | attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \ |
| 147 | --push valley > push.out |
| 148 | grep -q -- '--atomic' push.out |
| 149 | git -C "$TMPDIR/host.git" for-each-ref --format='%(refname)' > published.txt |
| 150 | grep -qx 'refs/heads/main' published.txt |
| 151 | grep -qx "$ref" published.txt |
| 152 | |
| 153 | # A second party attests to the same statement, and its |
| 154 | # signature lands beside the first under the text both cover. |
| 155 | # This is what the note format is here for. |
| 156 | attest sign --key "$TMPDIR/witness" --name "$witness" statement.note \ |
| 157 | > countersigned.note |
| 158 | if [ "$(grep -c '^— ' countersigned.note)" -ne 2 ]; then |
| 159 | echo "attest-e2e: countersigning did not add a sibling signature line" >&2 |
| 160 | cat countersigned.note >&2 |
| 161 | exit 1 |
| 162 | fi |
| 163 | sed '/^$/q' countersigned.note | sed '$d' | cmp - <(sed '/^$/q' statement.note | sed '$d') |
| 164 | attest verify --note countersigned.note --known-keys "$TMPDIR/known_keys" \ |
| 165 | --repo "$repo" --signer "$host" --signer "$witness" > countersigned.out |
| 166 | if [ "$(grep -c '^signature ok' countersigned.out)" -ne 2 ]; then |
| 167 | echo "attest-e2e: both signatures over one statement must verify" >&2 |
| 168 | cat countersigned.out >&2 |
| 169 | exit 1 |
| 170 | fi |
| 171 | |
| 172 | # A verifier that does not hold the witness's key still |
| 173 | # accepts the note, and says which signature it cannot speak |
| 174 | # to. Requiring that signer is how it refuses instead. |
| 175 | attest key --key "$TMPDIR/host" --name "$host" > "$TMPDIR/host_only" |
| 176 | attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \ |
| 177 | --repo "$repo" > partial.out |
| 178 | grep -q "^signature ok $host+" partial.out |
| 179 | grep -q "^signature unknown $witness+" partial.out |
| 180 | if attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \ |
| 181 | --repo "$repo" --signer "$witness" > required.out 2>&1; then |
| 182 | echo "attest-e2e: a required signer with no verified signature must fail" >&2 |
| 183 | exit 1 |
| 184 | fi |
| 185 | grep -q "no signature on this note is by $witness" required.out |
| 186 | |
| 187 | # A note signed by a key the verifier does not hold is not a |
| 188 | # bad signature — it is not a signer. |
| 189 | if verify statement.note "$TMPDIR/other_keys" > stranger.out 2>&1; then |
| 190 | echo "attest-e2e: a note signed by nobody we hold a key for must not verify" >&2 |
| 191 | exit 1 |
| 192 | fi |
| 193 | grep -q 'no signature on this note is by a key the verifier holds' stranger.out |
| 194 | |
| 195 | # Tampering with the signed bytes: one byte of the text the |
| 196 | # signature covers. |
| 197 | sed "s/$digest/00000000000000000000000000000000000000000000000000000000000000ff/" \ |
| 198 | statement.note > tampered.note |
| 199 | if verify tampered.note "$TMPDIR/known_keys" > tampered.out 2>&1; then |
| 200 | echo "attest-e2e: a tampered statement must not verify" >&2 |
| 201 | exit 1 |
| 202 | fi |
| 203 | grep -q 'does not check out over this text' tampered.out |
| 204 | |
| 205 | # A signature cannot be lifted onto a statement it was not |
| 206 | # made about: it sits under the text it covers, so moving it |
| 207 | # means moving that text with it. |
| 208 | sed '/^$/q' statement.note | sed '$d' > lifted.txt |
| 209 | sed 's/^provenance.harness .*/provenance.harness somebody-else/' lifted.txt > lifted.body |
| 210 | sed -n '/^$/,$p' statement.note | tail -n +2 > lifted.sigs |
| 211 | { cat lifted.body; echo; cat lifted.sigs; } > lifted.note |
| 212 | if verify lifted.note "$TMPDIR/known_keys" > lifted.out 2>&1; then |
| 213 | echo "attest-e2e: a signature was lifted onto another statement" >&2 |
| 214 | exit 1 |
| 215 | fi |
| 216 | grep -q 'does not check out over this text' lifted.out |
| 217 | |
| 218 | # A statement written some other way than the one written |
| 219 | # form, signed properly over those bytes. The signature is |
| 220 | # good and the statement is still refused: it would verify |
| 221 | # here and fail wherever it was written out again, so the |
| 222 | # form is a rule rather than a convention. |
| 223 | { head -n 1 lifted.txt; tail -n +2 lifted.txt | sort -r; } > loose.txt |
| 224 | cmp -s loose.txt lifted.txt \ |
| 225 | && { echo "attest-e2e: the reordering changed nothing" >&2; exit 1; } |
| 226 | attest sign --key "$TMPDIR/host" --name "$host" loose.txt > loose.note |
| 227 | if verify loose.note "$TMPDIR/known_keys" > loose.out 2>&1; then |
| 228 | echo "attest-e2e: a statement not in the written form must not verify" >&2 |
| 229 | exit 1 |
| 230 | fi |
| 231 | grep -q 'not the written form of what it says' loose.out |
| 232 | |
| 233 | # The subject is the tree. Change the tree and the same |
| 234 | # signed statement stops being about it … |
| 235 | echo goodbye > hello.txt |
| 236 | git commit --quiet -am "change the tree" |
| 237 | if verify statement.note "$TMPDIR/known_keys" > moved.out 2>&1; then |
| 238 | echo "attest-e2e: a statement about another tree must not verify" >&2 |
| 239 | exit 1 |
| 240 | fi |
| 241 | grep -q 'tree digest mismatch' moved.out |
| 242 | |
| 243 | # … while rewriting the commit over the same tree leaves the |
| 244 | # attestation standing, which is what a subject that is a |
| 245 | # tree digest buys. |
| 246 | git commit --quiet --amend -m "same tree, new commit" |
| 247 | rewritten="$(attest digest | sed 's/^valley-tree-v1://')" |
| 248 | git revert --quiet --no-edit HEAD |
| 249 | restored="$(attest digest | sed 's/^valley-tree-v1://')" |
| 250 | if [ "$restored" != "$digest" ]; then |
| 251 | echo "attest-e2e: restoring the tree did not restore its digest" >&2 |
| 252 | echo " original $digest" >&2 |
| 253 | echo " restored $restored" >&2 |
| 254 | exit 1 |
| 255 | fi |
| 256 | if [ "$rewritten" = "$digest" ]; then |
| 257 | echo "attest-e2e: a changed tree kept its digest" >&2 |
| 258 | exit 1 |
| 259 | fi |
| 260 | verify statement.note "$TMPDIR/known_keys" > rebased.out |
| 261 | grep -q 'matches the recorded subject digest' rebased.out |
| 262 | |
| 263 | # A replacement ref in the repository changes nothing a standalone |
| 264 | # verify reads. refs/replace/<commit> makes git read another commit |
| 265 | # wherever it looks that one up, so a reader that left replacement |
| 266 | # refs on would digest the attested tree for a revision holding a |
| 267 | # different one, and pass it. The caller's environment asking for |
| 268 | # replacement refs is not listened to either. |
| 269 | attested="$(git rev-parse HEAD)" |
| 270 | echo "a change nobody attested" > evil.txt |
| 271 | git add evil.txt |
| 272 | git commit --quiet -m "a change nobody attested" |
| 273 | evil="$(git rev-parse HEAD)" |
| 274 | git update-ref "refs/replace/$evil" "$attested" |
| 275 | if [ "$(git rev-parse "$evil^{tree}")" != "$(git rev-parse "$attested^{tree}")" ]; then |
| 276 | echo "attest-e2e: the replacement did not take, so this case proves nothing" >&2 |
| 277 | exit 1 |
| 278 | fi |
| 279 | if GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" attest verify --note statement.note \ |
| 280 | --known-keys "$TMPDIR/known_keys" --repo "$repo" --rev "$evil" > replaced.out 2>&1; then |
| 281 | echo "attest-e2e: a replacement ref made an unattested revision verify" >&2 |
| 282 | cat replaced.out >&2 |
| 283 | exit 1 |
| 284 | fi |
| 285 | if grep -q 'matches the recorded subject digest' replaced.out; then |
| 286 | echo "attest-e2e: the digest of a replaced revision matched the attested one" >&2 |
| 287 | exit 1 |
| 288 | fi |
| 289 | [ "$(attest digest --rev "$evil" | sed 's/^valley-tree-v1://')" != "$digest" ] || { |
| 290 | echo "attest-e2e: attest digested the replacement, not the revision" >&2 |
| 291 | exit 1 |
| 292 | } |
| 293 | git update-ref -d "refs/replace/$evil" |
| 294 | touch "$out" |