-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(sqlalchemy-spanner): escape names in reflection queries #18205
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
Open
Samin061
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
Samin061:sqlalchemy-spanner-reflection-escape
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -98,3 +98,59 @@ def test_max_size_exported(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(SpannerDialect.max_size, MAX_SIZE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(int_from_size("MAX"), 2621440) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(int_from_size("100"), 100) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _mock_connection(rows=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_snapshot = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_snapshot.execute_sql.return_value = rows if rows is not None else [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection.connection.database.snapshot.return_value.__enter__.return_value = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_snapshot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return connection, mock_snapshot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_get_columns_escapes_quote_in_table_name(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A single quote in a reflected table name must not break out of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| INFORMATION_SCHEMA string literal in get_columns.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dialect = SpannerDialect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection, mock_snapshot = self._mock_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dialect.get_columns(connection, table_name="t' OR '1'='1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executed_sql = mock_snapshot.execute_sql.call_args[0][0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "col.table_name = 't\\' OR \\'1\\'=\\'1'" in executed_sql | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "col.table_name = 't' OR '1'='1'" not in executed_sql | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_has_table_escapes_quote_in_table_name(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A double quote in a reflected table name must not break out of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| INFORMATION_SCHEMA string literal in has_table.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dialect = SpannerDialect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection, mock_snapshot = self._mock_connection() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dialect.has_table(connection, table_name='a" OR "1"="1') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executed_sql = mock_snapshot.execute_sql.call_args[0][0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert 'TABLE_NAME="a\\" OR \\"1\\"=\\"1"' in executed_sql | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert 'TABLE_NAME="a" OR "1"="1"' not in executed_sql | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_get_view_definition_escapes_quote(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A quote in a reflected view name must not break out of the literal.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dialect = SpannerDialect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection, mock_snapshot = self._mock_connection(rows=[["def"]]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dialect.get_view_definition(connection, view_name="v' OR '1'='1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executed_sql = mock_snapshot.execute_sql.call_args[0][0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "TABLE_NAME='v\\' OR \\'1\\'=\\'1'" in executed_sql | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_escape_sql_string_literal(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """The helper escapes backslashes, both quote styles and newlines.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from google.cloud.sqlalchemy_spanner.sqlalchemy_spanner import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _escape_sql_string_literal, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(_escape_sql_string_literal("a'b"), "a\\'b") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(_escape_sql_string_literal('a"b'), 'a\\"b') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(_escape_sql_string_literal("a\\b"), "a\\\\b") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(_escape_sql_string_literal("a\nb"), "a\\nb") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq_(_escape_sql_string_literal("plain"), "plain") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+146
to
+156
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. Let's add test assertions to verify that
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When
_escape_sql_string_literalreceives parameters of an unsupported type (such asNoneor non-string types), it should raise an error (e.g.,ProgrammingError) instead of silently returning empty values or converting them. This ensures fail-fast behavior and prevents potential issues with missing parameter values in database operations.References
ProgrammingError) instead of silently returning empty values. This ensures fail-fast behavior and prevents potential issues with missing parameter values in database operations.