Skip to content

environmentd: quote the SQL the memory visualizer builds - #38369

Merged
def- merged 2 commits into
MaterializeInc:mainfrom
def-:pr-memory-viz-sql-quoting
Aug 20, 2026
Merged

def- merged 2 commits into
MaterializeInc:mainfrom
def-:pr-memory-viz-sql-quoting

Conversation

@def-

@def- def- commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

The /memory page resolves an expanded dataflow back to the view its index was built on, and interpolated both the index name it parses out of the dataflow name and the view name that lookup returns straight into the statements it sends to /api/sql. Those names are chosen by whoever created the object, and the statements run in the session of whoever opened the page, so a catalog name carrying a quote could run SQL with the viewer's privileges.

The escaper the pages already had for cluster and replica names passed a string rather than a regex to String.replace, so it escaped only the first quote of a name, which is no escaping at all for a name carrying two. Those two names come straight from URL parameters, as does the dataflow id, which is interpolated as a number and so cannot be made safe by quoting at all, and is now rejected unless it is an integer.

Both pages now quote through sqlLiteral and sqlIdent in fetch.js, which they already share for the /api/sql call itself, and a browser test pins that both helpers escape every quote rather than just the first.

Found via the QA LLM review, sorry for only catching it after the PR merged: #38359 (comment)

The /memory page resolves an expanded dataflow back to the view its index
was built on, and interpolated both the index name it parses out of the
dataflow name and the view name that lookup returns straight into the
statements it sends to /api/sql. Those names are chosen by whoever created
the object, and the statements run in the session of whoever opened the
page, so a catalog name carrying a quote could run SQL with the viewer's
privileges.

The escaper the pages already had for cluster and replica names passed a
string rather than a regex to String.replace, so it escaped only the first
quote of a name, which is no escaping at all for a name carrying two. Those
two names come straight from URL parameters, as does the dataflow id, which
is interpolated as a number and so cannot be made safe by quoting at all,
and is now rejected unless it is an integer.

Both pages now quote through sqlLiteral and sqlIdent in fetch.js, which
they already share for the /api/sql call itself, and a browser test pins
that both helpers escape every quote rather than just the first.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@def-
def- requested a review from antiguru August 20, 2026 09:52
@def-
def- requested a review from a team as a code owner August 20, 2026 09:52

@antiguru antiguru left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right and it is complete. No apology needed for catching it post-merge, and you found more than the QA review did: it reported the getCreateView identifier splice, and you also spotted the dataflow id going in bare, which it never mentioned. Splitting the fix by interpolation position, quote for literals and identifiers, reject for numbers, is the correct shape. Hoisting into fetch.js also settles the duplication I raised on #38359, since both pages now share the escaping instead of carrying a copy each.

I checked the parts that can't be settled by reading, rather than assuming them:

  • window.sqlLiteral really is reachable. This was the one claim I couldn't take on faith, since fetch.js opens with 'use strict'. I served this PR's fetch.js to Chromium as a classic script next to a babel-transformed page script: both helpers land on window, and the two assertions return exactly 'a''b''c' and "a""b""c". The test passes, and for the reason it looks like it should.
  • The escaping bug was live, not theoretical. Through Materialize's own parser, SET cluster = <name> with a two-quote name escaped the old way parses as three statements (SET cluster, SELECT 1, SET cluster = 'x'); escaped the new way it is one, with the whole crafted string as the value. So the crafted-link vector was real.
  • standard_conforming_strings is .fixed() on, so the comment's claim holds. Details inline.
  • The Number.isInteger guard admits nothing injectable. I ran a dozen inputs through it; table inline.
  • Nothing left over. No raw SQL interpolation remains in either page, the leftover ${} are all DOT-graph construction, formatNameForQuery is gone from both files, and the typecheck is clean on this head.

The one thing I'd change before merging

The new test asserts on the helpers, not on the code that calls them. Revert getCreateView to the raw interpolation and it still goes green. Since the call sites are precisely what was broken here, and the helpers are two lines that double a quote, the coverage is pointed at the safer half. Detail and a concrete fixture on the test itself.

Not blocking on it, and the PR description is honest about the test's scope, so this is a gap rather than an overclaim. But it is the difference between this class of bug being caught next time and not.

Rest is minor: the new dot-in-name comment describes the wrong failure mechanism, and one @param disagrees with its body.


Generated by Claude Code

@def-
def- requested a review from antiguru August 20, 2026 10:41

@antiguru antiguru left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. All four points addressed, and buildkite/test/dataflow-visualizer is green on this head, so the new call-site test actually ran rather than just looking right. The fixture path is confirmed along with it: create_fixture_dataflow raises if an index doesn't hydrate inside 60s, so the quoted-identifier DDL executed and the dataflow showed up.

One correction to my own review. I suggested asserting merely that the View: panel renders, on the belief that the page shows the name as Materialize re-quotes it. That was wrong, and your version is better for it. SHOW CREATE VIEW's name column is FullItemName's Display (names.rs:90), which writes database/schema/item as plain strings with no quoting at all, so the raw name really does reach the DOM. Asserting the full name pins that the right view resolved, where my weaker version would have passed on any panel at all. I had assumed the quoting behaviour instead of checking it.

Also checked the parts of the new fixture most likely to bite, since a name carrying both quote kinds has to survive a few hops: the compose environment round trip is lossless (PyYAML emits it as a plain scalar and parses it back byte-identical), and there is no $ in it for compose to interpolate.

Optional, for whenever someone next touches this file rather than now: the call-site coverage is sqlIdent-only. Every sqlLiteral call site still sees a quote-free name in the fixture (quickstart, r1, materialize, public, visualizer_quoted_idx), so reverting one of those to raw interpolation would still go green. Far smaller than what this PR closes, and the helper-level test covers the escaping itself, so I would not hold the fix for it. A cluster or replica named with a quote would be the cheap way in.


Generated by Claude Code

@def-
def- merged commit e76d1b9 into MaterializeInc:main Aug 20, 2026
76 checks passed
@def-
def- deleted the pr-memory-viz-sql-quoting branch August 20, 2026 12:26
@bosconi bosconi mentioned this pull request Aug 21, 2026
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