Skip to content

[BUG] Incorrect handling of DB errors #606

@WagnerPMC

Description

@WagnerPMC

Versions

PGCat: 1.1.1
PostgreSQL: Postgres Pro 15 (similar to PostgreSQL 15)

Configurations

pgcat.toml

auth_query = "SELECT usename, passwd FROM public.pgcat_auth($1)"
auth_query_user = "pgcat_auth_user"
auth_query_password = ""

pg_hba.conf

hostssl all pgcat_auth_user 172.16.3.2/32 trust

Description

My pg_hba.conf file allows the pgcat_auth_user user to be allowed into the server unhindered, but only to execute the public.pgcat_auth function. But something goes wrong when PGCat does it.

I'm catching the next error:

pgcat-pgcat-1  | 2023-09-26T23:05:53.212508Z  WARN ThreadId(04) pgcat: Client disconnected with error
 ClientAuthPassthroughError("AuthPassthroughError(\"Error trying to obtain password from auth_query, ignoring hash for user 'xxxxxxxxxx'. Error: ServerStartupError(\\\"error message\\\", ServerIdentifier { username: \\\"pgcat_auth_user\\\", database: \\\"xxxxxx\\\" })\")", ClientIdentifier { application_name: "pgcat", username: "xxxxxxx", pool_name: "xxxxxxxx" })

This indicates that PGCat was unable to correctly read the error from the database server at this location:

pgcat/src/server.rs

Lines 716 to 726 in a054b45

let mut error = vec![0u8; len as usize];
match stream.read_exact(&mut error).await {
Ok(_) => (),
Err(_) => {
return Err(Error::ServerStartupError(
"error message".into(),
server_identifier,
))
}
};

The solution is to subtract the number 5 from the len variable, so the errors are read correctly. Proofs:
https://user-images.githubusercontent.com/97305733/271855206-13c86dbe-f80d-4340-a189-9baafb1cab51.png

How to reproduce?

Provoke PostgreSQL to generate any protocol error. (at least in auth_query)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions