Skip to content

fix(django-google-spanner): harden literal quoting in schema DDL - #19246

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-django-harden-literal-quoting
Open

olavloite wants to merge 1 commit into
mainfrom
spanner-django-harden-literal-quoting

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

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.

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.
@olavloite
olavloite requested a review from a team as a code owner November 27, 2026 10:36

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant