Repository navigation
environmentd: quote the SQL the memory visualizer builds - #38369
Conversation
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]>
antiguru
left a comment
There was a problem hiding this comment.
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.sqlLiteralreally is reachable. This was the one claim I couldn't take on faith, sincefetch.jsopens with'use strict'. I served this PR'sfetch.jsto Chromium as a classic script next to a babel-transformed page script: both helpers land onwindow, 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_stringsis.fixed()on, so the comment's claim holds. Details inline.- The
Number.isIntegerguard 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,formatNameForQueryis 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
antiguru
left a comment
There was a problem hiding this comment.
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
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)