Repository navigation
Do not corrupt XCom values that already parse as JSON in the bytea to JSONB migration - #71926
Conversation
…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
…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
uranusjr
left a comment
There was a problem hiding this comment.
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.
Backport failed to create: v3-3-test. View the failure log Run detailsNote: As of Merging PRs targeted for Airflow 3.X In matter of doubt please ask in #release-management Slack channel.
You can attempt to backport this manually by running: cherry_picker 6b60399 v3-3-testThis should apply the commit to the v3-3-test branch and leave the commit in conflict state marking After you have resolved the conflicts, you can continue the backport process by running: cherry_picker --continueIf 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).""" |
There was a problem hiding this comment.
This won't work in offline mode though.
… 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]>
… 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)
… 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)
closes: #71921
Summary
Migration 0049 quotes non-finite floats before the
bytea -> JSONBcast: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 closesthe wrapping string, the following
NaNis left bare, and the cast aborts:The sanitizer is what creates the invalid JSON. The stored bytea is exactly what Airflow 2's
json.dumpswrote and it casts fine before sanitization. #53812, #57614, #57866, #62686 and #69064each refined which tokens match, never the replacement, so no release fixes this.
Fix
Two statements per dialect instead of one nested expression:
null, only on rows that do not already parse as JSON.If a value parses, its
NaNcan only be inside a string, so it is left byte for byte alone. Rowsthat do need the rewrite get
null, because a value can be invalid at the top level and wrap anescaped document, and only
nullis valid at any depth. Order matters: strip first, or a valueinvalid only because of the escape looks like it needs the rewrite.
pg_tempfunction;pg_input_is_validis 16+ and 14 is still supportedJSON_VALID()json_valid(), with a JSON1 probe and unguarded fallback as in migration 0117MySQL had a second bug the new tests exposed:
[ ]*sat outside group 1, so the replacement droppedthe 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
nullinstead of"NaN", and one inside aJSON 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:
json_valid(), which does not exist in PostgreSQL:ERROR: function json_valid(text) does not existon 16.15. Every PostgreSQL upgrade would abort, NaN or not.{"note": "x\u0000y"}is validjsonbut not validjsonb, so the rowis skipped as already-valid then rejected by the cast, undoing Guard XCom/DagRun pickle-removal migrations (0049, 0055) against NaN/Infinity and U+0000 in bytea->JSONB conversion #69064.
unchanged quoting regex and comes out corrupt.
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.10passes.On
mainthe same fixtures reproduce the production failure.