Skip to content

Commit 501d8da

Browse files
committed
credential_format(): also encode <host>[:<port>]
An upcoming change wants to sanitize the credential password prompt where a URL is displayed that may potentially come from a `.gitmodules` file. To this end, the `credential_format()` function is employed. To sanitize the host name (and optional port) part of the URL, we need a new mode of the `strbuf_add_percentencode()` function because the current mode is both too strict and too lenient: too strict because it encodes `:`, `[` and `]` (which should be left unencoded in `<host>:<port>` and in IPv6 addresses), and too lenient because it does not encode invalid host name characters `/`, `_` and `~`. So let's introduce and use a new mode specifically to encode the host name and optional port part of a URI, leaving alpha-numerical characters, periods, colons and brackets alone and encoding all others. This only leads to a change of behavior for URLs that contain invalid host names. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a62f00c commit 501d8da

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

credential.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ static void credential_format(struct credential *c, struct strbuf *out)
227227
strbuf_addch(out, '@');
228228
}
229229
if (c->host)
230-
strbuf_addstr(out, c->host);
230+
strbuf_add_percentencode(out, c->host,
231+
STRBUF_ENCODE_HOST_AND_PORT);
231232
if (c->path) {
232233
strbuf_addch(out, '/');
233234
strbuf_add_percentencode(out, c->path, 0);

strbuf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags)
497497
unsigned char ch = src[i];
498498
if (ch <= 0x1F || ch >= 0x7F ||
499499
(ch == '/' && (flags & STRBUF_ENCODE_SLASH)) ||
500-
strchr(URL_UNSAFE_CHARS, ch))
500+
((flags & STRBUF_ENCODE_HOST_AND_PORT) ?
501+
!isalnum(ch) && !strchr("-.:[]", ch) :
502+
!!strchr(URL_UNSAFE_CHARS, ch)))
501503
strbuf_addf(dst, "%%%02X", (unsigned char)ch);
502504
else
503505
strbuf_addch(dst, ch);

strbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ void strbuf_expand_bad_format(const char *format, const char *command);
356356
void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
357357

358358
#define STRBUF_ENCODE_SLASH 1
359+
#define STRBUF_ENCODE_HOST_AND_PORT 2
359360

360361
/**
361362
* Append the contents of a string to a strbuf, percent-encoding any characters

t/t0300-credentials.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,19 @@ test_expect_success 'match percent-encoded values in username' '
696696
EOF
697697
'
698698

699+
test_expect_success 'match percent-encoded values in hostname' '
700+
test_config "credential.https://a%20b%20c/.helper" "$HELPER" &&
701+
check fill <<-\EOF
702+
url=https://a b c/
703+
--
704+
protocol=https
705+
host=a b c
706+
username=foo
707+
password=bar
708+
--
709+
EOF
710+
'
711+
699712
test_expect_success 'fetch with multiple path components' '
700713
test_unconfig credential.helper &&
701714
test_config credential.https://example.com/foo/repo.git.helper "verbatim foo bar" &&

0 commit comments

Comments
 (0)