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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

## [1.1.5] - 2026-10-29

### Security
- **A raw `&` in a password parameter showed the rest of the password**: pgx and the SQL Server driver end a parameter's value at the next `&`, and so did dbdiff when it hid one, so `postgres://user@host/db?sslmode=disable&password=pass&word` was printed as `…&password=***&word`. That reached `validate`'s output and the `-v` and `--debug` logs, on PostgreSQL and SQL Server, and on PostgreSQL, when pgx couldn't read what followed the `&` (`&word`, `&a=b=c`), the error of every command that connects, twice: ``cannot parse `…&password=***&word`: failed to parse as URL (missing key/value separator "=" in URI query parameter: "word")``. It held for `password=`, `sslpassword=`, `pwd=` and the other password parameters, with or without a port, and for settings and DSNs given where a command or a flag value belongs: `server=host;password=pass&word;database=db` was printed as `server=host;password=***&word;database=db`. 1.1.4 and every earlier version do this. A driver can't tell an `&` in a password from the start of the next parameter, and neither can dbdiff, so it now hides all that follows a password parameter's `=`, to the end of the URL or the settings, or to the quote that closes them in an error (see Changed). The drivers read what followed the `&` as parameters, and when the server or the driver refused one, its error quoted it, as PostgreSQL's `unrecognized configuration parameter "word"` does. MySQL's driver has no password parameter: it sends an unknown one to the server as a `SET`. That never logs anyone in, and the server's syntax error quoted the value, `&` or not (`near 'pass' at line 1`), past the 80 characters the server cuts such an error to, across a second parameter, and whether or not the password was also in the user info. Worse, when the value was quoted (`?password=%27pass%27`), MySQL ran the `SET password = 'pass'` it received as a `SET PASSWORD` and changed the logged-in account's own password to it (confirmed on throwaway accounts). **If you ran a MySQL URL carrying a `password=`, `pwd=` or `passwd=` query parameter under 1.1.4 or earlier, that account's password may have been reset to the parameter's value: check and reset the affected MySQL accounts.** dbdiff now refuses a password parameter in a `mysql://` or `mysql+tcp://` URL and says to put the password in the user info, so the `SET` is never sent. For PostgreSQL and SQL Server, where a password parameter is real, dbdiff leaves out a connection error that quotes a token of what it hides, at any length — a parameter name the server rejects (a 1-2 character piece after a raw `&` included, and one holding a quote), one padded with the spaces, tabs or control bytes a `%`-escape decoded to, a setting inside an `options` value, a value go-mssqldb read with a `+` as a space, or go-mssqldb's unquoted duplicate key — and names the URL with that part hidden instead. On PostgreSQL's parse path, pgx repeats some values it rejects, at times escaped or trimmed (`unknown target_session_attrs value`, `unknown channel_binding value`, a missing `sslrootcert`/`sslcert`/`sslkey`/`servicefile` file, `connect_timeout`, `pool_max_conns`, `require_auth`), and quotes the URL it parsed, which a backtick after the password ended early. For a URL with a password parameter, dbdiff now shows only one of a few fixed reasons that name no value (`sslmode is invalid`, `invalid port`), matched against pgx's whole error, after the URL with that part hidden, and the generic parse hint for any other. What still shows, as in every version: a tail which spells a connection setting that an error repeats unquoted or in backticks — such as `options=` reaching the backend (`invalid command-line argument for server process: …`), `host=` or SQL Server's `server=` reaching the resolver (`lookup … no such host`), `port=` in a dial error, or `user=`, `dbname=` or `database=` in pgx's ``failed to connect to `user=… database=…` `` — and the digits-only password with no `@host` noted under 1.1.4 (`postgres://app:20241031/db`), which cannot be told from a port
- Migration files hold what followed the `&` only where such a URL connected: dbdiff writes one once both databases have accepted the connection, and the drivers sent only the part of the password before the `&`, which a server that checks that password refuses. Such a URL connected when nothing checked that password: on a PostgreSQL server with the `trust` method, with an `sslpassword=` and no encrypted client key to open with it, or with a SQL Server `pwd=`, which the driver ignores in a URL; and on PostgreSQL only when what followed the `&` read as settings pgx and the server take, as in `sslpassword=pass&application_name=word` or any dotted custom setting (`word.x=y`), which PostgreSQL accepts. The migration's `-- Source:` or `-- Target:` line then shows that part: if you wrote such a URL, scrub those lines, and change the password wherever it is checked
- A URL given in place of a file or a schema name was printed as given: `tables --schema "$URL"` printed `No tables found in schema: <URL>`, `migrate -o "$URL"` printed it in its prompt and its `-v` log line, `compare -o` and `interactive -o` did the same, and `migrate --backup-dir "$URL"` printed it in its `Backup created:` and `Failed to create backup:` `-v` lines. The password in it is hidden now. A URL given to `-o` or `--backup-dir` still becomes directories on disk named after it, with the password in those names; only what dbdiff prints is hidden. `--config "$URL"` printed what followed the last `.` in it, part of the password included, which viper took for the file's type (`Unsupported Config Type "word@host:5432"`); dbdiff no longer reads a URL given for `--config` or in `DBDIFF_CONFIG`, and says it takes a file

