Skip to content

Commit 6061601

Browse files
chooglengitster
authored andcommitted
safe.directory: use git_protected_config()
Use git_protected_config() to read `safe.directory` instead of read_very_early_config(), making it 'protected configuration only'. As a result, `safe.directory` now respects "-c", so update the tests and docs accordingly. It used to ignore "-c" due to how it was implemented, not because of security or correctness concerns [1]. [1] https://lore.kernel.org/git/[email protected]/ Signed-off-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5b3c650 commit 6061601

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

Documentation/config/safe.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ via `git config --add`. To reset the list of safe directories (e.g. to
1212
override any such directories specified in the system config), add a
1313
`safe.directory` entry with an empty value.
1414
+
15-
This config setting is only respected when specified in a system or global
16-
config, not when it is specified in a repository config, via the command
17-
line option `-c safe.directory=<path>`, or in environment variables.
15+
This config setting is only respected in protected configuration (see
16+
<<SCOPES>>). This prevents the untrusted repository from tampering with this
17+
value.
1818
+
1919
The value of this setting is interpolated, i.e. `~/<path>` expands to a
2020
path relative to the home directory and `%(prefix)/<path>` expands to a

setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ static int ensure_valid_ownership(const char *gitfile,
11551155
* constant regardless of what failed above. data.is_safe should be
11561156
* initialized to false, and might be changed by the callback.
11571157
*/
1158-
read_very_early_config(safe_directory_cb, &data);
1158+
git_protected_config(safe_directory_cb, &data);
11591159

11601160
return data.is_safe;
11611161
}

t/t0033-safe-directory.sh

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,20 @@ test_expect_success 'safe.directory is not set' '
1616
expect_rejected_dir
1717
'
1818

19-
test_expect_success 'ignoring safe.directory on the command line' '
20-
test_must_fail git -c safe.directory="$(pwd)" status 2>err &&
21-
grep "dubious ownership" err
19+
test_expect_success 'safe.directory on the command line' '
20+
git -c safe.directory="$(pwd)" status
2221
'
2322

24-
test_expect_success 'ignoring safe.directory in the environment' '
25-
test_must_fail env GIT_CONFIG_COUNT=1 \
26-
GIT_CONFIG_KEY_0="safe.directory" \
27-
GIT_CONFIG_VALUE_0="$(pwd)" \
28-
git status 2>err &&
29-
grep "dubious ownership" err
23+
test_expect_success 'safe.directory in the environment' '
24+
env GIT_CONFIG_COUNT=1 \
25+
GIT_CONFIG_KEY_0="safe.directory" \
26+
GIT_CONFIG_VALUE_0="$(pwd)" \
27+
git status
3028
'
3129

32-
test_expect_success 'ignoring safe.directory in GIT_CONFIG_PARAMETERS' '
33-
test_must_fail env \
34-
GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
35-
git status 2>err &&
36-
grep "dubious ownership" err
30+
test_expect_success 'safe.directory in GIT_CONFIG_PARAMETERS' '
31+
env GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
32+
git status
3733
'
3834

3935
test_expect_success 'ignoring safe.directory in repo config' '

0 commit comments

Comments
 (0)