I’m exploring SonarCloud and I’m experimenting with a simple Python desktop app that uses SQL-Alchemy. When I try to run Sonar to detect a new Vulnerability (in this case, Database queries should not be vulnerable to injection attacks), copying almost verbatim the example, Sonar does not detect this issue. In fact, it still reports everything as before coding the vulnerability.
Is this working as intended? Are SQL injection vulnerabilities only detected for Flask apps? Am I missing something?
Any help would be greatly appreciated.
Edit:
I introduced a code vulnerable to SQL injections like this:
def dar_album_por_id(self, album_id):
consulta = text('SELECT * from Album where id=%s' % album_id)
query = session.query(Album).from_statement(consulta)
return query.one().__dict__
I also introduced a bug following the " All “except” blocks should be able to catch exceptions" rule but I’m getting a code smell instead. Here is the code:
def eliminar_album(self, album_id):
try:
album = session.query(Album).filter(Album.id == album_id).first()
session.delete(album)
session.commit()
return True
except SQLAlchemyError as e:
print(e)
yield False
except SQLAlchemyError as e:
print(e)
yield False
except:
return False
The function you mentioned i being used by the GUI which is developed in PyQt5. It is also invoked by the test classes.
My suspicion would be that Sonar has trouble detecting this kind of vulnerability in non-web apps, but that still doesn’t explain why the exception rule is still being ignored.
Coming back on the exception that is caught twice, indeed S1045 should raise an issue in your code snippet.
I believe it doesn’t do it because the analyzer doesn’t resolve correctly the SQLAlchemyError as an exception class. Still, knowing this is actually the same symbol that is caught twice, an issue could still be raised safely.