Skip to content

Commit f2eb0a7

Browse files
committed
sideband: offer to configure sanitizing on a per-URL basis
The main objection against sanitizing the sideband that was raised during the review of the sideband sanitizing patches, first on the git-security mailing list, then on the public mailing list, was that there are some setups where server-side `pre-receive` hooks want to error out, giving colorful messages to the users on the client side (if they are not redirecting the output into a file, that is). To avoid breaking such setups, the default chosen by the sideband sanitizing patches is to pass through ANSI color sequences. Still, there might be some use case out there where that is not enough. Therefore the `sideband.allowControlCharacters` config setting allows for configuring levels of sanitizing. As Junio Hamano pointed out, to keep users safe by default, we need to be able to scope this to some servers because while a user may trust their company's Git server, the same might not apply to other Git servers. To allow for this, let's imitate the way `http.<url>.*` offers to scope config settings to certain URLs, by letting users override the `sideband.allowControlCharacters` setting via `sideband.<url>.allowControlCharacters`. Suggested-by: Junio Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent cc57846 commit f2eb0a7

File tree

5 files changed

+102
-24
lines changed

5 files changed

+102
-24
lines changed

Documentation/config/sideband.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ sideband.allowControlCharacters::
2222
`true`::
2323
Allow all control characters to be sent to the terminal.
2424
--
25+
26+
sideband.<url>.*::
27+
Apply the `sideband.*` option selectively to specific URLs. The
28+
same URL matching logic applies as for `http.<url>.*` settings.

sideband.c

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "help.h"
1010
#include "pkt-line.h"
1111
#include "write-or-die.h"
12+
#include "urlmatch.h"
1213

