Skip to content

[P1] [bug] extractTableName catches wrong table for JOINs/CTEs — silent corruption on Save #399

Closed

Description

@EVWorth

[P1] [bug] extractTableName regex catches wrong table for JOINs/CTEs/subqueries — silent data corruption on Save

Context

The grid's handleSave calls extractTableName(sql) to derive the table for UPDATE/INSERT/DELETE generation. The regex only matches the first FROM clause.

Problem

Per docs/audits/results.md F6.

SELECT u.* FROM users u JOIN orders o ON u.id = o.user_id WHERE o.id = 42 returns users but the user filtered by orders and probably wants to edit the orders row. CTE/subquery forms (WITH cte AS ..., SELECT * FROM (SELECT ...) t) return the wrong name or fail. Multi-statement scripts always pick the first FROM. Save generates UPDATE against the wrong table — silent data corruption.

Files

  • src/lib/sql-generator.ts:60-67
  • src/components/grid/ResultsGrid.tsx:243-258

Repro (if applicable)

Static analysis only.

Expected

Either (a) confirm table before Save via modal "Apply changes to table X?", or (b) parse with a real SQL parser that handles JOINs/CTEs/subqueries.

Proposed fix

Scope M. Option (a) is the safer interim: confirm modal with detected table + dropdown to override.

Acceptance

A JOIN query shows the modal with the user-selected table; UPDATE is generated against the confirmed table.

Needs human verify

Yes

Labels: audit, area/results, severity/p1, kind/bug

Activity

  1. added
    audit
    severity/p1
    kind/bug
    area/results
    on Jul 31, 2026
  2. EVWorth commented on Sep 4, 2026

    @EVWorth
    OwnerAuthor

    Fixed by #529 (commit dc1f76c). extractTableName was replaced with resolveEditTarget in src/lib/sql-generator.ts, which refuses joins (both syntaxes), CTEs, UNION, subquery-in-FROM, GROUP BY/DISTINCT, multi-statement and non-SELECT rather than guessing a table from the first FROM clause. It errs towards refusing: a query it wrongly declines is an inconvenience, one it wrongly accepts is a silent write to the wrong table.

    Not auto-closed because the PR body wrote Refs #399 rather than a closing keyword.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/resultsauditkind/bugseverity/p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions