Skip to content

Commit 04daf30

Browse files
author
Alexey Botchkov
committed
MDEV-13921 Audit log writes invalid SQL if single-line comments are
present. Escape special characters (like \r \n \t) instead of replacing them with spaces.
1 parent c4c48e9 commit 04daf30

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

mysql-test/suite/plugins/r/server_audit.result

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ alter table t1 rename renamed_t1;
4747
set global server_audit_events='connect,query';
4848
select 1,
4949
2,
50+
# comment
5051
3;
5152
1 2 3
5253
1 2 3
@@ -161,7 +162,9 @@ id
161162
2
162163
CREATE USER u1 IDENTIFIED BY 'pwd-123';
163164
GRANT ALL ON sa_db TO u2 IDENTIFIED BY "pwd-321";
164-
SET PASSWORD FOR u1 = PASSWORD('pwd 098');
165+
SET PASSWORD
166+
# comment
167+
FOR u1 = PASSWORD('pwd 098');
165168
SET PASSWORD FOR u1=<secret>;
166169
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '<secret>' at line 1
167170
CREATE USER u3 IDENTIFIED BY '';
@@ -253,7 +256,7 @@ TIME,HOSTNAME,root,localhost,ID,ID,WRITE,mysql,index_stats,
253256
TIME,HOSTNAME,root,localhost,ID,ID,RENAME,test,t1|test.renamed_t1,
254257
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'alter table t1 rename renamed_t1',0
255258
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'set global server_audit_events=\'connect,query\'',0
256-
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'select 1, 2, 3',0
259+
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'select 1,\n2,\n# comment\n3',0
257260
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'insert into t2 values (1), (2)',0
258261
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'select * from t2',0
259262
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,test,'select * from t_doesnt_exist',ID
@@ -336,7 +339,7 @@ TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'/*! select 2*/',0
336339
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'/*comment*/ select 2',0
337340
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'CREATE USER u1 IDENTIFIED BY *****',0
338341
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'GRANT ALL ON sa_db TO u2 IDENTIFIED BY *****',0
339-
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'SET PASSWORD FOR u1 = PASSWORD(*****)',0
342+
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'SET PASSWORD \n# comment\nFOR u1 = PASSWORD(*****)',0
340343
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'SET PASSWORD FOR u1=<secret>',ID
341344
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'CREATE USER u3 IDENTIFIED BY *****',0
342345
TIME,HOSTNAME,root,localhost,ID,ID,QUERY,sa_db,'drop user u1, u2, u3',0

mysql-test/suite/plugins/t/server_audit.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ alter table t1 rename renamed_t1;
3838
set global server_audit_events='connect,query';
3939
select 1,
4040
2,
41+
# comment
4142
3;
4243
insert into t2 values (1), (2);
4344
select * from t2;
@@ -106,7 +107,9 @@ insert into t1 values (1), (2);
106107
select * from t1;
107108
CREATE USER u1 IDENTIFIED BY 'pwd-123';
108109
GRANT ALL ON sa_db TO u2 IDENTIFIED BY "pwd-321";
109-
SET PASSWORD FOR u1 = PASSWORD('pwd 098');
110+
SET PASSWORD
111+
# comment
112+
FOR u1 = PASSWORD('pwd 098');
110113
--error 1064
111114
SET PASSWORD FOR u1=<secret>;
112115
CREATE USER u3 IDENTIFIED BY '';

plugin/server_audit/server_audit.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,21 @@ do { \
11211121
} while(0)
11221122

11231123

1124+
#define ESC_MAP_SIZE 0x60
1125+
static const char esc_map[ESC_MAP_SIZE]=
1126+
{
1127+
0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
1128+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1129+
0, 0, 0, 0, 0, 0, 0, '\'', 0, 0, 0, 0, 0, 0, 0, 0,
1130+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1131+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1132+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\\', 0, 0, 0
1133+
};
1134+
1135+
static char escaped_char(char c)
1136+
{
1137+
return ((unsigned char ) c) >= ESC_MAP_SIZE ? 0 : esc_map[(unsigned char) c];
1138+
}
11241139

11251140

11261141
static void setup_connection_initdb(struct connection_info *cn,
@@ -1327,21 +1342,16 @@ static size_t escape_string(const char *str, unsigned int len,
13271342
const char *res_end= result + result_len - 2;
13281343
while (len)
13291344
{
1345+
char esc_c;
1346+
13301347
if (result >= res_end)
13311348
break;
1332-
if (*str == '\'')
1349+
if ((esc_c= escaped_char(*str)))
13331350
{
13341351
if (result+1 >= res_end)
13351352
break;
13361353
*(result++)= '\\';
1337-
*(result++)= '\'';
1338-
}
1339-
else if (*str == '\\')
1340-
{
1341-
if (result+1 >= res_end)
1342-
break;
1343-
*(result++)= '\\';
1344-
*(result++)= '\\';
1354+
*(result++)= esc_c;
13451355
}
13461356
else if (is_space(*str))
13471357
*(result++)= ' ';
@@ -1430,19 +1440,12 @@ static size_t escape_string_hide_passwords(const char *str, unsigned int len,
14301440
no_password:
14311441
if (result >= res_end)
14321442
break;
1433-
if (*str == '\'')
1434-
{
1435-
if (result+1 >= res_end)
1436-
break;
1437-
*(result++)= '\\';
1438-
*(result++)= '\'';
1439-
}
1440-
else if (*str == '\\')
1443+
if ((b_char= escaped_char(*str)))
14411444
{
14421445
if (result+1 >= res_end)
14431446
break;
14441447
*(result++)= '\\';
1445-
*(result++)= '\\';
1448+
*(result++)= b_char;
14461449
}
14471450
else if (is_space(*str))
14481451
*(result++)= ' ';

0 commit comments

Comments
 (0)