Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(spanner): escape embedded backticks in dbapi escape_name#17810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -382,17 +382,38 @@ def ensure_where_clause(sql): | ||
| return sql + " WHERE 1=1" | ||
| _QUOTED_IDENTIFIER_RE = re.compile(r"\A`(\\.|[^`\\])*`\Z", re.DOTALL) | ||
| def escape_name(name): | ||
| """ | ||
| 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, | ||
| or if it is a Cloud Spanner's reserved keyword. | ||
| :type name: str | ||
| :param name: Name to escape. | ||
| :rtype: str | ||
| :returns: Name escaped if it has to be escaped. | ||
| """ | ||
| if "-" in name or " " in name or name.upper() in SPANNER_RESERVED_KEYWORDS: | ||
| return "`" + name + "`" | ||
| if not name: | ||
| return name | ||
| if _QUOTED_IDENTIFIER_RE.match(name): | ||
| return name | ||
| if "." in name: | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check should be done after the check whether the identifier is already quoted. Otherwise, a dot inside a validly quoted identifier would break up that identifier into multiple (invalid) parts. Also, the current loop below actually allows similar SQL injection attacks that this is intended to fix. The following string would be seen as a validly quoted identifier: # String literal with 1 literal backslash before the internal backtick:payload="`col"+"\\"+"`; DROP TABLE users; --`"# Represented in Python string repr as: '`col\\`; DROP TABLE users; --`'This results in this SQL: `col\`; DROP TABLE users; --`Could we instead use a regex to prevent this type of SQL injection attack, and also simplify the implementation a bit: importre# re.DOTALL ensures '.' in '\\.' matches newlines inside valid multiline backtick identifiers.# \A and \Z enforce strict start and end anchors._QUOTED_IDENTIFIER_RE=re.compile(r"\A`(\\.|[^`\\])*`\Z", re.DOTALL)
defescape_name(name):
ifnotname:
returnname# 1. Check if the string is ALREADY a valid GoogleSQL quoted identifierif_QUOTED_IDENTIFIER_RE.match(name):
returnname# 2. Handle qualified identifiers (e.g., schema.table or `schema`.table)if"."inname:
parts=name.split(".")
return".".join(escape_name(part) forpartinparts)
# 3. Check if it is a regular ASCII identifier and not a reserved keywordis_valid_regular_identifier= (
(name[0].isalpha() orname[0] =="_")
andall(c.isalnum() orc=="_"forcinname)
andname.isascii()
)
ifnotis_valid_regular_identifierorname.upper() inSPANNER_RESERVED_KEYWORDS:
escaped=name.replace("\\", "\\\\").replace("`", "\\`")
returnf"`{escaped}`"returnnameContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done | ||
| parts = name.split(".") | ||
| return ".".join(escape_name(part) for part in parts) | ||
| is_valid_regular_identifier = ( | ||
| (name[0].isalpha() or name[0] == "_") | ||
| and all(c.isalnum() or c == "_" for c in name) | ||
| and name.isascii() | ||
| ) | ||
| if not is_valid_regular_identifier or name.upper() in SPANNER_RESERVED_KEYWORDS: | ||
| escaped = name.replace("\\", "\\\\").replace("`", "\\`") | ||
| return f"`{escaped}`" | ||
| return name | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are more corner cases that need to be thought through here:
my_schema.my_tableis now turned into this: `my_schema.my_table`.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done