Skip to content

fix(spanner): escape embedded backticks in dbapi escape_name - #17810

Merged
sakthivelmanii merged 1 commit into
mainfrom
fix-spanner-dbapi-escape-name
Jul 27, 2026
Merged

fix(spanner): escape embedded backticks in dbapi escape_name#17810
sakthivelmanii merged 1 commit into
mainfrom
fix-spanner-dbapi-escape-name

Conversation

@sakthivelmanii

@sakthivelmanii sakthivelmanii commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

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 adding backslashes (replace("", "\")) when enclosing identifiers in backticks.
  • Add unit test cases for embedded backticks in test_escape_name.

@sakthivelmanii
sakthivelmanii requested a review from a team as a code owner July 21, 2026 17:59

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/google-cloud-spanner/google/cloud/spanner_dbapi/parse_utils.py Outdated
@sakthivelmanii
sakthivelmanii force-pushed the fix-spanner-dbapi-escape-name branch 2 times, most recently from 044851b to 15dcb8a Compare July 22, 2026 04:49
@sakthivelmanii
sakthivelmanii requested a review from olavloite July 23, 2026 07:51
@sakthivelmanii
sakthivelmanii force-pushed the fix-spanner-dbapi-escape-name branch from 15dcb8a to 579da90 Compare July 23, 2026 08:13
@parthea

parthea commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Wait for #17880 to be merged first

Done. Please merge main into this branch

@parthea
parthea marked this pull request as draft July 24, 2026 18:49
@sakthivelmanii
sakthivelmanii force-pushed the fix-spanner-dbapi-escape-name branch from 579da90 to b115b8e Compare July 27, 2026 06:42
@sakthivelmanii
sakthivelmanii marked this pull request as ready for review July 27, 2026 08:03
)

if not is_valid_regular_identifier or name.upper() in SPANNER_RESERVED_KEYWORDS:
return "`" + name.replace("`", "``") + "`"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"""
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,

Copy link
Copy Markdown
Contributor

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:

  1. 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\"
  2. Identifiers with multiple elements (dot operator): The identifier my_schema.my_table is now turned into this: `my_schema.my_table`.
  3. Already quoted identifiers: `my_table` becomes ``my_table``

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@sakthivelmanii
sakthivelmanii force-pushed the fix-spanner-dbapi-escape-name branch from b115b8e to 0235fa6 Compare July 27, 2026 09:14
if not name:
return name

if "." in name:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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.
@sakthivelmanii
sakthivelmanii force-pushed the fix-spanner-dbapi-escape-name branch from 0235fa6 to 302b7d8 Compare July 27, 2026 12:01
@sakthivelmanii
sakthivelmanii merged commit c8b0b28 into main Jul 27, 2026
39 checks passed
@sakthivelmanii
sakthivelmanii deleted the fix-spanner-dbapi-escape-name branch July 27, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants