Skip to content

Harden sqlite_schema predicates for quoted identifiers#5682

Merged
PThorpe92 merged 6 commits intotursodatabase:mainfrom
kumarUjjawal:fix/quoted_table_currption
Mar 12, 2026
Merged

Harden sqlite_schema predicates for quoted identifiers#5682
PThorpe92 merged 6 commits intotursodatabase:mainfrom
kumarUjjawal:fix/quoted_table_currption

Conversation

@kumarUjjawal
Copy link
Contributor

Description

There was a schema corruption/injection paths caused by quoted identifiers containing ' in internal schema maintenance SQL.

Changes included:

  • Added a shared SQL string-literal escape helper and used it for internal sqlite_schema predicates in:
    • ParseSchema filters for create table/index/view/trigger paths
    • ALTER TABLE schema row update predicates (WHERE name = ...)
    • CDC version table lookups using table-name literals
  • Fixed ADD COLUMN runtime schema update to use normalized table-name lookup.

Motivation and context

Issue #5632 showed that crafted quoted table names could break internal schema predicate matching and cause incorrect schema reparsing/rewrites, leading to malformed/corrupt database state.

Closes #5632

Description of AI Usage

Copy link

@turso-bot turso-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review @sivukhin

Copy link
Collaborator

@sivukhin sivukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumarUjjawal , thanks for the contribution.

I left one minor comment about one more extra test we can add for the bug you fixed

/// `CREATE TABLE t (x)`, whereas sqlite stores it with the original extra whitespace.
pub fn to_sql(&self) -> String {
let mut sql = format!("CREATE TABLE {} (", self.name);
let mut sql = format!("CREATE TABLE {} (", quote_ident(&self.name));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for one more bug I think:

$> tursodb bug.db                                                                                                                                                 turso> CREATE TABLE "t t"("a a" TEXT, "b b" TEXT);
turso> ALTER TABLE "t t" ADD COLUMN "c c" TEXT;
$> tursodb bug.db
Error: unexpected token 't' at offset 15

Note, that reproducer requires restart of the DB.
Let's add test for this bug too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the test

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 2, 2026

Merging this PR will not alter performance

✅ 279 untouched benchmarks
⏩ 105 skipped benchmarks1


Comparing kumarUjjawal:fix/quoted_table_currption (9d2c5e6) with main (4cad850)

Open in CodSpeed

Footnotes

  1. 105 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@PThorpe92 PThorpe92 merged commit a128d7f into tursodatabase:main Mar 12, 2026
89 checks passed
penberg pushed a commit that referenced this pull request Mar 13, 2026
…umar Ujjawal

There was a schema corruption/injection paths caused by quoted
identifiers containing `'` in internal schema maintenance SQL.
  Changes included:
  - Added a shared SQL string-literal escape helper and used it for
internal `sqlite_schema` predicates in:
    - `ParseSchema` filters for create table/index/view/trigger paths
    - `ALTER TABLE` schema row update predicates (`WHERE name = ...`)
    - CDC version table lookups using table-name literals
  - Fixed `ADD COLUMN` runtime schema update to use normalized table-
name lookup.
 Issue #5632 showed that crafted quoted table names could break internal
schema predicate matching and cause incorrect schema reparsing/rewrites,
leading to malformed/corrupt database state.
Closes #5632

Reviewed-by: Nikita Sivukhin (@sivukhin)

Closes #5682

(cherry picked from commit a128d7f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quoted table name can corrupt database if sql is targetted intentionally.

3 participants