-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -450,7 +450,10 @@ def add_index(self, model, index): | |
| def quote_value(self, value): | ||
| # A more complete implementation isn't currently required. | ||
| if isinstance(value, str): | ||
| return "'%s'" % value.replace("'", "''") | ||
| # GoogleSQL string literals use backslash escaping; '' quote | ||
| # doubling is not recognized, so escape the backslash first and | ||
| # then the quote (matching the db_default/generated inlining above). | ||
| return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'") | ||
|
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. In GoogleSQL, single-quoted string literals cannot contain literal unescaped newlines (\n) or carriage returns (\r). If a multiline default or comment is passed to quote_value, it causes a syntax error in Spanner DDL. Please escape them using Also add this test to verify: 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'",
) |
||
| if isinstance(value, bool): | ||
| return "TRUE" if value else "FALSE" | ||
| return str(value) | ||
|
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. 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: def test_quote_value_handles_none(self):
schema_editor = DatabaseSchemaEditor(self.connection)
self.assertEqual(schema_editor.quote_value(None), "NULL")
import datetime
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'",
) |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,19 @@ 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") |
||
| """ | ||
| 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_skip_default(self): | ||
| """ | ||
| Tries skipping default as Cloud spanner doesn't support it. | ||
|
|
||
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.
In GoogleSQL, single-quoted string literals cannot contain literal newlines (
\n) or carriage returns (\r). If a string containing these characters is passed toquote_value, it will result in a syntax error in Spanner. To prevent this, we should also escape newlines and carriage returns.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.
@Samin061 can you address this comment as well?