Skip to content

Fix: unsafe raw SQL in check_unique() - #219

Open
rajnisht7 wants to merge 3 commits into
101Loop:masterfrom
rajnisht7:fix
Open

Fix: unsafe raw SQL in check_unique()#219
rajnisht7 wants to merge 3 commits into
101Loop:masterfrom
rajnisht7:fix

Conversation

@rajnisht7

@rajnisht7 rajnisht7 commented Aug 3, 2026

Copy link
Copy Markdown

Description

Replaces User.objects.extra() with User.objects.filter() in check_unique() utility function.
The extra() method with raw string construction is unnecessary here since prop is already restricted to known field names. Using the ORM's filter() with keyword argument unpacking is cleaner and safer.

Motivation and Context

The check_unique() function was using User.objects.extra() with raw string concatenation to build a WHERE clause. this pattern is unsafe as it does not use parameterized queries.

Have you tested this? If so, how?

executed existing test suite and all test passed

Checklist for PR author(s)

  • Changes are covered by unit tests (no major decrease in code coverage %).
  • All tests pass.
  • Docstring coverage is 100% via interrogate drf_user (I mean, we should set a good example 😄).
  • Updates to documentation:
    • Document any relevant additions/changes in README.md.
    • Manually update both the README.md and docs/index.rst for any new changes.
    • Any changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives. Find the appropriate next version in the project's __init__.py file.

Release note


Summary by Sourcery

Bug Fixes:

  • Prevent potential SQL injection by replacing raw WHERE clause construction in check_unique() with a parameterized ORM filter query.

Summary by Sourcery

Bug Fixes:

  • Prevent potential SQL injection in check_unique() by using a parameterized ORM filter instead of constructing a raw WHERE clause.

@sourcery-ai

sourcery-ai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the check_unique utility to replace a hand-built raw SQL WHERE clause using extra() with a safe ORM filter() call that leverages keyword argument unpacking and exists(), improving safety and readability while preserving behavior.

Flow diagram for updated check_unique utility

flowchart LR
    A["check_unique(prop, value)"] --> B["User.objects.filter(**{prop: value})"]
    B --> C["QuerySet.exists()"]
    C --> D{"Any matching User?"}
    D -- Yes --> E["return False"]
    D -- No --> F["return True"]
Loading

File-Level Changes

Change Details Files
Refactor uniqueness check to use ORM filter with keyword argument unpacking instead of raw SQL via extra().
  • Replace User.objects.extra() call that concatenated a WHERE clause string from prop and value with a parameterized User.objects.filter(**{prop: value}) query
  • Use exists() and boolean negation to return a boolean uniqueness indication instead of counting queryset rows
drf_user/utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Since you only need to know whether any user exists, consider replacing user.count() == 0 with not User.objects.filter(**{prop: value}).exists() to avoid an unnecessary count query and improve efficiency.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since you only need to know whether any user exists, consider replacing `user.count() == 0` with `not User.objects.filter(**{prop: value}).exists()` to avoid an unnecessary count query and improve efficiency.

## Individual Comments

### Comment 1
<location path="drf_user/utils.py" line_range="96" />
<code_context>
     True
     """
-    user = User.objects.extra(where=[prop + " = '" + value + "'"])
+    user = User.objects.filter(**{prop: value})
     return user.count() == 0

</code_context>
<issue_to_address>
**suggestion (performance):** Consider using `.exists()` instead of `.count() == 0` for efficiency.

Using `User.objects.filter(**{prop: value}).exists()` will short‑circuit on the first match instead of counting all matches, which is more efficient on larger tables.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread drf_user/utils.py Outdated
@rajnisht7

Copy link
Copy Markdown
Author

@sourcery-ai review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant