Skip to content

Bug: special char in dbname is not correctly handled #77

Closed

Description

@kainwen

Reproduce Steps

First go to Postgres to create a database:

CREATE DATABASE "funny""db'with\\quotes";

Then run the following python code:

In [1]: import pgdb

In [2]: dbname = 'funny\"db\'with\\\\quotes'

In [3]: pgdb.connect(database=dbname)
Out[3]: <pgdb.Connection at 0x10e4f0af0>

In [4]: pgdb.connect(database=dbname, port=5432)
---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
<ipython-input-4-ac96bf54cbc4> in <module>
----> 1 pgdb.connect(database=dbname, port=5432)

/Library/Python/3.8/site-packages/pgdb.py in connect(dsn, user, password, host, database, **kwargs)
   1725     # open the connection
   1726     # noinspection PyArgumentList
-> 1727     cnx = _connect(dbname, dbhost, dbport, dbopt, dbuser, dbpasswd)
   1728     return Connection(cnx)
   1729

InternalError: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "funny"db'with\quotes" does not exist

The strange thing is:

  • [3] is OK
  • [4] errors out

RCA

[4] pass port=5432 as kwarg, so it will enter this line of code https://github.com/PyGreSQL/PyGreSQL/blob/master/pgdb.py#L1721, and here it only escape dbname when there is space in dbname.

And the dbname variable here is confusing, it will also add port in the string.

Activity

  1. SKeyerror commented on Aug 18, 2022

    @SKeyerror

    Maybe some refactoring should be done to the function connect() 🤔

  2. SKeyerror commented on Aug 18, 2022

    @SKeyerror

    @Cito Hi~ Do we have a plan to refactor the function of connect() in pgdb.py? Since the semantics and parameter settings of this function are weird and ambiguous.

    For example, the DSN string can't support adding a port to it, cause the format must be 'host:base:user:passwd:opt', we can just add a port to the named parameter host using IP:PORT.

    And if we use both DSN and named parameters, there will be mutual coverage. From the current code point of view, named parameters will override the value in DSN.

    From my experience, it's better to make the function semantics clearer, just like pymysql.connect:

    connection = pymysql.connect(host='localhost',
                                 user='user',
                                 password='passwd',
                                 database='db',
                                 port='port')
    
  3. added this to the 6.0 milestone on Aug 18, 2022
  4. Cito commented on Aug 18, 2022

    @Cito
    Member

    Will look into this in the next major version which will also allow us to introduce some breaking changes.

  5. added a commit that references this issue on Jan 9, 2025
  6. added a commit that references this issue on Jan 10, 2025
  7. modified the milestones: 7.0, 6.3 on Nov 22, 2026
  8. Cito commented on Nov 22, 2026

    @Cito
    Member

    Fixed in #113, which will be part of version 6.3. When pgdb.connect() is called with additional keyword arguments, all values, including the database name, are now always quoted and escaped, so names containing quotes or backslashes work as expected.

    The broader redesign of connect() discussed above would be a breaking change, so it is now tracked in #114 for version 7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions