3 ms·
Practically every framework gives a developer the capability to run raw SQL queries on a database. The latest version of the popular PHP framework Laravel still
by onion2k 10y ago
Practically every framework gives a developer the capability to run raw SQL queries on a database. The latest version of the popular PHP framework Laravel still has DB::raw. Express in NodeJS uses the NPM MySQL client that offers connection.query for raw SQL. Python's Flask framework uses SQLAlchemy and gives a developer db.engine.execute access. And so on for every language people make database driven websites in.
Using a framework well stops SQL injection attacks dead in their tracks. Using one badly though, and they're still just as dangerous.
- jrcii 10y agoAlthough we can agree we should be sanitizing inputs, I haven't seen a consensus on the use of ORM as a best practice and wouldn't say its employment is inherent in using a framework well. There's nothing wrong with writing SQL queries if everything leading up to that point is handled correctly.
- jerf 10y agoThe pattern I've seen on a recurring basis is: 1. You need a query that can't be handled by your ORM, since in general ORMs can't expose all functions of a database. (This condition is also met if you have no ORM.) 2. You need a query that is going to have a varying number of parameters, so you need to manually assemble the query. When that happens, there's a temptation in almost all systems to try to write the moral equivalent of: sql_query = "WHERE condition = '" + param1 + "' if something: sql_query += " AND other_thing = '" + param2 + "' " and so on and so on. This is where I see most of the SQL injection, even in language communities that have otherwise thoroughly internalized the need for query parameters (for example, perhaps surprisingly, the Perl community for a very long time). Programmers who are not really fluent yet and are still having to think about every line either don't think about, or are trying to avoid the complexity of, sql_query = "WHERE condition = ?" params.append(param1) if something: sql_query += " AND other_thing = ? " params.append(param2) are the source. This pattern is, broadly speaking, safe. But it has just that extra bit of the sort of complexity that a lot of programmers shy away from. (As opposed to the sorts that programmers almost seem to chase, like "adding 15 boolean parameters to a function over the course of a year".) There's also usually a library in the target language that is a pretty good reflection of SQL that avoids this problem (such as SQLAlchemy in Python), but I think that most people consider this "too complicated".