### Changed
- What dbdiff prints of a connection string, in the migration header, `-v` logs, `validate` and errors, now ends at a password parameter: `sqlserver://host?database=db&password=x&encrypt=disable` is printed as `sqlserver://host?database=db&password=***`, and `server=host;password=x;database=db` as `server=host;password=***`. A connection error is replaced by a generic message when it quotes, in single or double quotes, a whole name or value that occurs after the first password parameter's `=`, or names one as go-mssqldb's duplicate key (matched token by token, so an ordinary error that merely contains the user or database name inside a longer word is kept; a token that holds a quote is matched wherever it appears). As a fail-closed trade-off, that also replaces a genuine login error when it quotes a real parameter you wrote **after** the password — a `user id` or an `application_name` equal to the user or database name, or to it with a quote beside it — with `connecting with "…" failed with an error that quotes part of what follows the '=' of its password parameter …`. To keep the other parameters and the real error in view, put the password in the user-info part (`postgres://user:pass%26word@host/db?sslmode=disable`) or, on PostgreSQL and SQL Server, its parameter last; and in a parameter, write an `&` in the password as `%26`, or the driver reads only the part before it. A MySQL URL's password must be in the user info now: a password parameter there is refused, since go-sql-driver never logs in with one (so putting it last would not have protected it either). dbdiff scans a MySQL query for a password parameter from its first `?`, so besides the URLs 1.1.4 refused it also refuses a few contrived ones 1.1.4 connected with, where a `password`/`pwd`/`passwd` token sits after an `=` inside another parameter's value (`?connectionAttributes=password=x`). Connections don't change otherwise: the drivers still read a password parameter up to the next `&`. A faulty parameter after a PostgreSQL password still reports why the URL won't parse when pgx's reason is one of a few fixed ones that name no value (`sslmode is invalid`, `invalid port`), with the password hidden; any other reason gives the generic hint

## [1.1.4] - 2026-10-28

### Security
- Passwords still reached the terminal, logs and migration files in 1.1.3 in these cases, found by running every command with a password marked in each of its parts. A URL glued to a flag (`compare -v"$URL"`, `--source"$URL"`, `--"$URL"`) was quoted whole in the error for the unknown flag (`unknown shorthand flag: 'p' in -postgres://user:password@host/db`); passwords are now hidden in a URL wherever it starts. `dbdiff help "$URL"` printed it in ``Unknown help topic [`postgres://user:password@host/db`]``, and `dbdiff __complete "$URL" ''`, which shells run for completions, in `unable to find a command for arguments: [postgres://user:password@host/db]`, on stderr and in `$BASH_COMP_DEBUG_FILE`: cobra prints both itself, and dbdiff now hides the password in their arguments first. A URL whose password lacked the `@host` after it, as in `postgres://user:password`, was printed whole, in `-v` logs and in the error for it; what stands in the place of a port is now hidden unless it is a number. A password setting whose value holds a URL (`password=postgres://u:p;word@host/db`) was hidden only up to the `;`; all of it is hidden now. MySQL's errors for a URL without `/database` after the address quoted part of the password: `mysql://user:pass/word@tcp(host:3306)` failed with `default addr for network 'user:pass' unknown`, and `mysql://user:pass/word%ZZ@tcp(host:3306)` with `invalid dbname "word%ZZ@tcp(host:3306)"`. dbdiff now passes on only go-sql-driver's errors that quote nothing of the DSN, and for the others says what a MySQL URL looks like
- **A password parameter holding an `@`, after a port, was hidden only up to the `@`**: `sqlserver://host:1433?database=db&password=P@ssw0rd` was printed as `sqlserver://host:***@ssw0rd` in `-v` logs, errors and the migration header. Migration files written from such a URL hold the rest of the password: scrub them and rotate it. dbdiff now hides all of that value, and refuses such a URL (see below)
- **A password parameter holding a `;` was hidden only up to the `;`**: pgx reads `postgres://app@host/db?password=pass;word` as the password `pass;word`, which the `-v` logs and the migration header showed as `?password=***;word`. Migration files written from such a URL hold the rest of the password: scrub them and rotate it. In a URL, a parameter's value now runs to the next `&`, as the drivers read it
- dbdiff refuses a URL whose driver would read part of the password as the user name, the host or the database, which the driver's errors quote: `postgres://user:/pass@host:5432/db` failed with ``failed to connect to `user=me database=pass@host:5432/db` ``. Such URLs are a PostgreSQL URL with `@` or `/` in its user name or password, a SQL Server URL with `/`, `?` or `#` there, a MySQL URL without `/database` after the address, and one with a password parameter holding an `@` like the one above. The error names the URL with its password hidden and says what to percent-encode or add. A PostgreSQL or SQL Server URL with another `@` after its host, as in `?application_name=a@b`, can be refused too: write that `@` as `%40`
- A MySQL URL with a password and a raw `@` after its `/database`, as in `?connectionAttributes=owner:ops@example`, is refused too, though 1.1.3 connected with it: write that `@` as `%40`

