FRiCKLE / ngx_postgres

upstream module that allows nginx to communicate directly with PostgreSQL database.
http://labs.frickle.com/nginx_ngx_postgres/
BSD 2-Clause "Simplified" License
545 stars 122 forks source link

Support for prepared statements and placeholders #45

Open nvx opened 8 years ago

nvx commented 8 years ago

The current way of passing untrusted user input is via manually escaping and then forming the sql query with the escaped values.

This is prone to human error and could result in sql injection attacks being possible.

The ideal situation would be to add support for prepared statements using placeholders. Is there any technical limitation of the nginx engine preventing this?

agentzh commented 8 years ago

@nvx There is no technical limitations on the nginx side except the lack of tagging support in the standard ngx_http_upstream_keepalive module (where we need to tag in-pool connections with their prepared statements). Well, patches welcome :)

agentzh commented 8 years ago

@nvx Oh, please ignore the ngx_http_upstream_keepalive_module part in my previous comment. The ngx_postgres module implements its own connection pool anyway. So there's no issues here.

nvx commented 8 years ago

Ah interesting point. My comment on technical limitation was actually thinking on the configuration side if the nginx config parser was able to cope with vararg-like statements. I haven't done any work with nginx plugins themselves before, so wondered if that was a blocker to implementing this.

agentzh commented 8 years ago

@nvx Yes, nginx directives can accept a variable number of arguments. This is not a problem.

I just hope that the user does not have to prepare the same statement upon every request if the Pg connection is reused via the pool. That is, we just have to prepare the statement once for every single Pg connection (yes, this is the real performance benefit of preparing a statement and then executing it over and over again). So tagging connections in pool somehow is important for such stateful treatment.