PyCQA / PyCQA/bandit

some SQL injections not detected

Open
#977 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

None of the lines get reported, although each of the foo functions contains an SQL injection

from django.db import transaction, connections, connection


def foo(booking_id: str):
    with transaction.atomic():
        connections['default'].cursor().execute(f'SELECT pg_advisory_xact_lock({booking_id})')


def foo2(booking_id: str):
    with transaction.atomic():
        connection.cursor().execute(f'SELECT pg_advisory_xact_lock({booking_id})')


def foo3(booking_id: str):
    with connection.cursor() as cursor:
        cursor.execute(f'SELECT pg_advisory_xact_lock({booking_id})')


def foo4(booking_id: str):
    with connection.cursor() as cursor:
        cursor.execute(f'SELECT pg_advisory_xact_lock({booking_id}) FROM table')


def foo5(col: str):
    with connection.cursor() as cursor:
        cursor.execute(f'select {col} from table')


def foo6(col: str):
    q = f'select {col} from table'


def foo7(col, booking_id):
    q = f'select {col} from table where id = {booking_id}'

Bandit output:

bandit -r ./injection.py
[main]	INFO	profile include tests: None
[main]	INFO	profile exclude tests: None
[main]	INFO	cli include tests: None
[main]	INFO	cli exclude tests: None
[main]	INFO	running on Python 3.9.14
Run started:2022-12-16 08:57:06.964396

Test results:
	No issues identified.

Code scanned:
	Total lines of code: 20
	Total lines skipped (#nosec): 0

Run metrics:
	Total issues (by severity):
		Undefined: 0
		Low: 0
		Medium: 0
		High: 0
	Total issues (by confidence):
		Undefined: 0
		Low: 0
		Medium: 0
		High: 0
Files skipped (0):

Bandit version: bandit==1.7.4

Reproduction steps
1. bandit -r ./injection.py
Expected behavior

Expected to detect SQL injection in each of the foo* functions

Bandit version

1.7.4 (Default)

Python version

3.9

Additional context

No response

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 with injection.py and Bandit’s SQL-injection scan, reproducing the no-issues output for the cursor.execute f-strings and assigned queries. Trace the relevant checker entry point and add regression coverage so the foo* examples are reported as SQL injections while the existing scan remains passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python, sql
Domain
devtools, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.