### Fixed
- `mysql+tcp://` URLs, and MySQL URLs whose scheme is in upper case, connect as the user they name. dbdiff accepted both, but read the scheme as the user name: `Access denied for user 'mysql+tcp'`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ go install github.com/meru143/dbdiff@latest
brew install meru143/homebrew-dbdiff/dbdiff

# Via release binary (Linux x86-64 shown)
curl -fsSL https://github.com/Meru143/dbdiff/releases/download/v1.1.4/dbdiff_1.1.4_linux_amd64.tar.gz | tar -xz dbdiff
curl -fsSL https://github.com/Meru143/dbdiff/releases/download/v1.1.5/dbdiff_1.1.5_linux_amd64.tar.gz | tar -xz dbdiff
sudo mv dbdiff /usr/local/bin/
```

Expand Down
2 changes: 1 addition & 1 deletion cmd/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var CompareCmd = &cobra.Command{
if err := output.WriteFile(cfg.Output, outputStr); err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
logging.Info(fmt.Sprintf("Output written to: %s", cfg.Output))
logging.Info(fmt.Sprintf("Output written to: %s", dsn.Redact(cfg.Output)))
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion cmd/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/meru143/dbdiff/internal/db"
"github.com/meru143/dbdiff/internal/db/dsn"
"github.com/meru143/dbdiff/internal/diff"
"github.com/meru143/dbdiff/internal/logging"
"github.com/meru143/dbdiff/internal/output"
Expand Down Expand Up @@ -130,7 +131,7 @@ var InteractiveCmd = &cobra.Command{
return fmt.Errorf("failed to save interactive migration: %w", err)
}

fmt.Printf("\nSelected migration output stored securely to: %s\n", cfg.Output)
fmt.Printf("\nSelected migration output stored securely to: %s\n", dsn.Redact(cfg.Output))
return nil
},
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ var MigrateCmd = &cobra.Command{

// Confirmation prompt (only for non-dry-run, non-force)
if !cfg.DryRun && !cfg.Force && cfg.Output != "stdout" {
fmt.Printf("\nMigration will write to: %s\n", cfg.Output)
fmt.Printf("\nMigration will write to: %s\n", dsn.Redact(cfg.Output))
if !confirmPrompt() {
logging.Info("Migration cancelled")
return nil
Expand All @@ -169,9 +169,10 @@ var MigrateCmd = &cobra.Command{
existingContent, _ := os.ReadFile(cfg.Output)
backupPath, err := backupMgr.Backup("migration", existingContent)
if err != nil {
logging.Warn(fmt.Sprintf("Failed to create backup: %v", err))
// --backup-dir, like -o, may hold a URL, which the path and the error echo.
logging.Warn(fmt.Sprintf("Failed to create backup: %v", dsn.Redact(err.Error())))
} else {
logging.Info(fmt.Sprintf("Backup created: %s", backupPath))
logging.Info(fmt.Sprintf("Backup created: %s", dsn.Redact(backupPath)))
}
}
}
Expand All @@ -182,7 +183,7 @@ var MigrateCmd = &cobra.Command{
if err := output.WriteFile(cfg.Output, fullSQL); err != nil {
return fmt.Errorf("failed to write migration: %w", err)
}
logging.Info(fmt.Sprintf("Migration written to: %s", cfg.Output))
logging.Info(fmt.Sprintf("Migration written to: %s", dsn.Redact(cfg.Output)))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var TablesCmd = &cobra.Command{
}

if len(tables) == 0 {
fmt.Printf("No tables found in schema: %s\n", cfg.Schema)
fmt.Printf("No tables found in schema: %s\n", dsn.Redact(cfg.Schema))
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package config

import (
"fmt"
"strings"
"time"

"github.com/meru143/dbdiff/internal/db/dsn"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -57,6 +59,11 @@ func Load(flags map[string]interface{}) (*Config, error) {
}

if configFile, ok := flags["config"].(string); ok && configFile != "" {
// viper takes what follows a path's last '.' for the file's type, and its error quotes
// that: in a URL, part of the password (Unsupported Config Type "word@host:5432").
if strings.Contains(configFile, "://") {
return nil, fmt.Errorf("failed to read config file: --config takes a file, not a URL: %s", dsn.Redact(configFile))
}
v.SetConfigFile(configFile)
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"os"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -128,6 +129,15 @@ schema: testschema
}
}

// A URL given for --config isn't read: viper takes what follows its last '.' for the file's
// type, and quoted that in its error (Unsupported Config Type "QZYb@localhost:5432").
func TestLoad_ConfigURL(t *testing.T) {
_, err := Load(map[string]interface{}{"config": "postgres://app:QZXa.QZYb@localhost:5432"})
if err == nil || strings.Contains(err.Error(), "QZ") || !strings.Contains(err.Error(), "postgres://app:***@localhost:5432") {
t.Errorf("expected an error naming the URL without its password, got %v", err)
}
}

func TestLoad_IgnorePatterns(t *testing.T) {
flags := map[string]interface{}{
"ignore-patterns": []string{"*_created_at", "*_updated_at"},
Expand Down
Loading
Loading