database: PostgreSQL DSN breaks IPv6 hosts and Validate accepts out-of-range ports

Đang mở
#336 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
67/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go, mysql, postgresql
Lĩnh vực
database

Hướng nghiên cứu

Locate PostgresConfig.DSN(), PostgresConfig.Validate(), MySQLConfig.Validate(), and the existing MySQLConfig.DSN() implementation in the database package. Read those methods and their tests first; the issue gives the IPv6 and port cases to cover. Done means PostgreSQL DSNs parse for the listed host forms, both validators reject ports below 0 or above 65535 while accepting 0, and the database tests pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Type/Bug
Describe the Bug

PostgresConfig.DSN() builds an invalid URL when Host is an IPv6 address, and Validate() accepts any port.

  • IPv6 hosts: DSN() joins the host and port with fmt.Sprintf("%s:%d", c.Host, c.Port). For Host: "::1", Port: 5432 the URL host is ::1:5432, which pgx rejects ("too many colons in address"), so New fails. IPv4-mapped literals such as ::ffff:10.0.0.1 fail the same way. MySQLConfig.DSN() already uses net.JoinHostPort, so only PostgreSQL is affected.
  • Out-of-range ports: PostgresConfig.Validate() and MySQLConfig.Validate() don't check Port, so a negative port or one above 65535 passes validation and fails later, at connect time, with a driver error. A port of 0 (omitted) is documented as "use the driver default" and is fine.
To Reproduce
cfg := database.PostgresConfig{
    Host: "::1", Port: 5432, User: "postgres", Password: "secret",
    Name: "app", SSLMode: "disable",
}
fmt.Println(cfg.Validate()) // <nil>
_, err := pgconn.ParseConfig(cfg.DSN())
fmt.Println(err)
// cannot parse `postgres://postgres:xxxxx@::1:5432/app?sslmode=disable`: failed to parse as URL
// (failed to split host:port in '::1:5432', err: address ::1:5432: too many colons in address)

cfg.Host, cfg.Port = "localhost", 70000
fmt.Println(cfg.Validate()) // <nil>
Expected Behavior
  • DSN() brackets IPv6 hosts, e.g. postgres://...@[::1]:5432/app, by using net.JoinHostPort as MySQLConfig.DSN() does. A host with no port that is an IPv6 literal is bracketed too ([::1]).
  • Validate() for PostgreSQL and MySQL rejects ports below 0 or above 65535. 0 keeps meaning "driver default".
  • A test parses DSN() with pgconn.ParseConfig for a hostname, IPv4, IPv6, an IPv4-mapped IPv6 address, and an already-bracketed [::1], with and without a port.
Screenshots

N/A

Version

database/v0.3.0

Environment Details (with versions)

Go 1.26, pgx v5, PostgreSQL 16

Additional Context

Found while moving the OpenNDX Portal Backend onto this package (openndx/openndx-core#688). The PB's old hand-built keyword DSN accepted host=::1, so this is a small regression for OpenNDX deployments that use an IPv6 database address. OpenNDX will bump the dependency once a fix is released, rather than work around it in each service.

Ngôn ngữ chính
Go
Star
6
Fork
8
Merge trung bình
21 giờ 36 phút
Pull request đã merge (30 ngày)
42

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của OpenNSW/core

Tất cả issue của OpenNSW/core

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.