Repository navigation
fix(api-gateway): Guard member-expression SQL eval against JS injection - #12207
Conversation
Member expressions from the SQL API carry a SQL fragment that the gateway
splices into a JS template literal and evaluates via Function.constructor
(parseMemberExpression/evalMemberExpression). The only interpolations the
planner ever emits there are references to the declared cube params
(e.g. ${Orders.amount}); the existing backtick/backslash escaping does not
neutralize ${...}.
Add assertSafeMemberExpressionSql(), which rejects any ${...} whose root is
not one of the declared cubeParams, whose segments are not plain identifiers,
or that touches a prototype key (constructor/__proto__/prototype). This blocks
arbitrary JavaScript (${7*191}, ${process.env...}, ${x()}, Function-constructor
pivots) from reaching the eval while preserving every legitimate member
reference. The guard runs in the gateway, upstream of both the legacy and
Tesseract planners, so it closes the sink for both.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 3m 23s —— View job Changes requested: 1 high, 1 medium, 3 low. The Review details
Findings
Notes
Review threadsThe listing returned 0 of my own threads (no prior threads), so nothing was resolved or re-affirmed. |
The guard checked only the ${...} interpolations in `sql`, but `cubeParams`
entries are spliced into `Function(...)` as raw parameter source
(evalMemberExpression), so a non-identifier entry can carry an executing
default-value / destructuring parameter (e.g. `[a = <expr>] = []` or
`x = <expr>`) even when `sql` has no interpolation. Reject any cubeParams
entry that is not a plain identifier, and add regression tests.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
Reviewing PR #12207
|
…on SQL
Address review: the sink `return \`${sql}\`` places sql in template-literal
position, so an unescaped backtick (or a trailing odd backslash escaping the
closing backtick) breaks out into arbitrary JS without any `${...}`. Add
hasTemplateBreakout() so the guard no longer relies on CubeSQL's escaping.
Also simplify the interpolation scan to String.matchAll and tighten the
file comments. Adds regression tests for the breakout shapes.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
Reviewing PR #12207
|
CodeQL flagged /\$\{([^}]*)\}/g as a polynomial-time regex on user input
(it backtracks per start position under the global scan). Replace it with a
linear manual indexOf scan for `${...}` interpolations; an unterminated `${`
is now rejected explicitly. Verified linear: 1M-char hostile input ~12ms.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 26s —— View job Review of #12207: guard looks sound, no blocking issues (0 high, 0 medium, 2 low)Details
Security I checked
Compatibility The guard accepts everything CubeSQL emits:
The guard only runs on the SQL API path: Inline (low)
Not verified: I didn't run the tests myself. The sandbox wouldn't run Review threads: the listing returned 0 of my own threads (none from earlier rounds), so I resolved 0 and re-affirmed 0. The existing threads from paveltiunov and the CodeQL bot were left alone. The CodeQL polynomial-regex alert looks fixed by |
Address review: the guard was only unit-tested via assertSafeMemberExpressionSql,
so removing the call in parseMemberExpression wouldn't fail any test. Add
gateway-level cases through apiGateway.sql (memberExpressions: true) covering both
wiring sites — SqlFunction (arbitrary ${...}, non-identifier cubeParams, backtick
breakout) and PatchMeasure addFilters — asserting rejection; the existing
"expressions are allowed" case covers the accept path. Also trim the guard's JSDoc.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 36s —— View job Looks good to merge. 0 high, 0 medium, 2 low. I found no way around the guard on the SQL API path. Review details
Findings
Bypass analysis (no issues found)Code runs through
TestsThe unit cases cover the known exploit shapes plus the shapes the planner really emits. The gateway-level tests in Review threadsThe listing returned 0 of my own threads (no prior threads), so I resolved 0 and re-affirmed 0. The paveltiunov and CodeQL threads aren't mine, so I left them alone. |
Address review (defence in depth): the parse-time guard only runs on string members, but Function.constructor in evalMemberExpression also runs on members that arrive already parsed as objects (querySchema accepts parsedMemberExpression), which skip parseMemberExpression entirely. Add assertSafeCompiledMemberExpression, run at the eval sink on the compiled `[...cubeParams, body]` array (and each PatchMeasure addFilters entry): it validates params as identifiers and re-checks the `return \`<sql>\`` body via the same sql guard. Adds unit tests for the object-form bypass shapes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
Address review nit: collapse the 4-line explanatory comment to one line. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 16s —— View job No security holes left in the guard. 0 high, 0 medium, 2 low (one test-accuracy issue, one simplification). Review details
SecurityI traced each input that reaches
Inline (low)
CommentsAll comments are within 3 lines and explain why, not what. Review threadsThe listing returned 0 of my own unresolved threads (none from earlier rounds), so I resolved 0 and re-affirmed 0. |
Address review: correct the now-inaccurate comment (since the eval-sink guard, the four cases no longer pin the parse-time call) and add the one vector only the sink guard closes — an object-form member (parsedMemberExpression) placed next to a string expression so hasExpressionsInQuery routes it to evalMemberExpression, asserting it is rejected. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Reviewing PR #12207
|
Address review: with assertSafeCompiledMemberExpression running in evalMemberExpression, the parse-time assertSafeMemberExpressionSql calls are a strict subset (every parsed expression reaches the sink) and only add a second choke point to keep in sync. Drop the parse-time calls and keep the sink as the single guard; parseMemberExpression returns to its original shape. The gateway-level and unit tests are unchanged and still pass via the sink guard. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 34s —— View job LGTM: the sink guard closes the reported injection vectors and I found no bypass. Findings: 0 high, 0 medium, 3 low (all comment or PR metadata). Review details
SecurityThe guard now lives at the only eval sinks:
There are no other Bypasses I tried, none of which get through:
Compatibility with what CubeSQL emits
Low
TestsThe unit cases cover the exploit shapes and the valid planner shapes. The gateway-level cases in Review threadsThe listing returned 0 of my threads, so I resolved 0 and re-affirmed 0. |
Address review nits: drop the parse-site comment that duplicated the sink comment, and condense the assertSafeCompiledMemberExpression JSDoc. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 24s —— View job Review of PR #12207: no blocking issues (0 high, 0 medium, 2 low)Details
SecurityThe sink guard at
Both planners call the function the gateway builds, so the fix covers Tesseract and the legacy planner alike. Findings
TestsThe unit cases in Review threadsThe listing returned 0 of my own threads (the PR had no review threads), so I resolved 0 and re-affirmed 0. |
The object-form gateway test used `definition: ''`, which Joi rejects (Joi.string() disallows empty strings), so the query failed schema validation before reaching the eval sink — the assertion saw "Invalid query format" instead of "Unsafe member expression". Use a non-empty definition so the member reaches evalMemberExpression and is rejected there. Also refresh the now-stale sink comment. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 11s —— View job LGTM: no security or correctness issues, 1 low (test duplication). Review details
Security: no bypass foundI checked every way into
False-positive risk is low. CubeSQL's The guard runs in the gateway before SQL planning, so it covers Tesseract and the legacy planner equally. Findings
CommentsAll new comments are 3 lines or fewer, and each one guards against a regression (the regex-ReDoS note, the backtick/backslash rationale, the object-form test setup). Nothing to flag. TestsI couldn't run them: running Review threadsThe listing returned 0 of my own threads, so I resolved 0 and re-affirmed 0 (no prior threads). |
Codecov Report❌ Patch coverage is
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Address review: widen the rejectedMemberExpression helper's members type to (string | Record<string, unknown>)[] so the object-form test reuses it instead of duplicating the full QueryRequest literal. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE
|
Claude finished @paveltiunov's task in 2m 30s —— View job LGTM: no issues found (0 high, 0 medium, 0 low) Review details
Security: checked for bypasses, found noneThere are two eval sinks, both in I tried these inputs against
The guard runs in the gateway before planning starts, so it covers both Tesseract and the legacy planner. Compatibility: what CubeSQL emits still passes
TestsThe unit cases cover each check in both directions (accepted and rejected). The gateway-level cases go through both sink call sites and the object-form path. I couldn't run Jest locally because CommentsEvery comment is 3 lines or fewer, and each one guards against a reintroduced bug (the regex ReDoS note, the even/odd backslash rule, the single-guard note at the sink). Nothing to flag. Review threadsThe listing returned 0 of my threads, so there were no prior threads and nothing to resolve or re-affirm. There are 0 unresolved threads from others. |
…on (#12207) **Check List** - [x] Tests have been run in packages where changes have been made if available - [x] Linter has been run for changed code - [x] Tests for the changes have been added if not covered yet - [ ] Docs have been added / updated if required **Description of Changes Made** Member expressions arriving over the SQL API carry a SQL fragment (`expr.sql`) that the gateway compiles into `Function.constructor(...cubeParams, \`return \`${sql}\`\`)` and evaluates (`evalMemberExpression` in `gateway.ts`). Nothing validated that compiled expression, so a fragment (or a crafted `cubeParams` entry) could execute arbitrary JavaScript in the API process. This adds a single guard at the eval sink: - New `src/member-expression-security.ts` exporting `assertSafeCompiledMemberExpression(expression)`. For the compiled `[...cubeParams, body]` array it: - requires each `cubeParams` entry to be a plain identifier (they are spliced as raw `Function` parameter source, so a non-identifier entry like `[a = <expr>] = []` would execute); - requires `body` to be the canonical `` return `<sql>` `` wrapper, then validates `<sql>`: no template-literal breakout (unescaped backtick / trailing odd backslash), and every `${…}` interpolation a dotted path rooted at one of `cubeParams`, segments being identifiers and never `constructor`/`__proto__`/`prototype`. The `${…}` scan is a linear `indexOf` walk (no polynomial-regex ReDoS). - `gateway.ts` calls the guard in `evalMemberExpression` for both sinks — the `SqlFunction` array and each `PatchMeasure` `addFilters` entry — before `Function.constructor`. Guarding at the eval sink (rather than at parse time) covers string-form members, object-form members (the `parsedMemberExpression` query-schema alternative, which skips `parseMemberExpression`), and the extra parse+eval pass after RLS / `queryRewrite`. It runs in the gateway before either planner, so it protects both Tesseract and the legacy planner. It does not depend on CubeSQL's `escape_interpolation_quotes`, and accepts everything CubeSQL legitimately emits (`${Cube.member}`, `$N$` param placeholders, escaped SQL). **Tests** - `test/member-expression-security.test.ts` — unit cases for the guard: exploit shapes (arbitrary JS, non-identifier `cubeParams`, backtick breakout, `${process…}`, prototype pivots, unterminated `${`) and the shapes the planner really emits. - `test/index.test.ts` — gateway-level cases through `apiGateway.sql({ memberExpressions: true })` that hit both sink call sites (`SqlFunction` and `PatchMeasure.addFilters`) and the object-form path, asserting rejection, alongside the existing accept case. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE --------- Co-authored-by: Claude <noreply@anthropic.com> (cherry picked from commit d24d1d6)
…on (cube-js#12207) **Check List** - [x] Tests have been run in packages where changes have been made if available - [x] Linter has been run for changed code - [x] Tests for the changes have been added if not covered yet - [ ] Docs have been added / updated if required **Description of Changes Made** Member expressions arriving over the SQL API carry a SQL fragment (`expr.sql`) that the gateway compiles into `Function.constructor(...cubeParams, \`return \`${sql}\`\`)` and evaluates (`evalMemberExpression` in `gateway.ts`). Nothing validated that compiled expression, so a fragment (or a crafted `cubeParams` entry) could execute arbitrary JavaScript in the API process. This adds a single guard at the eval sink: - New `src/member-expression-security.ts` exporting `assertSafeCompiledMemberExpression(expression)`. For the compiled `[...cubeParams, body]` array it: - requires each `cubeParams` entry to be a plain identifier (they are spliced as raw `Function` parameter source, so a non-identifier entry like `[a = <expr>] = []` would execute); - requires `body` to be the canonical `` return `<sql>` `` wrapper, then validates `<sql>`: no template-literal breakout (unescaped backtick / trailing odd backslash), and every `${…}` interpolation a dotted path rooted at one of `cubeParams`, segments being identifiers and never `constructor`/`__proto__`/`prototype`. The `${…}` scan is a linear `indexOf` walk (no polynomial-regex ReDoS). - `gateway.ts` calls the guard in `evalMemberExpression` for both sinks — the `SqlFunction` array and each `PatchMeasure` `addFilters` entry — before `Function.constructor`. Guarding at the eval sink (rather than at parse time) covers string-form members, object-form members (the `parsedMemberExpression` query-schema alternative, which skips `parseMemberExpression`), and the extra parse+eval pass after RLS / `queryRewrite`. It runs in the gateway before either planner, so it protects both Tesseract and the legacy planner. It does not depend on CubeSQL's `escape_interpolation_quotes`, and accepts everything CubeSQL legitimately emits (`${Cube.member}`, `$N$` param placeholders, escaped SQL). **Tests** - `test/member-expression-security.test.ts` — unit cases for the guard: exploit shapes (arbitrary JS, non-identifier `cubeParams`, backtick breakout, `${process…}`, prototype pivots, unterminated `${`) and the shapes the planner really emits. - `test/index.test.ts` — gateway-level cases through `apiGateway.sql({ memberExpressions: true })` that hit both sink call sites (`SqlFunction` and `PatchMeasure.addFilters`) and the object-form path, asserting rejection, alongside the existing accept case. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE --------- Co-authored-by: Claude <noreply@anthropic.com>

Check List
Description of Changes Made
Member expressions arriving over the SQL API carry a SQL fragment (
expr.sql) that the gateway compiles intoFunction.constructor(...cubeParams, \return `${sql}``)and evaluates (evalMemberExpressioningateway.ts). Nothing validated that compiled expression, so a fragment (or a craftedcubeParams` entry) could execute arbitrary JavaScript in the API process.This adds a single guard at the eval sink:
src/member-expression-security.tsexportingassertSafeCompiledMemberExpression(expression). For the compiled[...cubeParams, body]array it:cubeParamsentry to be a plain identifier (they are spliced as rawFunctionparameter source, so a non-identifier entry like[a = <expr>] = []would execute);bodyto be the canonicalreturn `<sql>`wrapper, then validates<sql>: no template-literal breakout (unescaped backtick / trailing odd backslash), and every${…}interpolation a dotted path rooted at one ofcubeParams, segments being identifiers and neverconstructor/__proto__/prototype. The${…}scan is a linearindexOfwalk (no polynomial-regex ReDoS).gateway.tscalls the guard inevalMemberExpressionfor both sinks — theSqlFunctionarray and eachPatchMeasureaddFiltersentry — beforeFunction.constructor.Guarding at the eval sink (rather than at parse time) covers string-form members, object-form members (the
parsedMemberExpressionquery-schema alternative, which skipsparseMemberExpression), and the extra parse+eval pass after RLS /queryRewrite. It runs in the gateway before either planner, so it protects both Tesseract and the legacy planner. It does not depend on CubeSQL'sescape_interpolation_quotes, and accepts everything CubeSQL legitimately emits (${Cube.member},$N$param placeholders, escaped SQL).Tests
test/member-expression-security.test.ts— unit cases for the guard: exploit shapes (arbitrary JS, non-identifiercubeParams, backtick breakout,${process…}, prototype pivots, unterminated${) and the shapes the planner really emits.test/index.test.ts— gateway-level cases throughapiGateway.sql({ memberExpressions: true })that hit both sink call sites (SqlFunctionandPatchMeasure.addFilters) and the object-form path, asserting rejection, alongside the existing accept case.🤖 Generated with Claude Code
https://claude.ai/code/session_017j4dBMJMVpABP9CvD6jzrE