-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -384,15 +384,45 @@ def ensure_where_clause(sql): | |
|
|
||
| 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 "." 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: import re
# 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)
def escape_name(name):
if not name:
return name
# 1. Check if the string is ALREADY a valid GoogleSQL quoted identifier
if _QUOTED_IDENTIFIER_RE.match(name):
return name
# 2. Handle qualified identifiers (e.g., schema.table or `schema`.table)
if "." in name:
parts = name.split(".")
return ".".join(escape_name(part) for part in parts)
# 3. Check if it is a regular ASCII identifier and not a reserved keyword
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 |
||
| parts = name.split(".") | ||
| return ".".join(escape_name(part) for part in parts) | ||
|
|
||
| if len(name) >= 2 and name.startswith("`") and name.endswith("`"): | ||
| inner = name[1:-1] | ||
| i = 0 | ||
| is_properly_quoted = True | ||
| while i < len(inner): | ||
| if inner[i] == "\\": | ||
| i += 2 | ||
| elif inner[i] == "`": | ||
| is_properly_quoted = False | ||
| break | ||
| else: | ||
| i += 1 | ||
| if is_properly_quoted: | ||
| return name | ||
|
|
||
| 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 | ||
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