Fundamentally Safe Database APIs
Friday, April 6th, 2007Is anyone aware of work on fundamentally safe database APIs for server side programs that completely avoid the possibility of SQL injection? What I envision is a somewhat limited API that does not execute raw SQL statements or provide any facility to do so. Instead you’d set up something like this:
Statement s = database.getSelectStatement();
s.setTable("customers");
s.addField("email");
s.addField("telephone");
s.addCondition(
new EqualsCondition("id", "p17")
);
ResultSet r = s.execute();
The library would turn this into the usual SQL statement
SELECT email, telephone FROM customers WHERE id = "p17"
The library could verify the individual parts of the query before submitting it to the database. If you passed a string like "id = \"p17\" OR true; DELETE * FROM customers; SELECT * FROM customers WHERE " to EqualsCondition() it would throw an exception.
(more…)