#!/usr/bin/env bash # Self-test of changed-go-blobs.sh and of the fetch setting the PR checks use: # a PR tree that names a '..' entry must neither write outside the output # directory nor be fetched at all. set -euo pipefail script=$(cd "$(dirname "$0")" && pwd)/changed-go-blobs.sh t=$(mktemp -d) trap 'rm -rf "$t"' EXIT fail() { echo "changed-go-blobs-test: FAIL: $*" >&2; exit 1; } git init -q "$t/r" cd "$t/r" git config user.email t@example.com git config user.name t mkdir -p a printf 'package a\n' >a/a.go printf 'package a\n' >a/a_test.go git add . && git commit -q -m base base=$(git rev-parse HEAD) # 1. An ordinary change: the old and new blobs land under the output directory. printf 'package a\n\nvar X = 1\n' >a/a.go printf 'package b\n' >b.go git add . && git commit -q -m change bash "$script" "$base" HEAD "$t/out1" grep -q 'var X' "$t/out1/new/a/a.go" || fail "new blob of a/a.go missing" [ -f "$t/out1/old/a/a.go" ] || fail "old blob of a/a.go missing" [ -f "$t/out1/new/b.go" ] && [ ! -e "$t/out1/old/b.go" ] || fail "added file b.go not new-only" [ ! -e "$t/out1/new/a/a_test.go" ] || fail "a test file was written" # 2. A crafted tree: '..' nested three times, holding x.go. git writes such # a tree only with --literally; the diff still lists '../../../x.go', and # writing blobs by that path would create new/../../../x.go, outside the # output directory (the inline loop this script replaced did exactly that). blob=$(printf 'package evil\n' | git hash-object -w --stdin) entry() { perl -e 'print $ARGV[0], "\0", pack("H*", $ARGV[1])' "$1" "$2"; } tr=$(entry "100644 x.go" "$blob" | git hash-object -t tree --literally -w --stdin) for _ in 1 2 3; do tr=$(entry "40000 .." "$tr" | git hash-object -t tree --literally -w --stdin); done evil=$(git commit-tree "$tr" -p "$base" -m evil) mkdir -p "$t/deep/ws/out2" if bash "$script" "$base" "$evil" "$t/deep/ws/out2" 2>"$t/err"; then fail "a '..' path was accepted"; fi grep -q 'refusing path' "$t/err" || fail "refusal not reported: $(cat "$t/err")" [ -z "$(find "$t/deep" -name x.go)" ] || fail "a file was written outside the output directory" # 3. The checks fetch the PR head with fetch.fsckObjects=true: the crafted # tree is refused at the fetch. git update-ref refs/pull/1/head "$evil" git init -q "$t/c" cd "$t/c" if git -c fetch.fsckObjects=true fetch -q --no-tags "$t/r" "+refs/pull/1/head:refs/remotes/pr/head" 2>"$t/err2"; then fail "fetch with fsckObjects accepted a '..' tree" fi echo "changed-go-blobs-test: PASS (3 cases)"