Skip to content

Commit fe109cd

Browse files
committed
sideband: add options to allow more control sequences to be passed through
Even though control sequences that erase characters are quite juicy for attack scenarios, where attackers are eager to hide traces of suspicious activities, during the review of the side band sanitizing patch series concerns were raised that there might be some legimitate scenarios where Git server's `pre-receive` hooks use those sequences in a benign way. Control sequences to move the cursor can likewise be used to hide tracks by overwriting characters, and have been equally pointed out as having legitimate users. Let's add options to let users opt into passing through those ANSI Escape sequences: `sideband.allowControlCharacters` now supports also `cursor` and `erase`, and it parses the value as a comma-separated list. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 44585ba commit fe109cd

File tree

3 files changed

+123
-15
lines changed

3 files changed

+123
-15
lines changed

Documentation/config/sideband.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ sideband.allowControlCharacters::
22
By default, control characters that are delivered via the sideband
33
are masked, except ANSI color sequences. This prevents potentially
44
unwanted ANSI escape sequences from being sent to the terminal. Use
5-
this config setting to override this behavior:
5+
this config setting to override this behavior (the value can be
6+
a comma-separated list of the following keywords):
67
+
78
--
89
default::
910
color::
1011
Allow ANSI color sequences, line feeds and horizontal tabs,
1112
but mask all other control characters. This is the default.
13+
cursor::
14+
Allow control sequences that move the cursor. This is
15+
disabled by default.
16+
erase::
17+
Allow control sequences that erase charactrs. This is
18+
disabled by default.
1219
false::
1320
Mask all control characters other than line feeds and
1421
horizontal tabs.

sideband.c

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,43 @@ static struct keyword_entry keywords[] = {
2828
static enum {
2929
ALLOW_NO_CONTROL_CHARACTERS = 0,
3030
ALLOW_ANSI_COLOR_SEQUENCES = 1<<0,
31+
ALLOW_ANSI_CURSOR_MOVEMENTS = 1<<1,
32+
ALLOW_ANSI_ERASE = 1<<2,
3133
ALLOW_DEFAULT_ANSI_SEQUENCES = ALLOW_ANSI_COLOR_SEQUENCES,
32-
ALLOW_ALL_CONTROL_CHARACTERS = 1<<1,
33-
} allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES;
34+
ALLOW_ALL_CONTROL_CHARACTERS = 1<<3,
35+
} allow_control_characters = ALLOW_DEFAULT_ANSI_SEQUENCES;
36+
37+
static inline int skip_prefix_in_csv(const char *value, const char *prefix,
38+
const char **out)
39+
{
40+
if (!skip_prefix(value, prefix, &value) ||
41+
(*value && *value != ','))
42+
return 0;
43+
*out = value + !!*value;
44+
return 1;
45+
}
46+
47+
static void parse_allow_control_characters(const char *value)
48+
{
49+
allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
50+
while (*value) {
51+
if (skip_prefix_in_csv(value, "default", &value))
52+
allow_control_characters |= ALLOW_DEFAULT_ANSI_SEQUENCES;
53+
else if (skip_prefix_in_csv(value, "color", &value))
54+
allow_control_characters |= ALLOW_ANSI_COLOR_SEQUENCES;
55+
else if (skip_prefix_in_csv(value, "cursor", &value))
56+
allow_control_characters |= ALLOW_ANSI_CURSOR_MOVEMENTS;
57+
else if (skip_prefix_in_csv(value, "erase", &value))
58+
allow_control_characters |= ALLOW_ANSI_ERASE;
59+
else if (skip_prefix_in_csv(value, "true", &value))
60+
allow_control_characters = ALLOW_ALL_CONTROL_CHARACTERS;
61+
else if (skip_prefix_in_csv(value, "false", &value))
62+
allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
63+
else
64+
warning(_("unrecognized value for `sideband."
65+
"allowControlCharacters`: '%s'"), value);
66+
}
67+
}
3468

3569
/* Returns a color setting (GIT_COLOR_NEVER, etc). */
3670
static int use_sideband_colors(void)
@@ -54,13 +88,8 @@ static int use_sideband_colors(void)
5488
if (git_config_get_string_tmp("sideband.allowcontrolcharacters",
5589
&value))
5690
; /* huh? `get_maybe_bool()` returned -1 */
57-
else if (!strcmp(value, "default"))
58-
allow_control_characters = ALLOW_DEFAULT_ANSI_SEQUENCES;
59-
else if (!strcmp(value, "color"))
60-
allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES;
6191
else
62-
warning(_("unrecognized value for `sideband."
63-
"allowControlCharacters`: '%s'"), value);
92+
parse_allow_control_characters(value);
6493
break;
6594
default:
6695
break; /* not configured */
@@ -93,7 +122,7 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
93122
list_config_item(list, prefix, keywords[i].keyword);
94123
}
95124

