Skip to content

Commit f9e7677

Browse files
10ne1gitster
authored andcommitted
submodule: fix case-folding gitdir filesystem colisions
Add a new check when extension.submoduleEncoding is enabled to detect and prevent case-folding filesystem colisions. When this new check is triggered, a stricter casefolding aware URI encoding is used to percent-encode uppercase characters. By using this check/retry mechanism the uppercase encoding is only applied when necessary, so case-sensitive filesystems are not affected. Signed-off-by: Adrian Ratiu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8bd559c commit f9e7677

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

submodule.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,15 +2238,58 @@ int submodule_move_head(const char *path, const char *super_prefix,
22382238
return ret;
22392239
}
22402240

2241+
static int check_casefolding_conflict(const char *git_dir,
2242+
const char *submodule_name,
2243+
const bool suffixes_match)
2244+
{
2245+
char *p, *modules_dir = xstrdup(git_dir);
2246+
struct dirent *de;
2247+
DIR *dir = NULL;
2248+
int ret = 0;
2249+
2250+
if ((p = find_last_dir_sep(modules_dir)))
2251+
*p = '\0';
2252+
2253+
/* No conflict is possible if modules_dir doesn't exist (first clone) */
2254+
if (!is_directory(modules_dir))
2255+
goto cleanup;
2256+
2257+
dir = opendir(modules_dir);
2258+
if (!dir) {
2259+
ret = -1;
2260+
goto cleanup;
2261+
}
2262+
2263+
/* Check for another directory under .git/modules that differs only in case. */
2264+
while ((de = readdir(dir))) {
2265+
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2266+
continue;
2267+
2268+
if ((suffixes_match || is_git_directory(git_dir)) &&
2269+
!strcasecmp(de->d_name, submodule_name) &&
2270+
strcmp(de->d_name, submodule_name)) {
2271+
ret = -1; /* collision found */
2272+
break;
2273+
}
2274+
}
2275+
2276+
cleanup:
2277+
if (dir)
2278+
closedir(dir);
2279+
FREE_AND_NULL(modules_dir);
2280+
return ret;
2281+
}
2282+
22412283
/*
22422284
* Encoded gitdir validation function used when extensions.submoduleEncoding is enabled.
22432285
* This does not print errors like the non-encoded version, because encoding is supposed
22442286
* to mitigate / fix all these.
22452287
*/
2246-
static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name UNUSED)
2288+
static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name)
22472289
{
22482290
const char *modules_marker = "/modules/";
22492291
char *p = git_dir, *last_submodule_name = NULL;
2292+
int config_ignorecase = 0;
22502293

22512294
if (!the_repository->repository_format_submodule_encoding)
22522295
BUG("validate_submodule_encoded_git_dir() must be called with "
@@ -2262,6 +2305,14 @@ static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodu
22622305
if (!last_submodule_name || strchr(last_submodule_name, '/'))
22632306
return -1;
22642307

2308+
/* Prevent conflicts on case-folding filesystems */
2309+
repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase);
2310+
if (ignore_case || config_ignorecase) {
2311+
bool suffixes_match = !strcmp(last_submodule_name, submodule_name);
2312+
return check_casefolding_conflict(git_dir, submodule_name,
2313+
suffixes_match);
2314+
}
2315+
22652316
return 0;
22662317
}
22672318

@@ -2650,13 +2701,37 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
26502701
if (!validate_and_set_submodule_gitdir(buf, submodule_name))
26512702
return;
26522703

