Skip to content

insert --truncate accepts an empty JSON string and silently clears existing rows #978

Backlog

Description

@fernandezbaptiste

Hullo folks,

I'm doing some research for Tessl Code Review and we ran it against your repo. It flagged a potential issue, and I figured it made sense to share it with you:

insert --truncate accepts an empty JSON string ("") as an empty record collection and deletes all existing rows, exiting successfully. A nonempty JSON string is rejected as invalid record input.

Reproduction

Reproduced against current main 74c0b6cdce3e9234cdbcc0e3fcb339d950f04f19, using Python 3.14.2. Run this only against the disposable database below:

import pathlib
import sqlite3
import tempfile
from click.testing import CliRunner
from sqlite_utils.cli import cli

for payload in ['""', '[]', '"bad"', '{}']:
    with tempfile.TemporaryDirectory() as tmp:
        path = pathlib.Path(tmp) / "data.db"
        with sqlite3.connect(path) as conn:
            conn.execute("create table items(id integer primary key)")
            conn.execute("insert into items values(1)")
        result = CliRunner().invoke(
            cli, ["insert", str(path), "items", "-", "--truncate"], input=payload
        )
        with sqlite3.connect(path) as conn:
            rows = conn.execute("select * from items").fetchall()
        print(repr(payload), result.exit_code, rows, repr(result.output))

Actual:

'""'    0 []     ''
'[]'    0 []     ''
'"bad"' 1 [(1,)] "Error: Rows must all be dictionaries, got: 'b'\n"
'{}'    0 [(1,)] ''

Expected: the JSON string should fail validation and preserve the existing row, like the nonempty string. The valid empty array should still truncate, as intended by #935 / #936.

Cause and impact

The CLI parses "" into an empty Python string. Its per-record dictionary check never runs because there are no elements. The empty-iterator branch then calls delete_where().

An upstream exporter accidentally returning an empty string instead of a record array can therefore clear the destination table without an error. Validating the top-level JSON shape before reaching truncation looks like a possible fix direction.

This exercised the unchanged CLI via Click's test runner and real disposable SQLite databases. I checked the original empty-import issue and PR discussion; this concerns invalid input, rather than the intended empty-array behavior. No fix PR or full-suite run is included.

Full transparency: I work at Tessl, and we've spotted that using Tessl Code Review - using repository-level context and specific review lenses that improve over time through feedback. That’s how this finding surfaced.

We’re just getting started and want to offer the tool free to open-source projects. If this is useful, happy to give you access.

Baptiste

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions