Skip to content

Commit a26c4ed

Browse files
committed
sideband: do allow ANSI color sequences by default
The preceding two commits introduced special handling of the sideband channel to neutralize ANSI escape sequences before sending the payload to the terminal, and `sideband.allowControlCharacters` to override that behavior. However, some `pre-receive` hooks that are actively used in practice want to color their messages and therefore rely on the fact that Git passes them through to the terminal. In contrast to other ANSI escape sequences, it is highly unlikely that coloring sequences can be essential tools in attack vectors that mislead Git users e.g. by hiding crucial information. Therefore we can have both: Continue to allow ANSI coloring sequences to be passed to the terminal, and neutralize all other ANSI escape sequences. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 14c612c commit a26c4ed

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

Documentation/config/sideband.txt

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
sideband.allowControlCharacters::
22
By default, control characters that are delivered via the sideband
3-
are masked, to prevent potentially unwanted ANSI escape sequences
4-
from being sent to the terminal. Use this config setting to override
5-
this behavior.
3+
are masked, except ANSI color sequences. This prevents potentially
4+
unwanted ANSI escape sequences from being sent to the terminal. Use
5+
this config setting to override this behavior:
6+
+
7+
--
8+
color::
9+
Allow ANSI color sequences, line feeds and horizontal tabs,
10+
but mask all other control characters. This is the default.
11+
false::
12+
Mask all control characters other than line feeds and
13+
horizontal tabs.
14+
true::
15+
Allow all control characters to be sent to the terminal.
16+
--

sideband.c

+56-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ static struct keyword_entry keywords[] = {
2525
{ "error", GIT_COLOR_BOLD_RED },
2626
};
2727

28-
static int allow_control_characters;
28+
static enum {
29+
ALLOW_NO_CONTROL_CHARACTERS = 0,
30+
ALLOW_ALL_CONTROL_CHARACTERS = 1,
31+
ALLOW_ANSI_COLOR_SEQUENCES = 2
32+
} allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES;
2933

3034
/* Returns a color setting (GIT_COLOR_NEVER, etc). */
3135
static int use_sideband_colors(void)
@@ -40,8 +44,24 @@ static int use_sideband_colors(void)
4044
if (use_sideband_colors_cached >= 0)
4145
return use_sideband_colors_cached;
4246

43-
git_config_get_bool("sideband.allowcontrolcharacters",
44-
&allow_control_characters);
47+
switch (git_config_get_maybe_bool("sideband.allowcontrolcharacters", &i)) {
48+
case 0: /* Boolean value */
49+
allow_control_characters = i ? ALLOW_ALL_CONTROL_CHARACTERS :
50+
ALLOW_NO_CONTROL_CHARACTERS;
51+
break;
52+
case -1: /* non-Boolean value */
53+
if (git_config_get_string_tmp("sideband.allowcontrolcharacters",
54+
&value))
55+
; /* huh? `get_maybe_bool()` returned -1 */
56+
else if (!strcmp(value, "color"))
57+
allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES;
58+
else
59+
warning(_("unrecognized value for `sideband."
60+
"allowControlCharacters`: '%s'"), value);
61+
break;
62+
default:
63+
break; /* not configured */
64+
}
4565

4666
if (!git_config_get_string_tmp(key, &value))
4767
use_sideband_colors_cached = git_config_colorbool(key, value);
@@ -70,9 +90,37 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
7090
list_config_item(list, prefix, keywords[i].keyword);
7191
}
7292

93+
static int handle_ansi_color_sequence(struct strbuf *dest, const char *src, int n)
94+
{
95+
int i;
96+
97+
/*
98+
* Valid ANSI color sequences are of the form
99+
*
100+
* ESC [ [<n> [; <n>]*] m
101+
*/
102+
103+
if (allow_control_characters != ALLOW_ANSI_COLOR_SEQUENCES ||
104+
n < 3 || src[0] != '\x1b' || src[1] != '[')
105+
return 0;
106+
107+
for (i = 2; i < n; i++) {
108+
if (src[i] == 'm') {
109+
strbuf_add(dest, src, i + 1);
110+
return i;
111+
}
112+
if (!isdigit(src[i]) && src[i] != ';')
113+
break;
114+
}
115+
116+
return 0;
117+
}
118+
73119
static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
74120
{
75-
if (allow_control_characters) {
121+
int i;
122+
123+
if (allow_control_characters == ALLOW_ALL_CONTROL_CHARACTERS) {
76124
strbuf_add(dest, src, n);
77125
return;
78126
}
@@ -81,7 +129,10 @@ static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
81129
for (; n && *src; src++, n--) {
82130
if (!iscntrl(*src) || *src == '\t' || *src == '\n')
83131
strbuf_addch(dest, *src);
84-
else {
132+
else if ((i = handle_ansi_color_sequence(dest, src, n))) {
133+
src += i;
134+
n -= i;
135+
} else {
85136
strbuf_addch(dest, '^');
86137
strbuf_addch(dest, 0x40 + *src);
87138
}

t/t5409-colorize-remote-messages.sh

+14-2
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,32 @@ test_expect_success 'fallback to color.ui' '
101101

102102
test_expect_success 'disallow (color) control sequences in sideband' '
103103
write_script .git/color-me-surprised <<-\EOF &&
104-
printf "error: Have you \\033[31mread\\033[m this?\\n" >&2
104+
printf "error: Have you \\033[31mread\\033[m this?\\a\\n" >&2
105105
exec "$@"
106106
EOF
107107
test_config_global uploadPack.packObjectshook ./color-me-surprised &&
108108
test_commit need-at-least-one-commit &&
109109
110110
git clone --no-local . throw-away 2>stderr &&
111111
test_decode_color <stderr >decoded &&
112+
test_grep RED decoded &&
113+
test_grep "\\^G" stderr &&
114+
tr -dc "\\007" <stderr >actual &&
115+
test_must_be_empty actual &&
116+
117+
rm -rf throw-away &&
118+
git -c sideband.allowControlCharacters=false \
119+
clone --no-local . throw-away 2>stderr &&
120+
test_decode_color <stderr >decoded &&
112121
test_grep ! RED decoded &&
122+
test_grep "\\^G" stderr &&
113123
114124
rm -rf throw-away &&
115125
git -c sideband.allowControlCharacters clone --no-local . throw-away 2>stderr &&
116126
test_decode_color <stderr >decoded &&
117-
test_grep RED decoded
127+
test_grep RED decoded &&
128+
tr -dc "\\007" <stderr >actual &&
129+
test_file_not_empty actual
118130
'
119131

120132
test_done

0 commit comments

Comments
 (0)