1314
struct keyword_entry {
1415
/*
@@ -26,13 +27,14 @@ static struct keyword_entry keywords[] = {
2627
};
2728

2829
static enum {
29-
ALLOW_NO_CONTROL_CHARACTERS = 0,
30-
ALLOW_ANSI_COLOR_SEQUENCES = 1<<0,
31-
ALLOW_ANSI_CURSOR_MOVEMENTS = 1<<1,
32-
ALLOW_ANSI_ERASE = 1<<2,
33-
ALLOW_DEFAULT_ANSI_SEQUENCES = ALLOW_ANSI_COLOR_SEQUENCES,
34-
ALLOW_ALL_CONTROL_CHARACTERS = 1<<3,
35-
} allow_control_characters = ALLOW_DEFAULT_ANSI_SEQUENCES;
30+
ALLOW_CONTROL_SEQUENCES_UNSET = -1,
31+
ALLOW_NO_CONTROL_CHARACTERS = 0,
32+
ALLOW_ANSI_COLOR_SEQUENCES = 1<<0,
33+
ALLOW_ANSI_CURSOR_MOVEMENTS = 1<<1,
34+
ALLOW_ANSI_ERASE = 1<<2,
35+
ALLOW_DEFAULT_ANSI_SEQUENCES = ALLOW_ANSI_COLOR_SEQUENCES,
36+
ALLOW_ALL_CONTROL_CHARACTERS = 1<<3,
37+
} allow_control_characters = ALLOW_CONTROL_SEQUENCES_UNSET;
3638

3739
static inline int skip_prefix_in_csv(const char *value, const char *prefix,
3840
const char **out)
@@ -44,8 +46,19 @@ static inline int skip_prefix_in_csv(const char *value, const char *prefix,
4446
return 1;
4547
}
4648

47-
static void parse_allow_control_characters(const char *value)
49+
int sideband_allow_control_characters_config(const char *var, const char *value)
4850
{
51+
switch (git_parse_maybe_bool(value)) {
52+
case 0:
53+
allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
54+
return 0;
55+
case 1:
56+
allow_control_characters = ALLOW_ALL_CONTROL_CHARACTERS;
57+
return 0;
58+
default:
59+
break;
60+
}
61+
4962
allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
5063
while (*value) {
5164
if (skip_prefix_in_csv(value, "default", &value))
@@ -61,9 +74,37 @@ static void parse_allow_control_characters(const char *value)
6174
else if (skip_prefix_in_csv(value, "false", &value))
6275
allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS;
6376
else
64-
warning(_("unrecognized value for `sideband."
65-
"allowControlCharacters`: '%s'"), value);
77+
warning(_("unrecognized value for '%s': '%s'"), var, value);
6678
}
79+
return 0;
80+
}
81+
82+
static int sideband_config_callback(const char *var, const char *value,
83+
const struct config_context *ctx UNUSED,
84+
void *data UNUSED)
85+
{
86+
if (!strcmp(var, "sideband.allowcontrolcharacters"))
87+
return sideband_allow_control_characters_config(var, value);
88+
89+
return 0;
90+
}
91+
92+
void sideband_apply_url_config(const char *url)
93+
{
94+
struct urlmatch_config config = URLMATCH_CONFIG_INIT;
95+
char *normalized_url;
96+
97+
if (!url)
98+
BUG("must not call sideband_apply_url_config(NULL)");
99+
100+
config.section = "sideband";
101+
config.collect_fn = sideband_config_callback;
102+
103+
normalized_url = url_normalize(url, &config.url);
104+
git_config(urlmatch_config_entry, &config);
105+
free(normalized_url);
106+
string_list_clear(&config.vars, 1);
107+
urlmatch_config_release(&config);
67108
}
68109

69110
/* Returns a color setting (GIT_COLOR_NEVER, etc). */
@@ -79,20 +120,12 @@ static int use_sideband_colors(void)
79120
if (use_sideband_colors_cached >= 0)
80121
return use_sideband_colors_cached;
81122

82-
switch (git_config_get_maybe_bool("sideband.allowcontrolcharacters", &i)) {
83-
case 0: /* Boolean value */
84-
allow_control_characters = i ? ALLOW_ALL_CONTROL_CHARACTERS :
85-
ALLOW_NO_CONTROL_CHARACTERS;
86-
break;
87-
case -1: /* non-Boolean value */
88-
if (git_config_get_string_tmp("sideband.allowcontrolcharacters",
89-
&value))
90-
; /* huh? `get_maybe_bool()` returned -1 */
91-
else
92-
parse_allow_control_characters(value);
93-
break;
94-
default:
95-
break; /* not configured */
123+
if (allow_control_characters == ALLOW_CONTROL_SEQUENCES_UNSET) {
124+
if (!git_config_get_value("sideband.allowcontrolcharacters", &value))
125+
sideband_allow_control_characters_config("sideband.allowcontrolcharacters", value);
126+
127+
if (allow_control_characters == ALLOW_CONTROL_SEQUENCES_UNSET)
128+
allow_control_characters = ALLOW_DEFAULT_ANSI_SEQUENCES;
96129
}
97130

98131
if (!git_config_get_string_tmp(key, &value))

sideband.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ int demultiplex_sideband(const char *me, int status,
3030

3131
void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max);
3232

33+
/*
34+
* Apply sideband configuration for the given URL. This should be called
35+
* when a transport is created to allow URL-specific configuration of
36+
* sideband behavior (e.g., sideband.<url>.allowControlCharacters).
37+
*/
38+
void sideband_apply_url_config(const char *url);
39+
40+
/*
41+
* Parse and set the sideband allow control characters configuration.
42+
* The var parameter should be the key name (without section prefix).
43+
* Returns 0 if the variable was recognized and handled, non-zero otherwise.
44+
*/
45+
int sideband_allow_control_characters_config(const char *var, const char *value);
46+
3347
#endif

t/t5409-colorize-remote-messages.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,28 @@ test_expect_success 'control sequences in sideband allowed by default' '
167167
test_grep ! "\\^\\[\\[G" decoded
168168
'
169169

170+
test_expect_success 'allow all control sequences for a specific URL' '
171+
write_script .git/eraser <<-\EOF &&
172+
printf "error: Ohai!\\r\\033[K" >&2
173+
exec "$@"
174+
EOF
175+
test_config_global uploadPack.packObjectsHook ./eraser &&
176+
test_commit one-more-please &&
177+
178+
rm -rf throw-away &&
179+
git clone --no-local . throw-away 2>stderr &&
180+
test_decode_color <stderr >color-decoded &&
181+
test_decode_csi <color-decoded >decoded &&
182+
test_grep ! "CSI \\[K" decoded &&
183+
test_grep "\\^\\[\\[K" decoded &&
184+
185+
rm -rf throw-away &&
186+
git -c "sideband.file://.allowControlCharacters=true" \
187+
clone --no-local "file://$PWD" throw-away 2>stderr &&
188+
test_decode_color <stderr >color-decoded &&
189+
test_decode_csi <color-decoded >decoded &&
190+
test_grep "CSI \\[K" decoded &&
191+
test_grep ! "\\^\\[\\[K" decoded
192+
'
193+
170194
test_done

transport.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "object-name.h"
2929
#include "color.h"
3030
#include "bundle-uri.h"
31+
#include "sideband.h"
3132

3233
static int transport_use_color = -1;
3334
static char transport_colors[][COLOR_MAXLEN] = {
@@ -1210,6 +1211,8 @@ struct transport *transport_get(struct remote *remote, const char *url)
12101211

12111212
ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
12121213

1214+
sideband_apply_url_config(ret->url);
1215+
12131216
return ret;
12141217
}
12151218

0 commit comments

Comments
 (0)