forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanitize sideband channel messages #1853
Open
dscho
wants to merge
3
commits into
gitgitgadget:maint-2.47
Choose a base branch
from
dscho:sanitize-sideband
base: maint-2.47
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
sideband.allowControlCharacters:: | ||
By default, control characters that are delivered via the sideband | ||
are masked, except ANSI color sequences. This prevents potentially | ||
unwanted ANSI escape sequences from being sent to the terminal. Use | ||
this config setting to override this behavior: | ||
+ | ||
-- | ||
color:: | ||
Allow ANSI color sequences, line feeds and horizontal tabs, | ||
but mask all other control characters. This is the default. | ||
false:: | ||
Mask all control characters other than line feeds and | ||
horizontal tabs. | ||
true:: | ||
Allow all control characters to be sent to the terminal. | ||
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,12 @@ static struct keyword_entry keywords[] = { | |
{ "error", GIT_COLOR_BOLD_RED }, | ||
}; | ||
|
||
static enum { | ||
ALLOW_NO_CONTROL_CHARACTERS = 0, | ||
ALLOW_ALL_CONTROL_CHARACTERS = 1, | ||
ALLOW_ANSI_COLOR_SEQUENCES = 2 | ||
} allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES; | ||
|
||
/* Returns a color setting (GIT_COLOR_NEVER, etc). */ | ||
static int use_sideband_colors(void) | ||
{ | ||
|
@@ -38,6 +44,25 @@ static int use_sideband_colors(void) | |
if (use_sideband_colors_cached >= 0) | ||
return use_sideband_colors_cached; | ||
|
||
switch (git_config_get_maybe_bool("sideband.allowcontrolcharacters", &i)) { | ||
case 0: /* Boolean value */ | ||
allow_control_characters = i ? ALLOW_ALL_CONTROL_CHARACTERS : | ||
ALLOW_NO_CONTROL_CHARACTERS; | ||
break; | ||
case -1: /* non-Boolean value */ | ||
if (git_config_get_string_tmp("sideband.allowcontrolcharacters", | ||
&value)) | ||
; /* huh? `get_maybe_bool()` returned -1 */ | ||
else if (!strcmp(value, "color")) | ||
allow_control_characters = ALLOW_ANSI_COLOR_SEQUENCES; | ||
else | ||
warning(_("unrecognized value for `sideband." | ||
"allowControlCharacters`: '%s'"), value); | ||
break; | ||
default: | ||
break; /* not configured */ | ||
} | ||
|
||
if (!git_config_get_string_tmp(key, &value)) | ||
use_sideband_colors_cached = git_config_colorbool(key, value); | ||
else if (!git_config_get_string_tmp("color.ui", &value)) | ||
|
@@ -65,6 +90,55 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref | |
list_config_item(list, prefix, keywords[i].keyword); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the Git mailing list, Andreas Schwab wrote (reply to this): On Jan 14 2025, Johannes Schindelin via GitGitGadget wrote:
> diff --git a/sideband.c b/sideband.c
> index 02805573fab..c0b1cb044a3 100644
> --- a/sideband.c
> +++ b/sideband.c
> @@ -65,6 +65,19 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
> list_config_item(list, prefix, keywords[i].keyword);
> }
>
> +static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
> +{
> + strbuf_grow(dest, n);
> + for (; n && *src; src++, n--) {
> + if (!iscntrl(*src) || *src == '\t' || *src == '\n')
The argument of iscntrl needs to be converted to unsigned char.
--
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the Git mailing list, Junio C Hamano wrote (reply to this): Andreas Schwab <[email protected]> writes:
> On Jan 14 2025, Johannes Schindelin via GitGitGadget wrote:
>
>> diff --git a/sideband.c b/sideband.c
>> index 02805573fab..c0b1cb044a3 100644
>> --- a/sideband.c
>> +++ b/sideband.c
>> @@ -65,6 +65,19 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
>> list_config_item(list, prefix, keywords[i].keyword);
>> }
>>
>> +static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n)
>> +{
>> + strbuf_grow(dest, n);
>> + for (; n && *src; src++, n--) {
>> + if (!iscntrl(*src) || *src == '\t' || *src == '\n')
>
> The argument of iscntrl needs to be converted to unsigned char.
If this were system-provided one, you are absolutely correct.
But I think this comes from
sane-ctype.h:15:#undef iscntrl
sane-ctype.h:40:#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
and sane_istest() does the casting to uchar for us, so this may be
OK (even if it may be a bit misleading).
|
||
} | ||
|
||
static int handle_ansi_color_sequence(struct strbuf *dest, const char *src, int n) | ||
{ | ||
int i; | ||
|
||
/* | ||
* Valid ANSI color sequences are of the form | ||
* | ||
* ESC [ [<n> [; <n>]*] m | ||
*/ | ||
|
||
if (allow_control_characters != ALLOW_ANSI_COLOR_SEQUENCES || | ||
n < 3 || src[0] != '\x1b' || src[1] != '[') | ||
return 0; | ||
|
||
for (i = 2; i < n; i++) { | ||
if (src[i] == 'm') { | ||
strbuf_add(dest, src, i + 1); | ||
return i; | ||
} | ||
if (!isdigit(src[i]) && src[i] != ';') | ||
break; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n) | ||
{ | ||
int i; | ||
|
||
if (allow_control_characters == ALLOW_ALL_CONTROL_CHARACTERS) { | ||
strbuf_add(dest, src, n); | ||
return; | ||
} | ||
|
||
strbuf_grow(dest, n); | ||
for (; n && *src; src++, n--) { | ||
if (!iscntrl(*src) || *src == '\t' || *src == '\n') | ||
strbuf_addch(dest, *src); | ||
else if ((i = handle_ansi_color_sequence(dest, src, n))) { | ||
src += i; | ||
n -= i; | ||
} else { | ||
strbuf_addch(dest, '^'); | ||
strbuf_addch(dest, 0x40 + *src); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Optionally highlight one keyword in remote output if it appears at the start | ||
* of the line. This should be called for a single line only, which is | ||
|
@@ -80,7 +154,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) | |
int i; | ||
|
||
if (!want_color_stderr(use_sideband_colors())) { | ||
strbuf_add(dest, src, n); | ||
strbuf_add_sanitized(dest, src, n); | ||
return; | ||
} | ||
|
||
|
@@ -113,7 +187,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) | |
} | ||
} | ||
|
||
strbuf_add(dest, src, n); | ||
strbuf_add_sanitized(dest, src, n); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Phillip Wood wrote (reply to this):