Skip to content

fix: harden CLI flag precedence, positional arg validation, target-only bucket diffing, and temp SQLite URIs - #18

Merged
loverustfs merged 1 commit into
mainfrom
loverustfs-fix-audit-bugs
Nov 4, 2026
Merged

loverustfs merged 1 commit into
mainfrom
loverustfs-fix-audit-bugs

Conversation

@loverustfs

@loverustfs loverustfs commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR resolves four correctness and safety issues identified during the codebase audit:

  1. Reject unexpected positional arguments (cmd/root.go)

    • Enforce cobra.NoArgs on root and all subcommands (list, run, scan, migrate, diff, status).
    • Previously, running migration run photos (omitting -b / --bucket) with bucket: "" in config.yaml silently ignored the positional photos argument and migrated every bucket on the source cluster.
  2. Enforce CLI > env > YAML > default precedence before validation (internal/config/config.go, cmd/root.go)

    • Introduce config.LoadForCLI with CLIReadOptions so cmd.loadConfig applies explicit CLI overrides (--bucket, --workers, --multipart-concurrency, --rate-limit, --source-frozen) before validating those settings.
    • Previously, an invalid or out-of-range environment variable or YAML setting (e.g. WORKERS=2048 or an invalid BUCKET in YAML/env) caused config.Load to fail before explicit CLI flags like --workers 16 or --bucket photos could override it.
  3. Detect target-only buckets in all-bucket diff --extra and diff --strict (cmd/root.go, internal/differ/differ.go)

    • When running diff in all-bucket mode (bucket: "") with --extra or --strict, also list buckets on the target cluster and run differ.RunExtraBucket for any bucket absent from the source cluster.
    • Ensures both non-empty and empty target-only buckets are reported in the summary and --issues-jsonl, and fail the command under --strict.
  4. Use properly encoded file: SQLite URIs for temporary diff databases (internal/differ/differ.go)

    • Format the temporary diff database DSN via net/url.URL (Scheme: "file") with explicit 0600 permissions rather than raw string concatenation (tmp + "?_pragma=...").
    • Prevents TMPDIR paths containing ? or # from being misinterpreted as URI query/fragment delimiters and leaking uncleaned SQLite files outside TMPDIR.

Verification

  • go vet -tags real_s3 ./...
  • go test -race -count=1 ./...

Copilot AI balanced review requested due to automatic review settings November 4, 2026 15:55
…ly bucket diffing, and temp SQLite URIs

- Reject unexpected positional arguments across root and all subcommands (`cobra.NoArgs`) so mistyped arguments like `migration run photos` cannot silently trigger all-bucket operations.
- Respect `CLI > env > YAML > default` precedence in `loadConfig` via `config.LoadForCLI` so explicit CLI flags (`--bucket`, `--workers`, `--multipart-concurrency`, `--rate-limit`, `--source-frozen`) override invalid env or YAML values instead of failing before CLI flags are applied.
- Detect and report target-only buckets during all-bucket `diff --extra` and `diff --strict` via `differ.RunExtraBucket`.
- Construct encoded `file:` SQLite URIs for temporary diff databases so `TMPDIR` paths containing `?` or `#` are handled safely without leaking files.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Invalid YAML rate limits block valid CLI overrides, and empty source clusters prevent extra/strict diffing from reporting target-only buckets.

2 open issues
What changed in this PR

This PR hardens the migration CLI against unintended bucket selection, improves configuration precedence, and extends diff reporting to target-only buckets.

Changes:

  • Reject unexpected positional arguments and apply explicit CLI overrides before configurable-value validation.
  • Add target-only bucket reporting to all-bucket diff --extra and diff --strict.
  • Encode temporary SQLite file URIs safely and add regression tests.
File Description
internal/​differ/​differ.go Adds target-only bucket reporting and encoded temporary SQLite URIs.
internal/​differ/​audit_test.go Tests special-character temporary paths and target-only buckets.
internal/​config/​config.go Introduces CLI-aware loading and selective environment overrides.
cmd/​root.go Rejects positional arguments and coordinates CLI precedence and expanded diffing.
cmd/​audit_test.go Tests argument rejection, configuration precedence, and target-only diff output.

Review effort: Balanced

Comment thread cmd/root.go
Comment on lines +304 to +307
names, err := resolveBuckets(ctx, cfg, oldCli)
if err != nil {
return err
}
Comment thread internal/config/config.go
// corresponding CLI flag was explicitly set) and defaults, and validates fields
// that cannot be overridden by CLI flags. Callers must apply CLI overrides and
// then validate names, concurrency and rate limit.
func LoadForCLI(path string, opt CLIReadOptions) (*Config, error) {
@loverustfs
loverustfs merged commit 0ab711c into main Nov 4, 2026
9 checks passed
@loverustfs
loverustfs deleted the loverustfs-fix-audit-bugs branch November 4, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants