Skip to content

Commit 9a6bbee

Browse files
committed
credential: avoid writing values with newlines
The credential protocol that we use to speak to helpers can't represent values with newlines in them. This was an intentional design choice to keep the protocol simple, since none of the values we pass should generally have newlines. However, if we _do_ encounter a newline in a value, we blindly transmit it in credential_write(). Such values may break the protocol syntax, or worse, inject new valid lines into the protocol stream. The most likely way for a newline to end up in a credential struct is by decoding a URL with a percent-encoded newline. However, since the bug occurs at the moment we write the value to the protocol, we'll catch it there. That should leave no possibility of accidentally missing a code path that can trigger the problem. At this level of the code we have little choice but to die(). However, since we'd not ever expect to see this case outside of a malicious URL, that's an acceptable outcome. Reported-by: Felix Wilhelm <[email protected]>
1 parent a5ab8d0 commit 9a6bbee

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

credential.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ static void credential_write_item(FILE *fp, const char *key, const char *value)
194194
{
195195
if (!value)
196196
return;
197+
if (strchr(value, '\n'))
198+
die("credential value for %s contains newline", key);
197199
fprintf(fp, "%s=%s\n", key, value);
198200
}
199201

t/t0300-credentials.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,10 @@ test_expect_success 'empty helper spec resets helper list' '
309309
EOF
310310
'
311311

312+
test_expect_success 'url parser rejects embedded newlines' '
313+
test_must_fail git credential fill <<-\EOF
314+
url=https://one.example.com?%0ahost=two.example.com/
315+
EOF
316+
'
317+
312318
test_done

0 commit comments

Comments
 (0)