Skip to content

Commit b8bdb2f

Browse files
committed
Merge branch 'jc/safe-directory-leading-path'
The safe.directory configuration knob has been updated to optionally allow leading path matches. * jc/safe-directory-leading-path: safe.directory: allow "lead/ing/path/*" match
2 parents 22cf18f + 313eec1 commit b8bdb2f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

Documentation/config/safe.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ string `*`. This will allow all repositories to be treated as if their
4444
directory was listed in the `safe.directory` list. If `safe.directory=*`
4545
is set in system config and you want to re-enable this protection, then
4646
initialize your list with an empty value before listing the repositories
47-
that you deem safe.
47+
that you deem safe. Giving a directory with `/*` appended to it will
48+
allow access to all repositories under the named directory.
4849
+
4950
As explained, Git only allows you to access repositories owned by
5051
yourself, i.e. the user who is running Git, by default. When Git

setup.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,13 +1230,20 @@ static int safe_directory_cb(const char *key, const char *value,
12301230
} else if (!strcmp(value, "*")) {
12311231
data->is_safe = 1;
12321232
} else {
1233-
char *interpolated = NULL;
1234-
1235-
if (!git_config_pathname(&interpolated, key, value) &&
1236-
!fspathcmp(data->path, interpolated ? interpolated : value))
1237-
data->is_safe = 1;
1238-
1239-
free(interpolated);
1233+
char *allowed = NULL;
1234+
1235+
if (!git_config_pathname(&allowed, key, value)) {
1236+
const char *check = allowed ? allowed : value;
1237+
if (ends_with(check, "/*")) {
1238+
size_t len = strlen(check);
1239+
if (!fspathncmp(check, data->path, len - 1))
1240+
data->is_safe = 1;
1241+
} else if (!fspathcmp(data->path, check)) {
1242+
data->is_safe = 1;
1243+
}
1244+
}
1245+
if (allowed != value)
1246+
free(allowed);
12401247
}
12411248

12421249
return 0;

t/t0033-safe-directory.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,22 @@ test_expect_success 'safe.directory=*, but is reset' '
7171
expect_rejected_dir
7272
'
7373

74+
test_expect_success 'safe.directory with matching glob' '
75+
git config --global --unset-all safe.directory &&
76+
p=$(pwd) &&
77+
git config --global safe.directory "${p%/*}/*" &&
78+
git status
79+
'
80+
81+
test_expect_success 'safe.directory with unmatching glob' '
82+
git config --global --unset-all safe.directory &&
83+
p=$(pwd) &&
84+
git config --global safe.directory "${p%/*}no/*" &&
85+
expect_rejected_dir
86+
'
87+
7488
test_expect_success 'safe.directory in included file' '
89+
git config --global --unset-all safe.directory &&
7590
cat >gitconfig-include <<-EOF &&
7691
[safe]
7792
directory = "$(pwd)"

0 commit comments

Comments
 (0)