2653-
/* Case 2: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
2704+
/* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
26542705
strbuf_reset(buf);
26552706
repo_git_path_append(r, buf, "modules/");
26562707
strbuf_addstr_urlencode(buf, submodule_name, is_rfc3986_unreserved);
26572708
if (!validate_and_set_submodule_gitdir(buf, submodule_name))
26582709
return;
26592710

2711+
/* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */
2712+
strbuf_reset(buf);
2713+
repo_git_path_append(r, buf, "modules/");
2714+
strbuf_addstr_urlencode(buf, submodule_name, is_casefolding_rfc3986_unreserved);
2715+
if (!validate_and_set_submodule_gitdir(buf, submodule_name))
2716+
return;
2717+
2718+
/* Case 2.3: Try some derived gitdir names, see if one sticks */
2719+
for (char c = '0'; c <= '9'; c++) {
2720+
strbuf_reset(buf);
2721+
repo_git_path_append(r, buf, "modules/");
2722+
strbuf_addstr_urlencode(buf, submodule_name, is_rfc3986_unreserved);
2723+
strbuf_addch(buf, c);
2724+
if (!validate_and_set_submodule_gitdir(buf, submodule_name))
2725+
return;
2726+
2727+
strbuf_reset(buf);
2728+
repo_git_path_append(r, buf, "modules/");
2729+
strbuf_addstr_urlencode(buf, submodule_name, is_casefolding_rfc3986_unreserved);
2730+
strbuf_addch(buf, c);
2731+
if (!validate_and_set_submodule_gitdir(buf, submodule_name))
2732+
return;
2733+
}
2734+
26602735
/* Case 3: Nothing worked: error out */
26612736
die(_("Cannot construct a valid gitdir path for submodule '%s': "
26622737
"please set a unique git config for 'submodule.%s.gitdir'."),

t/t7425-submodule-encoding.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,39 @@ test_expect_success 'disabling extensions.submoduleEncoding prevents nested subm
161161
)
162162
'
163163

164+
test_expect_success CASE_INSENSITIVE_FS 'verify case-folding conflicts are correctly encoded' '
165+
git clone -c extensions.submoduleEncoding=true main cloned-folding &&
166+
(
167+
cd cloned-folding &&
168+
169+
# conflict: the "folding" gitdir will already be taken
170+
git submodule add ../new-sub "folding" &&
171+
test_commit lowercase &&
172+
git submodule add ../new-sub "FoldinG" &&
173+
test_commit uppercase &&
174+
175+
# conflict: the "foo" gitdir will already be taken
176+
git submodule add ../new-sub "FOO" &&
177+
test_commit uppercase-foo &&
178+
git submodule add ../new-sub "foo" &&
179+
test_commit lowercase-foo &&
180+
181+
# create a multi conflict between foobar, fooBar and foo%42ar
182+
# the "foo" gitdir will already be taken
183+
git submodule add ../new-sub "foobar" &&
184+
test_commit lowercase-foobar &&
185+
git submodule add ../new-sub "foo%42ar" &&
186+
test_commit encoded-foo%42ar &&
187+
git submodule add ../new-sub "fooBar" &&
188+
test_commit mixed-fooBar
189+
) &&
190+
verify_submodule_gitdir_path cloned-folding "folding" "modules/folding" &&
191+
verify_submodule_gitdir_path cloned-folding "FoldinG" "modules/%46oldin%47" &&
192+
verify_submodule_gitdir_path cloned-folding "FOO" "modules/FOO" &&
193+
verify_submodule_gitdir_path cloned-folding "foo" "modules/foo0" &&
194+
verify_submodule_gitdir_path cloned-folding "foobar" "modules/foobar" &&
195+
verify_submodule_gitdir_path cloned-folding "foo%42ar" "modules/foo%42ar" &&
196+
verify_submodule_gitdir_path cloned-folding "fooBar" "modules/fooBar0"
197+
'
198+
164199
test_done

url.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ int is_rfc3986_unreserved(char ch)
1414
ch == '-' || ch == '_' || ch == '.' || ch == '~';
1515
}
1616

17+
/*
18+
* This is a variant of is_rfc3986_unreserved() that treats uppercase
19+
* letters as "reserved". This forces them to be percent-encoded, allowing
20+
* 'Foo' (%46oo) and 'foo' (foo) to be distinct on case-folding filesystems.
21+
*/
22+
int is_casefolding_rfc3986_unreserved(char c)
23+
{
24+
return (c >= 'a' && c <= 'z') ||
25+
(c >= '0' && c <= '9') ||
26+
c == '-' || c == '.' || c == '_' || c == '~';
27+
}
28+
1729
int is_urlschemechar(int first_flag, int ch)
1830
{
1931
/*

url.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ void end_url_with_slash(struct strbuf *buf, const char *url);
2222
void str_end_url_with_slash(const char *url, char **dest);
2323

2424
int is_rfc3986_unreserved(char ch);
25+
int is_casefolding_rfc3986_unreserved(char c);
2526

2627
#endif /* URL_H */

0 commit comments

Comments
 (0)