-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply: reject input that touches outside the working area
By default, a patch that affects outside the working area (either a Git controlled working tree, or the current working directory when "git apply" is used as a replacement of GNU patch) is rejected as a mistake (or a mischief). Git itself does not create such a patch, unless the user bends over backwards and specifies a non-standard prefix to "git diff" and friends. When `git apply` is used as a "better GNU patch", the user can pass the `--unsafe-paths` option to override this safety check. This option has no effect when `--index` or `--cached` is in use. The new test was stolen from Jeff King with slight enhancements. Note that a few new tests for touching outside the working area by following a symbolic link are still expected to fail at this step, but will be fixed in later steps. Signed-off-by: Junio C Hamano <gitster@pobox.com>
- Loading branch information
Junio C Hamano
committed
Feb 10, 2015
1 parent
3d8a54e
commit c536c07
Showing
3 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#!/bin/sh | ||
|
||
test_description='paths written by git-apply cannot escape the working tree' | ||
. ./test-lib.sh | ||
|
||
# tests will try to write to ../foo, and we do not | ||
# want them to escape the trash directory when they | ||
# fail | ||
test_expect_success 'bump git repo one level down' ' | ||
mkdir inside && | ||
mv .git inside/ && | ||
cd inside | ||
' | ||
|
||
# $1 = name of file | ||
# $2 = current path to file (if different) | ||
mkpatch_add () { | ||
rm -f "${2:-$1}" && | ||
cat <<-EOF | ||
diff --git a/$1 b/$1 | ||
new file mode 100644 | ||
index 0000000..53c74cd | ||
--- /dev/null | ||
+++ b/$1 | ||
@@ -0,0 +1 @@ | ||
+evil | ||
EOF | ||
} | ||
|
||
mkpatch_del () { | ||
echo evil >"${2:-$1}" && | ||
cat <<-EOF | ||
diff --git a/$1 b/$1 | ||
deleted file mode 100644 | ||
index 53c74cd..0000000 | ||
--- a/$1 | ||
+++ /dev/null | ||
@@ -1 +0,0 @@ | ||
-evil | ||
EOF | ||
} | ||
|
||
# $1 = name of file | ||
# $2 = content of symlink | ||
mkpatch_symlink () { | ||
rm -f "$1" && | ||
cat <<-EOF | ||
diff --git a/$1 b/$1 | ||
new file mode 120000 | ||
index 0000000..$(printf "%s" "$2" | git hash-object --stdin) | ||
--- /dev/null | ||
+++ b/$1 | ||
@@ -0,0 +1 @@ | ||
+$2 | ||
\ No newline at end of file | ||
EOF | ||
} | ||
|
||
test_expect_success 'cannot create file containing ..' ' | ||
mkpatch_add ../foo >patch && | ||
test_must_fail git apply patch && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_success 'can create file containing .. with --unsafe-paths' ' | ||
mkpatch_add ../foo >patch && | ||
git apply --unsafe-paths patch && | ||
test_path_is_file ../foo | ||
' | ||
|
||
test_expect_success 'cannot create file containing .. (index)' ' | ||
mkpatch_add ../foo >patch && | ||
test_must_fail git apply --index patch && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_success 'cannot create file containing .. with --unsafe-paths (index)' ' | ||
mkpatch_add ../foo >patch && | ||
test_must_fail git apply --index --unsafe-paths patch && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_success 'cannot delete file containing ..' ' | ||
mkpatch_del ../foo >patch && | ||
test_must_fail git apply patch && | ||
test_path_is_file ../foo | ||
' | ||
|
||
test_expect_success 'can delete file containing .. with --unsafe-paths' ' | ||
mkpatch_del ../foo >patch && | ||
git apply --unsafe-paths patch && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_success 'cannot delete file containing .. (index)' ' | ||
mkpatch_del ../foo >patch && | ||
test_must_fail git apply --index patch && | ||
test_path_is_file ../foo | ||
' | ||
|
||
test_expect_failure SYMLINKS 'symlink escape via ..' ' | ||
{ | ||
mkpatch_symlink tmp .. && | ||
mkpatch_add tmp/foo ../foo | ||
} >patch && | ||
test_must_fail git apply patch && | ||
test_path_is_missing tmp && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_failure SYMLINKS 'symlink escape via .. (index)' ' | ||
{ | ||
mkpatch_symlink tmp .. && | ||
mkpatch_add tmp/foo ../foo | ||
} >patch && | ||
test_must_fail git apply --index patch && | ||
test_path_is_missing tmp && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_failure SYMLINKS 'symlink escape via absolute path' ' | ||
{ | ||
mkpatch_symlink tmp "$(pwd)" && | ||
mkpatch_add tmp/foo ../foo | ||
} >patch && | ||
test_must_fail git apply patch && | ||
test_path_is_missing tmp && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_expect_failure SYMLINKS 'symlink escape via absolute path (index)' ' | ||
{ | ||
mkpatch_symlink tmp "$(pwd)" && | ||
mkpatch_add tmp/foo ../foo | ||
} >patch && | ||
test_must_fail git apply --index patch && | ||
test_path_is_missing tmp && | ||
test_path_is_missing ../foo | ||
' | ||
|
||
test_done |