Skip to content

Commit ce75d32

Browse files
committed
Merge branch 'jc/safe-directory-leading-path' into maint-2.45
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 7b7db54 + 313eec1 commit ce75d32

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,13 +1177,21 @@ static int safe_directory_cb(const char *key, const char *value,
11771177
} else if (!strcmp(value, "*")) {
11781178
data->is_safe = 1;
11791179
} else {
1180-
const char *interpolated = NULL;
1181-
1182-
if (!git_config_pathname(&interpolated, key, value) &&
1183-
!fspathcmp(data->path, interpolated ? interpolated : value))
1184-
data->is_safe = 1;
1185-
1186-
free((char *)interpolated);
1180+
const char *allowed = NULL;
1181+
1182+
if (!git_config_pathname(&allowed, key, value)) {
1183+
if (!allowed)
1184+
allowed = value;
1185+
if (ends_with(allowed, "/*")) {
1186+
size_t len = strlen(allowed);
1187+
if (!fspathncmp(allowed, data->path, len - 1))
1188+
data->is_safe = 1;
1189+
} else if (!fspathcmp(data->path, allowed)) {
1190+
data->is_safe = 1;
1191+
}
1192+
}
1193+
if (allowed != value)
1194+
free((char *)allowed);
11871195
}
11881196

11891197
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)