Repository navigation
Conversation
Django's schema editor uses literal value quoting when generating DDL statements that do not support query parameters (such as check constraints, database defaults, and generated columns). Previously, string escaping only doubled single quotes—which GoogleSQL does not treat as an escape sequence—and did not escape backslashes or control characters, leaving a SQL injection risk and breaking strings containing backslashes or quotes. All non-string, non-boolean types also fell back to raw `str(value)`, producing invalid or unquoted SQL for dates, timestamps, UUIDs, decimals, bytes, JSON, and `None`. This change fixes literal quoting and escaping in the schema editor: - Escapes backslashes, single quotes, newlines, carriage returns, NUL bytes, and other ASCII control characters using valid GoogleSQL string escapes. - Formats all supported Django and Spanner value types explicitly (`None`, booleans, integers, floats including `NaN` and infinities, finite decimals as `NUMERIC`, dates, UTC-normalized timestamps, durations, UUIDs, JSON values, and hex-escaped byte literals), using built-in formatting rather than trusting custom subclass `__str__` methods. - Reuses the shared literal quoting logic for generated columns and database defaults instead of duplicating partial quoting loops. - Raises a `ValueError` for unsupported types (as well as non-finite decimals and timezone-aware times) instead of falling back to emitting raw, unquoted `str(value)` into DDL. - Adds unit tests covering GoogleSQL escaping, injection payloads, all supported value types, subclass safety, unsupported types, and DDL generation. Note on compatibility: Passing an unrecognized Python type to literal quoting (for example, a custom field returning an unadapted object or a lazy translation proxy inside a bare `Value()` expression) now raises `ValueError` instead of falling back to raw `str(value)`. This matches Django's SQLite backend and prevents invalid or unsafe DDL from being emitted silently.
Contributor
There was a problem hiding this comment.
Code Review
This pull request significantly enhances SQL escaping and literal quoting in the django-google-spanner schema editor. It introduces a robust quote_value method that correctly formats and escapes a wide range of Python types (including strings, decimals, temporal types, bytes, UUIDs, and JSON) into GoogleSQL-compliant literals, preventing SQL injection and formatting issues in DDL statements like GeneratedField and db_default. Additionally, comprehensive unit tests have been added to verify the correctness and safety of the quoting logic. There are no review comments, and I have no feedback to provide.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Django's schema editor uses literal value quoting when generating DDL statements that do not support query parameters (such as check constraints, database defaults, and generated columns). Previously, string escaping only doubled single quotes—which GoogleSQL does not treat as an escape sequence—and did not escape backslashes or control characters, leaving a SQL injection risk and breaking strings containing backslashes or quotes. All non-string, non-boolean types also fell back to raw
str(value), producing invalid or unquoted SQL for dates, timestamps, UUIDs, decimals, bytes, JSON, andNone.This change fixes literal quoting and escaping in the schema editor:
None, booleans, integers, floats includingNaNand infinities, finite decimals asNUMERIC, dates, UTC-normalized timestamps, durations, UUIDs, JSON values, and hex-escaped byte literals), using built-in formatting rather than trusting custom subclass__str__methods.ValueErrorfor unsupported types (as well as non-finite decimals and timezone-aware times) instead of falling back to emitting raw, unquotedstr(value)into DDL.Note on compatibility: Passing an unrecognized Python type to literal quoting (for example, a custom field returning an unadapted object or a lazy translation proxy inside a bare
Value()expression) now raisesValueErrorinstead of falling back to rawstr(value). This matches Django's SQLite backend and prevents invalid or unsafe DDL from being emitted silently.