Skip to content

fix(studio): keep valid backslash literals in Assistant SQL - #53226

Open
officialbidisha wants to merge 6 commits into
supabase:masterfrom
officialbidisha:fix/studio-assistant-backslash-literals
Open

officialbidisha wants to merge 6 commits into
supabase:masterfrom
officialbidisha:fix/studio-assistant-backslash-literals

Conversation

@officialbidisha

Copy link
Copy Markdown

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.

  1. For example, Open the live and preview links side-by-side.
  2. For example, See the issue is fixed.

Checklist

Check all before review:

  • I have read CONTRIBUTING.md
  • If I wrote a new docs topic or edited an existing topic, I used the /write-the-docs or /edit-the-docs skill, which applies the docs style guide

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').
@officialbidisha
officialbidisha requested review from a team as code owners November 10, 2026 14:34
@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

5 Skipped Deployments
Project Deployment Actions Updated
design-system Skipped Skipped Nov 23, 2026 8:00am UTC
docs Skipped Skipped Nov 23, 2026 8:00am UTC
kb Skipped Skipped Nov 23, 2026 8:00am UTC
ui-library Skipped Skipped Nov 23, 2026 8:00am UTC
zone-www-dot-com Skipped Skipped Nov 23, 2026 8:00am UTC

Request Review

@vercel

vercel Bot commented Nov 10, 2026

Copy link
Copy Markdown

@officialbidisha is attempting to deploy a commit to the Supabase Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

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.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: gregnr, joshenlim, mattrossman

Merge Risk: Minimal · previous review 0d0a5

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: Passed 5

Check name Status Explanation
Title check Passed The title clearly identifies the fix: preserving valid backslash literals in Assistant SQL.
Description check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

Commits

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.

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

Comment thread apps/studio/lib/ai/util.ts Outdated
*/
export function fixSqlBackslashEscapes(sql: string): string {
if (!hasMisescapedQuote(sql)) return sql
return sql.replace(/\$([^$]*)\$[\s\S]*?\$\1\$|\\'/g, (match, dollarTag) =>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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.

const literal = match[0]
const nextChar = sql.charAt(match.index + literal.length)
return literal.startsWith("'") && literal.endsWith("\\'") && /\w/.test(nextChar)
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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'.

Copy link
Copy Markdown
Author

Choose a reason for hiding this 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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants