Skip to content

Commit c7049c2

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 a8c289b commit c7049c2

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
@@ -20,7 +20,11 @@ static struct keyword_entry keywords[] = {
2020
{ "error", GIT_COLOR_BOLD_RED },
2121
};
2222

23-
static int allow_control_characters;
23+
static enum {
24+
ALLOW_NO_CONTROL_CHARACTERS = 0,
25+
ALLOW_ALL_CONTROL_CHARACTERS = 1,
26+
ALLOW_ANSI_COLOR_SEQUENCES = 2
27+
} allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES;
2428

2529
/* Returns a color setting (GIT_COLOR_NEVER, etc). */
2630
static int use_sideband_colors(void)
@@ -35,8 +39,24 @@ static int use_sideband_colors(void)
3539
if (use_sideband_colors_cached >= 0)
3640
return use_sideband_colors_cached;
3741

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

4161
if (!git_config_get_string(key, &value)) {
4262
use_sideband_colors_cached = git_config_colorbool(key, value);
@@ -66,9 +86,37 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
6686
list_config_item(list, prefix, keywords[i].keyword);
6787
}
6888

89+
static int handle_ansi_color_sequence(struct strbuf *dest, const char *src, int n)
90+
{
91+
int i;
92+
93+
/*
94+
* Valid ANSI color sequences are of the form
95+
*
96+
* ESC [ [<n> [; <n>]*] m
97+
*/
98+
99+
if (allow_control_characters != ALLOW_ANSI_COLOR_SEQUENCES ||
100+
n < 3 || src[0] != '\x1b' || src[1] != '[')
101+
return 0;
102+
103+
for (i = 2; i < n; i++) {
104+
if (src[i] == 'm') {
105+
strbuf_add(dest, src, i + 1);
106+
return i;
107+
}
108+
if (!isdigit(src[i]) && src[i] != ';')
109+
break;
110+
}
111+
112+
return 0;
113+
}
114+
69115
static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
70116
{
71-
if (allow_control_characters) {
117+
int i;
118+
119+
if (allow_control_characters == ALLOW_ALL_CONTROL_CHARACTERS) {
72120
strbuf_add(dest, src, n);
73121
return;
74122
}
@@ -77,7 +125,10 @@ static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
77125
for (; n && *src; src++, n--) {
78126
if (!iscntrl(*src) || *src == '\t' || *src == '\n')
79127
strbuf_addch(dest, *src);
80-
else {
128+
else if ((i = handle_ansi_color_sequence(dest, src, n))) {
129+
src += i;
130+
n -= i;
131+
} else {
81132
strbuf_addch(dest, '^');
82133
strbuf_addch(dest, 0x40 + *src);
83134
}

t/t5409-colorize-remote-messages.sh

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

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

119131
test_done

0 commit comments

Comments
 (0)