summaryrefslogtreecommitdiff
path: root/usage.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2017-01-11 09:02:23 -0500
committerJunio C Hamano <gitster@pobox.com>2017-01-11 13:54:08 -0800
commitf290089879501a855df2eb41db5b38cb0035a765 (patch)
tree1b0f25cf5f1af2306e8d1ce374e347f5251391ff /usage.c
parentb5a9e435c6dfb40df0a27521c1c6590c8f68ffb2 (diff)
downloadgit-f290089879501a855df2eb41db5b38cb0035a765.tar.gz
vreport: sanitize ASCII control charsjk/vreport-sanitize
Our error() and die() calls may report messages with arbitrary data (e.g., filenames or even data from a remote server). Let's make it harder to cause confusion with mischievous filenames. E.g., try: git rev-parse "$(printf "\rfatal: this argument is too sneaky")" -- or git rev-parse "$(printf "\x1b[5mblinky\x1b[0m")" -- Let's block all ASCII control characters, with the exception of TAB and LF. We use both in our own messages (and we are necessarily sanitizing the complete output of snprintf here, as we do not have access to the individual varargs). And TAB and LF are unlikely to cause confusion (you could put "\nfatal: sneaky\n" in your filename, but it would at least not _cover up_ the message leading to it, unlike "\r"). We'll replace the characters with a "?", which is similar to how "ls" behaves. It might be nice to do something less lossy, like converting them to "\x" hex codes. But replacing with a single character makes it easy to do in-place and without worrying about length limitations. This feature should kick in rarely enough that the "?" marks are almost never seen. We'll leave high-bit characters as-is, as they are likely to be UTF-8 (though there may be some Unicode mischief you could cause, which may require further patches). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'usage.c')
-rw-r--r--usage.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/usage.c b/usage.c
index e4fa6d2f03..50a6ccee44 100644
--- a/usage.c
+++ b/usage.c
@@ -12,7 +12,13 @@ void vreportf(const char *prefix, const char *err, va_list params)
{
char msg[4096];
FILE *fh = error_handle ? error_handle : stderr;
+ char *p;
+
vsnprintf(msg, sizeof(msg), err, params);
+ for (p = msg; *p; p++) {
+ if (iscntrl(*p) && *p != '\t' && *p != '\n')
+ *p = '?';
+ }
fprintf(fh, "%s%s\n", prefix, msg);
}