Skip to content

Commit 27bc901

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 b692e84 commit 27bc901

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
@@ -66,6 +66,19 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
6666
list_config_item(list, prefix, keywords[i].keyword);
6767
}
6868

69+
static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
70+
{
71+
strbuf_grow(dest, n);
72+
for (; n && *src; src++, n--) {
73+
if (!iscntrl(*src) || *src == '\t' || *src == '\n')
74+
strbuf_addch(dest, *src);
75+
else {
76+
strbuf_addch(dest, '^');
77+
strbuf_addch(dest, 0x40 + *src);
78+
}
79+
}
80+
}
81+
6982
/*
7083
* Optionally highlight one keyword in remote output if it appears at the start
7184
* of the line. This should be called for a single line only, which is
@@ -81,7 +94,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
8194
int i;
8295

8396
if (!want_color_stderr(use_sideband_colors())) {
84-
strbuf_add(dest, src, n);
97+
strbuf_add_sanitized(dest, src, n);
8598
return;
8699
}
87100

@@ -114,7 +127,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
114127
}
115128
}
116129

117-
strbuf_add(dest, src, n);
130+
strbuf_add_sanitized(dest, src, n);
118131
}
119132

120133

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_grep ! RED decoded
111+
'
112+
101113
test_done

0 commit comments

Comments
 (0)