96-
static int handle_ansi_color_sequence(struct strbuf *dest, const char *src, int n)
125+
static int handle_ansi_sequence(struct strbuf *dest, const char *src, int n)
97126
{
98127
int i;
99128

@@ -105,14 +134,47 @@ static int handle_ansi_color_sequence(struct strbuf *dest, const char *src, int
105134
* These are part of the Select Graphic Rendition sequences which
106135
* contain more than just color sequences, for more details see
107136
* https://en.wikipedia.org/wiki/ANSI_escape_code#SGR.
137+
*
138+
* The cursor movement sequences are:
139+
*
140+
* ESC [ n A - Cursor up n lines (CUU)
141+
* ESC [ n B - Cursor down n lines (CUD)
142+
* ESC [ n C - Cursor forward n columns (CUF)
143+
* ESC [ n D - Cursor back n columns (CUB)
144+
* ESC [ n E - Cursor next line, beginning (CNL)
145+
* ESC [ n F - Cursor previous line, beginning (CPL)
146+
* ESC [ n G - Cursor to column n (CHA)
147+
* ESC [ n ; m H - Cursor position (row n, col m) (CUP)
148+
* ESC [ n ; m f - Same as H (HVP)
149+
*
150+
* The sequences to erase characters are:
151+
*
152+
*
153+
* ESC [ 0 J - Clear from cursor to end of screen (ED)
154+
* ESC [ 1 J - Clear from cursor to beginning of screen (ED)
155+
* ESC [ 2 J - Clear entire screen (ED)
156+
* ESC [ 3 J - Clear entire screen + scrollback (ED) - xterm extension
157+
* ESC [ 0 K - Clear from cursor to end of line (EL)
158+
* ESC [ 1 K - Clear from cursor to beginning of line (EL)
159+
* ESC [ 2 K - Clear entire line (EL)
160+
* ESC [ n M - Delete n lines (DL)
161+
* ESC [ n P - Delete n characters (DCH)
162+
* ESC [ n X - Erase n characters (ECH)
163+
*
164+
* For a comprehensive list of common ANSI Escape sequences, see
165+
* https://www.xfree86.org/current/ctlseqs.html
108166
*/
109167

110-
if (allow_control_characters != ALLOW_ANSI_COLOR_SEQUENCES ||
111-
n < 3 || src[0] != '\x1b' || src[1] != '[')
168+
if (n < 3 || src[0] != '\x1b' || src[1] != '[')
112169
return 0;
113170

114171
for (i = 2; i < n; i++) {
115-
if (src[i] == 'm') {
172+
if (((allow_control_characters & ALLOW_ANSI_COLOR_SEQUENCES) &&
173+
src[i] == 'm') ||
174+
((allow_control_characters & ALLOW_ANSI_CURSOR_MOVEMENTS) &&
175+
strchr("ABCDEFGHf", src[i])) ||
176+
((allow_control_characters & ALLOW_ANSI_ERASE) &&
177+
strchr("JKMPX", src[i]))) {
116178
strbuf_add(dest, src, i + 1);
117179
return i;
118180
}
@@ -127,7 +189,7 @@ static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
127189
{
128190
int i;
129191

130-
if (allow_control_characters == ALLOW_ALL_CONTROL_CHARACTERS) {
192+
if ((allow_control_characters & ALLOW_ALL_CONTROL_CHARACTERS)) {
131193
strbuf_add(dest, src, n);
132194
return;
133195
}
@@ -136,7 +198,8 @@ static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
136198
for (; n && *src; src++, n--) {
137199
if (!iscntrl(*src) || *src == '\t' || *src == '\n')
138200
strbuf_addch(dest, *src);
139-
else if ((i = handle_ansi_color_sequence(dest, src, n))) {
201+
else if (allow_control_characters != ALLOW_NO_CONTROL_CHARACTERS &&
202+
(i = handle_ansi_sequence(dest, src, n))) {
140203
src += i;
141204
n -= i;
142205
} else {

t/t5409-colorize-remote-messages.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,42 @@ test_expect_success 'disallow (color) control sequences in sideband' '
129129
test_file_not_empty actual
130130
'
131131

132+
test_decode_csi() {
133+
awk '{
134+
while (match($0, /\033/) != 0) {
135+
printf "%sCSI ", substr($0, 1, RSTART-1);
136+
$0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
137+
}
138+
print
139+
}'
140+
}
141+
142+
test_expect_success 'control sequences in sideband allowed by default' '
143+
write_script .git/color-me-surprised <<-\EOF &&
144+
printf "error: \\033[31mcolor\\033[m\\033[Goverwrite\\033[Gerase\\033[K\\033?25l\\n" >&2
145+
exec "$@"
146+
EOF
147+
test_config_global uploadPack.packObjectsHook ./color-me-surprised &&
148+
test_commit need-at-least-one-commit-at-least &&
149+
150+
rm -rf throw-away &&
151+
git clone --no-local . throw-away 2>stderr &&
152+
test_decode_color <stderr >color-decoded &&
153+
test_decode_csi <color-decoded >decoded &&
154+
test_grep ! "CSI \\[K" decoded &&
155+
test_grep ! "CSI \\[G" decoded &&
156+
test_grep "\\^\\[?25l" decoded &&
157+
158+
rm -rf throw-away &&
159+
git -c sideband.allowControlCharacters=erase,cursor,color \
160+
clone --no-local . throw-away 2>stderr &&
161+
test_decode_color <stderr >color-decoded &&
162+
test_decode_csi <color-decoded >decoded &&
163+
test_grep "RED" decoded &&
164+
test_grep "CSI \\[K" decoded &&
165+
test_grep "CSI \\[G" decoded &&
166+
test_grep ! "\\^\\[\\[K" decoded &&
167+
test_grep ! "\\^\\[\\[G" decoded
168+
'
169+
132170
test_done

0 commit comments

Comments
 (0)