Skip to content

Commit

Permalink
fix(query): ident role_name not support ' " \b \f in parse (#17534)
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason authored Feb 27, 2025
1 parent 21a3416 commit 335cdff
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 27 deletions.
26 changes: 24 additions & 2 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3209,11 +3209,33 @@ pub fn create_def(i: Input) -> IResult<CreateDefinition> {
}

pub fn role_name(i: Input) -> IResult<String> {
let role_ident = map(
let role_ident = map_res(
rule! {
#ident
},
|role_name| role_name.name,
|role_name| {
let name = role_name.name;
let mut chars = name.chars();
while let Some(c) = chars.next() {
match c {
'\\' => match chars.next() {
Some('f') | Some('b') => {
return Err(nom::Err::Failure(ErrorKind::Other(
"' or \" or \\f or \\b are not allowed in role name",
)));
}
_ => {}
},
'\'' | '"' => {
return Err(nom::Err::Failure(ErrorKind::Other(
"' or \" or \\f or \\b are not allowed in role name",
)));
}
_ => {}
}
}
Ok(name)
},
);
let role_lit = map(
rule! {
Expand Down
7 changes: 5 additions & 2 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ fn test_statement() {
r#"alter user 'test-e' identified by 'new-password';"#,
r#"create role test"#,
r#"create role 'test'"#,
r#"create role `a"a`"#,
r#"create role `a'a`"#,
r#"create user `a'a` identified by '123'"#,
r#"drop role if exists test"#,
r#"drop role if exists 'test'"#,
Expand Down Expand Up @@ -945,6 +943,11 @@ fn test_statement_error() {
r#"alter user 'test-e' identifies by 'new-password';"#,
r#"create role 'test'@'%';"#,
r#"drop role 'test'@'%';"#,
r#"create role `a"a`"#,
r#"create role `a'a`"#,
r#"create role `a\ba`"#,
r#"create role `a\fa`"#,
r#"drop role `a\fa`"#,
r#"SHOW GRANT FOR ROLE 'role1';"#,
r#"GRANT ROLE 'test' TO ROLE test-user;"#,
r#"GRANT SELECT, ALL PRIVILEGES, CREATE ON * TO 'test-grant';"#,
Expand Down
70 changes: 70 additions & 0 deletions src/query/ast/tests/it/testdata/stmt-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,76 @@ error:
| ^ unexpected `@`, expecting `FORMAT` or `;`


---------- Input ----------
create role `a"a`
---------- Output ---------
error:
--> SQL:1:13
|
1 | create role `a"a`
| ------ ^^^^^
| | |
| | ' or " or \f or \b are not allowed in role name
| | while parsing <role_name>
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`


---------- Input ----------
create role `a'a`
---------- Output ---------
error:
--> SQL:1:13
|
1 | create role `a'a`
| ------ ^^^^^
| | |
| | ' or " or \f or \b are not allowed in role name
| | while parsing <role_name>
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`


---------- Input ----------
create role `a\ba`
---------- Output ---------
error:
--> SQL:1:13
|
1 | create role `a\ba`
| ------ ^^^^^^
| | |
| | ' or " or \f or \b are not allowed in role name
| | while parsing <role_name>
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`


---------- Input ----------
create role `a\fa`
---------- Output ---------
error:
--> SQL:1:13
|
1 | create role `a\fa`
| ------ ^^^^^^
| | |
| | ' or " or \f or \b are not allowed in role name
| | while parsing <role_name>
| while parsing `CREATE ROLE [IF NOT EXISTS] <role_name>`


---------- Input ----------
drop role `a\fa`
---------- Output ---------
error:
--> SQL:1:11
|
1 | drop role `a\fa`
| ---- ^^^^^^
| | |
| | ' or " or \f or \b are not allowed in role name
| | while parsing <role_name>
| while parsing `DROP ROLE [IF EXISTS] <role_name>`


---------- Input ----------
SHOW GRANT FOR ROLE 'role1';
---------- Output ---------
Expand Down
22 changes: 0 additions & 22 deletions src/query/ast/tests/it/testdata/stmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12002,28 +12002,6 @@ CreateRole {
}


---------- Input ----------
create role `a"a`
---------- Output ---------
CREATE ROLE 'a"a'
---------- AST ------------
CreateRole {
if_not_exists: false,
role_name: "a\"a",
}


---------- Input ----------
create role `a'a`
---------- Output ---------
CREATE ROLE 'a\'a'
---------- AST ------------
CreateRole {
if_not_exists: false,
role_name: "a'a",
}


---------- Input ----------
create user `a'a` identified by '123'
---------- Output ---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ create role 'Public'
statement error 2217
create role 'public'

onlyif http
statement error 2217
create role 'a"a'

statement error 2217
statement error 1005
create role "a'a"

0 comments on commit 335cdff

Please sign in to comment.