views:

34

answers:

3

I want to limit access to username+password table to one query pattern like:

SELECT count(id) AS auth_result
  FROM user
 WHERE username = '%s' 
   AND password = SHA1('%s')

(this query doesn't pretend to be working from the point of injection vulnerability, just an example)

Is that possible? or am I missing some different approach?

A: 

I don't know whether what you plan is good or not but it should be possible with rewrite rules. Never played with it though.

musiKk
that's a good hint! thanks!
forker
+2  A: 

You can revoke all access to user table from all users except the owner, and create a view with like this:

create view auth_view as
select id, username, sha1(password) as sha1pass
  from user;

Then your query will look like:

select count(id)
  from auth_view
 where username = '%s'
   and sha1pass = sha1('%s')

Other possibility might be using PostgreSQL's rule system to rewrite or avoid certain king of queries on user table. But I am not sure what you are trying to do here.

Pablo Santa Cruz
I want to exclude possibility of dumping the user table with such limited account. The approach with view still allows to dump all usernames and hashed passwords.
forker
You can write a function then. Your function would receive user/password and return true/false if the user/pass combination matches...
Pablo Santa Cruz
A: 

I would suggest you write a function, and make it SECURITY DEFINER. See for example http://www.hagander.net/talks/hidden%20gems%20of%20postgresql.pdf, pages 28-36.

Magnus Hagander