SKY-D234 · Security in code
Form or serializer accepts every model field
Mass assignment means copying every field a client sends straight onto a database record. In Django and Django REST Framework, fields = "__all__" in a form's or serializer's Meta class does exactly that. SKY-D234 reports that line.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Mass assignment
Why it matters
The client chooses which fields to send. A user editing their own profile can add "is_staff": true or "is_superuser": true to the request and give themselves admin rights, or change another record's owner.
What Skylos flags
from django.contrib.auth.models import Userfrom rest_framework import generics, permissions, serializers class ProfileSerializer(serializers.ModelSerializer): class Meta: model = User fields = "__all__" class ProfileView(generics.RetrieveUpdateAPIView): serializer_class = ProfileSerializer permission_classes = [permissions.IsAuthenticated] def get_object(self): return self.request.userWhat Skylos leaves alone
from django.contrib.auth.models import Userfrom rest_framework import generics, permissions, serializers class ProfileSerializer(serializers.ModelSerializer): class Meta: model = User fields = ["id", "username", "first_name", "last_name", "email", "is_staff"] read_only_fields = ["id", "username", "is_staff"] class ProfileView(generics.RetrieveUpdateAPIView): serializer_class = ProfileSerializer permission_classes = [permissions.IsAuthenticated] def get_object(self): return self.request.userWhy Skylos leaves it alone
The serializer names its fields one by one, and is_staff is shown but read-only, so a request cannot change it. Only the literal string "__all__" is reported.
More languages
Python
Flagged
from django import forms from shop.models import Order class OrderForm(forms.ModelForm): class Meta: model = Order fields = "__all__"Not flagged
from django import forms from shop.models import Order class OrderForm(forms.ModelForm): class Meta: model = Order fields = ["shipping_address", "delivery_notes"]Why Skylos leaves it alone
The customer can change only the address and the notes. Fields such as the price, the status or the owner stay out of the form.
How Skylos decides
- Runs with
--dangeron Python files that contain bothfieldsand__all__. - Looks at every class for a nested class named exactly
Meta. Inside it, reports HIGH on a plain assignmentfields = "__all__". - The base class is not checked, so a Django
ModelForm, a DRFModelSerializeror any other class with such aMetais reported.exclude = [...], a list such asfields = ["__all__"], an annotated assignment and other mass-assignment shapes are not.
How to fix it
List the fields a client may send. For a serializer, mark fields the client may see but not change as read_only_fields; keep permission flags, owners and prices out of writable fields.
from django.contrib.auth.models import Userfrom rest_framework import generics, permissions, serializers class ProfileSerializer(serializers.ModelSerializer): class Meta: model = UserRemoved: fields = "__all__"Added: fields = ["id", "username", "first_name", "last_name", "email"]Added: read_only_fields = ["id", "username"] class ProfileView(generics.RetrieveUpdateAPIView): serializer_class = ProfileSerializer permission_classes = [permissions.IsAuthenticated] def get_object(self): return self.request.userWhen it can be wrong
- Output-only uses are reported too: a serializer used only by a read-only list view, a form used only by staff, or a GraphQL type that only displays a model.
- Missed:
exclude = [...], which also lets new model fields through, and other ways to copy request data onto a record, such asOrder(**request.POST.dict()),setattr()loops orobj.__dict__.update(data).
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
Mark the
fields = "__all__"line and say why every field may be written.Python fields = "__all__" # skylos: ignore[SKY-D234] read-only report serializer[tool.skylos] ignore = ["SKY-D234"]turns the rule off for the whole project.skylos baseline .and then--baselineaccept the existing serializers and report only new ones.