Skip to content

Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration - #71926

Merged
vatsrahul1001 merged 3 commits into
apache:mainfrom
hkc-8010:fix/xcom-nan-escaped-string-migration
Sep 10, 2026
Merged

vatsrahul1001 merged 3 commits into
apache:mainfrom
hkc-8010:fix/xcom-nan-escaped-string-migration

Conversation

@hkc-8010

@hkc-8010 hkc-8010 commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

closes: #71921

Summary

Migration 0049 quotes non-finite floats before the bytea -> JSONB cast:

regexp_replace(txt, '([:,\[]\s*|^)(NaN|-?Infinity)(?=\s*[,}\]]|$)', '\1"\2"', 'g')

The quotes are unescaped, which is only valid at the top level of a JSON document. When a task
pushes already-serialized JSON the value is a JSON string whose interior quotes are
backslash-escaped, so \"commission\": NaN} becomes \"commission\": "NaN"}. The raw quote closes
the wrapping string, the following NaN is left bare, and the cast aborts:

DETAIL:  Token "NaN" is invalid.
CONTEXT:  JSON data, line 1: "{\"amount\": 604441.0, \"commission\": "NaN...

The sanitizer is what creates the invalid JSON. The stored bytea is exactly what Airflow 2's
json.dumps wrote and it casts fine before sanitization. #53812, #57614, #57866, #62686 and #69064
each refined which tokens match, never the replacement, so no release fixes this.

Fix

Two statements per dialect instead of one nested expression:

  1. Strip the U+0000 escape, unconditionally, on rows that contain it.
  2. Rewrite non-finite floats to bare null, only on rows that do not already parse as JSON.

If a value parses, its NaN can only be inside a string, so it is left byte for byte alone. Rows
that do need the rewrite get null, because a value can be invalid at the top level and wrap an
escaped document, and only null is valid at any depth. Order matters: strip first, or a value
invalid only because of the escape looks like it needs the rewrite.

Dialect Validity guard
PostgreSQL session-local pg_temp function; pg_input_is_valid is 16+ and 14 is still supported
MySQL native JSON_VALID()
SQLite native json_valid(), with a JSON1 probe and unguarded fallback as in migration 0117

MySQL had a second bug the new tests exposed: [ ]* sat outside group 1, so the replacement dropped
the space after the delimiter. Harmless in a top-level document, not when the document is itself a
JSON string. Migration 0055 is unaffected and changes only a comment.

Behaviour change: a top-level non-finite float becomes null instead of "NaN", and one inside a
JSON string is now left alone. Only affects deployments yet to run the 2.x -> 3.x upgrade.

Relationship to #71927

Same guard idea, reached independently. Three things I ran locally rather than read off the diff:

The guard is their idea and it is the right one; this adopts it with those fixed.

Tests

Six rows through the real per-dialect SQL on all three backends: the token classes plus an embedded
NUL, a literal backslash-u-0000 that must survive, a serialized document as the whole value, one
under a dict key, one invalid at the top level and wrapping a document, and one invalid only
because of the NUL escape, which pins the statement order. The already-valid rows assert unchanged
bytes and compare the inner document as an exact string, so dropped whitespace fails instead of
slipping past json.loads. One more test covers the no-JSON1 SQLite path.

Ran the statements directly against PostgreSQL 14.23, PostgreSQL 16.15 and MySQL 8.4: 6/6 rows
byte-exact and casting cleanly on each. Breeze core-tests --backend postgres --python 3.10 passes.
On main the same fixtures reproduce the production failure.

…ation

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: apache#71921
@hkc-8010
hkc-8010 requested a review from ephraimbuddy as a code owner August 21, 2026 07:44
@boring-cyborg boring-cyborg Bot added the area:db-migrations PRs with DB migration label Aug 21, 2026
…as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in apache#71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: apache#71921
@hkc-8010 hkc-8010 changed the title Substitute null for non-finite floats in the XCom bytea to JSONB migration Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration Aug 21, 2026
@eladkal eladkal added this to the Airflow 3.3.2 milestone Aug 29, 2026

@uranusjr uranusjr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One non-blocking comment. LGTM.

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.
@vatsrahul1001
vatsrahul1001 merged commit 6b60399 into apache:main Sep 10, 2026
78 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Backport failed to create: v3-3-test. View the failure log Run details

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
❌ v3-3-test Commit Link

You can attempt to backport this manually by running:

cherry_picker 6b60399 v3-3-test

This should apply the commit to the v3-3-test branch and leave the commit in conflict state marking
the files that need manual conflict resolution.

