|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +test_description='detection and prevention of out-of-tree symlinks' |
| 4 | +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 5 | +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 6 | +. ./test-lib.sh |
| 7 | + |
| 8 | +if ! test_have_prereq SYMLINKS |
| 9 | +then |
| 10 | + skip_all='skipping external symlink tests (missing SYMLINKS)' |
| 11 | + test_done |
| 12 | +fi |
| 13 | + |
| 14 | +create_symlink() { |
| 15 | + symlink=$1 |
| 16 | + target=$2 |
| 17 | + test_expect_success "create symlink ($symlink)" ' |
| 18 | + sha1=$(printf "%s" "$target" | git hash-object -w --stdin) && |
| 19 | + git update-index --add --cacheinfo "120000,$sha1,$symlink" |
| 20 | + ' |
| 21 | +} |
| 22 | + |
| 23 | +check_symlink () { |
| 24 | + symlink=$1 |
| 25 | + config=$2 |
| 26 | + outcome=$3 |
| 27 | + expect=$4 |
| 28 | + |
| 29 | + if test "$outcome" = "allow" |
| 30 | + then |
| 31 | + fail= |
| 32 | + : ${expect:=test_cmp ../target} |
| 33 | + else |
| 34 | + fail=test_must_fail |
| 35 | + : ${expect:=! cat} |
| 36 | + fi |
| 37 | + |
| 38 | + test_expect_success " check symlink ($symlink, $config -> $outcome)" " |
| 39 | + rm -f $symlink && |
| 40 | + $fail git -c core.allowExternalSymlinks=$config \\ |
| 41 | + checkout-index -- $symlink && |
| 42 | + $expect $symlink |
| 43 | + " |
| 44 | +} |
| 45 | + |
| 46 | +# we want to try breaking out of the repository, |
| 47 | +# so let's work inside a sub-repository, and break |
| 48 | +# out to the top-level trash directory |
| 49 | +test_expect_success 'set up repository' ' |
| 50 | + echo content >target && |
| 51 | + git init subrepo && |
| 52 | + cd subrepo && |
| 53 | + test_commit base && |
| 54 | + echo content >in-repo-target |
| 55 | +' |
| 56 | + |
| 57 | +create_symlink in-repo in-repo-target |
| 58 | +check_symlink in-repo false allow |
| 59 | + |
| 60 | +create_symlink subdir/in-repo ../in-repo-target |
| 61 | +check_symlink subdir/in-repo false allow |
| 62 | + |
| 63 | +create_symlink absolute "$TRASH_DIRECTORY/target" |
| 64 | +check_symlink absolute true allow |
| 65 | +check_symlink absolute false forbid |
| 66 | + |
| 67 | +create_symlink relative "../target" |
| 68 | +check_symlink relative true allow |
| 69 | +check_symlink relative false forbid |
| 70 | + |
| 71 | +create_symlink curdir . |
| 72 | +check_symlink curdir false allow test_path_is_dir |
| 73 | +create_symlink sneaky curdir/../target |
| 74 | +check_symlink sneaky true allow |
| 75 | +check_symlink sneaky false forbid |
| 76 | + |
| 77 | +test_expect_success 'applying a patch checks symlink config' ' |
| 78 | + git diff-index -p --cached HEAD -- relative >patch && |
| 79 | + rm -f relative && |
| 80 | + git -c core.allowExternalSymlinks=true apply <patch && |
| 81 | + test_cmp ../target relative && |
| 82 | + rm -f relative && |
| 83 | + test_must_fail git -c core.allowExternalSymlinks=false apply <patch |
| 84 | +' |
| 85 | + |
| 86 | +test_expect_success 'merge-recursive checks symlinks config' ' |
| 87 | + git reset --hard && |
| 88 | +
|
| 89 | + # create rename situation which requires processing |
| 90 | + # outside of unpack_trees() |
| 91 | + ln -s ../foo one && |
| 92 | + git add one && |
| 93 | + git commit -m base && |
| 94 | +
|
| 95 | + ln -sf ../target one && |
| 96 | + git commit -am modify && |
| 97 | +
|
| 98 | + git checkout -b side HEAD^ && |
| 99 | + git mv one two && |
| 100 | + git commit -am rename && |
| 101 | +
|
| 102 | + git -c core.allowExternalSymlinks=true merge main && |
| 103 | + test_cmp ../target two && |
| 104 | +
|
| 105 | + git reset --hard HEAD^ && |
| 106 | + test_must_fail git -c core.allowExternalSymlinks=false merge main |
| 107 | +' |
| 108 | + |
| 109 | +test_done |
0 commit comments