Skip to content

fix(pandas-gbq): reject backticks in parse_table_id - #18156

Merged
parthea merged 1 commit into
googleapis:mainfrom
Samin061:pandas-gbq-table-id-backtick
Aug 19, 2026
Merged

fix(pandas-gbq): reject backticks in parse_table_id#18156
parthea merged 1 commit into
googleapis:mainfrom
Samin061:pandas-gbq-table-id-backtick

Conversation

@Samin061

Copy link
Copy Markdown
Contributor

parse_table_id validates the table id but its regex only excludes "." and ":" at certain positions, so a backtick passes through into the parts that core/biglake.py and core/sample.py interpolate into a backtick-quoted FROM ... and run via bqclient.query. A backtick closes the identifier quoting and lets the remainder of the id run as SQL, and the public sample() entry point routes every table id through this one helper, so an id like p.c.n.t ORDER BY (SELECT 1) --` reaches the query. Reject backticks in parse_table_id where the id is already validated; valid project/dataset/table names can't contain one, so legitimate ids are unchanged.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

@Samin061
Samin061 requested review from a team as code owners August 19, 2026 11:27
@Samin061
Samin061 requested review from julianxu-goog and removed request for a team August 19, 2026 11:27

@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 introduces validation to reject backticks in table IDs within pandas-gbq to prevent SQL injection in downstream generated SQL queries, along with corresponding unit tests. The review feedback recommends extending this validation to also reject backslashes, which can be used to escape backticks in BigQuery SQL and bypass the protection, and suggests adding corresponding test cases.

Comment on lines +61 to +62
if "`" in table_id:
raise ValueError(f"Invalid table ID: {table_id}")

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.

security-high high

While rejecting backticks prevents direct closing of the identifier quoting, a backslash (\) can be used in BigQuery SQL to escape the closing backtick (e.g., ```). If a table ID ends with a backslash, it would escape the closing backtick in the generated SQL, potentially leading to SQL injection or syntax errors if there are subsequent backticks in the query. Since backslashes are not valid characters in BigQuery project, dataset, or table IDs, they should also be rejected here to prevent any escaping-based bypasses.

Suggested change
if "`" in table_id:
raise ValueError(f"Invalid table ID: {table_id}")
if "`" in table_id or "\\" in table_id:
raise ValueError(f"Invalid table ID: {table_id}")

Comment on lines +72 to +74
"my-project.my_dataset.my_table` ORDER BY (SELECT 1) -- ",
"my-project.my_catalog.my_namespace.evil` UNION ALL SELECT 1 -- ",
"my-project.my_dataset.`",

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.

medium

In addition to backticks, we should also test that table IDs containing backslashes are rejected, as they can be used to escape the closing backtick in generated SQL.

        "my-project.my_dataset.my_table` ORDER BY (SELECT 1) -- ",
        "my-project.my_catalog.my_namespace.evil` UNION ALL SELECT 1 -- ",
        "my-project.my_dataset.`",
        "my-project.my_dataset.my_table\\",
        "my-project.my_dataset.my_\\table",

@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Aug 19, 2026
@parthea parthea self-assigned this Aug 19, 2026
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Aug 19, 2026
# identifier quoting and let the rest of the string run as SQL, so reject it
# while we're validating the table ID rather than downstream.
if "`" in table_id:
raise ValueError(f"Invalid table ID: {table_id}")

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.

nit (non-blocking): We could move this before the regex_match on line 48 but I don't have a strong opinion about it

@parthea
parthea enabled auto-merge (squash) August 19, 2026 15:46
@parthea parthea removed their assignment Aug 19, 2026
@parthea
parthea merged commit f687060 into googleapis:main Aug 19, 2026
141 of 142 checks passed
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.

4 participants