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

Safe interpolation of column names #59

Open
Changaco opened this issue Jan 16, 2017 · 10 comments
Open

Safe interpolation of column names #59

Changaco opened this issue Jan 16, 2017 · 10 comments

Comments

@Changaco
Copy link
Member

Why don't we have this? It seems to me that a column name can be checked with a simple regexp, and a placeholder different than %s (e.g. %c) could be used to insert a column name into a query.

This is probably more of a psycopg2 issue, but let's discuss it here first, especially since we're considering porting to asyncpg (#58).

@Changaco
Copy link
Member Author

Found a related issue in psycopg2, quote psycopg/psycopg2#438 (comment):

We will probably add an "identifier" adapter to correctly quote table and column names in a future release (or at least expose a function to do the escaping).

@chadwhitacre
Copy link
Collaborator

Not a huge paint point in my life, but I'm open to this.

@Changaco
Copy link
Member Author

It's not a huge pain point, but it would be nice to have, especially to avoid security debt.

(I'm not planning to work on this right now, but I've just realized that I can't actually push to this repo since I'm no longer part of the Gratipay org.)

@chadwhitacre
Copy link
Collaborator

Oops! Happy to add you back as a contributor on this repo if you like?

@Changaco
Copy link
Member Author

Yes, that seems like a reasonable solution.

@chadwhitacre
Copy link
Collaborator

Invite sent! :)

@Changaco
Copy link
Member Author

Invite accepted. ;-) I think it's the 3rd time that I'm joining the Gratipay org. :D

@chadwhitacre
Copy link
Collaborator

https://github.com/martijndeh/lego reminds me of this.

@Changaco
Copy link
Member Author

AFAICT quoting an identifier is as simple as:

def quote_identifier(s):
    return '"%s"' % s.replace('"', '""')

https://www.postgresql.org/docs/9.6/static/sql-syntax-lexical.html

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected. The example can be written with quoted identifiers like this:

UPDATE "my_table" SET "a" = 5;

Quoted identifiers can contain any character, except the character with code zero. (To include a double quote, write two double quotes.) This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies.

@Changaco
Copy link
Member Author

Changaco commented Oct 19, 2018

psycopg 2.7, which was released less than two months after this issue was opened, introduced a module for "safe" SQL query composition: psycopg2.sql – SQL string composition.

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

No branches or pull requests

2 participants