SKY-D385 · Security in code
Django view saves a debit from a previously checked balance
A Django view reads a model row, checks that a numeric field holds enough units, subtracts the same amount in Python and saves the row. The finding points to the save and identifies the earlier read and check.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
Two requests can both pass the check against the same balance. The second save can overwrite the first debit, leaving too many units available.
What Skylos flags
from django.db import modelsfrom django.http import HttpResponsefrom django.urls import pathfrom django.views.decorators.http import require_POST class Inventory(models.Model): remaining = models.IntegerField() @require_POSTdef reserve(request, pk): item = Inventory.objects.get(pk=pk) if item.remaining < 1: return HttpResponse(status=409) item.remaining -= 1 item.save(update_fields=["remaining"]) return HttpResponse(status=204) urlpatterns = [path("reserve/<int:pk>/", reserve)]What Skylos leaves alone
from django.db import modelsfrom django.db.models import Ffrom django.http import HttpResponsefrom django.urls import pathfrom django.views.decorators.http import require_POST class Inventory(models.Model): remaining = models.IntegerField() @require_POSTdef reserve(request, pk): updated = Inventory.objects.filter(pk=pk, remaining__gte=1).update( remaining=F("remaining") - 1 ) return HttpResponse(status=204 if updated else 409) urlpatterns = [path("reserve/<int:pk>/", reserve)]Why Skylos leaves it alone
One database statement enforces the condition and applies the debit.
How Skylos decides
- Reads the model, view and Django URL registration from the project's Python sources. After a direct objects.get(), it recognizes an insufficient balance check that exits, or a sufficient balance branch that contains the debit. Either side of the comparison can hold the requested amount. Skylos checks that the subtraction and save() operate on the same instance.
- A transaction.atomic() block alone still allows this sequence at common isolation levels. A conditional database update can check the balance and debit it in the same statement.
- Detection requires the model and URL registration to resolve inside the bounded project snapshot. Redemptions implemented in raw SQL, background jobs or an external service fall outside this check.
- For a preview that saves and then unconditionally rolls back, Skylos checks the default atomic block and Django's set_rollback(True) call. It allows a direct exit or a simple HTTP response after the rollback. Visible custom database routing, multiple configured databases, an unresolved database setting, a different connection or a replaced transaction helper keeps the finding visible. Deployment-specific routing outside the analyzed sources requires review.
How to fix it
Make the debit conditional on enough units remaining and handle a zero-row update.
from django.db import modelsAdded: from django.db.models import Ffrom django.http import HttpResponsefrom django.urls import pathfrom django.views.decorators.http import require_POST class Inventory(models.Model): remaining = models.IntegerField() @require_POSTdef reserve(request, pk):Removed: item = Inventory.objects.get(pk=pk)Removed: if item.remaining < 1:Removed: return HttpResponse(status=409)Removed: item.remaining -= 1Removed: item.save(update_fields=["remaining"])Removed: return HttpResponse(status=204)Added: updated = Inventory.objects.filter(pk=pk, remaining__gte=1).update(Added: remaining=F("remaining") - 1Added: )Added: return HttpResponse(status=204 if updated else 409) urlpatterns = [path("reserve/<int:pk>/", reserve)]When it can be wrong
- An external lock that serializes every writer for this row can make the operation safe.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
A project can suppress this review with SKY-D385 in its Skylos ignore list.