You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(studio): keep valid backslash literals in Assistant SQL - #53226
fixSqlBackslashEscapes rewrote every ' in execute_sql input to '', so valid Postgres literals that end in a backslash were corrupted before the user approved the query: replace(path, '', '/') became a syntax error, E'C:\' silently became C:', and a = '' or b = '' turned into a single comparison against the string ' or b = '.
Rewrite only when the SQL can't be valid as written: a quote or backslash is left outside every literal and comment, or a '..' string ending in a backslash runs straight into a word character ('it's').
Problem
Describe what went wrong or what need emerged to justify this bug fix, feature, or change.
Link any relevant issues here.
Solution
Provide a brief description of the change and the key choices you made when architecting the solution.
Feel free to include screenshots if it includes visual changes.
Review instructions
Provide a clear numbered procedure that the PR reviewer can walk through.
For example, Open the live and preview links side-by-side.
fixSqlBackslashEscapes rewrote every \' in execute_sql input to '', so
valid Postgres literals that end in a backslash were corrupted before the
user approved the query: replace(path, '\', '/') became a syntax error,
E'C:\\' silently became C:', and a = '\' or b = '\' turned into a single
comparison against the string ' or b = '.
Rewrite only when the SQL can't be valid as written: a quote or backslash
is left outside every literal and comment, or a '..' string ending in a
backslash runs straight into a word character ('it\'s').
Thanks for contributing to Supabase! ❤️ Our team will review your PR.
A few tips for a smoother review process:
If you have a local version of the repo, run pnpm run format to make sure formatting checks pass.
Once we've reviewed your PR, please don't trivially merge master (don't click Update branch if there are no merge conflicts to be fixed). This invalidates any pre-merge checks we've run.
Reviewing files that changed from the base of the PR and between 3ed553e and a4abe27.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Walkthrough
fixSqlBackslashEscapes now checks for likely misescaped quotes before replacing backslash-apostrophes. Tests cover conversion in SQL containing comment-like text and valid inputs that remain unchanged.
Changes: SQL escape handling
Layer / File(s)
Summary
Detect and rewrite SQL escapes apps/studio/data/sql/utils.ts, apps/studio/lib/ai/util.ts, apps/studio/lib/ai/util.test.ts
The SQL literals-and-comments pattern is now exported. fixSqlBackslashEscapes uses a detector to identify likely misescaped quotes before rewriting. Tests cover conversion and unchanged SQL inputs.
The reported escape case is handled. Valid SQL using a non-ASCII dollar tag can still have its literal content changed, but this narrow case does not block merging.
Pre-merge checks: 5
Check name
Status
Explanation
Title check
The title clearly identifies the fix: preserving valid backslash literals in Assistant SQL.
Description check
The description explains the problem and the intended fix, including concrete examples. The template’s Problem and Solution sections and review instructions remain as placeholders, but the core inform…
Docstring Coverage
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Description check
Explanation
The description explains the problem and the intended fix, including concrete examples. The template’s Problem and Solution sections and review instructions remain as placeholders, but the core information is present.
Full details: Docstring Coverage
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 3 files.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between c5f9939 and 0d0a53c.
Files selected for processing: 3
apps/studio/data/sql/utils.ts
apps/studio/lib/ai/util.test.ts
apps/studio/lib/ai/util.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
The early return for SQL that is already valid PostgreSQL is the right idea, and the new cases ('\' in replace, strings ending in a backslash, E'C:\\') are the ones the old replace used to corrupt. Two mismatches between the scanner and the rewriter are worth tightening.
The reason will be displayed to describe this comment to others. Learn more.
The dollar-quote pattern here is \$([^$]*)\$ (any tag), but LITERALS_AND_COMMENTS only treats (\$[a-z_]*\$) as a tag. Even with the i flag that still excludes digits, so $foo1$ / $body2$ is a dollar-quoted body for this replace, and ordinary text for hasMisescapedQuote / stripLiteralsAndComments. A \' inside that body can make the guard return true, and a \' just after it can be rewritten. I'd use one shared tag pattern (PostgreSQL's is empty or [A-Za-z_][A-Za-z_0-9]*) in both places, and add a $foo1$...$foo1$ case to the "leave valid SQL unchanged" table.
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks. I confirmed it: SELECT $body2$a\'b$body2$, replace(p, '\', '/') was still being rewritten, and the '\' came out as '''.
Fixed in 1322aba. The scanner and the rewrite now both use one DOLLAR_QUOTE_DELIMITER (empty or [A-Za-z_][A-Za-z_0-9]*). I also dropped the i flag from LITERALS_AND_COMMENTS, matching E-strings with [eE] instead, so tags are case-sensitive the way Postgres treats them. I added a $foo1$...$foo1$ case to the "leave valid SQL unchanged" table and a $b2$ case to the rewrite path. capReadQuery also benefits, since $fn1$ bodies are now stripped as literals.
The reason will be displayed to describe this comment to others. Learn more.
literal.startsWith("'") misses escape-string literals. LITERALS_AND_COMMENTS matches e'...' / E'...', so E'it\'s more' starts with E or e and never hits this branch. If stripLiteralsAndComments does not already flag it, a misescaped E'...\'... would be left as-is. Checking after stripping a leading [eE], or treating a literal that ends in \' as a misescaped quote, would cover both forms. Worth a test next to 'It\'s -- not a comment'.
The reason will be displayed to describe this comment to others. Learn more.
I think this one is the intended behavior. Inside an E'...' escape string, \' is Postgres's own escaped quote, so E'it\'s more' is already valid and should be left alone (see "String Constants With C-Style Escapes" in the Postgres docs, section 4.1.2.2). The scanner already reads E-strings with backslash escapes, so it consumes the whole literal and the guard correctly returns false.
I added E'It\'s -- not a comment' and e'it\'s more' to the unchanged table and noted this in the hasMisescapedQuote doc comment. If you have a case where an E-string ends up misescaped, I'm happy to cover it.
The backslash rewrite treated any $...$ as a dollar-quote tag, but
LITERALS_AND_COMMENTS only accepted letters and underscores. A tag with a
digit ($body2$) was a quoted body for one and plain text for the other, so
valid SQL like $body2$a\'b$body2$, replace(p, '\', '/') was still rewritten.
Both now use DOLLAR_QUOTE_DELIMITER, the Postgres rule: empty, or a letter
or underscore followed by letters, digits or underscores. The scanner drops
the i flag and matches E'..' with [eE], so tags stay case-sensitive.
E'..' escape strings are still left alone: there \' is Postgres's own
escaped quote. Tests cover that and $tag1$ bodies in both paths.
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
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.
fixSqlBackslashEscapes rewrote every ' in execute_sql input to '', so valid Postgres literals that end in a backslash were corrupted before the user approved the query: replace(path, '', '/') became a syntax error, E'C:\' silently became C:', and a = '' or b = '' turned into a single comparison against the string ' or b = '.
Rewrite only when the SQL can't be valid as written: a quote or backslash is left outside every literal and comment, or a '..' string ending in a backslash runs straight into a word character ('it's').
Problem
Describe what went wrong or what need emerged to justify this bug fix, feature, or change.
Link any relevant issues here.
Solution
Provide a brief description of the change and the key choices you made when architecting the solution.
Feel free to include screenshots if it includes visual changes.
Review instructions
Provide a clear numbered procedure that the PR reviewer can walk through.
Open the live and preview links side-by-side.See the issue is fixed.Checklist
Check all before review:
/write-the-docsor/edit-the-docsskill, which applies the docs style guide