Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't print URL when url.Parse returns an error #2082

Merged
merged 2 commits into from
Jul 12, 2024

Conversation

heavycrystal
Copy link
Contributor

When pgx.Connector pgx.ParseConfig is used, it indirectly calls url.Parse which by default returns the URL as part of its error type in case it failed to parse it. This leads to the error message from pgx leaking the unredacted URL in any logs.

Fixed by typecasting to url.Error and then returning only the appropriate bit of the error, needed to change a variable name along with this.

Minimal Reproduction

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/jackc/pgx/v5"
)

func main() {
	conn, err := pgx.Connect(context.Background(), "postgres://postgres:password@ localhost:5432/postgres")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close(context.Background())
}

Without the change

Unable to connect to database: cannot parse `postgres://postgres:xxxxxx@ localhost:5432/postgres`: failed to parse as URL (parse "postgres://postgres:password@ localhost:5432/postgres": invalid character " " in host name)

With the change

Unable to connect to database: cannot parse `postgres://postgres:xxxxxx@ localhost:5432/postgres`: failed to parse as URL (invalid character " " in host name)

pgconn/config.go Outdated Show resolved Hide resolved
@jackc jackc merged commit 96791c8 into jackc:master Jul 12, 2024
14 checks passed
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.

3 participants