fix(spanner): escape embedded backticks in dbapi escape_name#17810
fix(spanner): escape embedded backticks in dbapi escape_name#17810sakthivelmanii wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the escape_name function in parse_utils.py to escape backticks by doubling them, and adds corresponding unit tests. The review feedback points out a critical security vulnerability (SQL injection) where identifiers containing other special characters (such as comments or semicolons) but no spaces, hyphens, or backticks could bypass escaping. A code suggestion is provided to resolve this by strictly validating identifiers and escaping any that are not valid regular ASCII identifiers.
044851b to
15dcb8a
Compare
15dcb8a to
579da90
Compare
|
Done. Please merge main into this branch |
579da90 to
b115b8e
Compare
| ) | ||
|
|
||
| if not is_valid_regular_identifier or name.upper() in SPANNER_RESERVED_KEYWORDS: | ||
| return "`" + name.replace("`", "``") + "`" |
There was a problem hiding this comment.
GoogleSQL does not use 'doubled quotes' to escape quotes inside quotes. Instead, you should use a backslash for that:
-- This is invalid in GoogleSQL
select 1 as `fo``o`
-- This is the correct way to escape a quote in GoogleSQL
select 1 as `fo\`o`| """ | ||
| Apply backticks to the name that either contain '-' or | ||
| ' ', or is a Cloud Spanner's reserved keyword. | ||
| Apply backticks to the name if it is not a valid regular ASCII identifier, |
There was a problem hiding this comment.
There are more corner cases that need to be thought through here:
- Backslash (see also below): Backslash starts an escape sequence in GoogleSQL, unless itself is escaped. Should we escape backslashes as well to be consistent with how we handle existing backticks in identifiers? E.g. what should we do with a string like this: "test\"
- Identifiers with multiple elements (dot operator): The identifier
my_schema.my_tableis now turned into this: `my_schema.my_table`. - Already quoted identifiers: `my_table` becomes ``my_table``
In google-cloud-spanner DB-API, escape_name() did not check for or double embedded backtick characters (`) when wrapping identifiers. This allowed identifiers containing backticks to break out of backtick-quoted identifier scopes (CWE-89 identifier injection).
This commit updates escape_name() to:
- Detect embedded backtick characters in identifier names.
- Escape internal backticks by doubling them (replace("`", "``")) when enclosing identifiers in backticks.
- Add unit test cases for embedded backticks in test_escape_name.
b115b8e to
0235fa6
Compare
In google-cloud-spanner DB-API, escape_name() did not check for or double embedded backtick characters (`) when wrapping identifiers. This allowed identifiers containing backticks to break out of backtick-quoted identifier scopes (CWE-89 identifier injection).
This commit updates escape_name() to:
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