Skip to content

Commit 335cdff

Browse files
authored
fix(query): ident role_name not support ' " \b \f in parse (#17534)
1 parent 21a3416 commit 335cdff

File tree

5 files changed

+101
-27
lines changed

5 files changed

+101
-27
lines changed

src/query/ast/src/parser/statement.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -3209,11 +3209,33 @@ pub fn create_def(i: Input) -> IResult<CreateDefinition> {
32093209
}
32103210

32113211
pub fn role_name(i: Input) -> IResult<String> {
3212-
let role_ident = map(
3212+
let role_ident = map_res(
32133213
rule! {
32143214
#ident
32153215
},
3216-
|role_name| role_name.name,
3216+
|role_name| {
3217+
let name = role_name.name;
3218+
let mut chars = name.chars();
3219+
while let Some(c) = chars.next() {
3220+
match c {
3221+
'\\' => match chars.next() {
3222+
Some('f') | Some('b') => {
3223+
return Err(nom::Err::Failure(ErrorKind::Other(
3224+
"' or \" or \\f or \\b are not allowed in role name",
3225+
)));
3226+
}
3227+
_ => {}
3228+
},
3229+
'\'' | '"' => {
3230+
return Err(nom::Err::Failure(ErrorKind::Other(
3231+
"' or \" or \\f or \\b are not allowed in role name",
3232+
)));
3233+
}
3234+
_ => {}
3235+
}
3236+
}
3237+
Ok(name)
3238+
},
32173239
);
32183240
let role_lit = map(
32193241
rule! {

src/query/ast/tests/it/parser.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,6 @@ fn test_statement() {
258258
r#"alter user 'test-e' identified by 'new-password';"#,
259259
r#"create role test"#,
260260
r#"create role 'test'"#,
261-
r#"create role `a"a`"#,
262-
r#"create role `a'a`"#,
263261
r#"create user `a'a` identified by '123'"#,
264262
r#"drop role if exists test"#,
265263
r#"drop role if exists 'test'"#,
@@ -945,6 +943,11 @@ fn test_statement_error() {
945943
r#"alter user 'test-e' identifies by 'new-password';"#,
946944
r#"create role 'test'@'%';"#,
947945
r#"drop role 'test'@'%';"#,
946+
r#"create role `a"a`"#,
947+
r#"create role `a'a`"#,
948+
r#"create role `a\ba`"#,
949+
r#"create role `a\fa`"#,
950+
r#"drop role `a\fa`"#,
948951
r#"SHOW GRANT FOR ROLE 'role1';"#,
949952
r#"GRANT ROLE 'test' TO ROLE test-user;"#,
950953
r#"GRANT SELECT, ALL PRIVILEGES, CREATE ON * TO 'test-grant';"#,

src/query/ast/tests/it/testdata/stmt-error.txt

+70
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,76 @@ error:
231231
| ^ unexpected `@`, expecting `FORMAT` or `;`
232232

233233

234+
---------- Input ----------
235+
create role `a"a`
236+
---------- Output ---------
237+
error:
238+
--> SQL:1:13
239+
|
240+
1 | create role `a"a`
241+
| ------ ^^^^^
242+
| | |
243+
| | ' or " or \f or \b are not allowed in role name
244+
| | while parsing <role_name>
245+
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`
246+
247+
248+
---------- Input ----------
249+
create role `a'a`
250+
---------- Output ---------
251+
error:
252+
--> SQL:1:13
253+
|
254+
1 | create role `a'a`
255+
| ------ ^^^^^
256+
| | |
257+
| | ' or " or \f or \b are not allowed in role name
258+
| | while parsing <role_name>
259+
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`
260+
261+
262+
---------- Input ----------
263+
create role `a\ba`
264+
---------- Output ---------
265+
error:
266+
--> SQL:1:13
267+
|
268+
1 | create role `a\ba`
269+
| ------ ^^^^^^
270+
| | |
271+
| | ' or " or \f or \b are not allowed in role name
272+
| | while parsing <role_name>
273+
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`
274+
275+
276+
---------- Input ----------
277+
create role `a\fa`
278+
---------- Output ---------
279+
error:
280+
--> SQL:1:13
281+
|
282+
1 | create role `a\fa`
283+
| ------ ^^^^^^
284+
| | |
285+
| | ' or " or \f or \b are not allowed in role name
286+
| | while parsing <role_name>
287+
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`
288+
289+
290+
---------- Input ----------
291+
drop role `a\fa`
292+
---------- Output ---------
293+
error:
294+
--> SQL:1:11
295+
|
296+
1 | drop role `a\fa`
297+
| ---- ^^^^^^
298+
| | |
299+
| | ' or " or \f or \b are not allowed in role name
300+
| | while parsing <role_name>
301+
| while parsing `DROP ROLE [IF EXISTS] <role_name>`
302+
303+
234304
---------- Input ----------
235305
SHOW GRANT FOR ROLE 'role1';
236306
---------- Output ---------

src/query/ast/tests/it/testdata/stmt.txt

-22
Original file line numberDiff line numberDiff line change
@@ -12002,28 +12002,6 @@ CreateRole {
1200212002
}
1200312003

1200412004

12005-
---------- Input ----------
12006-
create role `a"a`
12007-
---------- Output ---------
12008-
CREATE ROLE 'a"a'
12009-
---------- AST ------------
12010-
CreateRole {
12011-
if_not_exists: false,
12012-
role_name: "a\"a",
12013-
}
12014-
12015-
12016-
---------- Input ----------
12017-
create role `a'a`
12018-
---------- Output ---------
12019-
CREATE ROLE 'a\'a'
12020-
---------- AST ------------
12021-
CreateRole {
12022-
if_not_exists: false,
12023-
role_name: "a'a",
12024-
}
12025-
12026-
1202712005
---------- Input ----------
1202812006
create user `a'a` identified by '123'
1202912007
---------- Output ---------

tests/sqllogictests/suites/base/05_ddl/05_0014_ddl_create_role.test

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ create role 'Public'
4343
statement error 2217
4444
create role 'public'
4545

46+
onlyif http
4647
statement error 2217
4748
create role 'a"a'
4849

49-
statement error 2217
50+
statement error 1005
5051
create role "a'a"

0 commit comments

Comments
 (0)