Skip to content

pgpass doesn't work with builder methods #2658

@YgorSouza

Description

@YgorSouza

Bug Description

PgConnectionOptions tries to get the password from the pgpass file when the struct is created, but the caller can then use the builder API to change options that affect which password is taken from the file, so we can end up with the wrong password.

pub fn new() -> Self {
Self::new_without_pgpass().apply_pgpass()
}

pub(crate) fn apply_pgpass(mut self) -> Self {
if self.password.is_none() {
self.password = pgpass::load_password(
&self.host,
self.port,
&self.username,
self.database.as_deref(),
);
}
self
}

For example, we can select a different host here and it will not read the pgpass file again (and even if it did, it might already have gotten the wrong password and would not change it):

pub fn host(mut self, host: &str) -> Self {
self.host = host.to_owned();
self
}

Info

  • SQLx version: 0.7.1
  • SQLx features enabled: postgres
  • Database server and version: Postgres

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions