Skip to content

Commit 5b25741

Browse files
committed
sideband: mask control characters
The output of `git clone` is a vital component for understanding what has happened when things go wrong. However, these logs are partially under the control of the remote server (via the "sideband", which typically contains what the remote `git pack-objects` process sends to `stderr`), and is currently not sanitized by Git. This makes Git susceptible to ANSI escape sequence injection (see CWE-150, https://cwe.mitre.org/data/definitions/150.html), which allows attackers to corrupt terminal state, to hide information, and even to insert characters into the input buffer (i.e. as if the user had typed those characters). To plug this vulnerability, disallow any control character in the sideband, replacing them instead with the common `^<letter/symbol>` (e.g. `^[` for `\x1b`, `^A` for `\x01`). There is likely a need for more fine-grained controls instead of using a "heavy hammer" like this, which will be introduced subsequently. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b01b9b8 commit 5b25741

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

sideband.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
6161
list_config_item(list, prefix, keywords[i].keyword);
6262
}
6363

64+
static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
65+
{
66+
strbuf_grow(dest, n);
67+
for (; n && *src; src++, n--) {
68+
if (!iscntrl(*src) || *src == '\t' || *src == '\n')
69+
strbuf_addch(dest, *src);
70+
else {
71+
strbuf_addch(dest, '^');
72+
strbuf_addch(dest, 0x40 + *src);
73+
}
74+
}
75+
}
76+
6477
/*
6578
* Optionally highlight one keyword in remote output if it appears at the start
6679
* of the line. This should be called for a single line only, which is
@@ -73,7 +86,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
7386
int i;
7487

7588
if (!want_color_stderr(use_sideband_colors())) {
76-
strbuf_add(dest, src, n);
89+
strbuf_add_sanitized(dest, src, n);
7790
return;
7891
}
7992

@@ -106,7 +119,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
106119
}
107120
}
108121

109-
strbuf_add(dest, src, n);
122+
strbuf_add_sanitized(dest, src, n);
110123
}
111124

112125

t/t5409-colorize-remote-messages.sh

+12
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,16 @@ test_expect_success 'fallback to color.ui' '
9898
grep "<BOLD;RED>error<RESET>: error" decoded
9999
'
100100

101+
test_expect_success 'disallow (color) control sequences in sideband' '
102+
write_script .git/color-me-surprised <<-\EOF &&
103+
printf "error: Have you \\033[31mread\\033[m this?\\n" >&2
104+
exec "$@"
105+
EOF
106+
test_config_global uploadPack.packObjectshook ./color-me-surprised &&
107+
test_commit need-at-least-one-commit &&
108+
git clone --no-local . throw-away 2>stderr &&
109+
test_decode_color <stderr >decoded &&
110+
test_i18ngrep ! RED decoded
111+
'
112+
101113
test_done

0 commit comments

Comments
 (0)