Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Added
- Refuse inspect limits outside 1-200 and sensitive value scans outside 0-1000 instead of silently clamping them (#97). A named table still ignores the listing limit.
- `gulliver rules --format markdown|json` prints a generated rule table (markdown) or the same JSON as `--json`; the README Scale lint rules table is generated from it (#18).
- `gulliver spend -o/--output PATH` and `gulliver top -o/--output PATH` write the report as UTF-8 and print a one-line confirmation (#19).
- Opt-in `check --samples N` / MCP `samples` (0-20) reports guarded offending values and row counts for failed unique, accepted-values and range checks. Each extra query is budget-gated and logged; blocked or failed samples leave the check result intact (#48, #49).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ The read-only gate uses its own codes. A `GS` finding is always a `block` with t

```
gulliver status connections, budget, spend today (and your own, under caller)
gulliver inspect [TABLE] [--sensitive] [--scan N] biggest tables, one table's columns, or sensitive columns
gulliver inspect [TABLE] [--limit 25] [--sensitive] [--scan N] limit: 1-200 (ignored with TABLE); scan: 0-1000, 0 disables scanning
gulliver lint SQL [--dialect D] scale lint of one query
gulliver lint PATH... [--format text|json|sarif|github|gitlab|junit] [--fail-on error|warn|info|never]
[--exclude GLOB] [--warn-unused-ignores] [--metadata] [-o PATH] files and folders (CI)
Expand Down
4 changes: 2 additions & 2 deletions src/gulliver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ def add_parser(name: str, **kwargs: Any) -> argparse.ArgumentParser:

p = sub.add_parser("inspect", help="Biggest tables, or one table's columns")
p.add_argument("table", nargs="?")
p.add_argument("--limit", type=int, default=25)
p.add_argument("--limit", type=int, default=25, help="Tables to list (1-200); ignored with a table name")
p.add_argument("--sensitive", action="store_true",
help="List columns that look like personal data or secrets (by name and earlier values)")
p.add_argument("--scan", type=int, default=0, metavar="ROWS",
help="With --sensitive: also check this many rows of text values (one budget-gated query)")
help="With --sensitive: check 0-1000 rows of text values (0 disables the budget-gated scan)")

p = sub.add_parser("lint", help="Scale lint only (no database needed): one SQL string, or files and folders")
p.add_argument("sql", nargs="*", help="SQL text, '-' to read stdin, or .sql files and folders (CI mode)")
Expand Down
4 changes: 3 additions & 1 deletion src/gulliver/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def status(settings: Settings) -> dict[str, Any]:
# -- inspect ---------------------------------------------------------------------------
def inspect(settings: Settings, connection: str | None = None, table: str | None = None,
limit: int = 25) -> dict[str, Any]:
if not table and not 1 <= limit <= 200:
return _error("--limit must be between 1 and 200.", "Use a limit from 1 to 200, for example --limit 25.")
try:
with _engine(settings, connection) as (conn, engine):
if table:
Expand All @@ -127,7 +129,7 @@ def inspect(settings: Settings, connection: str | None = None, table: str | None
return {"status": "ok", "connection": conn.name, "engine": conn.engine,
"table": table_dict,
"next_step": "Write your SQL using only the columns you need, then call gulliver_preflight."}
tables = engine.list_tables(limit=max(1, min(int(limit), 200)))
tables = engine.list_tables(limit=limit)
return {"status": "ok", "connection": conn.name, "engine": conn.engine,
"tables": [t.as_dict() for t in tables],
"next_step": "Inspect one table for its columns, or call gulliver_preflight with your SQL."}
Expand Down
11 changes: 8 additions & 3 deletions src/gulliver/guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,11 @@ def inspect_sensitive(settings: Settings, connection: str | None = None, table:
from .engines import EngineError, dialect_for
from .features import Blocked, _gated_run

if not table and not 1 <= limit <= 200:
return _error("--limit must be between 1 and 200.", "Use a limit from 1 to 200, for example --limit 25.")
if not 0 <= scan_rows <= 1000:
return _error("--scan must be between 0 and 1000.", "Use --scan 0 for no value scan, or up to --scan 1000.")

try:
with _engine(settings, connection) as (conn, engine):
if table:
Expand All @@ -1184,7 +1189,7 @@ def inspect_sensitive(settings: Settings, connection: str | None = None, table:
infos = [info]
else:
infos = []
for t in engine.list_tables(limit=max(1, min(int(limit), 200))):
for t in engine.list_tables(limit=limit):
infos.append(t if t.columns else (engine.table_info(t.name) or t))
dialect = dialect_for(conn.engine)
tables_out: list[dict[str, Any]] = []
Expand All @@ -1197,10 +1202,10 @@ def inspect_sensitive(settings: Settings, connection: str | None = None, table:
if text_cols:
sql = (exp.select(*[exp.column(c, quoted=True) for c in text_cols])
.from_(exp.to_table(info.name, dialect=dialect))
.limit(max(1, min(int(scan_rows), 1000))).sql(dialect=dialect))
.limit(scan_rows).sql(dialect=dialect))
try:
res = _gated_run(settings, conn.name, engine, sql, "sensitive-scan",
preview_rows=max(1, min(int(scan_rows), 1000)))
preview_rows=scan_rows)
except Blocked as blocked:
notes.append(f"{info.name}: value scan not run: {blocked.result.get('next_step')}")
res = None
Expand Down
64 changes: 64 additions & 0 deletions tests/test_inspect_bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import json

import pytest

from gulliver import core, guard
from gulliver.cli import main


@pytest.mark.parametrize("limit", [-1, 0, 201, 500])
@pytest.mark.parametrize("sensitive", [False, True])
def test_invalid_inspect_limit_is_refused(settings, monkeypatch, capsys, limit, sensitive):
def unexpected_engine(*args, **kwargs):
pytest.fail("invalid bounds must be refused before opening an engine")

monkeypatch.setattr(core, "_engine", unexpected_engine)
inspect = guard.inspect_sensitive if sensitive else core.inspect
result = inspect(settings, limit=limit)
assert result["status"] == "error"
assert "--limit" in result["error"]
assert result["next_step"]

args = ["inspect", "--limit", str(limit), "--json"]
if sensitive:
args.append("--sensitive")
assert main(args) == 1
assert json.loads(capsys.readouterr().out)["status"] == "error"


@pytest.mark.parametrize("scan", [-5, -1, 1001, 5000])
def test_invalid_inspect_scan_is_refused(settings, monkeypatch, capsys, scan):
def unexpected_engine(*args, **kwargs):
pytest.fail("invalid scan must be refused before opening an engine")

monkeypatch.setattr(core, "_engine", unexpected_engine)
result = guard.inspect_sensitive(settings, scan_rows=scan)
assert result["status"] == "error"
assert "--scan" in result["error"]
assert result["next_step"]
assert main(["inspect", "--sensitive", "--scan", str(scan), "--json"]) == 1
assert json.loads(capsys.readouterr().out)["status"] == "error"


@pytest.mark.parametrize("limit", [1, 200])
def test_inspect_accepts_limit_boundaries(settings, limit):
assert core.inspect(settings, limit=limit)["status"] == "ok"
assert guard.inspect_sensitive(settings, limit=limit)["status"] == "ok"


@pytest.mark.parametrize("scan", [0, 1000])
def test_inspect_accepts_scan_boundaries(settings, scan):
result = guard.inspect_sensitive(settings, table="users", scan_rows=scan)
assert result["status"] == "ok"
assert result["scanned_rows"] == scan


@pytest.mark.parametrize("sensitive", [False, True])
def test_inspect_ignores_limit_for_named_table(settings, demo_db, capsys, sensitive):
inspect = guard.inspect_sensitive if sensitive else core.inspect
assert inspect(settings, table="users", limit=-1)["status"] == "ok"
args = ["--dsn", demo_db, "inspect", "users", "--limit", "-1", "--json"]
if sensitive:
args.append("--sensitive")
assert main(args) == 0
assert json.loads(capsys.readouterr().out)["status"] == "ok"
Loading