Create a new database-level user, and grant that user SELECT but
nothing else. Then fill in those credentials in the settings file used
by the site. You can always set up a read/write "real" administrative
area for yourself with a different settings file and different
database credentials.
--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."
Things your filter doesn't catch:
* PostgreSQL's table-creating SELECT INTO statement.
* Any "query" which consists of multiple statements separated by
semicolons (e.g., "SELECT * from foo; DROP TABLE auth_user;").
* etc., etc.
Your database already knows how to handle a read-only user. Either set
up a separate settings file and run the client-facing part of your
site on that (most secure) or have this method obtain its own separate
database connection as a read-only user (not as good, but still
decent). Trying to invent a security system which prevents any and all
data-altering queries will almost certainly end in pain and suffering,
especially when you already have easy access to a proven system that
does the same thing.
So the user puts in
SELECT * FROM app_whatever; DROP TABLE auth_users;
...
You *really* *do* want a RO DB connection. Anything else is just
asking for trouble. Push come to shove, you can create a second
connection to the DB using the RO credentials, and then do your
execute() through that connection.
-tim
Once again, either:
1. Set up a second Django settings file, fill in the read-only user
there, and use that settings file for the site that your clients use.
2. Write your custom query method so that it manually imports the
database adapter and creates its own completely separate database
connection, without ever using the one provided by Django.
The first option is the better of the two.
> I totally understand what you suggest, having a RO user at the
> database (in this case MySQL) level.
> But I am fairly new to Django and Python, and I am unsure how to
> implement that dual-setting option.
In the devlopment server you can do
./manage.py runserver --settings=yoursite.readonlysettings # for the
display side of the site
and
./manage.py runserver # for the admin side of the site
When you deploy the site you can set up which settings file is used too.
--
David Reynolds
da...@reynoldsfamily.org.uk
If all he wants is a single method doing raw queries on a different
connection, that's easy enough to do without needing a massive rewrite
of Django -- he can just import the correct DB adapter module, set up
a connection and go.