Skip to content

Commit 04c191b

Browse files
committed
Backport of WL#16949 to 8.0.
Introduced a new --commands option to mysql (default: ON) to disable mysql client side commands in non-interactive mode. Change-Id: I7c9c3352cc36c063cd011e7e43170f280602d485
1 parent 0c52e01 commit 04c191b

File tree

4 files changed

+506
-11
lines changed

4 files changed

+506
-11
lines changed

client/client_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ enum options_client {
185185
OPT_SSL_SESSION_DATA_CONTINUE_ON_FAILED_REUSE,
186186
OPT_LONG_QUERY_TIME,
187187
OPT_AUTHENTICATION_KERBEROS_CLIENT_MODE,
188+
OPT_MYSQL_COMMANDS,
188189
/* Add new option above this */
189190
OPT_MAX_CLIENT_OPTION
190191
};

client/mysql.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ static char *opt_mysql_unix_port = nullptr;
179179
static char *opt_bind_addr = nullptr;
180180
static int connect_flag = CLIENT_INTERACTIVE;
181181
static bool opt_binary_mode = false;
182+
static bool opt_commands = true;
182183
static bool opt_connect_expired_password = false;
183184
static char *current_host;
184185
static char *dns_srv_name;
@@ -1231,7 +1232,7 @@ inline int get_command_index(char cmd_char) {
12311232

12321233
static int delimiter_index = -1;
12331234
static int charset_index = -1;
1234-
static bool real_binary_mode = false;
1235+
static bool disable_commands = false;
12351236

12361237
#ifdef _WIN32
12371238
BOOL windows_ctrl_handler(DWORD fdwCtrlType) {
@@ -1690,6 +1691,9 @@ static struct my_option my_long_options[] = {
16901691
{"column-type-info", OPT_COLUMN_TYPES, "Display column type information.",
16911692
&column_types_flag, &column_types_flag, nullptr, GET_BOOL, NO_ARG, 0, 0, 0,
16921693
nullptr, 0, nullptr},
1694+
{"commands", OPT_MYSQL_COMMANDS,
1695+
"Enable or disable processing of local mysql commands.", &opt_commands,
1696+
&opt_commands, nullptr, GET_BOOL, NO_ARG, 1, 0, 0, nullptr, 0, nullptr},
16931697
{"comments", 'c',
16941698
"Preserve comments. Send comments to the server."
16951699
" The default is --skip-comments (discard comments), enable with "
@@ -2203,7 +2207,7 @@ static int read_and_execute(bool interactive) {
22032207
size_t line_length = 0;
22042208
status.exit_status = 1;
22052209

2206-
real_binary_mode = !interactive && opt_binary_mode;
2210+
disable_commands = !interactive && (opt_binary_mode || !opt_commands);
22072211
for (;;) {
22082212
/* Reset as SIGINT has already got handled. */
22092213
sigint_received = false;
@@ -2214,15 +2218,15 @@ static int read_and_execute(bool interactive) {
22142218
In that case, we need to double check that we have a valid
22152219
line before actually setting line_length to read_length.
22162220
*/
2217-
line = batch_readline(status.line_buff, real_binary_mode);
2221+
line = batch_readline(status.line_buff, opt_binary_mode);
22182222
if (line) {
22192223
line_length = status.line_buff->read_length;
22202224

22212225
/*
22222226
ASCII 0x00 is not allowed appearing in queries if it is not in
22232227
binary mode.
22242228
*/
2225-
if (!real_binary_mode && strlen(line) != line_length) {
2229+
if (!opt_binary_mode && strlen(line) != line_length) {
22262230
status.exit_status = 1;
22272231
String msg;
22282232
msg.append(
@@ -2351,10 +2355,10 @@ static int read_and_execute(bool interactive) {
23512355

23522356
/*
23532357
If the function is called by 'source' command, it will return to
2354-
interactive mode, so real_binary_mode should be false. Otherwise, it will
2355-
exit the program, it is safe to set real_binary_mode to false.
2358+
interactive mode, so disable_commands should be false. Otherwise, it will
2359+
exit the program, it is safe to set disable_commands to false.
23562360
*/
2357-
real_binary_mode = false;
2361+
disable_commands = false;
23582362
return status.exit_status;
23592363
}
23602364

@@ -2381,10 +2385,10 @@ static COMMANDS *find_command(char cmd_char) {
23812385
int index = -1;
23822386

23832387
/*
2384-
In binary-mode, we disallow all mysql commands except '\C'
2388+
If specified, we disallow all mysql commands except '\C'
23852389
and DELIMITER.
23862390
*/
2387-
if (real_binary_mode) {
2391+
if (disable_commands) {
23882392
if (cmd_char == 'C') index = charset_index;
23892393
} else
23902394
index = get_command_index(cmd_char);
@@ -2419,7 +2423,7 @@ static COMMANDS *find_command(char *name) {
24192423
this is not a delimiter command, let add_line() take care of
24202424
parsing the row and calling find_command().
24212425
*/
2422-
if ((!real_binary_mode && strstr(name, "\\g")) ||
2426+
if ((!disable_commands && strstr(name, "\\g")) ||
24232427
(strstr(name, delimiter) &&
24242428
!is_delimiter_command(name, DELIMITER_NAME_LEN)))
24252429
return (COMMANDS *)nullptr;
@@ -2432,7 +2436,7 @@ static COMMANDS *find_command(char *name) {
24322436
len = (uint)strlen(name);
24332437

24342438
int index = -1;
2435-
if (real_binary_mode) {
2439+
if (disable_commands) {
24362440
if (is_delimiter_command(name, len)) index = delimiter_index;
24372441
} else {
24382442
/*

mysql-test/r/mysql_commands.result

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#
2+
# WL#16949: A mysql command line option to disable all built-in commands
3+
#
4+
# FR1: --commands boolean option added
5+
1
6+
1
7+
2
8+
2
9+
3
10+
3
11+
4
12+
4
13+
5
14+
5
15+
6
16+
6
17+
# FR1.1: check the default value of --commands
18+
# test: must not fail
19+
include/assert_grep.inc [FR1.1: checking the default --commands value]
20+
include/assert_grep.inc [FR1.1: checking the updated --commands value]
21+
# FR1.2.1: help short disabled
22+
ERROR at line 1: Unknown command '\?'.
23+
# FR1.2.2: clear disabled
24+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'clear' at line 1
25+
# FR1.2.3: connect disabled
26+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'connect' at line 1
27+
# FR1.2.4: delimier enabled
28+
# FR1.2.5: edit disabled
29+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'edit' at line 1
30+
# FR1.2.6: ego disabled
31+
ERROR at line 1: Unknown command '\G'.
32+
# FR1.2.7: exit disabled
33+
1
34+
1
35+
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exit' at line 1
36+
# FR1.2.8: go enabled
37+
go
38+
1
39+
# FR1.2.9: help disabled
40+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
41+
# FR1.2.10: nopager disabled
42+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nopager' at line 1
43+
# FR1.2.11: notee disabled
44+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'notee' at line 1
45+
# FR1.2.12: pager disabled
46+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pager more' at line 1
47+
# FR1.2.13: print disabled
48+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'print' at line 1
49+
# FR1.2.14: prompt disabled
50+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'prompt foo' at line 1
51+
# FR1.2.15: quit disabled
52+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'quit' at line 1
53+
# FR1.2.16: rehash disabled
54+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rehash' at line 1
55+
# FR1.2.17: source disabled
56+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source gogo.text' at line 1
57+
# FR1.2.18: status disabled
58+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'status' at line 1
59+
# FR1.2.19: system disabled
60+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'system echo' at line 1
61+
# FR1.2.20: tee disabled
62+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tee foo' at line 1
63+
# FR1.2.21: use disabled
64+
ERROR at line 1: Unknown command '\u'.
65+
# FR1.2.22: charset disabled
66+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'charset latin1' at line 1
67+
# FR1.2.22.1: \c enabled
68+
# FR1.2.23: warnings disabled
69+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warnings' at line 1
70+
# FR1.2.24: nowarning disabled
71+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nowarning' at line 1
72+
# FR1.2.25: resetconnection disabled
73+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'resetconnection' at line 1
74+
# FR1.2.26: query_attributes disabled
75+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'query_attributes a b' at line 1
76+
# FR1.2.27: ssl_session_data_print disabled
77+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ssl_session_data_print foo' at line 1
78+
# FR1.3: system-command ignored when commands=off
79+
# must fail with syntax error
80+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'system cd foo' at line 1
81+
# FR1.3: system-command ignored when commands=off in reverse
82+
# must fail with syntax error
83+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'system cd foo' at line 1
84+
# FR1.4: resetconnection enabled when commands is ON
85+
# FR1.5: system-command=off in effect when commands=on
86+
# must fail with specific error
87+
ERROR at line 1: 'system' command received, but the --system-command option is off. Skipping.
88+
# FR1.5: system-command=off in effect when commands=on in reverse
89+
# must fail with specific error
90+
ERROR at line 1: 'system' command received, but the --system-command option is off. Skipping.
91+
# FR1.6: commands in .cnf files work
92+
include/assert_grep.inc [FR1.1: the --commands value must be true]
93+
# FR1.7: --binary-mode=1 overrides --commands
94+
# must fail with a syntax error
95+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'system echo foo' at line 1
96+
trying in reverse
97+
# must fail with a syntax error
98+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'system echo foo' at line 1
99+
# FR1.8: --binary-mode=0 enables --commands
100+
# must fail with a syntax error
101+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'system echo foo' at line 1
102+
# End of tests

0 commit comments

Comments
 (0)