-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(django-spanner): backslash-escape string literals in quote_value #18234
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
2
commits into
googleapis:main
Choose a base branch
from
Samin061:django-spanner-quote-value-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.
+83
−2
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| # https://developers.google.com/open-source/licenses/bsd | ||
|
|
||
|
|
||
| import datetime | ||
| from unittest import mock | ||
|
|
||
| from django.db import NotSupportedError, connection, connections | ||
|
|
@@ -40,6 +41,63 @@ def test_quote_value(self): | |
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual(schema_editor.quote_value(value=1.1), "1.1") | ||
|
|
||
| def test_quote_value_escapes_string(self): | ||
|
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. Also add: def test_quote_value_booleans(self):
schema_editor = DatabaseSchemaEditor(self.connection)
self.assertEqual(schema_editor.quote_value(True), "TRUE")
self.assertEqual(schema_editor.quote_value(False), "FALSE")
def test_prepare_default_delegates_to_quote_value(self):
schema_editor = DatabaseSchemaEditor(self.connection)
self.assertEqual(schema_editor.prepare_default("o'brien"), "'o\\'brien'")
self.assertEqual(schema_editor.prepare_default(True), "TRUE")
Contributor
Author
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. Added both in e109b70. |
||
| """ | ||
| String literals must be backslash-escaped for GoogleSQL. A quote or | ||
| backslash in the value must not be able to terminate the literal. | ||
| """ | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual(schema_editor.quote_value(value="o'brien"), "'o\\'brien'") | ||
| self.assertEqual(schema_editor.quote_value(value="a\\b"), "'a\\\\b'") | ||
| self.assertEqual( | ||
| schema_editor.quote_value(value="\\'; DROP TABLE t; --"), | ||
| "'\\\\\\'; DROP TABLE t; --'", | ||
| ) | ||
|
|
||
| def test_quote_value_escapes_newlines_and_carriage_returns(self): | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual( | ||
| schema_editor.quote_value("line1\nline2"), | ||
| "'line1\\nline2'", | ||
| ) | ||
| self.assertEqual( | ||
| schema_editor.quote_value("line1\r\nline2"), | ||
| "'line1\\r\\nline2'", | ||
| ) | ||
|
|
||
| def test_quote_value_handles_none(self): | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual(schema_editor.quote_value(None), "NULL") | ||
|
|
||
| def test_quote_value_handles_date_and_datetime(self): | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual( | ||
| schema_editor.quote_value(datetime.date(2026, 9, 4)), | ||
| "'2026-09-04'", | ||
| ) | ||
| self.assertEqual( | ||
| schema_editor.quote_value(datetime.datetime(2026, 9, 4, 12, 0, 0)), | ||
| "'2026-09-04 12:00:00'", | ||
| ) | ||
|
|
||
| def test_quote_value_handles_bytes(self): | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual(schema_editor.quote_value(b"abc"), "b'abc'") | ||
| self.assertEqual( | ||
| schema_editor.quote_value(b"\x00'\\\n\xff"), | ||
| "b'\\x00\\x27\\x5c\\x0a\\xff'", | ||
| ) | ||
|
|
||
| def test_quote_value_booleans(self): | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual(schema_editor.quote_value(True), "TRUE") | ||
| self.assertEqual(schema_editor.quote_value(False), "FALSE") | ||
|
|
||
| def test_prepare_default_delegates_to_quote_value(self): | ||
| schema_editor = DatabaseSchemaEditor(self.connection) | ||
| self.assertEqual(schema_editor.prepare_default("o'brien"), "'o\\'brien'") | ||
| self.assertEqual(schema_editor.prepare_default(True), "TRUE") | ||
|
|
||
| def test_skip_default(self): | ||
| """ | ||
| Tries skipping default as Cloud spanner doesn't support it. | ||
|
|
||
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.
Not directly part of this change, but still: quote_value falls through to str(value) when value is None, returning the literal string "None" instead of SQL "NULL". In GoogleSQL DDL, DEFAULT (None) causes a syntax/name-resolution error. Please handle None explicitly by returning "NULL".
Similar problems also occur for Date, Timestamp and Bytes.
Add these tests to verify:
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 in e109b70.
Nonenow returnsNULL,date/datetimeare quoted viaisoformat(space separator for datetime, so your tests pass as written), and bytes become a GoogleSQLb'...'literal with the quote, backslash and anything outside printable ASCII written as\xNN, so a bytes value can't close the literal either.One note on bytes:
effective_defaultalready runs a BinaryField default throughDatabase.Binary, which base64-encodes it, so what reachesquote_valueon that path is the base64 text. That matches whatconvert_binaryfield_valuedecodes on read. Tests added for None, date/datetime and bytes.