After you have resolved the conflicts, you can continue the backport process by running:

cherry_picker --continue

If you don't have cherry-picker installed, see the installation guide.

def _xcom_sqlite_sanitize_sql(table: str = "xcom") -> str:
return _XCOM_SQLITE_SANITIZE_SQL.replace("__TABLE__", table)
def _sqlite_has_json1(conn) -> bool:
"""Whether this SQLite build provides json_valid() (the JSON1 extension)."""

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.

This won't work in offline mode though.

vatsrahul1001 added a commit that referenced this pull request Sep 10, 2026
… JSONB migration (#71926) (#72886)

* Guard JSONB migration against invalid bytes conversion (#69064)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 54f4300)

* Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration (#71926)

* Substitute null for non-finite floats in the XCom bytea to JSONB migration

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: #71921

* Skip the non-finite float rewrite for XCom values that already parse as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in #71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: #71921

* Narrow the SQLite JSON1 probe to OperationalError in migration 0049

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.

(cherry picked from commit 6b60399)

---------

Co-authored-by: Sean Muth <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Hemkumar Chheda <[email protected]>
kravii pushed a commit to acceldata-io/airflow that referenced this pull request Nov 19, 2026
… JSONB migration (apache#71926) (apache#72886)

* Guard JSONB migration against invalid bytes conversion (apache#69064)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 54f4300)

* Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration (apache#71926)

* Substitute null for non-finite floats in the XCom bytea to JSONB migration

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: apache#71921

* Skip the non-finite float rewrite for XCom values that already parse as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in apache#71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: apache#71921

* Narrow the SQLite JSON1 probe to OperationalError in migration 0049

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.

(cherry picked from commit 6b60399)

---------

Co-authored-by: Sean Muth <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Hemkumar Chheda <[email protected]>
(cherry picked from commit 1ab3d9f)
senthh pushed a commit to acceldata-io/airflow that referenced this pull request Nov 23, 2026
… JSONB migration (apache#71926) (apache#72886)

* Guard JSONB migration against invalid bytes conversion (apache#69064)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 54f4300)

* Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration (apache#71926)

* Substitute null for non-finite floats in the XCom bytea to JSONB migration

The sanitizer in 0049 wrapped NaN/Infinity/-Infinity in unescaped double
quotes, which is only valid at the top level of a JSON document. When the
XCom value is a JSON string wrapping another JSON document every interior
quote is backslash-escaped, so the injected quote closes the wrapping
string and the bytea -> JSONB cast aborts. null needs no quoting, so it is
correct at any nesting depth.

Also moves the run of spaces after the delimiter inside group 1 in the
MySQL pattern, matching PostgreSQL, so the replacement stops dropping it.

closes: apache#71921

* Skip the non-finite float rewrite for XCom values that already parse as JSON

If a value parses as JSON, any NaN or Infinity in it can only be inside a
string, so the rewrite has nothing to fix and the value is left byte for
byte alone. That is the shape reported in apache#71921 and it means the migration
no longer edits the contents of a JSON string at all.

The sanitizer is now two statements per dialect. The U+0000 strip runs
first and unconditionally, only on rows that contain the escape; the
non-finite rewrite runs second, only on rows that do not parse. The order
matters: a value invalid only because of the escape would otherwise look
like it needs the rewrite and be corrupted by it.

Rows that do need the rewrite still get bare null, since a value can be
invalid at the top level and also wrap an escaped document, and only null
is valid at every nesting depth.

PostgreSQL has no non-throwing JSON validator before 16 and 14 is still
supported, so the check goes through a session-local pg_temp function with
a cheap regex pre-test. MySQL and SQLite use the native JSON_VALID, with a
JSON1 probe and an unguarded fallback on SQLite as in migration 0117. The
guard also shrinks the documented SQLite false positive, since a value
holding "NaN detected" now parses and is skipped.

closes: apache#71921

* Narrow the SQLite JSON1 probe to OperationalError in migration 0049

A missing json_valid() raises OperationalError. Catching every exception
also swallowed connection failures and silently sanitized without the
guard, rewriting rows that already parse.

(cherry picked from commit 6b60399)

---------

Co-authored-by: Sean Muth <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Hemkumar Chheda <[email protected]>
(cherry picked from commit 1ab3d9f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:db-migrations PRs with DB migration type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migration 0049 NaN sanitizer corrupts XCom values whose JSON is nested inside a JSON string, aborting the bytea to JSONB cast

5 participants