Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is SQL injection protection built into SQLAlchemy's ORM or Core?

I'm developing an aiohttp server application, and I just saw that apparently it isn't able to use SQLAlchemy's ORM layer. So, I was wondering: if my application will only be able to use SQLAlchemy's core, is it still protected against SQL injection attacks?

My code is the following:

async def add_sensor(db_engine, name):
    async with db_engine.acquire() as connection:
        query = model.Sensor.__table__.insert().values(name=name)
        await connection.execute(query)

A comment on the accepted answer in this related question makes me doubt:

you can still use execute() or other literal data that will NOT be escaped by SQLAlchemy.

So, with the execute() used in my code, does the above quote mean that my code is unsafe? And in general: is protection against SQL Injection only possible with the SQLAlchemy ORM layer, as with the Core layer you'll end up launching execute()?

like image 276
Sander Vanden Hautte Avatar asked Feb 19 '18 16:02

Sander Vanden Hautte


People also ask

Does ORM prevent SQL injection?

The benefits of using an ORM tool include quick generation of an object layer to communicate to a relational database, standardize code templates for these objects, and that they usually provide a set of safe functions to protect against SQL Injection attacks.

What is SQL injection protection?

SQL Injection (SQLi) is a type of an injection attack that makes it possible to execute malicious SQL statements. These statements control a database server behind a web application. Attackers can use SQL Injection vulnerabilities to bypass application security measures.

Does Django ORM prevent SQL injection?

Database Queries Within that layer, Django protects itself from SQL injection by using query parameterization. Within the ORM layer, Django defines SQL queries separated from the query's parameters, and the database driver is in charge of escaping each of the parameters.

What type of vulnerability is SQL injection?

SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries that an application makes to its database. It generally allows an attacker to view data that they are not normally able to retrieve.

How does an ORM protect against SQL injection?

Wikipedia's article on SQL Injection says that an ORM provides protection against SQL injection. The exact words are: "Using object-relational mapping libraries avoids the need to write SQL code. The ORM library in effect will generate parameterized SQL statements from object-oriented code."

What is%% in SQLAlchemy?

This wraps “%” around a variable, and passes it as the “LIKE” term to a database. There is, quite rightly, concern that this may be a vector for SQL Injection. However, SQLAlchemy is designed to help avoid these issues, and as such it passes the LIKE term not as part of a string but as a bindparam (escaped

What is SQL injection?

The primary form of SQL injection consists of direct insertion of code into user-input variables that are concatenated with SQL commands and executed. A less direct attack injects malicious code into strings that are destined for storage in a table or as metadata.

How do you protect against SQL injection?

One way to protect against SQL Injection is to use an ORM package, which maps your objects and actions on them into SQL for you. Using such packages means you’re never actually composing SQL, and so don’t get the chance to slip and allow malicious injections. Hooray!


1 Answers

in your example above i dont see any variable beeing supplied to the database query. Since there is no user supplied input there is also no Sql Injection possible.

Even if there would be a user supplied value as long as you dont use handwritten sql statements with sqlalchemy and instead use the orm model approach (model.Sensor.__table__.select()) as can be seen in your example you are secure against Sql Injection.

In the end its all about telling sqlalchemy explicitely what columns and tables should be used to select and insert data from/to and keeping that separate from the data that is beeing inserted or selected. Never combine the data string with the query string and always use sqlalchemy orm model objects to describe your query.

Bad way (Sql Injectable):

Session.execute("select * form users where name = %s" % request.GET['name'])

Good way (Not Sql Injectable):

Session.execute(model.users.__table__.select().where(model.users.name == request.GET['name']))
like image 75
quantumbyte Avatar answered Oct 13 '22 17:10

quantumbyte