Skip to content

QueryService interpolates filter values into SQL instead of binding them #2744

Closed

Description

@malibio

Problem Statement

QueryService builds its WHERE clause by formatting values directly into the SQL string rather than binding them as parameters:

// packages/core/src/services/query_service/mod.rs
fn format_value(&self, value: Option<&serde_json::Value>) -> Result<String> {
    match value {
        Some(serde_json::Value::String(s)) => Ok(format!("'{}'", self.escape_string(s))),
        ...

Safety currently rests on three things holding simultaneously: escape_string doubling single quotes, format_value accepting only String/Number/Bool/Null, and validate_identifier allowlisting [A-Za-z0-9_:-] for identifiers upstream in query_ops.

This is correct today. A review of #2742 specifically audited it and found no constructible injection: SQLite has no backslash escape in string literals, so quote-doubling is complete for a single-quoted literal, and escape_string_for_like orders its replacements correctly.

The concern is structural rather than an open vulnerability. The construction is correct-by-audit, not correct-by-construction: every future operator or filter type added to QueryService is a fresh opportunity to interpolate a value without routing it through escape_string, and nothing in the type system or a test will catch it. The guarantee lives in the discipline of whoever edits the file next.

Why now

The threat model changed in #2742. Previously this path was reachable only from agent and CLI callers; that PR routes the desktop app's saved queries through it, so user-authored filter values now reach the same string formatting. The filter editor emits only property filters over schema-known fields, so practical exposure is narrow — but "narrow" is a property of the current UI, not of the query engine.

Proposed Solution

Bind values as parameters. libsql supports bound parameters, so the WHERE-clause builders would emit placeholders and collect values into a parameter vector passed alongside the SQL.

Worth confirming during implementation:

  • execute_query_for_ids currently takes a finished SQL string (store.query_node_ids_raw); binding needs a path that carries parameters too.
  • The IN operator generates a variable-length list, so its placeholder count is dynamic.
  • escape_string_for_like still has a job after this — LIKE wildcard escaping is separate from parameter binding.
  • Identifier positions (table/column/JSON paths) cannot be bound and must keep using validate_identifier.

Acceptance Criteria

  • Filter values are bound as parameters rather than formatted into the SQL string
  • Identifier positions still go through validate_identifier (binding does not apply to them)
  • LIKE wildcard escaping is preserved for contains
  • Existing query_service_test.rs coverage passes unchanged — this is a refactor, not a behavior change
  • A test covers a value containing a quote to pin the behavior at the boundary

Context

Surfaced in review of #2742. Deferred there as a refactor of a file that PR does not modify.

Activity

  1. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Frontend: 256 test files passed (5214 tests)

  2. self-assigned this
    on Sep 17, 2026
  3. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Implementation complete; pre-push gate running.

    Filter values now bind as parameters. BoundSql owns the SQL and its parameter vector, and its bind is the only way to place a value — it pushes the value and returns the matching ?N in one step, so a placeholder cannot exist without its value and the numbering cannot drift. format_value and escape_string are deleted.

    On the four points the issue flagged for confirmation:

    • execute_query_for_ids/query_node_ids_raw now carry a parameter vector alongside the SQL.
    • IN generates one placeholder per member, numbered consecutively.
    • escape_string_for_like keeps its job but loses its quote-doubling. Wildcard escaping is pattern semantics, not SQL syntax; quote-doubling was syntax, which binding subsumes. Leaving it in would have matched a literal doubled quote — this is a real behavior fix, pinned by a test that fails against the old code.
    • Identifier positions are untouched and still arrive allowlisted by validate_identifier.

    Five new tests, each verified to fail against the previous implementation by reverting the fix and re-running. All 1573 Rust lib tests pass; quality:fix clean.

  4. added a commit that references this issue on Sep 17, 2026
    66edbfd
  5. added a commit that references this issue on Sep 18, 2026
    9096eb3
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