Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/pandas-gbq/pandas_gbq/core/resource_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def parse_table_id(table_id: str) -> Union[BigLakeTableId, BigQueryTableId]:
if any(part == "" for part in inner_parts):
raise ValueError(f"Invalid table ID: {table_id}")

# The parsed parts are interpolated into backtick-quoted table references in
# generated SQL (see core/biglake.py and core/sample.py). A backtick can't
# appear in a real project/dataset/table name, and one here would close the
# 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}")
Comment on lines +61 to +62

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}")

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


if len(inner_parts) == 1:
return BigQueryTableId(
project_id=regex_match.group("project"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def test_parse_table_id_valid(table_id, expected):
".my_dataset.my_table",
"my-project.my_dataset.",
"my-project..my_table",
# A backtick would close the identifier quoting in the generated SQL and
# let the rest of the string run as SQL, so it must be rejected here.
"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.`",
Comment on lines +72 to +74

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

],
)
def test_parse_table_id_invalid(table_id):
Expand Down
Loading