PyCQA / PyCQA/bandit

Detecting misuse of the `psycopg2.sql` module

Open
#412 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
8.3k
Forks
835
Avg merge
5d 3h
Merged PRs (30d)
1

Description

The psycopg2.sql module is meant to provide a safe way to compose SQL queries dynamically, however it is possible to misuse it in a way that would result in an SQL injection vulnerability, and bandit currently doesn't support detecting this.

Solution: create a new test to detect when a psycopg2.sql.SQL object is being created from a non-literal, e.g. SQL(foo) or SQL('%s AND %s' % (foo, bar)). The severity and confidence levels of this new test would both be at least "medium".

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading Bandit's existing security-check tests and the entry points used to register new checks. Add coverage for psycopg2.sql.SQL built from a non-literal value, including formatted input, with at least medium severity and confidence; the new test should detect these misuse patterns without flagging literal SQL.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases, security
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.