Fix pg array parsing dropping backslashes in quoted elements - #6181
Open
eeshsaxena wants to merge 1 commit into
Open
Fix pg array parsing dropping backslashes in quoted elements#6181eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
parsePgArrayValue unescaped a quoted array element with .replace(/\\/g, ''),
which strips every backslash. That is correct for an escaped quote (\" -> ")
but wrong for an escaped backslash (\\ -> should be \, not ''), so a text[]
value containing a backslash was corrupted on read: Postgres output {"a\\b"}
(the string a\b) parsed to 'ab', and a Windows path c:\temp\x parsed to
c:tempx.
Unescape with .replace(/\\(.)/g, '$1') so a backslash escapes the following
character, matching how makePgArray escapes and how Postgres formats array
output. Escaped quotes are unchanged. Adds tests for escaped backslashes.
Author
|
Worth flagging the blast radius: this hits any array column whose values can contain a backslash, so Windows paths, regex patterns, and escape sequences in a text[] silently lose data on read, and it is a read-path corruption rather than a crash so it can go unnoticed. The fix is a one-character-class change to the unescape and it leaves the escaped-quote handling and all existing tests untouched. |
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.
What / Why
parsePgArrayValueunescapes a quoted Postgres array element with.replace(/\\/g, ''), which strips every backslash. That is right for an escaped quote (\"→") but wrong for an escaped backslash (\\should become a single\, not be removed). So atext[]/varchar[]value containing a backslash is corrupted on read.Postgres doubles backslashes in its array output, so:
Reading that back through Drizzle today:
Any array value with a backslash is affected: Windows paths, regex patterns, LaTeX, escape sequences, etc.
Fix
Unescape with
.replace(/\\(.)/g, '$1')so a backslash escapes the following character. This is the inverse of whatmakePgArrayproduces (it escapes\→\\and"→\") and matches how Postgres formats array output.{"a\\b"}→['a\b'].{"a\"b"}→['a"b'](already covered by existing tests, still green).Added tests for escaped backslashes and for a value containing both an escaped backslash and an escaped quote.
Testing
I verified the fixed
parsePgArrayValue/parsePgNestedArrayagainst every existingparsePgArraytest input (all still pass) plus the new backslash cases. I wasn't able to spin up the full monorepo test run locally (slow dependency install in my environment), so I'd appreciate CI running theparsePgArraysuite.