Skip to content

Commit fd65fc3

Browse files
committed
Merge branch 'bc/wildcard-credential'
Update the parser used for credential.<URL>.<variable> configuration, to handle <URL>s with '/' in them correctly. * bc/wildcard-credential: credential: fix matching URLs with multiple levels in path
2 parents b34789c + b44d011 commit fd65fc3

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

credential.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ static void credential_format(struct credential *c, struct strbuf *out)
136136
return;
137137
strbuf_addf(out, "%s://", c->protocol);
138138
if (c->username && *c->username) {
139-
strbuf_add_percentencode(out, c->username);
139+
strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
140140
strbuf_addch(out, '@');
141141
}
142142
if (c->host)
143143
strbuf_addstr(out, c->host);
144144
if (c->path) {
145145
strbuf_addch(out, '/');
146-
strbuf_add_percentencode(out, c->path);
146+
strbuf_add_percentencode(out, c->path, 0);
147147
}
148148
}
149149

strbuf.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,17 @@ void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
479479
}
480480
}
481481

482-
#define URL_UNSAFE_CHARS " <>\"%{}|\\^`:/?#[]@!$&'()*+,;="
482+
#define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;="
483483

484-
void strbuf_add_percentencode(struct strbuf *dst, const char *src)
484+
void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags)
485485
{
486486
size_t i, len = strlen(src);
487487

488488
for (i = 0; i < len; i++) {
489489
unsigned char ch = src[i];
490-
if (ch <= 0x1F || ch >= 0x7F || strchr(URL_UNSAFE_CHARS, ch))
490+
if (ch <= 0x1F || ch >= 0x7F ||
491+
(ch == '/' && (flags & STRBUF_ENCODE_SLASH)) ||
492+
strchr(URL_UNSAFE_CHARS, ch))
491493
strbuf_addf(dst, "%%%02X", (unsigned char)ch);
492494
else
493495
strbuf_addch(dst, ch);

strbuf.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,16 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb,
378378
*/
379379
void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
380380

381+
#define STRBUF_ENCODE_SLASH 1
382+
381383
/**
382384
* Append the contents of a string to a strbuf, percent-encoding any characters
383385
* that are needed to be encoded for a URL.
386+
*
387+
* If STRBUF_ENCODE_SLASH is set in flags, percent-encode slashes. Otherwise,
388+
* slashes are not percent-encoded.
384389
*/
385-
void strbuf_add_percentencode(struct strbuf *dst, const char *src);
390+
void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags);
386391

387392
/**
388393
* Append the given byte size as a human-readable string (i.e. 12.23 KiB,

t/t0300-credentials.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,51 @@ test_expect_success 'match percent-encoded values' '
366366
EOF
367367
'
368368

369+
test_expect_success 'match percent-encoded UTF-8 values in path' '
370+
test_config credential.https://example.com.useHttpPath true &&
371+
test_config credential.https://example.com/perú.git.helper "$HELPER" &&
372+
check fill <<-\EOF
373+
url=https://example.com/per%C3%BA.git
374+
--
375+
protocol=https
376+
host=example.com
377+
path=perú.git
378+
username=foo
379+
password=bar
380+
--
381+
EOF
382+
'
383+
384+
test_expect_success 'match percent-encoded values in username' '
385+
test_config credential.https://user%[email protected]/foo/bar.git.helper "$HELPER" &&
386+
check fill <<-\EOF
387+
url=https://user%[email protected]/foo/bar.git
388+
--
389+
protocol=https
390+
host=example.com
391+
username=foo
392+
password=bar
393+
--
394+
EOF
395+
'
396+
397+
test_expect_success 'fetch with multiple path components' '
398+
test_unconfig credential.helper &&
399+
test_config credential.https://example.com/foo/repo.git.helper "verbatim foo bar" &&
400+
check fill <<-\EOF
401+
url=https://example.com/foo/repo.git
402+
--
403+
protocol=https
404+
host=example.com
405+
username=foo
406+
password=bar
407+
--
408+
verbatim: get
409+
verbatim: protocol=https
410+
verbatim: host=example.com
411+
EOF
412+
'
413+
369414
test_expect_success 'pull username from config' '
370415
test_config credential.https://example.com.username foo &&
371416
check fill <<-\EOF

0 commit comments

Comments
 (0)