1#!/bin/sh
23
test_description='paths written by git-apply cannot escape the working tree'
4. ./test-lib.sh
56
# tests will try to write to ../foo, and we do not
7# want them to escape the trash directory when they
8# fail
9test_expect_success 'bump git repo one level down' '
10mkdir inside &&
11mv .git inside/ &&
12cd inside
13'
1415
# $1 = name of file
16# $2 = current path to file (if different)
17mkpatch_add () {
18rm -f "${2:-$1}" &&
19cat <<-EOF
20diff --git a/$1 b/$1
21new file mode 100644
22index 0000000..53c74cd
23--- /dev/null
24+++ b/$1
25@@ -0,0 +1 @@
26+evil
27EOF
28}
2930
mkpatch_del () {
31echo evil >"${2:-$1}" &&
32cat <<-EOF
33diff --git a/$1 b/$1
34deleted file mode 100644
35index 53c74cd..0000000
36--- a/$1
37+++ /dev/null
38@@ -1 +0,0 @@
39-evil
40EOF
41}
4243
# $1 = name of file
44# $2 = content of symlink
45mkpatch_symlink () {
46rm -f "$1" &&
47cat <<-EOF
48diff --git a/$1 b/$1
49new file mode 120000
50index 0000000..$(printf "%s" "$2" | git hash-object --stdin)
51--- /dev/null
52+++ b/$1
53@@ -0,0 +1 @@
54+$2
55\ No newline at end of file
56EOF
57}
5859
test_expect_success 'cannot create file containing ..' '
60mkpatch_add ../foo >patch &&
61test_must_fail git apply patch &&
62test_path_is_missing ../foo
63'
6465
test_expect_success 'can create file containing .. with --unsafe-paths' '
66mkpatch_add ../foo >patch &&
67git apply --unsafe-paths patch &&
68test_path_is_file ../foo
69'
7071
test_expect_success 'cannot create file containing .. (index)' '
72mkpatch_add ../foo >patch &&
73test_must_fail git apply --index patch &&
74test_path_is_missing ../foo
75'
7677
test_expect_success 'cannot create file containing .. with --unsafe-paths (index)' '
78mkpatch_add ../foo >patch &&
79test_must_fail git apply --index --unsafe-paths patch &&
80test_path_is_missing ../foo
81'
8283
test_expect_success 'cannot delete file containing ..' '
84mkpatch_del ../foo >patch &&
85test_must_fail git apply patch &&
86test_path_is_file ../foo
87'
8889
test_expect_success 'can delete file containing .. with --unsafe-paths' '
90mkpatch_del ../foo >patch &&
91git apply --unsafe-paths patch &&
92test_path_is_missing ../foo
93'
9495
test_expect_success 'cannot delete file containing .. (index)' '
96mkpatch_del ../foo >patch &&
97test_must_fail git apply --index patch &&
98test_path_is_file ../foo
99'
100101
test_expect_success SYMLINKS 'symlink escape via ..' '
102{
103mkpatch_symlink tmp .. &&
104mkpatch_add tmp/foo ../foo
105} >patch &&
106test_must_fail git apply patch &&
107test_path_is_missing tmp &&
108test_path_is_missing ../foo
109'
110111
test_expect_success SYMLINKS 'symlink escape via .. (index)' '
112{
113mkpatch_symlink tmp .. &&
114mkpatch_add tmp/foo ../foo
115} >patch &&
116test_must_fail git apply --index patch &&
117test_path_is_missing tmp &&
118test_path_is_missing ../foo
119'
120121
test_expect_success SYMLINKS 'symlink escape via absolute path' '
122{
123mkpatch_symlink tmp "$(pwd)" &&
124mkpatch_add tmp/foo ../foo
125} >patch &&
126test_must_fail git apply patch &&
127test_path_is_missing tmp &&
128test_path_is_missing ../foo
129'
130131
test_expect_success SYMLINKS 'symlink escape via absolute path (index)' '
132{
133mkpatch_symlink tmp "$(pwd)" &&
134mkpatch_add tmp/foo ../foo
135} >patch &&
136test_must_fail git apply --index patch &&
137test_path_is_missing tmp &&
138test_path_is_missing ../foo
139'
140141
test_done