-
Notifications
You must be signed in to change notification settings - Fork 5.3k
fix(destination-postgres): strip NUL characters nested in JSON values #84321
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
Open
devin-ai-integration
wants to merge
3
commits into
master
Choose a base branch
from
devin/1786553022-postgres-nul-jsonb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d625721
fix(destination-postgres): sanitize nested null characters
devin-ai-integration[bot] 1febc26
docs(destination-postgres): add changelog PR link
devin-ai-integration[bot] dbf87a0
refactor(destination-postgres): simplify nul sanitization check
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
57 changes: 57 additions & 0 deletions
57
...in/io/airbyte/integrations/destination/postgres/write/transform/PostgresValueSanitizer.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright (c) 2026 Airbyte, Inc., all rights reserved. | ||
| */ | ||
|
|
||
| package io.airbyte.integrations.destination.postgres.write.transform | ||
|
|
||
| import io.airbyte.cdk.load.data.AirbyteValue | ||
| import io.airbyte.cdk.load.data.ArrayValue | ||
| import io.airbyte.cdk.load.data.ObjectValue | ||
| import io.airbyte.cdk.load.data.StringValue | ||
|
|
||
| internal fun sanitizePostgresValue(value: AirbyteValue): AirbyteValue = | ||
| sanitizePostgresValueWithChange(value).value | ||
|
|
||
| private data class SanitizedValue(val value: AirbyteValue, val changed: Boolean) | ||
|
|
||
| private fun sanitizePostgresValueWithChange(value: AirbyteValue): SanitizedValue = | ||
| when (value) { | ||
| is StringValue -> { | ||
| val sanitizedValue = value.value.replace("\u0000", "") | ||
| if (sanitizedValue.length != value.value.length) { | ||
| SanitizedValue(StringValue(sanitizedValue), true) | ||
| } else { | ||
| SanitizedValue(value, false) | ||
| } | ||
| } | ||
| is ArrayValue -> { | ||
| var sanitizedValues: MutableList<AirbyteValue>? = null | ||
| value.values.forEachIndexed { index, child -> | ||
| val sanitized = sanitizePostgresValueWithChange(child) | ||
| if (sanitized.changed) { | ||
| if (sanitizedValues == null) { | ||
| sanitizedValues = value.values.toMutableList() | ||
| } | ||
| sanitizedValues!![index] = sanitized.value | ||
| } | ||
| } | ||
| sanitizedValues?.let { SanitizedValue(ArrayValue(it), true) } | ||
| ?: SanitizedValue(value, false) | ||
| } | ||
| is ObjectValue -> { | ||
| var sanitizedValues: LinkedHashMap<String, AirbyteValue>? = null | ||
| value.values.forEach { (key, child) -> | ||
| val sanitized = sanitizePostgresValueWithChange(child) | ||
| if (sanitized.changed) { | ||
| if (sanitizedValues == null) { | ||
| sanitizedValues = LinkedHashMap(value.values) | ||
| } | ||
| sanitizedValues!![key] = sanitized.value | ||
| } | ||
| } | ||
| // Nested object keys are intentionally not sanitized; only values are destination data. | ||
| sanitizedValues?.let { SanitizedValue(ObjectValue(it), true) } | ||
| ?: SanitizedValue(value, false) | ||
| } | ||
| else -> SanitizedValue(value, false) | ||
| } |
79 changes: 79 additions & 0 deletions
79
...in/io/airbyte/integrations/destination/postgres/write/load/PostgresRecordFormatterTest.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright (c) 2026 Airbyte, Inc., all rights reserved. | ||
| */ | ||
|
|
||
| package io.airbyte.integrations.destination.postgres.write.load | ||
|
|
||
| import io.airbyte.cdk.load.data.ArrayValue | ||
| import io.airbyte.cdk.load.data.EnrichedAirbyteValue | ||
| import io.airbyte.cdk.load.data.ObjectTypeWithoutSchema | ||
| import io.airbyte.cdk.load.data.ObjectValue | ||
| import io.airbyte.cdk.load.data.StringValue | ||
| import io.airbyte.integrations.destination.postgres.write.transform.PostgresValueCoercer | ||
| import org.junit.jupiter.api.Assertions.assertFalse | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class PostgresRecordFormatterTest { | ||
|
|
||
| @Test | ||
| fun `schema formatter removes nested null characters before serialization`() { | ||
| val value = | ||
| ObjectValue( | ||
| linkedMapOf( | ||
| "entries" to | ||
| ArrayValue( | ||
| listOf(ObjectValue(linkedMapOf("text" to StringValue("a\u0000b")))) | ||
| ) | ||
| ) | ||
| ) | ||
| val enrichedValue = | ||
| EnrichedAirbyteValue( | ||
| abValue = value, | ||
| type = ObjectTypeWithoutSchema, | ||
| name = "entries", | ||
| changes = mutableListOf(), | ||
| airbyteMetaField = null, | ||
| ) | ||
| val coercer = PostgresValueCoercer() | ||
| coercer.map(enrichedValue) | ||
|
|
||
| val serialized = | ||
| PostgresSchemaRecordFormatter(listOf("entries")) | ||
| .format(mapOf("entries" to enrichedValue.abValue))[0] | ||
| .toString() | ||
|
|
||
| assertFalse(serialized.contains('\u0000')) | ||
| assertFalse(serialized.contains("\\u0000")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `raw formatter removes nested null characters before serialization`() { | ||
| val value = | ||
| ObjectValue( | ||
| linkedMapOf( | ||
| "entries" to | ||
| ArrayValue( | ||
| listOf(ObjectValue(linkedMapOf("text" to StringValue("a\u0000b")))) | ||
| ) | ||
| ) | ||
| ) | ||
| val enrichedValue = | ||
| EnrichedAirbyteValue( | ||
| abValue = value, | ||
| type = ObjectTypeWithoutSchema, | ||
| name = "entries", | ||
| changes = mutableListOf(), | ||
| airbyteMetaField = null, | ||
| ) | ||
| val coercer = PostgresValueCoercer() | ||
| coercer.map(enrichedValue) | ||
|
|
||
| val serialized = | ||
| PostgresRawRecordFormatter(listOf("_airbyte_data")) | ||
| .format(mapOf("entries" to enrichedValue.abValue))[0] | ||
| .toString() | ||
|
|
||
| assertFalse(serialized.contains('\u0000')) | ||
| assertFalse(serialized.contains("\\u0000")) | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
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.
why didn't this work before?
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.
🚫 No change needed — answering. It ran too late. By the time
Jsons.writeValueAsString(...)returns, Jackson has already encoded the NUL as the six-character escape sequence\u0000(backslash,u,0000), so there is no literal NUL code point left in the string for.replace("\u0000", "")to match. Thereplacesilently matched nothing on every record.Quick demonstration with the Jackson version we build against:
And
\u0000in that form is precisely what Postgres'jsonbinput function rejects (unsupported Unicode escape sequence/<NULL> cannot be converted to text), which is the failure in the linked issue. Sanitizing theAirbyteValuetree before serialization is what makes it effective, and the newPostgresRecordFormatterTestasserts the absence of both the NUL byte and the textual escape so this can't silently regress to a no-op again.Devin session
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.
shouldn't there be a way to use
replacewith the string value instead of the byte value?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.
🙋 There is, but I'd rather not — your call. You're right that
.replace("\\u0000", "")on the serialized text would actually match, unlike the old code. Two reasons I went the other way:It can corrupt valid JSON. If a source string legitimately contains the six characters
\u0000as text (backslash included), Jackson escapes the backslash and emits seven characters:\\u0000. A textual replace of\u0000matches the tail of that and leaves a dangling\, producing invalid JSON. Operating on theAirbyteValuetree has no such ambiguity — we edit the actual string content, and Jackson escapes whatever remains correctly.It only covers the raw path. The typed path doesn't serialize the whole record; each
jsonbcolumn is serialized independently (PostgresValueCoercer.map→toCsvValue) and the result is written into a CSV row. A post-serialization fixup would have to be repeated there, on strings already mixed with CSV quoting. Sanitizing before serialization is one place that covers both paths and keeps the typed and raw tables consistent.If you'd still prefer the textual replace on the raw path, I'll switch it — but I'd want to keep the pre-serialization sanitizer for the typed path regardless, so it would be two mechanisms instead of one.
Devin session