Skip to content

[SECURITY] Use backend-correct quoting for generated SQL literals and identifiers #821

Open

Description

@jeffreyaven

Constituent of #769. Reviewed current main at 1de4135 on 2026-10-08; this issue proposes work, no implementation is included.

CWEs: CWE-89.

Finding and evidence

The fragment rewriter uses the parser's MySQL-style EncodeSQL for string literals (internal/stackql/astvisit/fragment_rewriting.go). The SQLite/Postgres AST formatters fall back to the same node formatting, and several identifier paths wrap names in double quotes without escaping embedded quotes (pkg/astformat/ast_format_postgres.go, internal/stackql/sql_system/sqlite.go).

Reproduced with the current build and local k8s fixture: a mock node with kind="O'Reilly" is returned without a filter, but AND kind = 'O''Reilly' fails in backing SQLite with near "Reilly": syntax error.

At the formatter/SQLite boundary, the value x' OR 1=1 -- renders as 'x\' OR 1=1 --'; SELECT 1 WHERE 'x' = <rendered-value> returned 1. This proves syntax escape at that boundary; it is not a claim that every planner context accepts that exact payload or that stacked statements were demonstrated. Bare provider-free SELECT follows a different path and correctly handled the apostrophe in the probe.

Required outcome

  • Prefer bound values for generated predicates. Where rendering is required, use the target dialect's quoting rules, including apostrophes, backslashes, NULs and Unicode.
  • Centralize identifier quoting and double embedded quote delimiters. Apply it to user names, aliases and provider-defined column/relation names at actual SQL sinks.
  • Keep CLI, pgwire and MCP behavior consistent without changing legitimate values.

Validation and acceptance

Add robot regression tests on both backends for quoted response-column filters and aliases/identifiers containing delimiters. Add generator-level tests proving hostile-looking text remains data, including extended parameters and nested predicates. Existing PR #804 parameter substitution tests must continue passing.

Scope and related work

Separate from intentional NATIVEQUERY passthrough and the cache-request lookup interpolation. QueryMaterializedView has no callers in this checkout; remove or harden it within this work, without claiming a reachable exploit through dead code.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions