Unverified Commit 55fa91ae authored by Huang Zhw's avatar Huang Zhw Committed by GitHub
Browse files

redis-cli: some commands should bypass history file. (#8895)

Currently in redis-cli only AUTH and ACL SETUSER bypass history
file. We add CONFIG SET masterauth/masteruser/requirepass,
HELLO with AUTH, MIGRATE with AUTH or AUTH2 to bypass history
file too.

The drawback is HELLO and MIGRATE's code is a mess. Someday if
we change these commands, we have to change here too.
parent 8351a10b
...@@ -2114,6 +2114,58 @@ void cliLoadPreferences(void) { ...@@ -2114,6 +2114,58 @@ void cliLoadPreferences(void) {
sdsfree(rcfile); sdsfree(rcfile);
} }
/* Some commands can include sensitive information and shouldn't be put in the
* history file. Currently these commands are include:
* - AUTH
* - ACL SETUSER
* - CONFIG SET masterauth/masteruser/requirepass
* - HELLO with [AUTH username password]
* - MIGRATE with [AUTH password] or [AUTH2 username password] */
static int isSensitiveCommand(int argc, char **argv) {
if (!strcasecmp(argv[0],"auth")) {
return 1;
} else if (argc > 1 &&
!strcasecmp(argv[0],"acl") &&
!strcasecmp(argv[1],"setuser"))
{
return 1;
} else if (argc > 2 &&
!strcasecmp(argv[0],"config") &&
!strcasecmp(argv[1],"set") && (
!strcasecmp(argv[2],"masterauth") ||
!strcasecmp(argv[2],"masteruser") ||
!strcasecmp(argv[2],"requirepass")))
{
return 1;
/* HELLO [protover [AUTH username password] [SETNAME clientname]] */
} else if (argc > 4 && !strcasecmp(argv[0],"hello")) {
for (int j = 2; j < argc; j++) {
int moreargs = argc - 1 - j;
if (!strcasecmp(argv[j],"AUTH") && moreargs >= 2) {
return 1;
} else if (!strcasecmp(argv[j],"SETNAME") && moreargs) {
j++;
} else {
return 0;
}
}
/* MIGRATE host port key|"" destination-db timeout [COPY] [REPLACE]
* [AUTH password] [AUTH2 username password] [KEYS key [key ...]] */
} else if (argc > 7 && !strcasecmp(argv[0], "migrate")) {
for (int j = 6; j < argc; j++) {
int moreargs = argc - 1 - j;
if (!strcasecmp(argv[j],"auth") && moreargs) {
return 1;
} else if (!strcasecmp(argv[j],"auth2") && moreargs >= 2) {
return 1;
} else if (!strcasecmp(argv[j],"keys") && moreargs) {
return 0;
}
}
}
return 0;
}
static void repl(void) { static void repl(void) {
sds historyfile = NULL; sds historyfile = NULL;
int history = 0; int history = 0;
...@@ -2151,10 +2203,21 @@ static void repl(void) { ...@@ -2151,10 +2203,21 @@ static void repl(void) {
char *endptr = NULL; char *endptr = NULL;
argv = cliSplitArgs(line,&argc); argv = cliSplitArgs(line,&argc);
if (argv == NULL) {
printf("Invalid argument(s)\n");
fflush(stdout);
if (history) linenoiseHistoryAdd(line);
if (historyfile) linenoiseHistorySave(historyfile);
linenoiseFree(line);
continue;
} else if (argc == 0) {
sdsfreesplitres(argv,argc);
linenoiseFree(line);
continue;
}
/* check if we have a repeat command option and /* check if we have a repeat command option and
* need to skip the first arg */ * need to skip the first arg */
if (argv && argc > 0) {
errno = 0; errno = 0;
repeat = strtol(argv[0], &endptr, 10); repeat = strtol(argv[0], &endptr, 10);
if (argc > 1 && *endptr == '\0') { if (argc > 1 && *endptr == '\0') {
...@@ -2168,32 +2231,12 @@ static void repl(void) { ...@@ -2168,32 +2231,12 @@ static void repl(void) {
} else { } else {
repeat = 1; repeat = 1;
} }
}
/* Won't save auth or acl setuser commands in history file */ if (!isSensitiveCommand(argc - skipargs, argv + skipargs)) {
int dangerous = 0;
if (argv && argc > 0) {
if (!strcasecmp(argv[skipargs], "auth")) {
dangerous = 1;
} else if (skipargs+1 < argc &&
!strcasecmp(argv[skipargs], "acl") &&
!strcasecmp(argv[skipargs+1], "setuser"))
{
dangerous = 1;
}
}
if (!dangerous) {
if (history) linenoiseHistoryAdd(line); if (history) linenoiseHistoryAdd(line);
if (historyfile) linenoiseHistorySave(historyfile); if (historyfile) linenoiseHistorySave(historyfile);
} }
if (argv == NULL) {
printf("Invalid argument(s)\n");
fflush(stdout);
linenoiseFree(line);
continue;
} else if (argc > 0) {
if (strcasecmp(argv[0],"quit") == 0 || if (strcasecmp(argv[0],"quit") == 0 ||
strcasecmp(argv[0],"exit") == 0) strcasecmp(argv[0],"exit") == 0)
{ {
...@@ -2243,7 +2286,6 @@ static void repl(void) { ...@@ -2243,7 +2286,6 @@ static void repl(void) {
printf("(%.2fs)\n",(double)elapsed/1000); printf("(%.2fs)\n",(double)elapsed/1000);
} }
} }
}
/* Free the argument vector */ /* Free the argument vector */
sdsfreesplitres(argv,argc); sdsfreesplitres(argv,argc);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment